Data Services

Top  Previous  Next

The data for the dynamic pages is provided by data service classes that implements MoreMotion's  org.moremotion.datasrc.DataService interface.  The data service classes are introduced to the system in the configuration with dataSource element.

How a Data Service class is called?

There is an important difference in calling the Data Service classes. Unlike the Action Service classes which are called directly with links or form actions on the pages, the Data Service classes are called indirectly.

If a page contains a data source, the MoreMotion calls the Data Service Java class associated with it and lets it to populate the data in ADOM's (MoreMotion's Data Objects).

 
Defining a Data Source

A Data Source is defined with configuration element dataSource in either a global configuration file under {APPLICATION_PATH}/WEB-INF/MM-INF/config folder or a page configuration file under {APPLICATION_PATH}/WEB-INF/MM-INF/config/pages folder.

Here is an example:

  <root>
 
    <dataSource name="mydatasource" >
      <class>mypack.MyDataService</class>
      <scope>Session</scope>
      <refreshTimeOut>60</refreshTimeOut>
      <precond>ID != ''</precond>
      <itemsPerPage>10</itemsPerPage>
      <pagesPerBlock>5</pagesPerBlock>
      <res>my_resources</res>
      <accessControl checkPoint="CAN_SEE_DATA" />
      <debug>3</debug>
      <size>200</size>
    </dataSource>
 
  </root>

The value of the class parameter should be the name of a class that implements org.moremotion.datasrc.DataService interface. See Data Source Configuration for more detailed information about data source parameters.

An example data service implementation

 

  package mypack;

 

  import java.util.Properties;

  import org.moremotion.adom.*;

  import org.moremotion.datasrc.*;

  import org.moremotion.servlet.ServiceException;

 

  public class MyDataService implements DataService {

  

    public void doService(ADOM adom, DataServiceContext dsc) throws ServiceException {

    

      // Accessing to the data source configuration

      DataSourceConfig dsconfig = dsc.getDataSourceConfig();

      int size = dsconfig.getParameter("size").intValue(100);

    

      // Acquiring a user name and password if applicable

      String user = dsconfig.getParameter("username").stringValue(null);

      String pass = dsconfig.getParameter("password").passwordValue(null);

    

      // Prepare Sample Data 

      Properties source = new Properties();

      for (int i = 0; i < size; i++) {

        String name = "NAME_" + i;

        String value = "VALUE_" + i;

        source.setProperty(name,value);

      }

    

      Paging paging = dsc.getPaging(); // Paging Object

    

      if (paging.getItemsPerPage() > 0) {

        /* Paging requested in the configuration

         * Only a part of the whole data will be populated into ADOM. */

 

        /* Let the Paging object know the total number of items in order to 

         * make the necessary paging calculations */

        paging.setNumberOfItems(source.size());

      

        int counter = 0;

      

        for (int i = paging.getSkipAmount(); i < source.size(); i++) {

          if (++counter > paging.getItemsPerPage()) break;

          ADOMNode item = new ADOMNode("item");

          String name = "NAME_" + i;

          String value = source.getProperty(name);

          item.setNodeValue("value",value);

          adom.addNode(item);

        }

      } else {

        // No paging is requested in the configuration.

        // Populate all the data into the ADOM

        for (int i = 0; i < source.size(); i++) {

          ADOMNode item = new ADOMNode("item");

          String name = "NAME_" + i;

          String value = source.getProperty(name);

          item.setNodeValue("value",value);

          adom.addNode(item);

        }

      }

    }

  

  }