Search in sources :

Example 6 with FieldManager

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

the class StateManagerImpl method replaceFields.

/**
 * Called from the StoreManager to refresh data in the Persistable object associated with this StateManager.
 * @param fieldNumbers An array of field numbers to be refreshed by the Store
 * @param fm The updated values are stored in this object. This object is only valid for the duration of this call.
 * @param replaceWhenDirty Whether to replace the fields when they are dirty here
 */
public void replaceFields(int[] fieldNumbers, FieldManager fm, boolean replaceWhenDirty) {
    try {
        if (myEC.getMultithreaded()) {
            myEC.getLock().lock();
            lock.lock();
        }
        FieldManager prevFM = currFM;
        currFM = fm;
        try {
            int[] fieldsToReplace = fieldNumbers;
            if (!replaceWhenDirty) {
                int numberToReplace = fieldNumbers.length;
                for (int i = 0; i < fieldNumbers.length; i++) {
                    if (dirtyFields[fieldNumbers[i]]) {
                        numberToReplace--;
                    }
                }
                if (numberToReplace > 0 && numberToReplace != fieldNumbers.length) {
                    fieldsToReplace = new int[numberToReplace];
                    int n = 0;
                    for (int i = 0; i < fieldNumbers.length; i++) {
                        if (!dirtyFields[fieldNumbers[i]]) {
                            fieldsToReplace[n++] = fieldNumbers[i];
                        }
                    }
                } else if (numberToReplace == 0) {
                    fieldsToReplace = null;
                }
            }
            if (fieldsToReplace != null) {
                myPC.dnReplaceFields(fieldsToReplace);
            }
        } finally {
            currFM = prevFM;
        }
    } finally {
        if (myEC.getMultithreaded()) {
            lock.unlock();
            myEC.getLock().unlock();
        }
    }
}
Also used : LoadFieldManager(org.datanucleus.store.fieldmanager.LoadFieldManager) MakeTransientFieldManager(org.datanucleus.store.fieldmanager.MakeTransientFieldManager) AttachFieldManager(org.datanucleus.store.fieldmanager.AttachFieldManager) UnsetOwnerFieldManager(org.datanucleus.store.fieldmanager.UnsetOwnerFieldManager) DetachFieldManager(org.datanucleus.store.fieldmanager.DetachFieldManager) PersistFieldManager(org.datanucleus.store.fieldmanager.PersistFieldManager) SingleValueFieldManager(org.datanucleus.store.fieldmanager.SingleValueFieldManager) DeleteFieldManager(org.datanucleus.store.fieldmanager.DeleteFieldManager) L2CachePopulateFieldManager(org.datanucleus.cache.L2CachePopulateFieldManager) FieldManager(org.datanucleus.store.fieldmanager.FieldManager) SingleTypeFieldManager(org.datanucleus.store.fieldmanager.SingleTypeFieldManager) L2CacheRetrieveFieldManager(org.datanucleus.cache.L2CacheRetrieveFieldManager)

Example 7 with FieldManager

use of org.datanucleus.store.fieldmanager.FieldManager in project datanucleus-rdbms by datanucleus.

the class PersistableMapping method getValueForDatastoreMapping.

/**
 * Method to return the value to be stored in the specified datastore index given the overall value for this java type.
 * @param nucleusCtx Context
 * @param index The datastore index
 * @param value The overall value for this java type
 * @return The value for this datastore index
 */
public Object getValueForDatastoreMapping(NucleusContext nucleusCtx, int index, Object value) {
    ExecutionContext ec = nucleusCtx.getApiAdapter().getExecutionContext(value);
    if (cmd == null) {
        cmd = nucleusCtx.getMetaDataManager().getMetaDataForClass(getType(), ec != null ? ec.getClassLoaderResolver() : nucleusCtx.getClassLoaderResolver(null));
    }
    if (cmd.getIdentityType() == IdentityType.APPLICATION) {
        AbstractMemberMetaData mmd = cmd.getMetaDataForManagedMemberAtAbsolutePosition(cmd.getPKMemberPositions()[index]);
        ObjectProvider op = null;
        if (ec != null) {
            op = ec.findObjectProvider(value);
        }
        if (op == null) {
            // Transient or detached maybe, so use reflection to get PK field values
            if (mmd instanceof FieldMetaData) {
                return ClassUtils.getValueOfFieldByReflection(value, mmd.getName());
            }
            return ClassUtils.getValueOfMethodByReflection(value, ClassUtils.getJavaBeanGetterName(mmd.getName(), false));
        }
        if (!mmd.isPrimaryKey()) {
            // Make sure the field is loaded
            op.isLoaded(mmd.getAbsoluteFieldNumber());
        }
        FieldManager fm = new SingleValueFieldManager();
        op.provideFields(new int[] { mmd.getAbsoluteFieldNumber() }, fm);
        return fm.fetchObjectField(mmd.getAbsoluteFieldNumber());
    } else if (cmd.getIdentityType() == IdentityType.DATASTORE) {
        Object id = nucleusCtx.getApiAdapter().getIdForObject(value);
        return id != null ? IdentityUtils.getTargetKeyForDatastoreIdentity(id) : null;
    }
    return null;
}
Also used : FieldMetaData(org.datanucleus.metadata.FieldMetaData) ExecutionContext(org.datanucleus.ExecutionContext) AppIDObjectIdFieldManager(org.datanucleus.store.rdbms.mapping.AppIDObjectIdFieldManager) SingleValueFieldManager(org.datanucleus.store.fieldmanager.SingleValueFieldManager) FieldManager(org.datanucleus.store.fieldmanager.FieldManager) SingleValueFieldManager(org.datanucleus.store.fieldmanager.SingleValueFieldManager) ObjectProvider(org.datanucleus.state.ObjectProvider) AbstractMemberMetaData(org.datanucleus.metadata.AbstractMemberMetaData)

Example 8 with FieldManager

use of org.datanucleus.store.fieldmanager.FieldManager in project datanucleus-rdbms by datanucleus.

the class ExpressionUtils method getValueForMemberOfObject.

/**
 * Get the value of a managed field/property in the provided object.
 * @param ec execution context
 * @param mmd metadata for the field/property
 * @param object the pc object
 * @return The field value
 */
public static Object getValueForMemberOfObject(ExecutionContext ec, AbstractMemberMetaData mmd, Object object) {
    if (ec == null) {
        // Transient or detached maybe
        return ClassUtils.getValueOfFieldByReflection(object, mmd.getName());
    // TODO What if this is a property?
    }
    ObjectProvider sm = ec.findObjectProvider(object);
    if (!mmd.isPrimaryKey()) {
        // Make sure the field is loaded
        sm.isLoaded(mmd.getAbsoluteFieldNumber());
    }
    FieldManager fm = new SingleValueFieldManager();
    sm.provideFields(new int[] { mmd.getAbsoluteFieldNumber() }, fm);
    return fm.fetchObjectField(mmd.getAbsoluteFieldNumber());
}
Also used : SingleValueFieldManager(org.datanucleus.store.fieldmanager.SingleValueFieldManager) FieldManager(org.datanucleus.store.fieldmanager.FieldManager) SingleValueFieldManager(org.datanucleus.store.fieldmanager.SingleValueFieldManager) ObjectProvider(org.datanucleus.state.ObjectProvider)

Example 9 with FieldManager

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

the class StateManagerImpl method provideFields.

/**
 * Called from the StoreManager after StoreManager.update() is called to obtain updated values from the Persistable associated with this StateManager.
 * @param fieldNumbers An array of field numbers to be updated by the Store
 * @param fm The updated values are stored in this object. This object is only valid for the duration of this call.
 */
public void provideFields(int[] fieldNumbers, FieldManager fm) {
    try {
        if (myEC.getMultithreaded()) {
            myEC.getLock().lock();
            lock.lock();
        }
        FieldManager prevFM = currFM;
        currFM = fm;
        try {
            // This will respond by calling this.providedXXXFields() with the value of the field
            myPC.dnProvideFields(fieldNumbers);
        } finally {
            currFM = prevFM;
        }
    } finally {
        if (myEC.getMultithreaded()) {
            lock.unlock();
            myEC.getLock().unlock();
        }
    }
}
Also used : LoadFieldManager(org.datanucleus.store.fieldmanager.LoadFieldManager) MakeTransientFieldManager(org.datanucleus.store.fieldmanager.MakeTransientFieldManager) AttachFieldManager(org.datanucleus.store.fieldmanager.AttachFieldManager) UnsetOwnerFieldManager(org.datanucleus.store.fieldmanager.UnsetOwnerFieldManager) DetachFieldManager(org.datanucleus.store.fieldmanager.DetachFieldManager) PersistFieldManager(org.datanucleus.store.fieldmanager.PersistFieldManager) SingleValueFieldManager(org.datanucleus.store.fieldmanager.SingleValueFieldManager) DeleteFieldManager(org.datanucleus.store.fieldmanager.DeleteFieldManager) L2CachePopulateFieldManager(org.datanucleus.cache.L2CachePopulateFieldManager) FieldManager(org.datanucleus.store.fieldmanager.FieldManager) SingleTypeFieldManager(org.datanucleus.store.fieldmanager.SingleTypeFieldManager) L2CacheRetrieveFieldManager(org.datanucleus.cache.L2CacheRetrieveFieldManager)

Example 10 with FieldManager

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

the class StateManagerImpl method provideField.

/**
 * Method to retrieve the value of a field from the PC object. Assumes that it is loaded.
 * @param pc The PC object
 * @param fieldNumber Number of field
 * @return The value of the field
 */
protected Object provideField(Persistable pc, int fieldNumber) {
    Object obj;
    try {
        if (myEC.getMultithreaded()) {
            myEC.getLock().lock();
            lock.lock();
        }
        FieldManager prevFM = currFM;
        currFM = new SingleValueFieldManager();
        try {
            pc.dnProvideField(fieldNumber);
            obj = currFM.fetchObjectField(fieldNumber);
        } finally {
            currFM = prevFM;
        }
    } finally {
        if (myEC.getMultithreaded()) {
            lock.unlock();
            myEC.getLock().unlock();
        }
    }
    return obj;
}
Also used : LoadFieldManager(org.datanucleus.store.fieldmanager.LoadFieldManager) MakeTransientFieldManager(org.datanucleus.store.fieldmanager.MakeTransientFieldManager) AttachFieldManager(org.datanucleus.store.fieldmanager.AttachFieldManager) UnsetOwnerFieldManager(org.datanucleus.store.fieldmanager.UnsetOwnerFieldManager) DetachFieldManager(org.datanucleus.store.fieldmanager.DetachFieldManager) PersistFieldManager(org.datanucleus.store.fieldmanager.PersistFieldManager) SingleValueFieldManager(org.datanucleus.store.fieldmanager.SingleValueFieldManager) DeleteFieldManager(org.datanucleus.store.fieldmanager.DeleteFieldManager) L2CachePopulateFieldManager(org.datanucleus.cache.L2CachePopulateFieldManager) FieldManager(org.datanucleus.store.fieldmanager.FieldManager) SingleTypeFieldManager(org.datanucleus.store.fieldmanager.SingleTypeFieldManager) L2CacheRetrieveFieldManager(org.datanucleus.cache.L2CacheRetrieveFieldManager) SingleValueFieldManager(org.datanucleus.store.fieldmanager.SingleValueFieldManager)

Aggregations

FieldManager (org.datanucleus.store.fieldmanager.FieldManager)13 SingleValueFieldManager (org.datanucleus.store.fieldmanager.SingleValueFieldManager)10 L2CachePopulateFieldManager (org.datanucleus.cache.L2CachePopulateFieldManager)7 L2CacheRetrieveFieldManager (org.datanucleus.cache.L2CacheRetrieveFieldManager)7 AttachFieldManager (org.datanucleus.store.fieldmanager.AttachFieldManager)7 DeleteFieldManager (org.datanucleus.store.fieldmanager.DeleteFieldManager)7 DetachFieldManager (org.datanucleus.store.fieldmanager.DetachFieldManager)7 LoadFieldManager (org.datanucleus.store.fieldmanager.LoadFieldManager)7 MakeTransientFieldManager (org.datanucleus.store.fieldmanager.MakeTransientFieldManager)7 PersistFieldManager (org.datanucleus.store.fieldmanager.PersistFieldManager)7 SingleTypeFieldManager (org.datanucleus.store.fieldmanager.SingleTypeFieldManager)7 UnsetOwnerFieldManager (org.datanucleus.store.fieldmanager.UnsetOwnerFieldManager)7 AbstractMemberMetaData (org.datanucleus.metadata.AbstractMemberMetaData)5 ObjectProvider (org.datanucleus.state.ObjectProvider)5 ExecutionContext (org.datanucleus.ExecutionContext)4 ClassLoaderResolver (org.datanucleus.ClassLoaderResolver)3 NucleusUserException (org.datanucleus.exceptions.NucleusUserException)3 StatementMappingIndex (org.datanucleus.store.rdbms.query.StatementMappingIndex)3 ArrayList (java.util.ArrayList)2 ApiAdapter (org.datanucleus.api.ApiAdapter)2