# InvocationRequestPayload.java

    /*
    ** Module   : InvocationRequestPayload.java
    ** Abstract : A payload with data for an invoke request into the legacy converted code,
    **            originating from a customer-specific application.
    **
    ** Copyright (c) 2016-2020, Golden Code Development Corporation.
    **
    ** -#- -I- --Date-- ---------------------------------Description----------------------------------
    ** 001 CA  20160418 Created initial version
    ** 002 IAS 20200908 Rework (de)serialization.
    ** 003 IAS 20200922 Get rid of possible NPE on serialization.
    */
    /*
    ** 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 static com.goldencode.util.NativeTypeSerializer.*;
    import java.io.*;
    import java.util.*;

    /**
     * This payload is responsible for defining the details of a call into 4GL code, originating from
     * a customer-specific application (used in cases when the client runs in an embedded mode, inside
     * another application).
     * <p>
     * The invocation targets can be any external program, procedure or function which can be found
     * in the user's propath; it is possible to also emulate a <code>IN handle</code> clause when
     * targeting a procedure or function, by specifying the handle of an external program which was
     * previously executed in persistent mode.
     */
    public class InvocationRequestPayload
    implements Externalizable
    {
       /** The invocation types. */
       public static enum InvokeType
       {
          /** */
          EXTERNAL_PROGRAM((byte) 0),
          /** */
          PROCEDURE((byte) 1),
          /** */
          FUNCTION((byte) 2);

          /** A numeric representation of the enum's entry. */
          private final byte type;

          /**
           * Construct a new type and associate its unique numeric code.
           *
           * @param   type
           *          The numeric representation of this type.
           */
          private InvokeType(byte type)
          {
             this.type = type;
          }

          /**
           * Get the type of this target.
           *
           * @return   See above.
           */
          public byte getType()
          {
             return type;
          }

          /**
           * Resolve a invocation type, from its numeric representation.
           *
           * @param    code
           *           The numeric representation of this type.
           *
           * @return   The found enum entry.
           *
           * @throws   IllegalArgumentException
           *           If <code>code</code> is something other than 0, 1 or 2 value.
           */
          public static InvokeType fromByte(byte code)
          {
             switch (code)
             {
                case 0:
                   return EXTERNAL_PROGRAM;
                case 1:
                   return PROCEDURE;
                case 2:
                   return FUNCTION;
             }

             throw new IllegalArgumentException(code + " is not a valid InvokeType entry!");
          }
       }

       /** Flag indicating if this call should set the NO-ERROR mode on. */
       public boolean noError;

       /**
        * The call target name; depending on the {@link #type}, this is a name of an external program,
        * procedure or function.
        */
       public String name;

       /**
        * The handle ID.  Is used only when executing an internal procedure or function, in an external
        * program ran persistent.
        */
       public long handle;

       /** The call type.  See {@link InvokeType} for the supported types.*/
       public InvokeType type;

       /**
        * A map with the arguments to be passed to the caller.  The keys represent the legacy parameter
        * names (case-insensitive) as defined in the <code>name_map.xml</code> file, and the values
        * hold the string-representation of the argument's value.  At invocation, the values will be
        * converted to a value with the type specific to that argument.
        */
       public Map<String, String> arguments;

       /**
        * When invoking an {@link InvokeType#EXTERNAL_PROGRAM}, this flag will specify if the program
        * will be ran persistent or not.
        */
       public boolean persistent;

       /** Default constructor */
       public InvocationRequestPayload()
       {
          super();
       }

       /**
        * Replacement for the default object reading method. The request data is read from the input
        * source.
        *
        * @param    in
        *           The input source from which fields will be restored.
        *
        * @throws   IOException
        *           In case of I/O errors.
        * @throws   ClassNotFoundException
        *           If payload can't be instantiated.
        */
       @Override
       public void readExternal(ObjectInput in)
       throws IOException,
              ClassNotFoundException
       {
          noError    = in.readBoolean();
          name       = readString(in);
          handle     = in.readLong();
          type       = readEnum(in, InvokeType.values());
          persistent = in.readBoolean();
          arguments  = readMap(in, HashMap::new, ObjectInput::readUTF, ObjectInput::readUTF);
       }

       /**
        * Replacement for the default object writing method. The request data is written to the output
        * destination.
        *
        * @param    out
        *           The output destination to which the fields will be saved.
        *
        * @throws   IOException
        *           In case of I/O errors.
        */
       @Override
       public void writeExternal(ObjectOutput out)
       throws IOException
       {
          out.writeBoolean(noError);
          writeString(out, name);
          out.writeLong(handle);
          writeEnum(out, type);
          out.writeBoolean(persistent);
          writeMap(out, arguments, ObjectOutput::writeUTF, ObjectOutput::writeUTF);
       }
    }