Search in sources :

Example 6 with SCOID

use of org.datanucleus.identity.SCOID in project datanucleus-core by datanucleus.

the class ExecutionContextImpl method getClassDetailsForId.

/**
 * Convenience method that takes an id, an optional class name for the object it represents, and whether
 * to check for inheritance, and returns class details of the object being represented.
 * Used by the findObject process.
 * @param id The identity
 * @param objectClassName Class name for the object (if known, otherwise is derived)
 * @param checkInheritance Whether to check the inheritance level for this id
 * @return The details for the class
 */
private ClassDetailsForId getClassDetailsForId(Object id, String objectClassName, boolean checkInheritance) {
    // Object not found yet, so work out class name
    String className = null;
    String originalClassName = null;
    boolean checkedClassName = false;
    if (id instanceof SCOID) {
        throw new NucleusUserException(Localiser.msg("010006"));
    } else if (id instanceof DatastoreUniqueLongId) {
        // Should have been found using "persistenceHandler.findObject()"
        throw new NucleusObjectNotFoundException(Localiser.msg("010026"), id);
    } else if (objectClassName != null) {
        // Object class name specified so use that directly
        originalClassName = objectClassName;
    } else {
        originalClassName = getStoreManager().manageClassForIdentity(id, clr);
    }
    if (originalClassName == null) {
        // We dont know the object class so try to deduce it from what is known by the StoreManager
        originalClassName = getClassNameForObjectId(id);
        checkedClassName = true;
    }
    Object pc = null;
    if (checkInheritance) {
        // Validate the inheritance level
        className = checkedClassName ? originalClassName : getClassNameForObjectId(id);
        if (className == null) {
            throw new NucleusObjectNotFoundException(Localiser.msg("010026"), id);
        }
        if (!checkedClassName) {
            // Check if this id for any known subclasses is in the cache to save searching
            if (IdentityUtils.isDatastoreIdentity(id) || IdentityUtils.isSingleFieldIdentity(id)) {
                String[] subclasses = getMetaDataManager().getSubclassesForClass(className, true);
                if (subclasses != null) {
                    for (int i = 0; i < subclasses.length; i++) {
                        Object oid = null;
                        if (IdentityUtils.isDatastoreIdentity(id)) {
                            oid = nucCtx.getIdentityManager().getDatastoreId(subclasses[i], IdentityUtils.getTargetKeyForDatastoreIdentity(id));
                        } else if (IdentityUtils.isSingleFieldIdentity(id)) {
                            oid = nucCtx.getIdentityManager().getSingleFieldId(id.getClass(), getClassLoaderResolver().classForName(subclasses[i]), IdentityUtils.getTargetKeyForSingleFieldIdentity(id));
                        }
                        pc = getObjectFromCache(oid);
                        if (pc != null) {
                            className = subclasses[i];
                            break;
                        }
                    }
                }
            }
        }
        if (pc == null && originalClassName != null && !originalClassName.equals(className)) {
            // Inheritance check implies different inheritance level, so retry
            if (IdentityUtils.isDatastoreIdentity(id)) {
                // Create new OID using correct target class, and recheck cache
                id = nucCtx.getIdentityManager().getDatastoreId(className, ((DatastoreId) id).getKeyAsObject());
                pc = getObjectFromCache(id);
            } else if (IdentityUtils.isSingleFieldIdentity(id)) {
                // Create new SingleFieldIdentity using correct targetClass, and recheck cache
                id = nucCtx.getIdentityManager().getSingleFieldId(id.getClass(), clr.classForName(className), IdentityUtils.getTargetKeyForSingleFieldIdentity(id));
                pc = getObjectFromCache(id);
            }
        }
    } else {
        className = originalClassName;
    }
    return new ClassDetailsForId(id, className, pc);
}
Also used : DatastoreId(org.datanucleus.identity.DatastoreId) NucleusUserException(org.datanucleus.exceptions.NucleusUserException) SCOID(org.datanucleus.identity.SCOID) DatastoreUniqueLongId(org.datanucleus.identity.DatastoreUniqueLongId) NucleusObjectNotFoundException(org.datanucleus.exceptions.NucleusObjectNotFoundException)

Aggregations

SCOID (org.datanucleus.identity.SCOID)6 NucleusUserException (org.datanucleus.exceptions.NucleusUserException)5 ClassNotResolvedException (org.datanucleus.exceptions.ClassNotResolvedException)3 NucleusObjectNotFoundException (org.datanucleus.exceptions.NucleusObjectNotFoundException)3 DatastoreId (org.datanucleus.identity.DatastoreId)3 ObjectProvider (org.datanucleus.state.ObjectProvider)3 ArrayList (java.util.ArrayList)2 ApiAdapter (org.datanucleus.api.ApiAdapter)2 DatastoreUniqueLongId (org.datanucleus.identity.DatastoreUniqueLongId)2 IdentityStringTranslator (org.datanucleus.identity.IdentityStringTranslator)2 AbstractClassMetaData (org.datanucleus.metadata.AbstractClassMetaData)2 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 Map (java.util.Map)1 Entry (java.util.Map.Entry)1 DatastoreClass (org.datanucleus.store.rdbms.table.DatastoreClass)1 ConcurrentReferenceHashMap (org.datanucleus.util.ConcurrentReferenceHashMap)1 MacroString (org.datanucleus.util.MacroString)1