Reconnecting serialized DataObjects

From: Mike Kienenberger (mkienen..mail.com)
Date: Fri Sep 23 2005 - 13:24:05 EDT

  • Next message: Mike Kienenberger: "Re: DataContext.localObjects() for modified objects"

    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 : Fri Sep 23 2005 - 13:24:09 EDT