UIStatementsAspect.java

/*
** Module   : UIStatementsAspect.java
** Abstract : An aspect which associates GenericFrame APIs with their legacy 4GL UI statement.
**
** Copyright (c) 2015-2017, Golden Code Development Corporation.
**
** -#- -I- --Date-- ---------------------------------Description----------------------------------
** 001 CA  20151112 Created initial version.
** 002 CA  20160624 Fix for frame-related statements used with STDOUT, in appserver case.
*/ 
/*
** 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.aspects.ui;

import org.aspectj.lang.*;
import org.aspectj.lang.annotation.*;

import com.goldencode.p2j.ui.*;
import com.goldencode.p2j.util.*;

/**
 * For {@link GenericFrame} APIs emitted at conversion (and associated with 4GL UI statements),
 * it will save the UI statement currently being executed, to distinguish between external calls
 * of these APIs (from legacy converted code) and from internal P2J calls (for example, 
 * {@link GenericFrame#view} is called internally in some cases).
 * <p>
 * When one of these APIs is executed, {@link GenericFrame#setStatement} will save the passed
 * {@link UIStatement}, only if one is not already being executed - thus calls external from
 * P2J code will have priority over internal P2J calls.
 */
@Aspect
public class UIStatementsAspect
{
   /**
    * Triggered around {@link GenericFrame#view} statements, to set the {@link UIStatement#VIEW}.
    * 
    * @param   thisJoinPoint
    *          A join point reference.
    *          
    * @return  The value of the join point method.
    *   
    * @throws  Throwable
    *          Any exceptions are untouched propagated out. 
    */
   @Around("execution(public void com.goldencode.p2j.ui.GenericFrame.view*(..))")
   public Object view(ProceedingJoinPoint thisJoinPoint)
   throws Throwable
   {
      return statementWorker(thisJoinPoint, UIStatement.VIEW);
   }

   /**
    * Triggered around {@link GenericFrame#hide} statements, to set the {@link UIStatement#HIDE}.
    * 
    * @param   thisJoinPoint
    *          A join point reference.
    *          
    * @return  The value of the join point method.
    *   
    * @throws  Throwable
    *          Any exceptions are untouched propagated out. 
    */
   @Around("execution(public void com.goldencode.p2j.ui.GenericFrame.hide*(..))")
   public Object hide(ProceedingJoinPoint thisJoinPoint)
   throws Throwable
   {
      return statementWorker(thisJoinPoint, UIStatement.HIDE);
   }

   /**
    * Triggered around {@link GenericFrame#display} statements, to set the 
    * {@link UIStatement#DISPLAY}.
    * 
    * @param   thisJoinPoint
    *          A join point reference.
    *          
    * @return  The value of the join point method.
    *   
    * @throws  Throwable
    *          Any exceptions are untouched propagated out. 
    */
   @Around("execution(public void com.goldencode.p2j.ui.GenericFrame.display*(..))")
   public Object display(ProceedingJoinPoint thisJoinPoint)
   throws Throwable
   {
      return statementWorker(thisJoinPoint, UIStatement.DISPLAY);
   }

   /**
    * Triggered around {@link GenericFrame#enable} statements, to set the {@link UIStatement#ENABLE}.
    * 
    * @param   thisJoinPoint
    *          A join point reference.
    *          
    * @return  The value of the join point method.
    *   
    * @throws  Throwable
    *          Any exceptions are untouched propagated out. 
    */
   @Around("execution(public void com.goldencode.p2j.ui.GenericFrame.enable*(..))")
   public Object enable(ProceedingJoinPoint thisJoinPoint)
   throws Throwable
   {
      return statementWorker(thisJoinPoint, UIStatement.ENABLE);
   }

   /**
    * Triggered around {@link GenericFrame#disable} statements, to set the 
    * {@link UIStatement#DISABLE}.
    * 
    * @param   thisJoinPoint
    *          A join point reference.
    *          
    * @return  The value of the join point method.
    *   
    * @throws  Throwable
    *          Any exceptions are untouched propagated out. 
    */
   @Around("execution(public void com.goldencode.p2j.ui.GenericFrame.disable*(..))")
   public Object disable(ProceedingJoinPoint thisJoinPoint)
   throws Throwable
   {
      return statementWorker(thisJoinPoint, UIStatement.DISABLE);
   }

   /**
    * Triggered around {@link GenericFrame#set} statements, to set the {@link UIStatement#SET}.
    * 
    * @param   thisJoinPoint
    *          A join point reference.
    *          
    * @return  The value of the join point method.
    *   
    * @throws  Throwable
    *          Any exceptions are untouched propagated out. 
    */
   @Around("execution(public void com.goldencode.p2j.ui.GenericFrame.set(..))")
   public Object set(ProceedingJoinPoint thisJoinPoint)
   throws Throwable
   {
      return statementWorker(thisJoinPoint, UIStatement.SET);
   }

   /**
    * Triggered around {@link GenericFrame#promptFor} statements, to set the 
    * {@link UIStatement#PROMPT_FOR}.
    * 
    * @param   thisJoinPoint
    *          A join point reference.
    *          
    * @return  The value of the join point method.
    *   
    * @throws  Throwable
    *          Any exceptions are untouched propagated out. 
    */
   @Around("execution(public void com.goldencode.p2j.ui.GenericFrame.promptFor*(..))")
   public Object promptFor(ProceedingJoinPoint thisJoinPoint)
   throws Throwable
   {
      return statementWorker(thisJoinPoint, UIStatement.PROMPT_FOR);
   }

   /**
    * Triggered around {@link GenericFrame#update} statements, 
    * to set the {@link UIStatement#UPDATE}.
    * 
    * @param   thisJoinPoint
    *          A join point reference.
    *          
    * @return  The value of the join point method.
    *   
    * @throws  Throwable
    *          Any exceptions are untouched propagated out. 
    */
   @Around("execution(public void com.goldencode.p2j.ui.GenericFrame.update*(..))")
   public Object update(ProceedingJoinPoint thisJoinPoint)
   throws Throwable
   {
      return statementWorker(thisJoinPoint, UIStatement.UPDATE);
   }

   /**
    * A worker to set and restore the UI statement used by the current frame.
    * 
    * @param   thisJoinPoint
    *          A join point reference.
    * @param   stmt
    *          The 4GL {@link UIStatement} associated with the {@link GenericFrame} API.
    * @return  The value of the join point method.
    * 
    * @throws Throwable
    *          Any exceptions are untouched propagated out. 
    */
   private Object statementWorker(ProceedingJoinPoint thisJoinPoint, UIStatement stmt)
   throws Throwable
   {
      GenericFrame frame = (GenericFrame) thisJoinPoint.getThis();

      if (AppServerManager.isRemote())
      {
         Object[] args = thisJoinPoint.getArgs();
         if (args.length >= 1 && args[0] instanceof Stream)
         {
            // explicitly redirected, nothing special
         }
         else
         {
            boolean isInput = (stmt == UIStatement.ENABLE ||
                               stmt == UIStatement.UPDATE ||
                               stmt == UIStatement.SET    || 
                               stmt == UIStatement.PROMPT_FOR);
            if (isInput)
            {
               if (UnnamedStreams.input() == null)
               {
                  if (stmt == UIStatement.ENABLE)
                  {
                     String errMsg = 
                        "Cannot execute ENABLE statement when input stream is not the screen";
                     ErrorManager.displayError(4018, errMsg);

                     throw new StopConditionException(errMsg);
                  }
                  else
                  {
                     // UPDATE/SET/PROMPT-FOR 'abends' (but does not terminate the agent)!
                     LogicalTerminal.stopBatchSession();
                  }

                  return null;
               }
            }
            else
            {
               if (UnnamedStreams.output() == null)
               {
                  // all non-interactive frame stmts are no-oped, if the output is not redirected
                  return null;
               }
            }
         }
      }
      
      boolean pop = frame.setStatement(stmt);

      try
      {
         return thisJoinPoint.proceed();
      }
      
      finally
      {
         if (pop)
         {
            frame.resetStatement();
         }
      }
   }
}