Re: Setting Default Values for Entities

From: Adam Yocum (adamyocu..ahoo.com)
Date: Fri Dec 28 2007 - 11:36:26 EST


I thought I'd go ahead and post the Listener that I created to handle setting up the defaults for a MySQL DB dynamically. The listener gets the default values from the MySQL DB and then loops through the attributes of the entity object setting the defaults pulled down from the server.
   
  This of course does only work when using the CayenneDataObject, not the PersistentObject that the client classes use, but I figured it might be useful for somebody.
   
  package test;
  /**
 *
 *..uthor Adam
 */
import java.util.Collection;
import java.util.List;
import java.util.Map;
import org.apache.cayenne.CayenneDataObject;
import org.apache.cayenne.LifecycleListener;
import org.apache.cayenne.map.DbAttribute;
import org.apache.cayenne.map.ObjAttribute;
import org.apache.cayenne.map.ObjEntity;
import org.apache.cayenne.query.SQLTemplate;
  public class MyListener implements LifecycleListener {
      public void prePersist(Object entity) {
        setNewObjectDefaults((CayenneDataObject)entity);
    }
      private void setNewObjectDefaults(CayenneDataObject CayenneObj) {
        ObjEntity ent = CayenneObj.getObjEntity();
        Collection attribs = ent.getAttributes();
          String defaultSql = "SHOW columns FROM "+ent.getDbEntityName();
        SQLTemplate rawSelect = new SQLTemplate(CayenneObj.getClass(), defaultSql);
        rawSelect.setFetchingDataRows(true);
        List DefaultList = CayenneObj.getObjectContext().performQuery(rawSelect);
        
        for (int i = 0; i <= attribs.size() - 1; i++) {
            ObjAttribute ObjAtt = (ObjAttribute) attribs.toArray()[i];
            DbAttribute att = ObjAtt.getDbAttribute();
            String propName = ObjAtt.getName();
            Object propertyValue = CayenneObj.readProperty(propName);
              Object Default = null;
            for(int j=0;j<=DefaultList.size()-1;j++)
            {
                Map row = (Map)DefaultList.get(j);
                String rowFieldName = (String)row.get("Field");
                if(rowFieldName.equals(att.getName()))
                    Default = (Object)row.get("Default");
            }
              if (propertyValue == null)
            {
                CayenneObj.writeProperty(propName, Default);
            }
        }
    }
      public void postPersist(Object arg0) {
      }
      public void preRemove(Object arg0) {
      }
      public void postRemove(Object arg0) {
      }
      public void preUpdate(Object arg0) {
      }
      public void postUpdate(Object arg0) {
      }
      public void postLoad(Object arg0) {
      }
}

Andrus Adamchik <andru..bjectstyle.org> wrote:
  
On Dec 23, 2007, at 6:05 AM, Aristedes Maniatis wrote:

>>
>> Since the method for setting defaults in the db is the same accross
>> the database depending only on field type I can put together
>> something like the following to initialize objects following our
>> rules... still it does not work ROP :( PersistentObject used for
>> client seems to have no way to access the metadata like
>> CayenneDataObject does, i.e. no getObjEntity()
>
> Yeah, anything you do with this will need to be server side, not in
> the client. But lifecycle events work only on the server anyway, and
> only once you've committed the object from the client. The code you
> posted could go into the common superclass for your persistent
> objects on the server. Catch the lifecycle event (either preUpdate
> or prePersist) and fire off your code.
>
> http://cayenne.apache.org/doc/lifecycle-callbacks.html

Yep, looks like defaults extraction can be done in a generic fashion,
and be abstracted in a single listener (or persistent superclass). I
prefer listener, as it wouldn't have to store shared state in static
variables. BTW, this could be a nice addition to Cayenne core down the
line (we can support runtime defaults extraction via DbAdapter).

On the ROP end for now invalidation with RefreshQuery maybe the only
way to get the defaults from the server, but eventually we'd like to
implement a smarter protocol that would pass any custom changes made
via callbacks and listeners on the server back to the client.

Andrus

       
---------------------------------
Never miss a thing. Make Yahoo your homepage.



This archive was generated by hypermail 2.0.0 : Fri Dec 28 2007 - 11:37:02 EST