Page 1 of 1

--Server-Side Trigger for Storages

PostPosted: Sat Feb 26, 2011 2:00 pm
by Development@SIB


This article is outdated - please use our new system at

https://doc.sibvisions.com




With JVx, the business logic can be developed in either the database or the middle tier. For the development of the business logic entirely in the middle tier so-called Server-Side Triggers are required. These triggers work just like the DataBook Events at the client.

For example, a Server-Side Trigger is called when a new record was inserted, modified or deleted.


Example

We use a RemoteDataBook at the client for the creation of user accounts. A user is defined by username, password, first and last name. The password, however, has to be encrypted before it is stored in the database.

We define the storage:

Code: Select all
public DBStorage getUsers() throws Exception
{
   DBStorage users = (DBStorage)get("users");
   if (users == null)
   {
      users = new DBStorage();
      users.setDBAccess(getDBAccess());
      users.setWritebackTable("USERS");
      users.open();
      
      users.eventBeforeInsert().addListener(this, "doEncryptPwd");
      users.eventBeforeUpdate().addListener(this, "doEncryptPwd");

      put("users", users);
   }
   return users;
}

and the trigger to insert and update:

Code: Select all
public void doEncryptPwd(StorageEvent pEvent) throws Exception
{
   IBean bn = pEvent.getNew();
   
   String sNew = (String)bn.get("PASSWORD");
   String sOld;
   
   IBean bnOld = pEvent.getOld();
   
   if (bnOld != null)
   {
      sOld = (String)bnOld.get("PASSWORD");
   }
   else
   {
      sOld = null;
   }
   
   if (!CommonUtil.equals(sOld, sNew))
   {
      bn.put("PASSWORD", AbstractSecurityManager.getEncryptedPassword(
                         SessionContext.getCurrentSessionConfig(), sNew));
   }
}

In our example we use IBean to access the properties. The event also allows the use of POJOs, as shown in the following example:

Code: Select all
public void doEncryptPwd(StorageEvent pEvent)
{
    ...
    ...
    User user = pEvent.getNew(User.class);     

    user.setPassword(AbstractSecurityManager.getEncryptedPassword(
                     SessionContext.getCurrentSessionConfig(),
                     user.getPassword()));
}


Note

Any POJO can be used. The implemented mechanism attempts to align the properties via the databaseĀ“s column identifiers. In our previous example we could also use an Address POJO, as long as it contains the relevant properties. Only feasible properties are aligned.