# AppserverView.java

    /*
    ** Module   : AppserverView.java
    ** Abstract : AppserverView is a view to manage appservers.
    **
    ** Copyright (c) 2024-2025, Golden Code Development Corporation.
    **
    ** -#- -I- --Date-- ----------------Description----------------------
    ** 001 GBB 20240930 Created initial version.
    ** 002 GBB 20250403 Support for the new multi-session appserver.
    */
    /*
    ** This program is free software: you can redistribute it and/or modify
    ** it under the terms of the GNU Affero General Public License as
    ** published by the Free Software Foundation, either version 3 of the
    ** License, or (at your option) any later version.
    **
    ** This program is distributed in the hope that it will be useful,
    ** but WITHOUT ANY WARRANTY; without even the implied warranty of
    ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    ** GNU Affero General Public License for more details.
    **
    ** You may find a copy of the GNU Affero GPL version 3 at the following
    ** location: https://www.gnu.org/licenses/agpl-3.0.en.html
    **
    ** Additional terms under GNU Affero GPL version 3 section 7:
    **
    **   Under Section 7 of the GNU Affero GPL version 3, the following additional
    **   terms apply to the works covered under the License.  These additional terms
    **   are non-permissive additional terms allowed under Section 7 of the GNU
    **   Affero GPL version 3 and may not be removed by you.
    **
    **   0. Attribution Requirement.
    **
    **     You must preserve all legal notices or author attributions in the covered
    **     work or Appropriate Legal Notices displayed by works containing the covered
    **     work.  You may not remove from the covered work any author or developer
    **     credit already included within the covered work.
    **
    **   1. No License To Use Trademarks.
    **
    **     This license does not grant any license or rights to use the trademarks
    **     Golden Code, FWD, any Golden Code or FWD logo, or any other trademarks
    **     of Golden Code Development Corporation. You are not authorized to use the
    **     name Golden Code, FWD, or the names of any author or contributor, for
    **     publicity purposes without written authorization.
    **
    **   2. No Misrepresentation of Affiliation.
    **
    **     You may not represent yourself as Golden Code Development Corporation or FWD.
    **
    **     You may not represent yourself for publicity purposes as associated with
    **     Golden Code Development Corporation, FWD, or any author or contributor to
    **     the covered work, without written authorization.
    **
    **   3. No Misrepresentation of Source or Origin.
    **
    **     You may not represent the covered work as solely your work.  All modified
    **     versions of the covered work must be marked in a reasonable way to make it
    **     clear that the modified work is not originating from Golden Code Development
    **     Corporation or FWD.  All modified versions must contain the notices of
    **     attribution required in this license.
    */

    package com.goldencode.p2j.admin.client.application.home.console.appservers;

    import com.goldencode.p2j.admin.*;
    import com.goldencode.p2j.admin.client.*;
    import com.goldencode.p2j.admin.client.application.home.*;
    import com.goldencode.p2j.admin.client.widget.dialog.*;
    import com.goldencode.p2j.admin.shared.*;
    import com.google.gwt.regexp.shared.*;
    import com.google.gwt.uibinder.client.*;
    import com.google.gwt.user.client.ui.*;
    import com.google.inject.*;

    import java.util.*;

    /** AppserverView is a view to manage sessions. */
    public class AppserverView
    extends BaseViewWithUiHandlers<AppserverUIHandler>
    implements AppserverPresenter.MyView
    {
       /**
        * GWT UI creator
        */
       interface Binder
       extends UiBinder<Widget, AppserverView>
       {}

       /** Map of appserver name : widget pairs. */
       private final Map<String, BaseAppserverWidget> appserverContainers = new HashMap<>();

       /** Modal dialog manager */
       private final ModalDialogs modalDialogsManager;

       /**
        * Package-private constructor.
        *
        * @param    binder
        *           The injected GWT UI Binder
        * @param    modalDialogsManager
        *           The common modal dialogs manager
        */
       @Inject
       AppserverView(AppserverView.Binder binder, ModalDialogs modalDialogsManager)
       {
          initWidget(binder.createAndBindUi(this));
          this.modalDialogsManager = modalDialogsManager;
       }

       /**
        * Returns pairs of a report parameter key and its value
        *
        * @return   The enumeration map of report parameters keys to their values
        */
       @Override
       public EnumMap<ReportParameters, Object> getReportParameters()
       {
          EnumMap<ReportParameters, Object> parameters = new EnumMap<>(ReportParameters.class);
          parameters.put(ReportParameters.TITLE, "Appservers");
          parameters.put(ReportParameters.REPORT_ID, Reports.APPSERVERS);
          parameters.put(ReportParameters.PAPER_FORMAT, PaperFormat.A4);
          parameters.put(ReportParameters.PAPER_ORIENT, PaperOrientation.LANDSCAPE);
          parameters.put(ReportParameters.SELECTION, new AppserverInfo.AgentInfo[0]);

          return parameters;
       }

       /**
        * Returns the size of all data to preview or the size of selected data to preview
        *
        * @param    range
        *           The target range of selected data to preview
        *
        * @return   The size of the selected data
        */
       @Override
       public int getDataSize(Range range)
       {
          if (appserverContainers.size() == 0)
          {
             return 0;
          }

          int count = 0;
          for (String appserver : appserverContainers.keySet())
          {
             count += appserverContainers.get(appserver).getPrintableData().length;
          }

          logger.warning("getDataSize " + count);
          return 0;
       }

       /**
        * Returns the value of the preview dialog parameter.
        *
        * @param    parameter
        *           The preview dialog parameter key
        *
        * @return   The value of the preview dialog parameter
        */
       @Override
       public String getPreviewDialogParameter(PreviewDialogParameters parameter)
       {
          int allRecordsNumber = getDataSize(null);
          switch (parameter)
          {
             case HELP_TEXT:
                return "<h4>All appserver sessions being printed. Continue?</h4>";
             case TITLE:
                return "Printing agents";
             case RANGE_ALL_LABEL:
                return "all " + allRecordsNumber + " appserver sessions";
             case RANGE_SELECTION_LABEL:
                return "the selected appserver sessions";
          }

          return null;
       }

       /**
        * Returns url path to this view.
        *
        * @return   The url path.
        */
       @Override
       public String getPathToPrintPreviewOwner()
       {
          return NameTokens.APPSERVERS;
       }

       /**
        * Sets the appserver model.
        *
        * @param    appservers
        *           The array of appserver descriptors
        * @param    filter
        *           The filter criteria
        */
       @Override
       public void setAppservers(AppserverInfo[] appservers, RegExp filter)
       {
          RootPanel panelBody = RootPanel.get("panelBody");
          panelBody.clear();

          if (appservers == null || appservers.length == 0)
          {
             return;
          }

          for (AppserverInfo appserverInfo : appservers)
          {
             BaseAppserverWidget appserverView = appserverInfo.isNewMultiSession ?
                new MultiSessionAppserverView(appserverInfo, modalDialogsManager) :
                new ClassicAppserverView(appserverInfo, modalDialogsManager);
             panelBody.add(appserverView);
             appserverView.setUiHandlers(getUiHandlers());
             appserverContainers.put(appserverInfo.name, appserverView);
          }
       }

       /**
        * Sets the agents table data.
        *
        * @param    appserver
        *           The appserver name
        * @param    agentInfos
        *           The array of agent descriptors
        * @param    filterExp
        *           The filter criteria
        */
       @Override
       public void setAgents(String appserver, AppserverInfo.AgentInfo[] agentInfos, RegExp filterExp)
       {
          appserverContainers.get(appserver).setAgentTable(agentInfos);
       }

       /**
        * Shows a warning message.
        *
        * @param    msg
        *           The warning message
        */
       @Override
       public void showWarningMsg(String msg)
       {
          modalDialogsManager.showWarning(msg);
       }

    }