# SoapConfig.java

    /*
    ** Module   : SoapConfig.java
    ** Abstract : The SOAP configuration as it was computed from a .wsm file and the target programs.
    **
    ** Copyright (c) 2020, Golden Code Development Corporation.
    **
    ** -#- -I- --Date-- ---------------------------------------Description----------------------------------------
    ** 001 CA  20200514 First version.
    **     CA  20200528 The SOAP input are the .wsm files, not .xpxg (as these contain info configured when
    **                  running ProxyGen).
    */

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

    import org.w3c.dom.*;

    import com.goldencode.util.*;

    /**
     * Loads a .wsm file and computes the SOAP operations exported by this file.
     */
    class SoapConfig
    {
       /**
        * Contains the text and attributes for various XML elements in the .wsm files.  All nodes are loaded
        * here, except the <code>Procedure</code> and <code>SubAppObject</code> nodes.  The keys are paths to an
        * element and the attributes are the path, followed by the '#' char and the attribute name.
        */
       private Map<String, String> configs = new HashMap<>();

       /** The SOAP operations exported by this .wsm file. */
       private List<SoapOperation> operations = new ArrayList<>();

       /** The SOAP WSDL configuration, to which operations are added. */
       private SoapWsdl wsdl = new SoapWsdl();

       /**
        * Get the {@link #wsdl} configuration.
        *
        * @return   See above.
        */
       public SoapWsdl getWsdl()
       {
          return wsdl;
       }

       /**
        * Load all the specified configurations into {@link #configs}.
        *
        * @param    configs
        *           The configuration mappings.
        */
       public void putConfigs(Map<String, String> configs)
       {
          this.configs.putAll(configs);
       }

       /**
        * Put the specified configuration in {@link #configs}.
        *
        * @param    key
        *           The configuration key.
        * @param    val
        *           The configuration value.
        */
       public void putConfig(String key, String val)
       {
          this.configs.put(key, val);
       }

       /**
        * Add the list of operations to the {@link #operations} list.
        *
        * @param    ops
        *           The list of operations.
        */
       public void addOperations(List<SoapOperation> ops)
       {
          this.operations.addAll(ops);
       }

       /**
        * Add the operation to the {@link #operations} list.
        *
        * @param    op
        *           The operation.
        */
       public void addOperation(SoapOperation op)
       {
          this.operations.add(op);
       }

       /**
        * Find all SOAP operations configured in the .wsm document(s), for this external program and internal
        * entry, or class and method.
        *
        * @param    file
        *           The file name.
        * @param    program
        *           The program or class name.
        * @param    internalEntry
        *           The internal entry or method name.  May be <code>null</code>.
        *
        * @return   The list of SOAP operations exporting this program and internal entry.
        */
       public List<SoapOperation> getSoapOperation(String file, String program, String internalEntry)
       {
          List<SoapOperation> found = new ArrayList<>();

          for (SoapOperation operation : operations)
          {
             if (operation.isFor(file, program, internalEntry))
             {
                found.add(operation);
             }
          }

          return found;
       }

       /**
        * Get the configuration with the specified key, from the {@link #configs} map.
        *
        * @param    key
        *           The configuration key.
        *
        * @return   The value.
        */
       public String getConfig(String key)
       {
          return configs.get(key);
       }

       /**
        * Generate the WSDL document for this SOAP configuration and save it in the specified path.
        *
        * @param    path
        *           The path where to save the WSDL document.
        */
       public void generateWsdl(String path)
       {
          String name = getConfig("DeploymentWizard/FileName") + ".wsdl";
          Document doc = wsdl.generate();

          File fdoc = new File(new File(path), name);
          System.out.println("Generating WSDL file " + fdoc);
          try
          {
             XmlHelper.write(doc, new FileOutputStream(fdoc));
          }
          catch (IOException e)
          {
             throw new RuntimeException(e);
          }
       }

       /**
        * Check if any operations are not registered in the WSDL document.  These may be programs exported by the
        * .wsm file, but non-existent in the converted 4GL source code.
        */
       public void checkOperations()
       {
          for (SoapOperation operation : operations)
          {
             if (!operation.registered)
             {
                System.out.println("WARNING: SOAP file " + operation + " is not registered in WSDL.");
             }
          }
       }
    }