LegacyJavaAppserverProxyInvocationResult.java

/*
** Module   : LegacyJavaAppserverProxyInvocationResult.java
** Abstract : Container for a remote invocation result, for a legacy proxy open client Java program.
**
** Copyright (c) 2020-2022, Golden Code Development Corporation.
**
** -#- -I- --Date-- ---------------------------------------Description----------------------------------------
** 001 CA  20201008 Created initial version.
** 002 CA  20220428 Improved with APIs for getting OUTPUT parameters of other types.
**                  All remote function calls.
*/

/*
** 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.util;

import java.lang.reflect.*;
import java.math.*;
import java.util.*;

import commonj.sdo.*;

/**
 * Allow access to the RETURN-VALUE and OUTPUT/INPUT-OUTPUT arguments, after an invocation of a remote program.
 */
public class LegacyJavaAppserverProxyInvocationResult
{
   /** The RETURN-VALUE for a procedure or the function's returned value. */
   private Object returnValue;
   
   /*** The argument list. */
   private LegacyJavaAppserverParameter[] arguments;

   /** The instance used to perform the request. */
   private LegacyJavaAppserverClient client;
   
   /**
    * Initialize this instance.
    * 
    * @param    client
    *           The instance used to perform the request.
    * @param    returnValue
    *           The RETURN-VALUE.
    * @param    args
    *           The arguments for the request.
    */
   public LegacyJavaAppserverProxyInvocationResult(LegacyJavaAppserverClient client, 
                                                   String returnValue, 
                                                   LegacyJavaAppserverParameter... args)
   {
      this(client, (Object) returnValue, args);
   }
   /**
    * Initialize this instance.
    * 
    * @param    client
    *           The instance used to perform the request.
    * @param    returnValue
    *           The RETURN-VALUE.
    * @param    args
    *           The arguments for the request.
    */
   public LegacyJavaAppserverProxyInvocationResult(LegacyJavaAppserverClient client, 
                                                   Object returnValue, 
                                                   LegacyJavaAppserverParameter... args)
   {
      this.client = client;
      this.returnValue = returnValue;
      this.arguments = args;
   }
   
   /**
    * Get the {@link #returnValue}.
    * 
    * @return   See above.
    */
   public String getReturnValue()
   {
      return (String) returnValue;
   }
   
   /**
    * Get the function's return value.  This will be converted from a {@link BaseDataType} to a Java
    * type.
    * 
    * @return    See above.
    */
   public Object getFunctionReturn()
   {
      // TODO: extent
      
      if (returnValue instanceof BaseDataType)
      {
         BaseDataType bdt = (BaseDataType) returnValue;
         
         if (bdt.isUnknown())
         {
            return null;
         }
         
         String type = bdt.getTypeName().toLowerCase();
         switch (type)
         {
            case "logical":
               return ((logical) bdt).getValue();
            case "integer":
               return ((integer) bdt).getValue();
            case "int64":
               return ((int64) bdt).getValue();
            case "decimal":
               return ((decimal) bdt).getValue();
            case "date":
               return ((date) bdt).dateValue();
            case "datetime":
               return ((datetime) bdt).dateValue();
            case "datetimetz":
            case "datetime-tz":
               return ((datetimetz) bdt).calendarValue();
            case "character":
            case "longchar":
               return ((Text) bdt).getValue();
            case "raw":
               return ((raw) bdt).asByteArray();
            case "recid":
               return ((recid) bdt).getValue();
            case "rowid":
               return ((rowid) bdt).getValue();
         }
      }
      else if (returnValue instanceof MemoryBuffer)
      {
         return ((MemoryBuffer) returnValue).getValue();
      }
      
      return returnValue;
   }
   
   /**
    * Get the output parameter at the specified index.
    * 
    * @param    index
    *           The 1-based argument index.
    *           
    * @return   See above.
    */
   public Object getOutputParameter(int index)
   {
      if (index < 1 || index > arguments.length)
      {
         throw new IllegalArgumentException("Invalid argument index: " + index);
      }
      
      LegacyJavaAppserverParameter param = arguments[index - 1];
      
      int mode = param.getMode();
      if (mode == client.input())
      {
         throw new IllegalArgumentException("The parameter is INPUT!");
      }
      
      return client.getOutputParameter(param);
   }
   
   /**
    * Get the output parameter at the specified index as a int value.
    * 
    * @param    index
    *           The 1-based argument index.
    *           
    * @return   See above.
    */
   public Integer getIntOutputParameter(int index)
   {
      return (Integer) getOutputParameter(index);
   }
   
   /**
    * Get an {@link Integer} array OUTPUT parameter.
    * 
    * @param    index
    *           The 1-based argument index.
    *           
    * @return   See above.
    */
   public Integer[] getIntArrayOutputParameter(int index)
   {
      Object res = getOutputParameter(index);
      if (res.getClass() == int[].class)
      {
         int length = Array.getLength(res);
         Object newRes = Array.newInstance(Integer.class, length);
         for (int i = 0; i < length; i++)
         {
            Integer l = Array.getInt(res, i);
            Array.set(newRes, i, l);
         }
         
         res = newRes;
      }

      return (Integer[]) res;
   }

   /**
    * Get the output parameter at the specified index as a string value.
    * 
    * @param    index
    *           The 1-based argument index.
    *           
    * @return   See above.
    */
   public String getStringOutputParameter(int index)
   {
      return (String) getOutputParameter(index);
   }
   
   /**
    * Get a {@link String} array OUTPUT parameter.
    * 
    * @param    index
    *           The 1-based argument index.
    *           
    * @return   See above.
    */
   public String[] getStringArrayOutputParameter(int index)
   {
      return (String[]) getOutputParameter(index);
   }

   /**
    * Get the output parameter at the specified index as a double value.
    * 
    * @param    index
    *           The 1-based argument index.
    *           
    * @return   See above.
    */
   public Double getDoubleOutputParameter(int index)
   {
      return (Double) getOutputParameter(index);
   }
   
   /**
    * Get a {@link Double} array OUTPUT parameter.
    * 
    * @param    index
    *           The 1-based argument index.
    *           
    * @return   See above.
    */
   public Double[] getDoubleArrayOutputParameter(int index)
   {
      Object res = getOutputParameter(index);
      if (res.getClass() == double[].class)
      {
         int length = Array.getLength(res);
         Object newRes = Array.newInstance(Double.class, length);
         for (int i = 0; i < length; i++)
         {
            Double l = Array.getDouble(res, i);
            Array.set(newRes, i, l);
         }
         
         res = newRes;
      }

      return (Double[]) res;
   }

   /**
    * Get the output parameter at the specified index as a {@link BigDecimal} value.
    * 
    * @param    index
    *           The 1-based argument index.
    *           
    * @return   See above.
    */
   public BigDecimal getBigDecimalOutputParameter(int index)
   {
      return (BigDecimal) getOutputParameter(index);
   }

   /**
    * Get a {@link BigDecimal} array OUTPUT parameter.
    * 
    * @param    index
    *           The 1-based argument index.
    *           
    * @return   See above.
    */
   public BigDecimal[] getBigDecimalArrayOutputParameter(int index)
   {
      return (BigDecimal[]) getOutputParameter(index);
   }
   
   /**
    * Get the output parameter at the specified index as a boolean value.
    * 
    * @param    index
    *           The 1-based argument index.
    *           
    * @return   See above.
    */
   public Boolean getBooleanOutputParameter(int index)
   {
      return (Boolean) getOutputParameter(index);
   }
   
   /**
    * Get a {@link Boolean} array OUTPUT parameter.
    * 
    * @param    index
    *           The 1-based argument index.
    *           
    * @return   See above.
    */
   public Boolean[] getBooleanArrayOutputParameter(int index)
   {
      Object res = getOutputParameter(index);
      if (res.getClass() == boolean[].class)
      {
         int length = Array.getLength(res);
         Object newRes = Array.newInstance(Boolean.class, length);
         for (int i = 0; i < length; i++)
         {
            Boolean l = Array.getBoolean(res, i);
            Array.set(newRes, i, l);
         }
         
         res = newRes;
      }

      return (Boolean[]) res;
   }

   /**
    * Get the output parameter at the specified index as a long value.
    * 
    * @param    index
    *           The 1-based argument index.
    *           
    * @return   See above.
    */
   public Long getLongOutputParameter(int index)
   {
      return (Long) getOutputParameter(index);
   }
   
   /**
    * Get a {@link Long} array OUTPUT parameter.
    * 
    * @param    index
    *           The 1-based argument index.
    *           
    * @return   See above.
    */
   public Long[] getLongArrayOutputParameter(int index)
   {
      Object res = getOutputParameter(index);
      if (res.getClass() == long[].class)
      {
         int length = Array.getLength(res);
         Object newRes = Array.newInstance(Long.class, length);
         for (int i = 0; i < length; i++)
         {
            Long l = Array.getLong(res, i);
            Array.set(newRes, i, l);
         }
         
         res = newRes;
      }

      return (Long[]) res;
   }
   
   /**
    * Get the output parameter at the specified index as a {@link GregorianCalendar} value.
    * 
    * @param    index
    *           The 1-based argument index.
    *           
    * @return   See above.
    */
   public GregorianCalendar getCalendarOutputParameter(int index)
   {
      return (GregorianCalendar) getOutputParameter(index);
   }
   
   /**
    * Get a {@link GregorianCalendar} array OUTPUT parameter.
    * 
    * @param    index
    *           The 1-based argument index.
    *           
    * @return   See above.
    */
   public GregorianCalendar[] getCalendarArrayOutputParameter(int index)
   {
      return (GregorianCalendar[]) getOutputParameter(index);
   }
   
   /**
    * Get the output parameter at the specified index as a {@code byte[]} value.
    * 
    * @param    index
    *           The 1-based argument index.
    *           
    * @return   See above.
    */
   public byte[] getByteArrayOutputParameter(int index)
   {
      return (byte[]) getOutputParameter(index);
   }
   
   /**
    * Get a {@code byte[]} array OUTPUT parameter.
    * 
    * @param    index
    *           The 1-based argument index.
    *           
    * @return   See above.
    */
   public byte[][] getByteArrayArrayOutputParameter(int index)
   {
      return (byte[][]) getOutputParameter(index);
   }

   /**
    * Get the output parameter at the specified index as a {@link MemoryBuffer} value.
    * 
    * @param    index
    *           The 1-based argument index.
    *           
    * @return   See above.
    */
   public MemoryBuffer getMemoryBufferOutputParameter(int index)
   {
      return (MemoryBuffer) getOutputParameter(index);
   }

   /**
    * Get a {@link MemoryBuffer} array OUTPUT parameter.
    * 
    * @param    index
    *           The 1-based argument index.
    *           
    * @return   See above.
    */
   public MemoryBuffer[] getMemoryBufferArrayOutputParameter(int index)
   {
      return (MemoryBuffer[]) getOutputParameter(index);
   }

   /**
    * Get the output parameter at the specified index as a dataset.
    * 
    * @param    index
    *           The 1-based argument index.
    *           
    * @return   See above.
    */
   public DataGraph getDataSetOutputParameter(int index)
   {
      return (DataGraph) getOutputParameter(index);
   }

   /**
    * Get the output parameter at the specified index as a table.
    * 
    * @param    index
    *           The 1-based argument index.
    *           
    * @return   See above.
    */
   public DataGraph getTableOutputParameter(int index)
   {
      return (DataGraph) getOutputParameter(index);
   }
}