Difference between revisions of "Scout/HowTo/3.7/Create a Process Service"

From Eclipsepedia

< Scout‎ | HowTo‎ | 3.7
Jump to: navigation, search
(SQL statements for the example)
m
Line 1: Line 1:
 
A popular use of a process service is to access data on a database.
 
A popular use of a process service is to access data on a database.
Assuming a typical [[Creating a Scout Dialog|dialog]], here's what we need:
+
Assuming a simple [[Create a Scout Dialog|Scout dialog]], here's what we need:
  
 
# create a new item(insert)
 
# create a new item(insert)

Revision as of 12:15, 10 March 2010

A popular use of a process service is to access data on a database. Assuming a simple Scout dialog, here's what we need:

  1. create a new item(insert)
  2. load an existing item (select)
  3. update an existing item (update)
  4. delete an existing item (delete)

Create a new process service on the server in the folder Process Services. In our example, we'll call it SubAgreementProcessService. If you've already created a form to go along with it, make sure you indicate SubAgreementFormData.

This automatically creates the following ISubAgreementProcessService for you:

 public interface ISubAgreementProcessService extends IService{
   SubAgreementFormData create(SubAgreementFormData formData) throws ProcessingException;
   SubAgreementFormData load(SubAgreementFormData formData) throws ProcessingException;
   SubAgreementFormData prepareCreate(SubAgreementFormData formData) throws ProcessingException;
   SubAgreementFormData store(SubAgreementFormData formData) throws ProcessingException;
 }

The appropriate class is also created for you. Edit SubAgreementProcessService and replace all instances of "TODO business logic here" with the real thing. :)

Here's an example that assumes a database and uses the SQL service to access it. Note that all we needed to write was the SQL statements. The bind variables are determined by the Form Data object, which is in turn generated based on the Form itself. Thus, the bind variable names are based on the field and variable names. If the field is called TypeField, then the bind variable will have the suffix "Field" removed and the first letter downcased, resulting in :type.

 public SubAgreementFormData prepareCreate(SubAgreementFormData formData) throws ProcessingException{
   if(!ACCESS.check(new CreateSubAgreementPermission())){
     throw new VetoException(Texts.get("YouAreNotAuthorizedToRegisterformDataData"));
   }
   // nothing to do
   return formData;
 }
 
 public SubAgreementFormData create(SubAgreementFormData formData) throws ProcessingException{
   if(!ACCESS.check(new CreateSubAgreementPermission())){
     throw new VetoException(Texts.get("YouAreNotAuthorizedToRegisterformDataData"));
   }
   SQL.insert("INSERT INTO SL_SUB_AGREEMENT (" +
   		"            ( PROJECT_NR," +
   		"              TYPE_UID," +
   		"              STATUS_UID," +
   		"              EVT_VERSION," +
   		"              EVT_ISSUE," +
   		"              EVT_SIGN," +
   		"              EVT_EFFECTIVE," +
   		"              EVT_CANCEL," +
   		"              EVT_TERMINATE )" +
   		"     VALUES ( :projectNr," +
   		"              :type," +
   		"              :status," +
   		"              :version," +
   		"              :issued," +
               "              :signed," +
               "              :effective," +
               "              :cancellation," +
               "              :termination )",
               formData);
   return formData;
 }
 
 public SubAgreementFormData load(SubAgreementFormData formData) throws ProcessingException{
   if(!ACCESS.check(new ReadSubAgreementPermission())){
     throw new VetoException(Texts.get("YouAreNotAuthorizedToRegisterformDataData"));
   }
   SQL.select("SELECT STATUS_UID," +
       "              EVT_VERSION," +
       "              EVT_ISSUE," +
       "              EVT_SIGN," +
       "              EVT_EFFECTIVE," +
       "              EVT_CANCEL," +
       "              EVT_TERMINATE" +
       "         FROM SL_SUB_AGREEMENT" +
       "        WHERE PROJECT_NR = :projectNr" +
       "          AND TYPE_UID = :type" +
       "         INTO :status," +
       "              :version," +
       "              :issued," +
       "              :signed," +
       "              :effective," +
       "              :cancellation," +
       "              :termination )",
       formData);
   return formData;
 }
 
 public SubAgreementFormData store(SubAgreementFormData formData) throws ProcessingException{
   if(!ACCESS.check(new UpdateSubAgreementPermission())){
     throw new VetoException(Texts.get("YouAreNotAuthorizedToRegisterformDataData"));
   }
   SQL.update("UPDATE SL_SUB_AGREEMENT SET" +
       "              STATUS_UID = :status," +
       "              EVT_VERSION = :version," +
       "              EVT_ISSUE = :issued," +
       "              EVT_SIGN = :signed," +
       "              EVT_EFFECTIVE = :effective," +
       "              EVT_CANCEL = :cancellation," +
       "              EVT_TERMINATE = :termination" +
       "       WHERE PROJECT_NR = :projectNr" +
       "       AND TYPE_UID = :type",
       formData);
   return formData;
 }