Search in sources :

Example 1 with AttachFieldManager

use of org.datanucleus.store.fieldmanager.AttachFieldManager in project datanucleus-core by datanucleus.

the class StateManagerImpl method attach.

/**
 * Method to attach the object managed by this StateManager.
 * @param embedded Whether it is embedded
 */
public void attach(boolean embedded) {
    if (isAttaching()) {
        return;
    }
    setAttaching(true);
    try {
        // Check if the object is already persisted
        boolean persistent = false;
        if (embedded) {
            persistent = true;
        } else {
            if (!myEC.getBooleanProperty(PropertyNames.PROPERTY_ATTACH_SAME_DATASTORE)) {
                // We cant assume that this object was detached from this datastore so we check it
                try {
                    locate();
                    persistent = true;
                } catch (NucleusObjectNotFoundException onfe) {
                // Not currently present!
                }
            } else {
                // Assumed detached from this datastore
                persistent = true;
            }
        }
        // Call any "pre-attach" listeners
        getCallbackHandler().preAttach(myPC);
        // Retrieve the updated values from the detached object
        replaceStateManager(myPC, this);
        retrieveDetachState(this);
        if (!persistent) {
            // Persist the object into this datastore first
            makePersistent();
        }
        // Migrate the lifecycle state to persistent
        myLC = myLC.transitionAttach(this);
        // Make sure the attached object goes in the cache
        // [would not get cached when not changed if we didnt do this here]
        myEC.putObjectIntoLevel1Cache(this);
        int[] attachFieldNumbers = getFieldNumbersOfLoadedOrDirtyFields(loadedFields, dirtyFields);
        if (attachFieldNumbers != null) {
            // Only update the fields that were detached, and only update them if there are any to update
            NucleusLogger.GENERAL.debug("Attaching id=" + getInternalObjectId() + " fields=" + StringUtils.intArrayToString(attachFieldNumbers));
            provideFields(attachFieldNumbers, new AttachFieldManager(this, cmd.getSCOMutableMemberFlags(), dirtyFields, persistent, true, false));
        }
        // Call any "post-attach" listeners
        getCallbackHandler().postAttach(myPC, myPC);
    } finally {
        setAttaching(false);
    }
}
Also used : AttachFieldManager(org.datanucleus.store.fieldmanager.AttachFieldManager) NucleusObjectNotFoundException(org.datanucleus.exceptions.NucleusObjectNotFoundException)

Example 2 with AttachFieldManager

use of org.datanucleus.store.fieldmanager.AttachFieldManager in project datanucleus-core by datanucleus.

the class StateManagerImpl method internalAttachCopy.

/**
 * Attach the fields for this object using the provided detached object.
 * This will attach all loaded plus all dirty fields.
 * @param detachedOP ObjectProvider for the detached object.
 * @param loadedFields Fields that were detached with the object
 * @param dirtyFields Fields that have been modified while detached
 * @param persistent whether the object is already persistent
 * @param version the version
 * @param cascade Whether to cascade the attach to related fields
 */
private void internalAttachCopy(ObjectProvider detachedOP, boolean[] loadedFields, boolean[] dirtyFields, boolean persistent, Object version, boolean cascade) {
    // Need to take all loaded fields plus all modified fields
    // (maybe some werent detached but have been modified) and attach them
    int[] attachFieldNumbers = getFieldNumbersOfLoadedOrDirtyFields(loadedFields, dirtyFields);
    setVersion(version);
    if (attachFieldNumbers != null) {
        // Attach all dirty fields, and load other loaded fields
        NucleusLogger.GENERAL.debug("Attaching id=" + getInternalObjectId() + " fields=" + StringUtils.intArrayToString(attachFieldNumbers));
        detachedOP.provideFields(attachFieldNumbers, new AttachFieldManager(this, cmd.getSCOMutableMemberFlags(), dirtyFields, persistent, cascade, true));
    }
}
Also used : AttachFieldManager(org.datanucleus.store.fieldmanager.AttachFieldManager)

Example 3 with AttachFieldManager

use of org.datanucleus.store.fieldmanager.AttachFieldManager in project datanucleus-core by datanucleus.

the class StateManagerImpl method attach.

/* (non-Javadoc)
     * @see org.datanucleus.state.StateManager#attach(java.lang.Object)
     */
public void attach(Persistable detachedPC) {
    if (isAttaching()) {
        return;
    }
    setAttaching(true);
    try {
        // Call any "pre-attach" listeners
        getCallbackHandler().preAttach(myPC);
        // Connect the transient object to a StateManager so we can get its values
        ObjectProvider detachedSM = new StateManagerImpl(myEC, cmd);
        detachedSM.initialiseForDetached(detachedPC, myID, null);
        // Make sure the attached object is in the cache
        myEC.putObjectIntoLevel1Cache(this);
        int[] nonPKFieldNumbers = cmd.getNonPKMemberPositions();
        if (nonPKFieldNumbers != null && nonPKFieldNumbers.length > 0) {
            // Attach the (non-PK) fields from the transient
            NucleusLogger.GENERAL.debug("Attaching id=" + getInternalObjectId() + " fields=" + StringUtils.intArrayToString(nonPKFieldNumbers));
            detachedSM.provideFields(nonPKFieldNumbers, new AttachFieldManager(this, cmd.getSCOMutableMemberFlags(), cmd.getNonPKMemberFlags(), true, true, false));
        }
        // Disconnect the transient object
        replaceStateManager(detachedPC, null);
        // Call any "post-attach" listeners
        getCallbackHandler().postAttach(myPC, myPC);
    } finally {
        setAttaching(false);
    }
}
Also used : AttachFieldManager(org.datanucleus.store.fieldmanager.AttachFieldManager)

Aggregations

AttachFieldManager (org.datanucleus.store.fieldmanager.AttachFieldManager)3 NucleusObjectNotFoundException (org.datanucleus.exceptions.NucleusObjectNotFoundException)1