Process Classes

Top  Previous  Next

Process classes are business logic wrappers of the MoreMotion Process Management Framework. They are defined with process element in the configuration and called with callProcess and callQueryProcess elements from within processTemplate element.
 
See Process Configuration, Process Management

The inputs of a process class are the process configuration and the records of the process block that it is associated. A process class has no idea where the process records that it should process are coming from (whether from an HTML form or from its parent process) and how the changes that it causes are represented in the next page. The only responsibility of a process is to process the records given to it.

A process does not have to deal with parameter parsing or decision making because by design that is the responsibility of the process manager. The role of the MScript is outstanding in making the life of a process simpler.

A developer can configure a process in many different ways according to the applications needs. Once the control of the execution is taken by the Process Manager it evaluates the preconditions and the record filters and calls the process classes as configured in the configuration.

The Base Classes

An process class should extend either org.moremotion.process.Process base class or org.moremotion.process.QueryProcess base class.

The Process base class has three abstract methods to implement, which are:  prepare(), processRecord() and finalizeProcess().

The Query Process base class enforces the extender class to implement two more abstract methods which are getFieldNames() and getNextResultRecord().

 

 

An example of extending base class "Process"

 
The process configuration:

  <process name="MyProcess">
    <class>mypack.MyProcess</class>
    <param name="adultAge">25</param>
  </jdbcConnection>

 
The Java Class Code:

 

  package mypack;

 

  import org.moremotion.config.ConfigParameterException;

  import org.moremotion.process.Process;

  import org.moremotion.process.ProcessException;

  import org.moremotion.process.ProcessRecord;

  import org.moremotion.util.Logger;

 

  public class MyProcess extends Process {

 

    private int maxAge = 0;

    private int adultAge = 0;

 

    /* Process Manager calls the prepare() methods of the process to let it

     * get prepared for successive processRecord() calls.

     */

    public void prepare() throws ProcessException {

 

      /* Printing log record on the system console to trace the execution flow */

      if (logger.LEVEL1) {

        logger.print("Logging started. Process Step: " + callDef.getStepName());

      }

 

      try {

        maxAge = 0;

        adultAge = processConfig.getParameter("adultAge").intValue(20);

 

      } catch (ConfigParameterException ex) {

        /* An Error occurred when accessing to the configuration

         * Create an error message out of the thrown exception

         * and set the completion code to 12. The execution will not continue.

         */

        createMessage(ex);

        setCompletionCode(12);

      }

 

    }

 

    /* processRecord method will be called by the Process Manager for each record

     * existing in the process block that passes through the record filter.

     */

    public void processRecord(ProcessRecord prec) throws ProcessException {

 

      /* Get the values of the fields that exist in the process record.

       * The second parameters of the methods are the default values.

       */

      String name = prec.getField("NAME",null);

      int age = prec.getFieldAsInt("AGE",-1);

 

      if (age == -1) {

        /* Create a message if an error is encountered. The call below will

         * create an ADOM with name "<unitname>_messages".

         * "MY_RESOURCE_ID" is a resource that should exist in resource 
         * file "<unitname>.res".

         * Note that <unitname> is defined with "unitname" element in the

         * Process configuration.

         */

        createMessage("MY_RESOURCE_ID",new String[]{name},null);

 

        /* Set the completion code of the process as 8.

         * The execution will continue.

         */

        setCompletionCode(8);

 

  

      } else {

        boolean active = prec.getFieldAsBoolean("ACTIVE",false);

 

        if (active && age > maxAge) maxAge = age;

        String adult = age >= adultAge ? "yes" : "no";

 

        /* Create a new field in the process record. */

        prec.setField("ADULT",adult);

      }

 

    }

 

    /* When the calling of processRecord() method for the records is complete,

     * the Process Manager calls this method to give a chance to the

     * process to complete its tasks.

     */

    public void finalizeProcess() throws ProcessException {

 

      /* If this process is in error quit */

      if (getCompletionCode() > 8) return;
 

      /* Transfer the "maxAge" value to the variable pool.*/

      request.getVPool().put("maxAge", maxAge + "");

 

    }

 

  }

    

 

Query Process Base Class

The classes that extend org.moremotion.process.QueryProcess base class implement additional interface methods and generate 0 or more output records for each process record given to them.

It is configurable what to do with the records generated by a query process. See callQueryProcess configuration element.

An example of extending base class "QueryProcess"

The content of process block "fileNames":

  FILE_NAME

  ---------
x abc.txt
  bcd.txt
x xyz.txt

Assume that on the user interface the process block "fileNames" contains the above records. The process field "FILE_NAME" in each record contains the file to read. The user selects the first and the third record and submits the request to execute the process template "T1". 

 

The process configuration:

  <processTemplate name="T1">
    <callQueryProcess name="QueryRecords" blockName="fileNames" recordFilter="_selected">
      <callProcess name="Echo"/>
    </callQueryProcess>
  </processTemplate>
 
  <process name="QueryRecords">
    <class>mypack.MyQueryProcess</class>
    <param name="rootFolder">documents</param>
  </jdbcConnection>
 
  <process name="Echo" basedon="moremotion.MScriptingProcess">
    <param name="mscript">@echo('NAME:' + NAME + ', EMAIL:' + EMAIL)</param>
  </process>
 

 
The Java Class Code:

  package mypack;

 

  import java.io.BufferedInputStream;

  import java.io.BufferedReader;

  import java.io.FileInputStream;

  import java.io.FileNotFoundException;

  import java.io.FileReader;

  import java.io.IOException;

  import org.moremotion.config.ConfigParameter;

  import org.moremotion.config.ConfigParameterException;

  import org.moremotion.process.MemoryProcessRecord;

  import org.moremotion.process.ProcessException;

  import org.moremotion.process.ProcessRecord;

  import org.moremotion.process.QueryProcess;

 

  public class MyQueryProcess extends QueryProcess {

 

    ConfigParameter dirName = null;

    BufferedReader reader;

 

    public void prepare() throws ProcessException {

      try {

        dirName = callDef.getProcessConfig().getParameter("rootFolder");

 

      } catch (ConfigParameterException ex) {

        throw new ProcessException(ex);

      }

    }

 

    public void processRecord(ProcessRecord prec) throws ProcessException {

      String fileName = prec.getField("FILE_NAME",null);

      if (fileName != null) {

        try {

          File file = new File(dirName.resolve(request).stringValue(null), fileName);

          reader = new BufferedReader( new FileReader( file );

        } catch (FileNotFoundException ex) {

          throw new ProcessException(ex);

        }

      }

    }

 

    /* This method is called once after each processRecord() call.

     * It should provide the names of the fields existing in the

     * Process Record.

     */

    public String[] getFieldNames() throws ProcessException {

      return new String[]{"NAME", "EMAIL"};

    }

 

    /* After calling the processRecord() method this method is called repeatedly

     * until it returns a null value.

     */

    public MemoryProcessRecord getNextResultRecord() throws ProcessException {

 

      try {

        String line = reader.readLine();

        if (line == null) {

          /* End of file. Do not call me again 

           */

          reader.close();

          return null; 

        }

 

        int delimiterPos = line.indexOf(";");

        String NAME = line.substring(0,delimiterPos);

        String EMAIL = line.substring(delimiterPos+1);

        MemoryProcessRecord record = new MemoryProcessRecord();

        record.fields = new String[]{NAME,EMAIL};

        return record;

 

      } catch (IOException ex) {

        throw new ProcessException(ex);

      }

 

 

    }

 

    public void finalizeProcess() throws ProcessException {

    }

 

  }