Re: Reconnecting serialized DataObjects

From: Cris Daniluk (cris.danilu..mail.com)
Date: Mon Sep 26 2005 - 11:51:44 EDT

  • Next message: Mike Kienenberger: "Re: Reconnecting serialized DataObjects"

    Wouldn't the data context be serialized alongside the objects, and if
    so, why would the context and object ever mismatch? Maybe I'm missing
    something...

    Cris

    On 9/23/05, Mike Kienenberger <mkienen..mail.com> wrote:
    > What are others doing to handle reconnecting serialized DataObjects
    > back to a DataContext?
    >
    > My current situation is that JSF serializes all of my DataObjects
    > between requests, and I'm using this code to restore them. However, I
    > suspect this code doesn't properly handle the case where the data
    > context may change.
    >
    > Kevin's thread "CayenneDataObject#setDataContext()" has me worried
    > that I need to do more than what I'm currently doing.
    >
    > [In the code below, currentDataContext may or may not be the original
    > DataContext, although I think my dataContext has remained unchanged so
    > far.]
    >
    > public void reconnect(Object object)
    > {
    > if (null == object) return;
    >
    > if (false == (object instanceof DataObject))
    > throw new
    > IllegalArgumentException(object.getClass().getName() + " is not a
    > DataObject.");
    >
    > DataObject dataObject = (DataObject)object;
    >
    > if (null != dataObject.getDataContext()) return;
    >
    > dataObject.setDataContext(currentDataContext);
    >
    > EntityResolver entityResolver = currentDataContext.getEntityResolver();
    > ObjEntity objEntity = entityResolver.lookupObjEntity(dataObject);
    >
    > // initialize to-many relationships with a fault
    > Iterator it = objEntity.getRelationships().iterator();
    > while (it.hasNext()) {
    > ObjRelationship rel = (ObjRelationship) it.next();
    > if (rel.isToMany()) {
    > dataObject.writePropertyDirectly(rel.getName(),
    > Fault.getToManyFault());
    > Object relObject =
    > dataObject.readPropertyDirectly(rel.getName());
    > if (relObject instanceof List)
    > {
    > List relList = (List)relObject;
    > Iterator relObjectIterator = relList.iterator();
    > while (relObjectIterator.hasNext())
    > {
    > DataObject relDataObject = (DataObject)
    > relObjectIterator.next();
    > reconnect(relDataObject);
    > }
    > }
    > }
    > else
    > {
    > Object relObject =
    > dataObject.readPropertyDirectly(rel.getName());
    > if (relObject instanceof DataObject) reconnect(relObject);
    > }
    > }
    > }
    >



    This archive was generated by hypermail 2.0.0 : Mon Sep 26 2005 - 11:51:52 EDT