LegacyProxyConfig.java

/*
** Module   : LegacyProxyConfig.java
** Abstract : Contains details about the .xpxg configuration of exposed external programs as proxies.
**
** Copyright (c) 2020-2025, Golden Code Development Corporation.
**
** -#- -I- --Date-- ---------------------------------------Description----------------------------------------
** 001 CA  20201008 Created initial version.
** 002 CA  20220427 Allow unknown arguments, default change tracking is done only for .NET.
**     CA  20220513 Optimized proxy generation, all programs belonging to multiple (Sub)AppObjects are 
**                  processed multiple times, in batches.
** 003 CA  20250515 The files must be matched using the case-sensitivity of the legacy system.
*/

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

import java.io.File;
import java.util.*;
import java.util.regex.Matcher;

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

/**
 * Configuration for programs exposed as legacy open client proxy programs, for which a Java class will be
 * generated.
 */
public class LegacyProxyConfig
{
   /** The Java package for these programs. */
   private final String pkg;
   
   /** Flag indicating if unknown arguments are allowed. */
   private final boolean allowUnknown;
   
   /** Flag indicating if datagraph changes are tracked by default (only for .NET mode is true by default). */
   private final boolean trackChanges;
   
   /** The name of the AppObject for this config. */
   private final String appObject;
   
   /** The map of configured external programs. */
   private Map<String, LegacyProxyClientConfig> programs = Configuration.isCaseSensitive() 
                                                            ? new HashMap<>() 
                                                            : new CaseInsensitiveHashMap<>();

   /**
    * Initialize this instance.
    * 
    * @param    pkg
    *           The Java package name.
    * @param    appObject
    *           The AppObject name.
    * @param    allowUnknown
    *           Flag indicating if unknown values are allowed.
    * @param    trackChanges
    *           Flag indicating if changes are tracked by default (only for .NET).
    */
   public LegacyProxyConfig(String pkg, String appObject, boolean allowUnknown, boolean trackChanges)
   {
      this.pkg = pkg;
      this.appObject = appObject;
      this.allowUnknown = allowUnknown;
      this.trackChanges = trackChanges;
   }
   
   /**
    * Create a copy of the specified proxy configuration, without the {@link #programs}.
    * 
    * @param    src
    *           The source configuration.
    */
   public LegacyProxyConfig(LegacyProxyConfig src)
   {
      this(src.pkg, src.appObject, src.allowUnknown, src.trackChanges);
   }

   /**
    * Get the AppObject name.
    * 
    * @return   See above.
    */
   public String getAppObject()
   {
      return appObject;
   }
   
   /**
    * Check if datagraph changes are tracked by default (only for .NET mode is true by default).
    * 
    * @return   See above.
    */
   public boolean isTrackChanges()
   {
      return trackChanges;
   }

   /**
    * Check if unknown arguments are allowed.
    * 
    * @return   See above.
    */
   public boolean isAllowUnknown()
   {
      return allowUnknown;
   }

   /**
    * Add a new program.
    * 
    * @param    cfg
    *           The program's configuration.
    */
   public void addProxyProgram(LegacyProxyClientConfig cfg)
   {
      this.programs.put(cfg.getFullName(), cfg);
   }
   
   /**
    * Remove the specified proxy program file.
    * 
    * @param    file
    *           The proxy program file.
    *           
    * @return   The proxy program's configuration.
    */
   public LegacyProxyClientConfig removeProxyProgram(String file)
   {
      return programs.remove(file);
   }
   
   
   /**
    * Get the java package for these programs.
    * 
    * @return   See above.
    */
   public String getPkg()
   {
      return pkg;
   }
   
   /**
    * Get all the JAST file names for this configuration.
    * 
    * @param     warn
    *            Flag indicating if a warning message is shown if the AST is not set for a proxy program.
    * 
    * @return    See above.
    */
   public List<String> getAstFiles(boolean warn)
   {
      List<String> res = new ArrayList<>();
      
      for (LegacyProxyClientConfig lpcc : programs.values())
      {
         String name = lpcc.getAstFilename();
         if (name != null)
         {
            res.add(name);
         }
         else if (warn && (lpcc.isPersistent() || lpcc.isSingleRun() || lpcc.isSingleton()))
         {
            System.out.println("WARNING: Could not find proxy 4GL program for " + lpcc.getFullName());
         }
      }
      
      return res;
   }
   
   /**
    * Get the list of proxy program file names, including their extension.
    * 
    * @return   See above.
    */
   public List<String> getProxyProgramFiles()
   {
      List<String> res = new ArrayList<>();
      for (LegacyProxyClientConfig lpcc : programs.values())
      {
         String filename = lpcc.getFullName();
         String ext = lpcc.getProgExt();
         res.add(filename + "." + ext);
      }
      
      return res;
   }
   
   /**
    * Get a copy of the configured proxy {@link #programs}.
    * 
    * @return   See above.
    */
   public Map<String, LegacyProxyClientConfig> getProxyPrograms()
   {
      return new HashMap<>(programs);
   }
   
   /**
    * Get the proxy configuration for this program.  Considering that the filename is usually prefixed with
    * other folders (like 'abl/'), the match is done by searching backwards (starting with the nearest folder)
    * until a match is found.
    * 
    * @param     filename
    *            The full file name for a converted external program.
    *            
    * @return    The found configuration or <code>null</code>.
    */
   public LegacyProxyClientConfig getProgram(String filename)
   {
      String[] names = filename.split(Matcher.quoteReplacement(File.separator));
      String pname = names[names.length - 1];
      if (pname.indexOf('.') > 0)
      {
         pname = pname.substring(0, pname.lastIndexOf('.'));
      }
      for (int i = names.length - 2; i >= 0; i--)
      {
         if (programs.containsKey(pname))
         {
            return programs.get(pname);
         }
         
         pname = names[i] + "/" + pname;
      }
      
      return null;
   }
}