protected final void handleException(final Exception e) {
   final OnFail onFail = getOnFail();
   if (OnFail.ThrowException == onFail) {
     throw AdempiereException.wrapIfNeeded(e);
   } else if (OnFail.ShowErrorPopup == onFail) {
     clientUI.error(getParentWindowNo(), e);
   } else if (OnFail.SilentlyIgnore == onFail) {
     // Ignore it silently. Don't do logging.
     // logger.warn("Got error while running: " + runnable + ". Ignored.", e);
     return;
   } else if (OnFail.UseHandler == onFail) {
     final IExceptionHandler exceptionHandler = getExceptionHandler();
     if (exceptionHandler == null) {
       logger.warn(
           "No exception handler was configurated and OnFail=UseHandler. Throwing the exception");
       // fallback
       throw AdempiereException.wrapIfNeeded(e);
     } else {
       exceptionHandler.handleException(e);
     }
   }
   // Fallback: throw the exception
   else {
     throw AdempiereException.wrapIfNeeded(e);
   }
 }
 private EntityTypeEntry getEntityTypeEntry(final String entityType) {
   final EntityTypeEntry entry = getEntityTypeEntryOrNull(entityType);
   if (entry == null) {
     final AdempiereException ex =
         new AdempiereException(
             "No EntityType entry found for entity type: "
                 + entityType
                 + "\n Available EntityTypes are: "
                 + cache.keySet());
     logger.warn(ex.getLocalizedMessage(), ex);
   }
   return entry;
 }
  /** Asserts dialog is opened out of transaction. */
  public static final void assertUIOutOfTransaction() {
    final ITrxManager trxManager = Services.get(ITrxManager.class);
    final String trxName = trxManager.getThreadInheritedTrxName(OnTrxMissingPolicy.ReturnTrxNone);
    if (!trxManager.isNull(trxName)) {
      final AdempiereException ex =
          new AdempiereException(
              "Opening a dialog while running in a trasaction it's always a bad idea"
                  + " because the database will be kept locked until the user will answer.");

      // NOTE: this issue is so critical that it's better to throw exception instead to just advice
      if (Services.get(IDeveloperModeBL.class).isEnabled()) {
        throw ex;
      } else {
        // In case we run in production, we won't fail because that would be a show stopper, but we
        // will log the exception.
        log.log(Level.SEVERE, ex.getLocalizedMessage(), ex);
      }
    }
  }
  public final <ModelType> ModelType retrieveModel(
      final Properties ctx,
      final String tableName,
      final Class<?> modelClass,
      final ResultSet rs,
      final String trxName) {
    final PO po = getPO(ctx, tableName, rs, trxName);

    //
    // Case: we have a modelClass specified
    if (modelClass != null) {
      final Class<? extends PO> poClass = po.getClass();
      if (poClass.isAssignableFrom(modelClass)) {
        @SuppressWarnings("unchecked")
        final ModelType model = (ModelType) po;
        return model;
      } else {
        @SuppressWarnings("unchecked")
        final ModelType model = (ModelType) InterfaceWrapperHelper.create(po, modelClass);
        return model;
      }
    }
    //
    // Case: no "clazz" and no "modelClass"
    else {
      if (log.isDebugEnabled()) {
        final AdempiereException ex =
            new AdempiereException(
                "Query does not have a modelClass defined and no 'clazz' was specified as parameter."
                    + "We need to avoid this case, but for now we are trying to do a force casting"
                    + "\nQuery: "
                    + this
                    + "\nPO: "
                    + po);
        log.debug(ex.getLocalizedMessage(), ex);
      }

      @SuppressWarnings("unchecked")
      final ModelType model = (ModelType) po;
      return model;
    }
  }