RE: takeValuesFromDictionary: equvalent

From: Andrus (andru..bjectstyle.org)
Date: Fri Aug 16 2002 - 18:25:17 EDT

  • Next message: JR Ruggentaler: "RE: takeValuesFromDictionary: equvalent"

    It is a bit tougher if you also want to copy the relationships while
    maintaing consistent object graph.

    >If my
    >CayenneDataObject has a relationship to another CayenneDataObject what
    >should the key and value be for the relationship be in my Map instance that
    >I am using in props.putAll(map)? The relationship name and an instance of
    >the related CayenneDataObject?

    Yes, but see below.

    >What about to many relationships a List
    >containing instances of the related CayenneDataObject?

    To be able to do lazy fetching of to-many, we use a special List
    implementation called "ToManyList". When you register a new object with
    context, all to-many relationships are tracked and assigned an empty
    ToManyList. So I guess when copying is done, you must have custom logic
    that would take a list from your map, find corresponding list in the
    object, and replace all its contents:

    List newList = (List)map.get(nextKey);
    List existingList = (List)this.params.get(nextKey);
    if(existingList != null) {
        existingList.clear();
        existingList.addAll(newList);
    }

    Now, the above approach will NOT take care of reverse relationships to an
    object. Take a look at CayenneDataObject source code for cleaner ways to do
    object graph management. Relevant methods are:

    addToManyTarget
    setToOneTarget
    setToOneDependentTarget

    Here is a resulting "clean" method. I haven't tested or even compiled it,
    so let me know if this works at all, we might add this to Cayenne.

    public MyDataObject extends CayenneDataObject {
        public void updateValues(Map map) {
            // we are modified...
            if(persistenceState == PersistenceState.COMMITTED) {
                 persistenceState = PersistenceState.MODIFIED;
            }

             ObjEntity e =
    dataContext.lookupEntity(getObjectId().getObjEntityName());
            Iterator it = map.keySet().iterator();
            while(it.hasNext()) {
                String key = (String)it.next();
                Object val = map.get(key);
                if(e.getAttribute(key) != null) {
                     params.put(key, val);
                }
                else if(e.getRelationship() != null) {
                    ObjRelationship r = (ObjRelationship)e.getRelationship();
                    if(r.isToMany()) {
                         List newList = (List)val ;
                         List existingList = (List)params.get(key);
                         existingList.clear();
                          Iterator listIt = newList .iterator();
                         while(listIt.hasNext()) {
                              DataObject o = (DataObject)listIt.next();
                               addToManyTarget(key, o, true);
                         }

                    }
                    else if(r.isToDependentEntity()) {
                       setToOneDependentTarget(key, (DataObject)val);
                    }
                    else {
                       setToOneTarget(key, (DataObject)val, true);
                    }
                }
            }
        }
    }



    This archive was generated by hypermail 2.0.0 : Fri Aug 16 2002 - 18:24:50 EDT