Search in sources :

Example 1 with GraphManager

use of org.apache.cayenne.graph.GraphManager in project cayenne by apache.

the class BaseContext method injectInitialValue.

/**
 * If ObjEntity qualifier is set, asks it to inject initial value to an
 * object. Also performs all Persistent initialization operations
 */
protected void injectInitialValue(Object obj) {
    // must follow this exact order of property initialization per CAY-653,
    // i.e. have
    // the id and the context in place BEFORE setPersistence is called
    Persistent object = (Persistent) obj;
    object.setObjectContext(this);
    object.setPersistenceState(PersistenceState.NEW);
    GraphManager graphManager = getGraphManager();
    synchronized (graphManager) {
        graphManager.registerNode(object.getObjectId(), object);
        graphManager.nodeCreated(object.getObjectId());
    }
    ObjEntity entity;
    try {
        entity = getEntityResolver().getObjEntity(object.getClass());
    } catch (CayenneRuntimeException ex) {
        // ObjEntity cannot be fetched, ignored
        entity = null;
    }
    if (entity != null) {
        if (entity.getDeclaredQualifier() instanceof ValueInjector) {
            ((ValueInjector) entity.getDeclaredQualifier()).injectValue(object);
        }
    }
    // invoke callbacks
    getEntityResolver().getCallbackRegistry().performCallbacks(LifecycleEvent.POST_ADD, object);
}
Also used : ObjEntity(org.apache.cayenne.map.ObjEntity) GraphManager(org.apache.cayenne.graph.GraphManager) ValueInjector(org.apache.cayenne.exp.ValueInjector)

Example 2 with GraphManager

use of org.apache.cayenne.graph.GraphManager in project cayenne by apache.

the class ShallowMergeOperation method merge.

public <T extends Persistent> T merge(T peerInParentContext) {
    if (peerInParentContext == null) {
        throw new IllegalArgumentException("Null peerInParentContext");
    }
    // handling of HOLLOW peer state is here for completeness... Wonder if we ever
    // have a case where it is applicable.
    int peerState = peerInParentContext.getPersistenceState();
    ObjectId id = peerInParentContext.getObjectId();
    ClassDescriptor descriptor = context.getEntityResolver().getClassDescriptor(id.getEntityName());
    GraphManager graphManager = context.getGraphManager();
    // messing up dataobjects per CAY-845.
    synchronized (graphManager) {
        T object = (T) graphManager.getNode(id);
        // merge into an existing object
        if (object == null) {
            object = (T) descriptor.createObject();
            object.setObjectContext(context);
            object.setObjectId(id);
            if (peerState == PersistenceState.HOLLOW) {
                object.setPersistenceState(PersistenceState.HOLLOW);
            } else {
                object.setPersistenceState(PersistenceState.COMMITTED);
            }
            graphManager.registerNode(id, object);
        }
        // TODO: Andrus, 1/24/2006 implement smart merge for modified objects...
        if (peerState != PersistenceState.HOLLOW && object.getPersistenceState() != PersistenceState.MODIFIED && object.getPersistenceState() != PersistenceState.DELETED) {
            descriptor.shallowMerge(peerInParentContext, object);
            if (object.getPersistenceState() == PersistenceState.HOLLOW) {
                object.setPersistenceState(PersistenceState.COMMITTED);
            }
        }
        return object;
    }
}
Also used : GraphManager(org.apache.cayenne.graph.GraphManager) ClassDescriptor(org.apache.cayenne.reflect.ClassDescriptor) ObjectId(org.apache.cayenne.ObjectId)

Aggregations

GraphManager (org.apache.cayenne.graph.GraphManager)2 ObjectId (org.apache.cayenne.ObjectId)1 ValueInjector (org.apache.cayenne.exp.ValueInjector)1 ObjEntity (org.apache.cayenne.map.ObjEntity)1 ClassDescriptor (org.apache.cayenne.reflect.ClassDescriptor)1