Action Services

Top  Previous  Next

Instead of writing a Servlet class for each command on the user interface, It is advised to have only one controller Servlet that loads and executes the service classes that are mapped to specific commands.

This way you can hide the full names of the java classes executed for the commands and you gain flexibility for the future changes in your package structures.

Example:

custlist.doms?custid=723723

 

The java class, say it is mypack.getoperations.CustomerList, that works behind this URL is not known by the users and in the future you can map your URL to another class, e.g. mypack.customer.GetCustomerList without making any changes in the user interfaces.

ActionService is an Interface to develop services that complies to this Architecture

Here is an action service class example that implements org.moremotion.action.ActionService interface.

 

  package mypack;

 

  import java.io.IOException;

  import org.moremotion.action.*;

  import org.moremotion.adom.*;

  import org.moremotion.component.ComponentConfig;

  import org.moremotion.servlet.MoreMotionRequest;

  import org.moremotion.servlet.ServiceException;

 

  public class MyActionService implements ActionService {

  

    public void doService(ActionServiceContext asc)

    throws ServiceException, IOException {

    

      MoreMotionRequest request = asc.getRequest();

      //

      // Process the Request

      //

      try {

      

        // Access to the configuration of the service

        ActionServiceConfig asconfig = asc.getActionServiceConfig();

        double VAT = asconfig.getParameter("VAT").doubleValue(0.18);

      

        // Get request parameters

        double unitPrice = request.getParameterAsDouble("UNIT_PRICE",0);

        double quantity = request.getParameterAsInt("QUANTITY",1);

        double lineTotal = unitPrice * quantity * (1 + VAT);

      

        // Prepare "myds" ADOM. The data object

        ADOM adom = request.newRequestADOM("myds");

      

        ADOMNode item = new ADOMNode("item");

        adom.addNode(item);

      

        item.setNodeValue("UNIT_PRICE", unitPrice + "");

        item.setNodeValue("QUANTITY", quantity + "");

        item.setNodeValue("VAT", VAT + "");

        item.setNodeValue("LINE_TOTAL", lineTotal + "");

      

          /* "myds" XML will be as follows

           *   <myds>

           *     <item>

           *       <UNIT_PRICE>12.25</UNIT_PRICE>

           *       <QUANTITY>5</QUANTITY>

           *       <VAT>0.18</VAT>

           *       <LINE_TOTAL>72.275</LINE_TOTAL>

           *     </item>

           *   </myds>

           */

      

      } catch(Exception e) {

        throw new ServiceException(e);

      }

      request.generatePage("next_page");

    }

  

  }

 

 

The classes that implements org.moremotion.action.ActionService interface should implement the doService method.

public void doService(ActionServiceContext asc) throws ServiceException, IOException;

 

This method is the entry point of the action services. MoreMotion calls this method of the service and passes ActionServiceContext object to it. After finishing its work the service should call Display Page Service with request.generatePage() method and terminate.

How to call an Action Service?

Simply

mypack.MyActionService.doms?UNIT_PRICE=12.25&QUANTITY=5

 

The requests that contain .doms extension is received by the SCS. The SCS loads the service classes and call their doService() Methods.

The Service Controller Servlet (SCS)

Calling an Action Service with an URL like "mypack.MyActionService.doms?prm1=ABC&prm2=123" is possible since the extension .doms is already mapped to the MoreMotion's ServiceControllerServlet (SCS) in web.xml file. The SCS receives all the requests that has the .doms extension, loads the action service classes and executes them.

Mapping the Action Services

If the full name of the action class name should not be exhibited on the user interface or the action service needs service parameters then an Action definition should be made in the MoreMotion configuration using the configuration element action.

  <root>
    <action name="myservice">
      <class>mypack.MyActionService</class>
      <param name="VAT">0.18</param>
    </action>
  </root>

Once the action service is defined in the configuration, the calling of it becomes as simple as the following.

myservice.doms?UNIT_PRICE=12.25&QUANTITY=5