ServerMessagesPresenter.java

/*
** Module   : ServerMessagesPresenter.java
** Abstract : ServerMessagesPresenter is a controller that implements business methods to work
**            with server messages.
**
** Copyright (c) 2017, Golden Code Development Corporation.
**
** -#- -I- --Date-- ----------------Description----------------------
** 001 SBI 20170601 Created initial version.
*/
/*
** 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.help;


import java.util.logging.Level;

import com.goldencode.p2j.admin.client.NameTokens;
import com.goldencode.p2j.admin.client.application.Alarm;
import com.goldencode.p2j.admin.client.application.home.BasePresenter;
import com.goldencode.p2j.admin.client.application.home.HomePresenter;
import com.goldencode.p2j.admin.client.widget.dialog.ModalDialogs;
import com.goldencode.p2j.admin.shared.AdminServiceAsync;
import com.google.gwt.user.client.*;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.inject.Inject;
import com.google.web.bindery.event.shared.EventBus;
import com.gwtplatform.mvp.client.HasUiHandlers;
import com.gwtplatform.mvp.client.View;
import com.gwtplatform.mvp.client.annotations.NameToken;
import com.gwtplatform.mvp.client.annotations.ProxyStandard;
import com.gwtplatform.mvp.client.presenter.slots.NestedSlot;
import com.gwtplatform.mvp.client.proxy.PlaceManager;
import com.gwtplatform.mvp.client.proxy.ProxyPlace;
import com.gwtplatform.mvp.shared.proxy.PlaceRequest;


/**
 * ServerMessagesPresenter is a controller that implements business methods to work with server
 * messages.
 */
public class ServerMessagesPresenter
extends BasePresenter<ServerMessagesPresenter.MyView, ServerMessagesPresenter.MyProxy>
implements ServerMesagesUiHandlers
{
   /** The attachment point for dependent modal dialogs on this view */
   public static final NestedSlot MODAL_CONTENT = new NestedSlot();
   
   /**
    * This view interface that must be implemented by the concrete view according to GWTP
    * framework. Defines the exposed business methods.
    */
   public interface MyView
   extends View, HasUiHandlers<ServerMesagesUiHandlers>
   {
      /**
       * Sets the server messages list model.
       * 
       * @param    messages
       *           The array of messages
       */
      void setMessages(String[] messages);
   }

   /**
    * Defines the proxy place for GWTP framework. Binds /help/messages url to this presenter.
    */
   @ProxyStandard
   @NameToken(NameTokens.SERVER_MESSAGES)
   public interface MyProxy
   extends ProxyPlace<ServerMessagesPresenter>
   {}

   /** Administration server interface */
   private final AdminServiceAsync adminService;
   
   /** The common modal dialogs manager */
   private final ModalDialogs modalDialogsManager;
   
   /** The server alarms manager */
   private final Alarm serverAlarmsManager;
   
   /**
    * Defines the Server Messages presenter.
    * 
    * @param    eventBus
    *           The event bus
    * @param    view
    *           The associated view
    * @param    proxy
    *           The proxy place
    * @param    placeManager
    *           The place manager
    * @param    adminService
    *           The administration service
    * @param    modalDialogsManager
    *           The common modal dialogs manager
    * @param    serverAlarmsManager
    *           The server alarms
    */
   @Inject
   public ServerMessagesPresenter(EventBus eventBus,
                                  MyView view,
                                  MyProxy proxy,
                                  PlaceManager placeManager,
                                  AdminServiceAsync adminService,
                                  ModalDialogs modalDialogsManager,
                                  Alarm serverAlarmsManager)
   {
      super(eventBus, view, proxy, HomePresenter.SLOT_CONTENT, placeManager);
      this.adminService = adminService;
      this.modalDialogsManager = modalDialogsManager;
      this.serverAlarmsManager = serverAlarmsManager;
      getView().setUiHandlers(this);
   }

   /**
    * Loads the data before the associated view will be displayed.
    * 
    * @param    request
    *           The current request
    */
   @Override
   public void prepareFromRequest(PlaceRequest request)
   {
      super.prepareFromRequest(request);
      String action = request.getParameter("action", "");
      if ("clear".equals(action))
      {
         clearMessages();
      }
      else
      {
         showMessages();
      }
   }
   
   /**
    * Retrieves the server messages asynchronously and updates its view accordingly if this action
    * is completed successfully.
    */
   private void showMessages()
   {
      this.serverAlarmsManager.getAllMessages(messages -> {
         if (messages != null)
         {
            getView().setMessages(messages.toArray(new String[messages.size()]));
         }
      }, false);
   }
   
   /**
    * Requests to clear the server messages and updates its view accordingly if this action is
    * completed.
    */
   private void clearMessages()
   {
      this.adminService.clearMessages(new AsyncCallback<Void>()
      {
         
         @Override
         public void onSuccess(Void result)
         {
            showMessages();
         }
         
         @Override
         public void onFailure(Throwable caught)
         {
            logger.log(Level.SEVERE, "Failed to call clearMessages", caught);
            showMessages();
         }
      });
   }

   /**
    * Hides the Server Messages view and displays the invoker view.
    */
   @Override
   public void onBackAction()
   {
      History.back();
   }
}