Search in sources :

Example 21 with ApiAdapter

use of org.datanucleus.api.ApiAdapter in project datanucleus-core by datanucleus.

the class MakeTransientFieldManager method internalFetchObjectField.

/**
 * Method to fetch an object field whether it is SCO collection, PC, or whatever for the fetchplan process.
 * @param fieldNumber Number of the field
 * @return The object
 */
protected Object internalFetchObjectField(int fieldNumber) {
    SingleValueFieldManager sfv = new SingleValueFieldManager();
    op.provideFields(new int[] { fieldNumber }, sfv);
    Object value = sfv.fetchObjectField(fieldNumber);
    if (value != null) {
        AbstractMemberMetaData mmd = op.getClassMetaData().getMetaDataForManagedMemberAtAbsolutePosition(fieldNumber);
        RelationType relType = mmd.getRelationType(op.getExecutionContext().getClassLoaderResolver());
        if (relType != RelationType.NONE) {
            if (mmd.hasContainer()) {
                // Replace with SCO, when possible
                value = SCOUtils.wrapSCOField(op, fieldNumber, value, true);
                TypeManager typeManager = op.getExecutionContext().getTypeManager();
                ContainerAdapter containerAdapter = typeManager.getContainerAdapter(value);
                ApiAdapter api = op.getExecutionContext().getApiAdapter();
                // Process all elements of the Container that are PC
                for (Object object : containerAdapter) {
                    if (api.isPersistable(object)) {
                        processPersistable(object);
                    }
                }
            } else {
                processPersistable(value);
            }
        }
        if (value instanceof SCO) {
            ((SCO) value).unsetOwner();
        }
    }
    return value;
}
Also used : ApiAdapter(org.datanucleus.api.ApiAdapter) RelationType(org.datanucleus.metadata.RelationType) TypeManager(org.datanucleus.store.types.TypeManager) ContainerAdapter(org.datanucleus.store.types.ContainerAdapter) AbstractMemberMetaData(org.datanucleus.metadata.AbstractMemberMetaData) SCO(org.datanucleus.store.types.SCO)

Example 22 with ApiAdapter

use of org.datanucleus.api.ApiAdapter in project datanucleus-core by datanucleus.

the class PersistFieldManager method processMapContainer.

private void processMapContainer(int fieldNumber, Object container, AbstractMemberMetaData mmd) {
    TypeManager typeManager = op.getExecutionContext().getTypeManager();
    ContainerHandler<Object, MapContainerAdapter<Object>> containerHandler = typeManager.getContainerHandler(mmd.getType());
    ApiAdapter api = op.getExecutionContext().getApiAdapter();
    // Process all keys, values of the Map that are PC
    MapContainerAdapter<Object> mapAdapter = containerHandler.getAdapter(container);
    for (Entry<Object, Object> entry : mapAdapter.entries()) {
        Object mapKey = entry.getKey();
        Object mapValue = entry.getValue();
        Object newMapKey = mapKey;
        Object newMapValue = mapValue;
        if (api.isPersistable(mapKey)) {
            // Persist (or attach) the key
            int mapKeyObjectType = mmd.getMap().isEmbeddedKey() || mmd.getMap().isSerializedKey() ? ObjectProvider.EMBEDDED_MAP_KEY_PC : ObjectProvider.PC;
            newMapKey = processPersistable(mapKey, fieldNumber, mapKeyObjectType);
        }
        if (api.isPersistable(mapValue)) {
            // Persist (or attach) the value
            int mapValueObjectType = mmd.getMap().isEmbeddedValue() || mmd.getMap().isSerializedValue() ? ObjectProvider.EMBEDDED_MAP_VALUE_PC : ObjectProvider.PC;
            newMapValue = processPersistable(mapValue, fieldNumber, mapValueObjectType);
        }
        if (newMapKey != mapKey || newMapValue != mapValue) {
            // Maybe we have just have attached key or value
            boolean updateKey = false;
            boolean updateValue = false;
            if (newMapKey != mapKey) {
                ObjectProvider keyOP = op.getExecutionContext().findObjectProvider(newMapKey);
                if (keyOP.getReferencedPC() != null) {
                    // Attaching the key
                    updateKey = true;
                }
            }
            if (newMapValue != mapValue) {
                ObjectProvider valOP = op.getExecutionContext().findObjectProvider(newMapValue);
                if (valOP.getReferencedPC() != null) {
                    // Attaching the value
                    updateValue = true;
                }
            }
            if (updateKey) {
                mapAdapter.remove(mapKey);
                mapAdapter.put(newMapKey, updateValue ? newMapValue : mapValue);
            } else if (updateValue) {
                mapAdapter.put(mapKey, newMapValue);
            }
        }
    }
}
Also used : MapContainerAdapter(org.datanucleus.store.types.MapContainerAdapter) ApiAdapter(org.datanucleus.api.ApiAdapter) TypeManager(org.datanucleus.store.types.TypeManager) ObjectProvider(org.datanucleus.state.ObjectProvider)

Example 23 with ApiAdapter

use of org.datanucleus.api.ApiAdapter in project datanucleus-core by datanucleus.

the class PersistFieldManager method processElementContainer.

private void processElementContainer(int fieldNumber, Object container, AbstractMemberMetaData mmd) {
    TypeManager typeManager = op.getExecutionContext().getTypeManager();
    ElementContainerHandler<Object, ElementContainerAdapter<Object>> elementContainerHandler = typeManager.getContainerHandler(mmd.getType());
    // Process all elements of the container that are PC
    ElementContainerAdapter containerAdapter = elementContainerHandler.getAdapter(container);
    ApiAdapter api = op.getExecutionContext().getApiAdapter();
    int objectType = elementContainerHandler.getObjectType(mmd);
    if (objectType == ObjectProvider.PC) {
        int elementPosition = 0;
        for (Object element : containerAdapter) {
            if (api.isPersistable(element)) {
                Object newElement = processPersistable(element, -1, objectType);
                ObjectProvider elementSM = op.getExecutionContext().findObjectProvider(newElement);
                if (elementSM.getReferencedPC() != null) {
                    // Must be attaching this element, so swap element (detached -> attached)
                    if (containerAdapter instanceof SequenceAdapter) {
                        ((SequenceAdapter) containerAdapter).update(newElement, elementPosition);
                    } else {
                        containerAdapter.remove(elementSM);
                        containerAdapter.add(newElement);
                    }
                }
            }
            elementPosition++;
        }
    } else {
        // Embedded/Serialized
        for (Object element : containerAdapter) {
            if (api.isPersistable(element)) {
                processPersistable(element, fieldNumber, objectType);
            }
        }
    }
}
Also used : ApiAdapter(org.datanucleus.api.ApiAdapter) ElementContainerAdapter(org.datanucleus.store.types.ElementContainerAdapter) SequenceAdapter(org.datanucleus.store.types.SequenceAdapter) TypeManager(org.datanucleus.store.types.TypeManager) ObjectProvider(org.datanucleus.state.ObjectProvider)

Example 24 with ApiAdapter

use of org.datanucleus.api.ApiAdapter in project datanucleus-core by datanucleus.

the class ReachabilityFieldManager method processContainer.

private void processContainer(int fieldNumber, Object container, AbstractMemberMetaData mmd) {
    // Process all objects of the Container that are PC
    if (NucleusLogger.PERSISTENCE.isDebugEnabled()) {
        NucleusLogger.PERSISTENCE.debug(Localiser.msg("007002", mmd.getFullFieldName()));
    }
    ApiAdapter api = op.getExecutionContext().getApiAdapter();
    TypeManager typeManager = op.getExecutionContext().getTypeManager();
    for (Object object : typeManager.getContainerAdapter(container)) {
        if (api.isPersistable(object)) {
            processPersistable(object, mmd);
        }
    }
}
Also used : ApiAdapter(org.datanucleus.api.ApiAdapter) TypeManager(org.datanucleus.store.types.TypeManager)

Example 25 with ApiAdapter

use of org.datanucleus.api.ApiAdapter in project datanucleus-rdbms by datanucleus.

the class RDBMSStoreManager method getDatastoreClass.

/**
 * Returns the primary datastore table serving as backing for the given class.
 * If the class is not yet known to the store manager, {@link #manageClasses} is called
 * to add it. Classes which have inheritance strategy of "new-table" and
 * "superclass-table" will return a table here, whereas "subclass-table" will
 * return null since it doesn't have a table as such.
 * <p>
 * @param className Name of the class whose table is be returned.
 * @param clr The ClassLoaderResolver
 * @return The corresponding class table.
 * @exception NoTableManagedException If the given class has no table managed in the database.
 */
public DatastoreClass getDatastoreClass(String className, ClassLoaderResolver clr) {
    DatastoreClass ct = null;
    if (className == null) {
        NucleusLogger.PERSISTENCE.error(Localiser.msg("032015"));
        return null;
    }
    schemaLock.readLock().lock();
    try {
        StoreData sd = storeDataMgr.get(className);
        if (sd != null && sd instanceof RDBMSStoreData) {
            ct = (DatastoreClass) sd.getTable();
            if (ct != null) {
                // Class known about
                return ct;
            }
        }
    } finally {
        schemaLock.readLock().unlock();
    }
    // Class not known so consider adding it to our list of supported classes.
    // Currently we only consider PC classes
    boolean toBeAdded = false;
    if (clr != null) {
        Class cls = clr.classForName(className);
        ApiAdapter api = getApiAdapter();
        if (cls != null && !cls.isInterface() && api.isPersistable(cls)) {
            toBeAdded = true;
        }
    } else {
        toBeAdded = true;
    }
    boolean classKnown = false;
    if (toBeAdded) {
        // Add the class to our supported list
        manageClasses(clr, className);
        // Retry
        schemaLock.readLock().lock();
        try {
            StoreData sd = storeDataMgr.get(className);
            if (sd != null && sd instanceof RDBMSStoreData) {
                classKnown = true;
                ct = (DatastoreClass) sd.getTable();
            }
        } finally {
            schemaLock.readLock().unlock();
        }
    }
    // Note : "subclass-table" inheritance strategies will return null from this method
    if (!classKnown && ct == null) {
        throw new NoTableManagedException(className);
    }
    return ct;
}
Also used : ApiAdapter(org.datanucleus.api.ApiAdapter) DatastoreClass(org.datanucleus.store.rdbms.table.DatastoreClass) DatastoreClass(org.datanucleus.store.rdbms.table.DatastoreClass) NoTableManagedException(org.datanucleus.store.rdbms.exceptions.NoTableManagedException) StoreData(org.datanucleus.store.StoreData)

Aggregations

ApiAdapter (org.datanucleus.api.ApiAdapter)53 NucleusUserException (org.datanucleus.exceptions.NucleusUserException)19 ObjectProvider (org.datanucleus.state.ObjectProvider)16 ExecutionContext (org.datanucleus.ExecutionContext)12 Map (java.util.Map)11 NucleusException (org.datanucleus.exceptions.NucleusException)11 AbstractMemberMetaData (org.datanucleus.metadata.AbstractMemberMetaData)10 Iterator (java.util.Iterator)8 NucleusObjectNotFoundException (org.datanucleus.exceptions.NucleusObjectNotFoundException)8 JavaTypeMapping (org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping)8 Collection (java.util.Collection)7 HashMap (java.util.HashMap)7 RelationType (org.datanucleus.metadata.RelationType)7 DatastoreClass (org.datanucleus.store.rdbms.table.DatastoreClass)7 ClassLoaderResolver (org.datanucleus.ClassLoaderResolver)6 ClassNotResolvedException (org.datanucleus.exceptions.ClassNotResolvedException)6 AbstractClassMetaData (org.datanucleus.metadata.AbstractClassMetaData)5 SQLExpression (org.datanucleus.store.rdbms.sql.expression.SQLExpression)5 TypeManager (org.datanucleus.store.types.TypeManager)5 SortedMap (java.util.SortedMap)4