Package com.daikit.daikit4gxt.client.controller

Source Code of com.daikit.daikit4gxt.client.controller.BaseErrorHandler$ExceptionDetailsMessageBox

/**
* Copyright (C) 2013 DaiKit.com - daikit4gxt module (admin@daikit.com)
*
*         Project home : http://code.daikit.com/daikit4gxt
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*         http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.daikit.daikit4gxt.client.controller;

import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;

import com.daikit.commons.shared.bean.AbstractDkBeanWithId;
import com.daikit.commons.shared.bean.BaseUser;
import com.daikit.commons.shared.exception.DkAmbiguousIdentifierException;
import com.daikit.commons.shared.exception.DkBadParametersException;
import com.daikit.commons.shared.exception.DkConversionException;
import com.daikit.commons.shared.exception.DkDataIntegrityException;
import com.daikit.commons.shared.exception.DkDataValidationException;
import com.daikit.commons.shared.exception.DkException;
import com.daikit.commons.shared.exception.DkServerUnreachableException;
import com.daikit.commons.shared.exception.DkUnknownErrorException;
import com.daikit.commons.shared.exception.DkUnknownIdentifierException;
import com.daikit.commons.shared.exception.DkUnsupportedCaseException;
import com.daikit.commons.shared.exception.DkUnsupportedMethodException;
import com.daikit.commons.shared.exception.authentication.DkBadLoginOrPasswordException;
import com.daikit.commons.shared.exception.authentication.DkHackingAttemptException;
import com.daikit.commons.shared.exception.authentication.DkNotEnoughPermissionException;
import com.daikit.commons.shared.exception.authentication.DkUnauthorizedZoneException;
import com.daikit.commons.shared.exception.authentication.DkUserAlreadyAuthenticatedException;
import com.daikit.commons.shared.exception.authentication.DkUserLoggedChangedException;
import com.daikit.commons.shared.exception.authentication.DkUserNotAuthenticatedException;
import com.daikit.commons.shared.miscs.Couple;
import com.daikit.commons.shared.utils.DkObjectUtils;
import com.daikit.daikit4gxt.client.DkMain;
import com.daikit.daikit4gxt.client.action.BaseAction;
import com.daikit.daikit4gxt.client.action.standard.BaseOnServerLoggedUserChangedAction;
import com.daikit.daikit4gxt.client.log.BaseLogger;
import com.daikit.daikit4gxt.client.ui.popup.MyFormDialog;
import com.daikit.daikit4gxt.client.ui.popup.MyMessageBox;
import com.daikit.daikit4gxt.client.ui.popup.MyMessageBox.AfterCloseListener;
import com.daikit.daikit4gxt.client.ui.popup.MyMessageBox.MessageType;
import com.daikit.daikit4gxt.client.ui.popup.MyOkCancelMessageBox;
import com.daikit.daikit4gxt.client.ui.popup.MyOkDetailsMessageBox;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.StatusCodeException;
import com.sencha.gxt.widget.core.client.form.TextArea;


/**
* Base Error handler to handle actions exceptions.
*
* @author tcaselli
* @version $Revision$ Last modifier: $Author$ Last commit: $Date$
*/
public class BaseErrorHandler
{

  protected BaseLogger log = BaseLogger.getLog(BaseErrorHandler.class);

  // *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
  // METHOD CALLED FROM CALLBACK ON FAILURE METHOD
  // *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-

  /**
   * Override this method to provide custom Error Management for your application. The overriding method should do its
   * stuff and then call this super method.
   *
   * @param callingAction
   *           the action in which the error was risen.
   * @param exception
   *           the risen exception
   */
  public void handle(final BaseAction<?> callingAction, final Throwable exception)
  {
    // If the error is an Authentication Error it will be handled by this method
    if (exception instanceof StatusCodeException)
    {
      logError(callingAction, exception);
      handleKOStatusCode(callingAction, (StatusCodeException) exception);
    }
    if (exception instanceof DkBadLoginOrPasswordException)
    {
      logError(callingAction, exception);
      handleAuthenticationErrorBadLoginOrPassword(callingAction, (DkBadLoginOrPasswordException) exception);
    }
    else if (exception instanceof DkNotEnoughPermissionException)
    {
      logError(callingAction, exception);
      handleAuthenticationErrorNotEnoughPermission(callingAction, (DkNotEnoughPermissionException) exception);
    }
    else if (exception instanceof DkUserNotAuthenticatedException)
    {
      logError(callingAction, exception);
      handleAuthenticationErrorUserNotAuthenticated(callingAction, (DkUserNotAuthenticatedException) exception);
    }
    else if (exception instanceof DkUnauthorizedZoneException)
    {
      logError(callingAction, exception);
      handleAuthenticationErrorUnauthorizedZone(callingAction, (DkUnauthorizedZoneException) exception);
    }
    else if (exception instanceof DkUserLoggedChangedException)
    {
      logError(callingAction, exception);
      handleAuthenticationErrorUserLoggedChanged(callingAction, (DkUserLoggedChangedException) exception);
    }
    else if (exception instanceof DkUserAlreadyAuthenticatedException)
    {
      logError(callingAction, exception);
      handleAuthenticationErrorUserAlreadyAuthenticated(callingAction, (DkUserAlreadyAuthenticatedException) exception);
    }
    else if (exception instanceof DkHackingAttemptException)
    {
      logError(callingAction, exception);
      handleAuthenticationErrorHackingAttempt(callingAction, (DkHackingAttemptException) exception);
    }
    else if (exception instanceof DkServerUnreachableException)
    {
      logError(callingAction, exception);
      handleServerUnreachableError(callingAction, (DkServerUnreachableException) exception);
    }
    else if (exception instanceof DkUnknownErrorException)
    {
      logError(callingAction, exception);
      handleUnknownError(callingAction, (DkUnknownErrorException) exception);
    }
    else if (exception instanceof DkBadParametersException)
    {
      logError(callingAction, exception);
      handleBadParametersError(callingAction, (DkBadParametersException) exception);
    }
    else if (exception instanceof DkAmbiguousIdentifierException)
    {
      logError(callingAction, exception);
      handleAmbiguousIdentifierException(callingAction, (DkAmbiguousIdentifierException) exception);
    }
    else if (exception instanceof DkConversionException)
    {
      logError(callingAction, exception);
      handleConversionException(callingAction, (DkConversionException) exception);
    }
    else if (exception instanceof DkDataIntegrityException)
    {
      logError(callingAction, exception);
      handleDataIntegrityException(callingAction, (DkDataIntegrityException) exception);
    }
    else if (exception instanceof DkDataValidationException)
    {
      logError(callingAction, exception);
      handleDataValidationException(callingAction, (DkDataValidationException) exception);
    }
    else if (exception instanceof DkUnknownIdentifierException)
    {
      logError(callingAction, exception);
      handleUnknownIdentifierException(callingAction, (DkUnknownIdentifierException) exception);
    }
    else if (exception instanceof DkUnsupportedMethodException)
    {
      logError(callingAction, exception);
      handleUnsupportedMethodException(callingAction, (DkUnsupportedMethodException) exception);
    }
    else if (exception instanceof DkUnsupportedCaseException)
    {
      logError(callingAction, exception);
      handleUnsupportedCaseException(callingAction, (DkUnsupportedCaseException) exception);
    }
    else
    {
      if (log.isErrorEnabled())
      {
        log.error("UNHANDLED EXCEPTION callingAction=[" + callingAction + "] ", exception);
      }
      handleUnexpectedError(callingAction, exception);
    }
  }

  protected void logError(final BaseAction<?> callingAction, final Throwable exception)
  {
    if (log.isErrorEnabled())
    {
      log.error("EXCEPTION callingAction=[" + callingAction + "] code=[" + exception.getMessage() + "]", exception);
    }
  }

  // *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
  // ERRORS ACTIONS
  // *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-

  protected void handleKOStatusCode(final BaseAction<?> callingAction, final StatusCodeException exception)
  {
    displayError(DkMain.i18n().error_ko_status_code_popup_title(), DkMain.i18n().error_ko_status_code_popup_message(),
        exception, callingAction, new AfterCloseListener()
        {
          @Override
          public void afterClose()
          {
            onKOStatusCodePopupClick(callingAction);
          }
        });
  }

  /**
   * Method showing message box displaying informative message after an exception is thrown in surrounding
   * {@link BaseAction} with message = {@link BaseErrorCodes.Authentication#BAD_LOGIN_OR_PASSWORD}. Override this
   * method to display a custom message box. If you only want to change behavior of the OK button just override
   * {@link #onBadLoginOrPasswordPopupClick(BaseAction)}.
   *
   * @param callingAction
   *           the action in which the error was risen.
   * @param exception
   *           the exception
   */
  public void handleAuthenticationErrorBadLoginOrPassword(final BaseAction<?> callingAction,
      final DkBadLoginOrPasswordException exception)
  {
    displayError(DkMain.i18n().error_authentication_failure_bad_login_or_password_popup_title(), DkMain.i18n()
        .error_authentication_failure_bad_login_or_password_popup_message(), exception, callingAction,
        new AfterCloseListener()
        {
          @Override
          public void afterClose()
          {
            onBadLoginOrPasswordPopupClick(callingAction);
          }
        });
  }

  /**
   * Method showing message box displaying informative message after an exception is thrown in surrounding
   * {@link BaseAction} with message = {@link BaseErrorCodes.Authentication#NOT_ENOUGH_PERMISSION}. Override this
   * method to display a custom message box. If you only want to change behavior of the OK button just override
   * {@link #onNotEnoughPermissionPopupClick(BaseAction)}.
   *
   * @param callingAction
   *           the action in which the error was risen.
   */
  public void handleAuthenticationErrorNotEnoughPermission(final BaseAction<?> callingAction,
      final DkNotEnoughPermissionException exception)
  {
    displayError(DkMain.i18n().error_authentication_failure_not_enough_permission_popup_title(), DkMain.i18n()
        .error_authentication_failure_not_enough_permission_popup_message(), exception, callingAction,
        new AfterCloseListener()
        {
          @Override
          public void afterClose()
          {
            onNotEnoughPermissionPopupClick(callingAction);
          }
        });
  }

  /**
   * Method showing message box displaying informative message after an exception is thrown in surrounding
   * {@link BaseAction} with message = {@link BaseErrorCodes.Authentication#USER_NOT_AUTHENTICATED}. Override this
   * method to display a custom message box. If you only want to change behavior of the OK button just override
   * {@link #onUserNotAuthenticatedPopupClick(BaseAction)}.<br>
   * If the application is stand-alone or if the embedded reconnection is authorized, the application will display the
   * connection pop-up to let the user get reconnected and then continues the current action.
   *
   * @param callingAction
   *           the action in which the error was risen.
   */
  public void handleAuthenticationErrorUserNotAuthenticated(final BaseAction<?> callingAction,
      final DkUserNotAuthenticatedException exception)
  {
    final String message = DkMain.config().isStandalone() || DkMain.config().isEmbeddedReconnectionAuthorized() ? DkMain.i18n()
        .error_authentication_failure_user_not_authenticated_popup_message_but_will_try_to_reconnect() : DkMain.i18n()
        .error_authentication_failure_user_not_authenticated_popup_message();

    displayError(DkMain.i18n().error_authentication_failure_user_not_authenticated_popup_title(), message, exception,
        callingAction, new AfterCloseListener()
        {
          @Override
          public void afterClose()
          {
            onUserNotAuthenticatedPopupClick(callingAction);
          }
        });
  }

  /**
   * Method showing message box displaying informative message after an exception is thrown in surrounding
   * {@link BaseAction} with message = {@link BaseErrorCodes.Authentication#UNAUTHORIZED_ZONE}. Override this method to
   * display a custom message box. If you only want to change behavior of the OK button just override
   * {@link #onUnauthorizedZonePopupClick(BaseAction)}.
   *
   * @param callingAction
   *           the action in which the error was risen.
   */
  public void handleAuthenticationErrorUnauthorizedZone(final BaseAction<?> callingAction,
      final DkUnauthorizedZoneException exception)
  {
    displayError(DkMain.i18n().error_authentication_failure_unauthorized_zone_popup_title(), DkMain.i18n()
        .error_authentication_failure_unauthorized_zone_popup_message(), exception, callingAction, new AfterCloseListener()
    {
      @Override
      public void afterClose()
      {
        onUnauthorizedZonePopupClick(callingAction);
      }
    });
  }

  /**
   * Method showing message box displaying informative message after an exception is thrown in surrounding
   * {@link BaseAction} with message = {@link BaseErrorCodes.Authentication#USER_LOGGED_CHANGED}. Override this method
   * to display a custom message box. If you only want to change behavior of the OK button just override
   * {@link #onUserLoggedChangedPopupClick(BaseAction, boolean)}.
   *
   * @param callingAction
   *           the action in which the error was risen.
   * @param exception
   */
  public void handleAuthenticationErrorUserLoggedChanged(final BaseAction<?> callingAction,
      final DkUserLoggedChangedException exception)
  {
    final BaseUser userLogged = DkMain.model().getUserLogged();
    final BaseUser previousLoggedUser = DkMain.model().getPreviousUserLogged();
    if (DkObjectUtils.equalsWithNull(AbstractDkBeanWithId.getId(userLogged), exception.getNewLoggedUserId())
        || userLogged == null
        && DkObjectUtils.equalsWithNull(AbstractDkBeanWithId.getId(previousLoggedUser), exception.getNewLoggedUserId()))
    {
      // In this case the new logged user is the same than the old one.
      // We just launch the reconnection action.
      onUserLoggedChangedPopupClick(callingAction, false);
    }
    else
    {
      new MyOkCancelMessageBox(DkMain.i18n().error_authentication_failure_user_logged_changed_popup_title(), DkMain.i18n()
          .error_authentication_failure_user_logged_changed_popup_message(), MessageType.WARNING)
      {
        @Override
        public void processOk()
        {
          onUserLoggedChangedPopupClick(callingAction, true);
        }

        @Override
        public void processCancelOrClose()
        {
          DkMain.controller().invalidateUi(false);
        }

      };
    }
  }

  /**
   * Method showing message box displaying informative message after an exception is thrown in surrounding
   * {@link BaseAction} with message = {@link BaseErrorCodes.Authentication#USER_ALREADY_AUTHENTICATED}. Override this
   * method to display a custom message box. If you only want to change behavior of the OK button just override
   * {@link #onUserAlreadyAuthenticatedPopupClick(BaseAction)}.
   *
   * @param callingAction
   *           the action in which the error was risen.
   */
  public void handleAuthenticationErrorUserAlreadyAuthenticated(final BaseAction<?> callingAction,
      final DkUserAlreadyAuthenticatedException exception)
  {
    displayError(DkMain.i18n().error_authentication_failure_user_already_authenticated_popup_title(), DkMain.i18n()
        .error_authentication_failure_user_already_authenticated_popup_message(), exception, callingAction,
        new AfterCloseListener()
        {
          @Override
          public void afterClose()
          {
            onUserAlreadyAuthenticatedPopupClick(callingAction);
          }
        });
  }

  /**
   * Method showing message box displaying informative message after an exception is thrown in surrounding
   * {@link BaseAction} with message = {@link BaseErrorCodes.Authentication#HACKING_ATTEMPT}. Override this method to
   * display a custom message box. If you only want to change behavior of the OK button just override
   * {@link #onHackingAttemptPopupClick(BaseAction)}.
   *
   * @param callingAction
   *           the action in which the error was risen.
   */
  public void handleAuthenticationErrorHackingAttempt(final BaseAction<?> callingAction,
      final DkHackingAttemptException exception)
  {
    displayError(DkMain.i18n().error_authentication_failure_hacking_attempt_popup_title(), DkMain.i18n()
        .error_authentication_failure_hacking_attempt_popup_message(), exception, callingAction, new AfterCloseListener()
    {
      @Override
      public void afterClose()
      {
        onHackingAttemptPopupClick(callingAction);
      }
    });
  }

  /**
   * Method showing message box displaying informative message after an exception is thrown in surrounding
   * {@link BaseAction} with message = {@link BaseErrorCodes.Common#SERVER_UNREACHABLE}. Override this method to
   * display a custom message box. If you only want to change behavior of the OK button just override
   * {@link #onServerUnreachablePopupClick(BaseAction)}.
   *
   * @param callingAction
   *           the action in which the error was risen.
   */
  public void handleServerUnreachableError(final BaseAction<?> callingAction, final DkServerUnreachableException exception)
  {
    displayError(DkMain.i18n().error_Title(), DkMain.i18n().error_ServerUnreachable(), exception, callingAction,
        new AfterCloseListener()
        {
          @Override
          public void afterClose()
          {
            onServerUnreachablePopupClick(callingAction);
          }
        });
  }

  /**
   * Method showing message box displaying informative message after an exception is thrown in surrounding
   * {@link BaseAction} with message = {@link BaseErrorCodes.Common#UNKNWOWN_ERROR}. Override this method to display a
   * custom message box. If you only want to change behavior of the OK button just override
   * {@link #onUnknownErrorPopupClick(BaseAction)}.
   *
   * @param callingAction
   *           the action in which the error was risen.
   */
  public void handleUnknownError(final BaseAction<?> callingAction, final DkUnknownErrorException exception)
  {
    displayError(DkMain.i18n().error_Title(), DkMain.i18n().error_UnknownError(), exception, callingAction,
        new AfterCloseListener()
        {
          @Override
          public void afterClose()
          {
            onUnknownErrorPopupClick(callingAction);
          }
        });
  }

  /**
   * Method showing message box displaying informative message after an exception is thrown in surrounding
   * {@link BaseAction} with message = {@link BaseErrorCodes.Common#BAD_PARAMETERS}. Override this method to display a
   * custom message box. If you only want to change behavior of the OK button just override
   * {@link #onBadParametersPopupClick(BaseAction)}.
   *
   * @param callingAction
   *           the action in which the error was risen.
   */
  public void handleBadParametersError(final BaseAction<?> callingAction, final DkBadParametersException exception)
  {
    displayError(DkMain.i18n().error_Title(), DkMain.i18n().error_BadParameters(), exception, callingAction,
        new AfterCloseListener()
        {
          @Override
          public void afterClose()
          {
            onBadParametersPopupClick(callingAction);
          }
        });
  }

  /**
   * Method showing message box displaying informative message after an exception is thrown in surrounding
   * {@link BaseAction} with UNKNOWN MESSAGE. These exception can either be risen on client side due to any possible
   * reason that was not expected. Override this method to display a custom message box. If you only want to change
   * behavior of the OK button just override {@link #onUnexpectedErrorPopupClick(BaseAction)} .
   *
   * @param callingAction
   *           the action in which the error was risen.
   * @param exception
   */
  public void handleUnexpectedError(final BaseAction<?> callingAction, final Throwable exception)
  {
    displayError(DkMain.i18n().error_Title(), DkMain.i18n().error_UnexpectedError(), exception, callingAction,
        new AfterCloseListener()
        {
          @Override
          public void afterClose()
          {
            onUnexpectedErrorPopupClick(callingAction);
          }
        });
  }

  /**
   *
   * @param callingAction
   * @param exception
   */
  public void handleUnsupportedMethodException(final BaseAction<?> callingAction, final DkUnsupportedMethodException exception)
  {
    displayError(DkMain.i18n().error_Title(), DkMain.i18n().error_UnsupportedMethodException(), exception, callingAction,
        new AfterCloseListener()
        {
          @Override
          public void afterClose()
          {
            onUnsupportedMethodExceptionPopupClick(callingAction);
          }
        });
  }

  /**
   *
   * @param callingAction
   * @param exception
   */
  public void handleUnsupportedCaseException(final BaseAction<?> callingAction, final DkUnsupportedCaseException exception)
  {
    displayError(DkMain.i18n().error_Title(), DkMain.i18n().error_UnsupportedCaseException(), exception, callingAction,
        new AfterCloseListener()
        {
          @Override
          public void afterClose()
          {
            onUnsupportedCaseExceptionPopupClick(callingAction);
          }
        });
  }

  /**
   *
   * @param callingAction
   * @param exception
   */
  public void handleUnknownIdentifierException(final BaseAction<?> callingAction, final DkUnknownIdentifierException exception)
  {
    displayError(DkMain.i18n().error_Title(), DkMain.i18n().error_UnknownIdentifierException(), exception, callingAction,
        new AfterCloseListener()
        {
          @Override
          public void afterClose()
          {
            onUnknownIdentifierExceptionPopupClick(callingAction);
          }
        });
  }

  /**
   *
   * @param callingAction
   * @param exception
   */
  public void handleDataValidationException(final BaseAction<?> callingAction, final DkDataValidationException exception)
  {
    displayError(DkMain.i18n().error_Title(), DkMain.i18n().error_DataValidationException(), exception, callingAction,
        new AfterCloseListener()
        {
          @Override
          public void afterClose()
          {
            onDataValidationExceptionPopupClick(callingAction);
          }
        });
  }

  /**
   *
   * @param callingAction
   * @param exception
   */
  public void handleDataIntegrityException(final BaseAction<?> callingAction, final DkDataIntegrityException exception)
  {
    displayError(DkMain.i18n().error_Title(), DkMain.i18n().error_DataIntegrityException(), exception, callingAction,
        new AfterCloseListener()
        {
          @Override
          public void afterClose()
          {
            onDataIntegrityExceptionPopupClick(callingAction);
          }
        });
  }

  /**
   *
   * @param callingAction
   * @param exception
   */
  public void handleConversionException(final BaseAction<?> callingAction, final DkConversionException exception)
  {
    displayError(DkMain.i18n().error_Title(), DkMain.i18n().error_ConversionException(), exception, callingAction,
        new AfterCloseListener()
        {
          @Override
          public void afterClose()
          {
            onConversionExceptionPopupClick(callingAction);
          }
        });
  }

  /**
   *
   * @param callingAction
   * @param exception
   */
  public void handleAmbiguousIdentifierException(final BaseAction<?> callingAction,
      final DkAmbiguousIdentifierException exception)
  {
    displayError(DkMain.i18n().error_Title(), DkMain.i18n().error_AmbiguousIdentifierException(), exception, callingAction,
        new AfterCloseListener()
        {
          @Override
          public void afterClose()
          {
            onAmbiguousIdentifierExceptionPopupClick(callingAction);
          }
        });
  }

  // *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
  // DISPLAY ERROR METHODS
  // *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-

  /**
   * Method user to display error message box for each methods in this class. Override this method to provide custom
   * message boxes in your application for all errors.
   *
   * @param title
   *           the title of the message box
   * @param message
   *           the message of the message box
   * @param exception
   *           the exception
   * @param action
   *           the action
   * @param callback
   *           the callback to be called after message box is closed
   */
  protected void displayError(final String title, final String message, final Throwable exception, final BaseAction<?> action,
      final AfterCloseListener afterCloseListener)
  {
    if (exception == null)
    {
      MyMessageBox.alert(title, message, afterCloseListener);
    }
    else
    {
      new MyOkDetailsMessageBox(title, message, MessageType.ERROR)
      {
        @Override
        public void processClose()
        {
          super.processClose();
          afterCloseListener.afterClose();
        }

        @Override
        public void processOk()
        {
          afterCloseListener.afterClose();
        }

        @Override
        public void processDetails()
        {
          new ExceptionDetailsMessageBox(action, exception).show();
        };
      }.show();
    }
  }

  private static final String newLine = "\n";

  private class ExceptionDetailsMessageBox extends MyFormDialog
  {
    public ExceptionDetailsMessageBox(final BaseAction<?> action, final Throwable exception)
    {
      super(false, 1, DisplayableButton.CANCEL);
      setResizable(true);
      final TextArea textArea = new TextArea();
      textArea.setHeight(500);
      setWidth(1050);
      final StringBuilder stringBuilder = new StringBuilder(1000);
      stringBuilder.append("ERROR REPORT: [").append(new Date().toString()).append("]").append(newLine);
      stringBuilder.append("Page URL: ").append(Window.Location.getHref()).append(newLine).append(newLine);
      if (action != null)
      {
        stringBuilder.append("Actions chain :").append(newLine);
        BaseAction<?> currentAction = action;
        int count = 0; // prevent infinite loop ... we never know :P
        while (currentAction != null && count++ < 100)
        {
          if (count > 1)
          {
            stringBuilder.append(newLine).append("Called by: ");
          }
          stringBuilder.append(currentAction.getProgressMessage());
          currentAction = currentAction.getParentChainAction();
        }
        stringBuilder.append(newLine).append(newLine);
      }
      if (exception instanceof DkException)
      {
        final DkException gwtlibException = (DkException) exception;
        stringBuilder.append("Server Error Context: ").append(newLine);
        for (final Entry<String, String> entry : gwtlibException.getContext().getContextMap().entrySet())
        {
          stringBuilder.append(entry.getKey()).append(": ").append(entry.getValue()).append(newLine);
        }
        stringBuilder.append(newLine).append(newLine);
      }
      if (exception.getMessage() != null)
      {
        stringBuilder.append("Exception Message: ");
      }
      if (exception instanceof DkException)
      {
        final DkException gwtlibException = (DkException) exception;
        final Iterator<Couple<String, List<String>>> it = gwtlibException.getContext().getStackTrace().iterator();
        while (it.hasNext())
        {
          final Couple<String, List<String>> couple = it.next();
          stringBuilder.append(couple.first).append(newLine);
          for (final String stackTraceElement : couple.second)
          {
            stringBuilder.append(stackTraceElement).append(newLine);
          }
          if (it.hasNext())
          {
            stringBuilder.append(newLine).append("Caused by: ");
          }
        }
      }
      else
      {
        stringBuilder.append(exception.getMessage()).append(newLine);
        for (final StackTraceElement stackTraceElement : exception.getStackTrace())
        {
          stringBuilder.append(stackTraceElement).append(newLine);
        }
        if (exception.getCause() != null
            && !DkObjectUtils.equalsWithNull(exception.getMessage(), exception.getCause().getMessage()))
        {
          stringBuilder.append(newLine).append("Caused by: ").append(exception.getCause().getMessage()).append(newLine);
          for (final StackTraceElement stackTraceElement : exception.getCause().getStackTrace())
          {
            stringBuilder.append(stackTraceElement).append(newLine);
          }
        }
      }
      stringBuilder.append(newLine);
      textArea.setValue(stringBuilder.toString());
      addField(textArea);
    }

    @Override
    protected String getLabelPopupTitle()
    {
      return DkMain.i18n().error_details_Title();
    }

    @Override
    protected String getCancelButtonLabel()
    {
      return DkMain.i18n().label_close();
    }
  }

  /**
   * Wrapper to {@link #displayError(String, String, AfterCloseListener)} with Null Listener.
   *
   * @param title
   *           the title of the message box
   * @param message
   *           the message of the message box
   */
  protected void displayError(final String title, final String message)
  {
    displayError(title, message, null, null, null);
  }

  // *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
  // ERRORS CALLBACKS
  // *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-

  /**
   * Method called by the OK or close button callback of the message box displayed by
   * {@link #handleKOStatusCode(BaseAction, StatusCodeException)} . Override this method to provide custom behavior.
   * Default is invalidating UI.
   *
   * @param callingAction
   *           the action in which the error was risen.
   */
  protected void onKOStatusCodePopupClick(final BaseAction<?> callingAction)
  {
    DkMain.controller().invalidateUi(false);
  }

  /**
   * Method called by the OK or close button callback of the message box displayed by
   * {@link #handleAuthenticationErrorBadLoginOrPassword(BaseAction, DkBadLoginOrPasswordException)} . Override this
   * method to provide custom behavior. Default is invalidating UI and if application is stand-alone, displays
   * connection message box.
   *
   * @param callingAction
   *           the action in which the error was risen.
   */
  protected void onBadLoginOrPasswordPopupClick(final BaseAction<?> callingAction)
  {
    DkMain.controller().invalidateUi(false);
    if (DkMain.config().isStandalone())
    {
      DkMain.controller().getConnectionPopupInstance().show(callingAction);
    }
  }

  /**
   * Method called by the OK or close button callback of the message box displayed by
   * {@link #handleAuthenticationErrorUserNotAuthenticated(BaseAction)} . Override this method to provide custom
   * behavior. Default is invalidating UI and if application is stand-alone, displays connection message box.
   *
   * @param callingAction
   *           the action in which the error was risen.
   */
  protected void onUserNotAuthenticatedPopupClick(final BaseAction<?> callingAction)
  {
    DkMain.model().setUserLogged(null);
    DkMain.controller().invalidateUi(false);
    if (DkMain.config().isStandalone())
    {
      DkMain.controller().getConnectionPopupInstance().show(callingAction);
    }
  }

  /**
   * Method called by the YES button callback of the message box displayed by
   * {@link #handleAuthenticationErrorUserLoggedChanged(BaseAction)} . Override this method to provide custom behavior.
   * Default is invalidating UI and if application is stand-alone, call {@link BaseAction#execute()} on
   * {@link BaseOnServerLoggedUserChangedAction#get()}. If another button than YES is pressed, just an UI invalidation
   * occurs.
   *
   * @param callingAction
   *           the action in which the error was risen.
   * @param userReallyChanged
   *           is true if the new logged user is really different from the old one and it is just an error risen
   *           because of a reconnection. False if the previous logged user is really different from the new one.
   */
  protected void onUserLoggedChangedPopupClick(final BaseAction<?> callingAction, final boolean userReallyChanged)
  {
    DkMain.controller().invalidateUi(false);
    if (!DkMain.config().isStandalone() || DkMain.config().isEmbeddedReconnectionAuthorized())
    {
      // BaseAction<?> actionOnUserNotAuthenticated = BaseOnUserNotAuthenticatedAction.get() ;
      // actionOnUserNotAuthenticated.setLastChainAction(BaseOnServerLoggedUserChangedAction.get()) ;
      // actionOnUserNotAuthenticated.execute() ;
      BaseOnServerLoggedUserChangedAction.get(userReallyChanged ? null : callingAction).execute();
    }
  }

  /**
   * Method called by the OK or close button callback of the message box displayed by
   * {@link #handleAuthenticationErrorNotEnoughPermission(BaseAction)} . Override this method to provide custom
   * behavior. Default is just invalidating UI.
   *
   * @param callingAction
   *           the action in which the error was risen.
   */
  protected void onNotEnoughPermissionPopupClick(final BaseAction<?> callingAction)
  {
    DkMain.controller().invalidateUi(false);
  }

  /**
   * Method called by the OK or close button callback of the message box displayed by
   * {@link #handleAuthenticationErrorUnauthorizedZone(BaseAction)} . Override this method to provide custom behavior.
   * Default is just invalidating UI.
   *
   * @param callingAction
   *           the action in which the error was risen.
   * @param exception
   */
  protected void onUnauthorizedZonePopupClick(final BaseAction<?> callingAction)
  {
    DkMain.controller().invalidateUi(false);
  }

  /**
   * Method called by the OK or close button callback of the message box displayed by
   * {@link #handleAuthenticationErrorUserAlreadyAuthenticated(BaseAction)} . Override this method to provide custom
   * behavior. Default is just invalidating UI.
   *
   * @param callingAction
   *           the action in which the error was risen.
   */
  protected void onUserAlreadyAuthenticatedPopupClick(final BaseAction<?> callingAction)
  {
    DkMain.controller().invalidateUi(false);
  }

  /**
   * Method called by the OK or close button callback of the message box displayed by
   * {@link #handleAuthenticationErrorHackingAttempt(BaseAction)} . Override this method to provide custom behavior.
   * Default is just invalidating UI.
   *
   * @param callingAction
   *           the action in which the error was risen.
   */
  protected void onHackingAttemptPopupClick(final BaseAction<?> callingAction)
  {
    DkMain.controller().invalidateUi(false);
  }

  /**
   * Method called by the OK or close button callback of the message box displayed by
   * {@link #handleServerUnreachableError(BaseAction)} . Override this method to provide custom behavior. Default is
   * just invalidating UI.
   *
   * @param callingAction
   *           the action in which the error was risen.
   */
  protected void onServerUnreachablePopupClick(final BaseAction<?> callingAction)
  {
    DkMain.controller().invalidateUi(false);
  }

  /**
   * Method called by the OK or close button callback of the message box displayed by
   * {@link #handleUnknownError(BaseAction)}. Override this method to provide custom behavior. Default is just
   * invalidating UI.
   *
   * @param callingAction
   *           the action in which the error was risen.
   */
  protected void onUnknownErrorPopupClick(final BaseAction<?> callingAction)
  {
    DkMain.controller().invalidateUi(false);
  }

  /**
   * Method called by the OK or close button callback of the message box displayed by
   * {@link #handleBadParametersError(BaseAction)}. Override this method to provide custom behavior. Default is just
   * invalidating UI.
   *
   * @param callingAction
   *           the action in which the error was risen.
   */
  protected void onBadParametersPopupClick(final BaseAction<?> callingAction)
  {
    DkMain.controller().invalidateUi(false);
  }

  /**
   * Method called by the OK or close button callback of the message box displayed by
   * {@link #handleUnexpectedError(BaseAction)}. Override this method to provide custom behavior. Default is just
   * invalidating UI.
   *
   * @param callingAction
   *           the action in which the error was risen.
   */
  protected void onUnexpectedErrorPopupClick(final BaseAction<?> callingAction)
  {
    DkMain.controller().invalidateUi(false);
  }

  /**
   * Method called by the OK or close button callback of the message box displayed by
   * {@link #handleUnsupportedMethodException(BaseAction)}. Override this method to provide custom behavior. Default is
   * just invalidating UI.
   *
   * @param callingAction
   *           the action in which the error was risen.
   */
  protected void onUnsupportedMethodExceptionPopupClick(final BaseAction<?> callingAction)
  {
    DkMain.controller().invalidateUi(false);
  }

  /**
   * Method called by the OK or close button callback of the message box displayed by
   * {@link #handleUnsupportedCaseException(BaseAction)}. Override this method to provide custom behavior. Default is
   * just invalidating UI.
   *
   * @param callingAction
   *           the action in which the error was risen.
   */
  protected void onUnsupportedCaseExceptionPopupClick(final BaseAction<?> callingAction)
  {
    DkMain.controller().invalidateUi(false);
  }

  /**
   * Method called by the OK or close button callback of the message box displayed by
   * {@link #handleUnknownIdentifierException(BaseAction)}. Override this method to provide custom behavior. Default is
   * just invalidating UI.
   *
   * @param callingAction
   *           the action in which the error was risen.
   */
  protected void onUnknownIdentifierExceptionPopupClick(final BaseAction<?> callingAction)
  {
    DkMain.controller().invalidateUi(false);
  }

  /**
   * Method called by the OK or close button callback of the message box displayed by
   * {@link #handleDataValidationException(BaseAction)}. Override this method to provide custom behavior. Default is
   * just invalidating UI.
   *
   * @param callingAction
   *           the action in which the error was risen.
   */
  protected void onDataValidationExceptionPopupClick(final BaseAction<?> callingAction)
  {
    DkMain.controller().invalidateUi(false);
  }

  /**
   * Method called by the OK or close button callback of the message box displayed by
   * {@link #handleDataIntegrityException(BaseAction)}. Override this method to provide custom behavior. Default is
   * just invalidating UI.
   *
   * @param callingAction
   *           the action in which the error was risen.
   */
  protected void onDataIntegrityExceptionPopupClick(final BaseAction<?> callingAction)
  {
    DkMain.controller().invalidateUi(false);
  }

  /**
   * Method called by the OK or close button callback of the message box displayed by
   * {@link #handleConversionException(BaseAction)}. Override this method to provide custom behavior. Default is just
   * invalidating UI.
   *
   * @param callingAction
   *           the action in which the error was risen.
   */
  protected void onConversionExceptionPopupClick(final BaseAction<?> callingAction)
  {
    DkMain.controller().invalidateUi(false);
  }

  /**
   * Method called by the OK or close button callback of the message box displayed by
   * {@link #handleAmbiguousIdentifierException(BaseAction)}. Override this method to provide custom behavior. Default
   * is just invalidating UI.
   *
   * @param callingAction
   *           the action in which the error was risen.
   */
  protected void onAmbiguousIdentifierExceptionPopupClick(final BaseAction<?> callingAction)
  {
    DkMain.controller().invalidateUi(false);
  }

}
TOP

Related Classes of com.daikit.daikit4gxt.client.controller.BaseErrorHandler$ExceptionDetailsMessageBox

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.