LegacyJavaAppserverClientProxy.java

/*
** Module   : LegacyJavaAppserverClientProxy.java
** Abstract : Base class for generated legacy open client proxy Java programs.
**
** Copyright (c) 2020-2023, Golden Code Development Corporation.
**
** -#- -I- --Date-- ---------------------------------------Description----------------------------------------
** 001 CA  20201008 Created initial version.
**     CA  20210831 Allow clients to always enable the change logging, as for the .NET client OE uses a 
**                  DataTable, which always track changes.
**     CA  20220427 Added support for SINGLETON and SINGLE-RUN modes.
**     CA  20220513 Added 'cancelAllRequests'.
**     CA  20220929 Emit the proxy metadata APIs as static methods, to not be dependent on an active FWD 
**                  connection.
** 002 CA  20230712 Added 'invokeWithArgs', an API which can send any number of arguments to a remote program 
**                  acting as a controller, which can prepare and execute the real target.
*/

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

import commonj.sdo.*;

/**
 * Base class for legacy open client proxy programs.  This provides also high-level APIs to manage the remote
 * persistent procedure, data objects and more.
 */
public abstract class LegacyJavaAppserverClientProxy
{
   /**
    * A static helper which is used by generated proxy's metadata APIs.  This must be initialized by the client
    * application using initializeAppserverHelper
    * 
    */
   protected static final LegacyJavaAppserver HELPER = new LegacyJavaAppserver();

   /** Helper which is used to connect and invoke remote programs. */
   protected final LegacyJavaAppserverClient client;

   /** The proxy procedure. */
   protected handle proxy;

   /**
    * Create a new instance with the specified client.
    * 
    * @param    client
    *           The proxy client.
    */
   public LegacyJavaAppserverClientProxy(LegacyJavaAppserverClient client)
   {
      this(client, true);
   }
   
   /**
    * Create a new instance with the specified client.
    * 
    * @param    client
    *           The proxy client.
    * @param    trackChanges
    *           Flag indicating if changes are tracked for all datasets, by default.
    */
   public LegacyJavaAppserverClientProxy(LegacyJavaAppserverClient client, boolean trackChanges)
   {
      this.client = client;
      this.client.setDefaultTrackChanges(trackChanges);
      HELPER.setDefaultTrackChanges(trackChanges);
   }
   
   /**
    * Configure the {@link #HELPER} used by the generated proxies to create the metadata.
    * 
    * @param    defaultTrackingChanges
    *           Flag indicating if changes are tracked for all datasets, by default.
    * @param    normalizedExtent
    *           The extent mode.
    */
   public static void configureMetadataHelper(boolean defaultTrackingChanges, boolean normalizedExtent)
   {
      HELPER.setDefaultTrackChanges(defaultTrackingChanges);
      HELPER.setNormalizedExtent(normalizedExtent);
   }
   
   /**
    * Get the helper used by generated proxy metadata APIs.
    * 
    * @return   The {@link #HELPER}.
    */
   public LegacyJavaAppserver getMetadataHelper()
   {
      return HELPER;
   }
   
   /**
    * Delete the {@link #proxy}.
    */
   public void dispose()
   {
      if (proxy != null)
      {
         if (proxy.getResource() instanceof DeferredProgramWrapper)
         {
            proxy.setUnknown();
         }
         else
         {
            client.deleteProcedure(proxy);
         }
         proxy = null;
      }
   }

   /**
    * List all the rows in the specified table.
    * 
    * @param    graph
    *           The parent data graph.
    * @param    table
    *           The table name.
    *           
    * @return   The row list.
    */
   public List<DataObject> listRows(DataGraph graph, String table)
   {
      return client.getDataObjects(graph, table);
   }

   /**
    * Create a row for the specified table.
    * 
    * @param    graph
    *           The parent data graph.
    * @param    table
    *           The table name.
    *           
    * @return   The created row.  This is not added to the table - use {@link #addRow} to add it.
    */
   public DataObject createRow(DataGraph graph, String table)
   {
      return client.createDataObject(graph, table);
   }
   
   /**
    * Add the specified row to its table.
    * 
    * @param    graph
    *           The parent data graph.
    * @param    row
    *           The row to insert.
    */
   public void addRow(DataGraph graph, DataObject row)
   {
      client.addDataObject(graph, row);
   }
   
   /**
    * Cancel all active requests on this client.
    */
   public void cancelAllRequests()
   {
      client.cancelAllRequests();
   }
   
   /**
    * Perform a pseudo-dynamic call where the target procedure doesn't receive the parameter as arguments at
    * the Java method definition, but instead they are managed via {@link LegacyOpenClientCaller} APIs.
    * <p>
    * This allows the target program to act as a controller, where it can prepare the arguments, perform
    * security checks, etc, before dispatching the call to the real target, which can be resolved from the
    * arguments or in some other way.
    *  
    * @param    target
    *           The target external program.  Must have no parameters defined.
    * @param    paramArray
    *           The parameter array.
    */
   public void invokeWithArgs(String target, LegacyJavaAppserverParameter... paramArray)
   {
      client.invokeWithArgs(target, paramArray);
   }
}