JATO Tutorial

Chapter 2, Task 2

Task 2—Create the CustomersModel class

Let's create a model to access the RDBMS.

Step 1: Create the Customers Model interface

  1. Copy the template file __queryModel__.java.java from the /templates/1_2/ModuleTemplates directory to your /WEB-INF/classes/jatotutorial/module1 directory.

    /JatoTutorial/WEB-INF/classes/jatotutorial/module1/__queryModel__.java.java

  2. Rename the file CustomersModel.java
  3. Edit the file and replace the following tokens as indicated:

    Token Replacement Text
    __appPackage__ jatotutorial
    __modulePackage__ module1
    __queryModel__ CustomersModel

  4. As indicated below, create the field name constants FIELD_CUSTOMERID, FIELD_COMPANYNAME, FIELD_CONTACTNAME, and FIELD_PHONE. Note, some parts of the template code have been omitted here. Any omitted sections are not pertinent to this example:
package jatotutorial.module1;

...

public interface CustomersModel extends SelectQueryModel
{
    public static final String FIELD_CUSTOMERID="CustomerID";
    public static final String FIELD_COMPANYNAME="CompanyName";
    public static final String FIELD_CONTACTNAME="ContactName";
    public static final String FIELD_PHONE="Phone";
}

Step 2: Create the Customers Model implementaton

  1. Copy the template file __queryModel__Impl.java.java from the /templates/1_2/ModuleTemplates directory to your /WEB-INF/classes/jatotutorial/module1 directory.
  2. /JatoTutorial/WEB-INF/classes/jatotutorial/module1/__queryModel__Impl.java.java

  3. Rename the file CustomersModelImpl.java
  4. Edit the file and replace the following tokens as indicated:

    Token Replacement Text
    __appPackage__ jatotutorial
    __modulePackage__ module1
    __queryModel__ CustomersModel

  5. Modify the code as indicated below. Note, some parts of the template code have been omitted here. Any omitted sections are not pertinent to this example:
package jatotutorial.module1;

...

public class CustomersModelImpl extends QueryModelBase
    implements CustomersModel
{
    public CustomersModelImpl()
    {	
        super();	
        setDataSourceName(DATA_SOURCE_NAME);
        setDefaultConnectionUser("sa");	
        setDefaultConnectionPassword("");

        setSelectSQLTemplate(SELECT_SQL_TEMPLATE);
        setStaticWhereCriteriaString(STATIC_WHERE_CRITERIA);
        setModifyingQueryTableName(MODIFYING_QUERY_TABLE_NAME);
        setFieldSchema(FIELD_SCHEMA);
    }
    
    ...

    ////////////////////////////////////////////////////////////////////////////
    // Class variables
    ////////////////////////////////////////////////////////////////////////////
 
    public static final String DATA_SOURCE_NAME="jdbc/NorthwindDataSource";
    public static final String SELECT_SQL_TEMPLATE=
        "SELECT ndnwCustomers.CustomerID, ndnwCustomers.CompanyName, "+
        "ndnwCustomers.ContactName, ndnwCustomers.Phone " +
        "FROM ndnwCustomers __WHERE__";

    public static final String MODIFYING_QUERY_TABLE_NAME="ndnwCustomers";
    public static final String STATIC_WHERE_CRITERIA="";

    public static final QueryFieldSchema FIELD_SCHEMA=new QueryFieldSchema();

    static
    { 
        FIELD_SCHEMA.addFieldDescriptor(
            new QueryFieldDescriptor(
                FIELD_CUSTOMERID,
                "CustomerID",
                "ndnwCustomers.CustomerID",
                String.class,
                true));
        
        FIELD_SCHEMA.addFieldDescriptor(
            new QueryFieldDescriptor(
                FIELD_COMPANYNAME,
                "CompanyName",
                "ndnwCustomers.CompanyName",
                String.class));
        
        FIELD_SCHEMA.addFieldDescriptor(
            new QueryFieldDescriptor(
                FIELD_CONTACTNAME,
                "ContactName",
                "ndnwCustomers.ContactName",
                String.class));
      
        FIELD_SCHEMA.addFieldDescriptor(
            new QueryFieldDescriptor(
                FIELD_PHONE,
                "Phone",
                "ndnwCustomers.Phone",
                String.class));
    }
}

Step 3: Add a mapping of model interface to model implementation

  1. Edit the ModelTypeMapImpl.java file. Add a model interface mapping entry as indicated below. Note, some parts of the template code have been omitted here. Any omitted sections are not pertinent to this example:
package jatotutorial.module1;

...

public class ModelTypeMapImpl extends ModelTypeMapBase
    implements ModelTypeMap
{
    public ModelTypeMapImpl() 
    {
        super();	
    }


    static
    { 
        addModelInterfaceMapping(
            jatotutorial.module1.CustomersModel.class,
            jatotutorial.module1.CustomersModelImpl.class);
    }
}