Re: Saving a single object

From: Mikaël Cluseau (nwr..wrk.dyndns.org)
Date: Tue May 10 2005 - 06:17:26 EDT

  • Next message: Andrus Adamchik: "Re: Saving a single object"

    It was a little bit more complicated... was talking about the general
    case. Here my code (copyleft, unstable ;-)) :

    public static void singleSave(DataObject o) {
            // Perform the single save
            DataContext c = DataContext.createDataContext(o.getDataContext()
                            .getParentDataDomain().getName());

            DataObject newObject;
            switch (o.getPersistenceState()) {
            case NEW:
                    newObject = c.createAndRegisterNewObject(o.getClass());
                    copyModifiedAttributes(o, newObject);
                    // FIXME Granted?
                    o.setPersistenceState(PersistenceState.COMMITTED);
                    break;
            case MODIFIED:
                    newObject = DataObjectUtils.objectForPK(c, o.getObjectId());
                    copyModifiedAttributes(o, newObject);
                    // FIXME Granted?
                    o.setPersistenceState(PersistenceState.COMMITTED);
                    break;
            case DELETED:
                    newObject = DataObjectUtils.objectForPK(c, o.getObjectId());
                    c.deleteObject(newObject);
                    break;
            default:
                    throw new InvalidParameterException("Cannot singleSave() "
                                    + PersistenceState.persistenceStateName(o
                                    .getPersistenceState()) + " data objects");
            }
            c.commitChanges();
    }

    private static void copyModifiedAttributes(DataObject from, DataObject to) {
            DataContext context = from.getDataContext();
            ObjEntity entity = context.getEntityResolver().lookupObjEntity(from);

            Map<String, ?> committed = context.getObjectStore().getCachedSnapshot(
                    from.getObjectId());
            Map<String, ?> current = context.currentSnapshot(from);

            for (Object it : entity.getAttributes()) {
                    final String attr = ((ObjAttribute) it).getName();

                    if (committed == null) { // Mainly for NEW objects
                            to.writeProperty(attr, current.get(attr));
                    } else { // For MODIFIED objects
                            Object value = committed.get(attr);
                            if (value != current.get(attr)) {
                                    to.writeProperty(attr, value);
                            }
                    }
            }
    }

    Le mercredi 27 avril 2005 à 20:19 -0400, Mike Kienenberger a écrit :
    > =?ISO-8859-1?Q?Mika=EBl?= Cluseau <nwr..wrk.dyndns.org> wrote:
    > > I'm sorry to ask that but neither google nor the cayenne doc enlighted
    > > me on a stupid question... how do you save a single object? (yes, I must
    > > support this feature...)
    >
    > The same way you save multiple items -- dataContextInstance.commitChanges();
    >
    > A simplified free-form example off the top of my head so there's probably
    > errors:
    >
    > DataContext dc = DataContext.createNewDataContext();
    > MyObject mo = dc.createAndRegisterNewObject(MyObject.class);
    > mo.setBar("foo");
    > dc.commitChanges();
    >

    -- 
    Mikaël Cluseau <nwr..wrk.dyndns.org>
    



    This archive was generated by hypermail 2.0.0 : Tue May 10 2005 - 08:43:09 EDT