Search in sources :

Example 1 with IdentityKeyTranslator

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

the class ExecutionContextImpl method newObjectId.

/**
 * This method returns an object id instance corresponding to the pcClass and key arguments.
 * Operates in 2 modes :-
 * <ul>
 * <li>The class uses SingleFieldIdentity and the key is the value of the key field</li>
 * <li>In all other cases the key is the String form of the object id instance</li>
 * </ul>
 * @param pcClass Class of the persistable object to create the identity for
 * @param key Value of the key for SingleFieldIdentity (or the toString value)
 * @return The new object-id instance
 */
public Object newObjectId(Class pcClass, Object key) {
    if (pcClass == null) {
        throw new NucleusUserException(Localiser.msg("010028"));
    }
    assertClassPersistable(pcClass);
    AbstractClassMetaData cmd = getMetaDataManager().getMetaDataForClass(pcClass, clr);
    if (cmd == null) {
        throw new NoPersistenceInformationException(pcClass.getName());
    }
    // If the class is not yet managed, manage it
    if (!getStoreManager().managesClass(cmd.getFullClassName())) {
        getStoreManager().manageClasses(clr, cmd.getFullClassName());
    }
    IdentityKeyTranslator translator = getNucleusContext().getIdentityManager().getIdentityKeyTranslator();
    if (translator != null) {
        // Use the provided translator to convert it
        key = translator.getKey(this, pcClass, key);
    }
    Object id = null;
    if (cmd.usesSingleFieldIdentityClass()) {
        // Single Field Identity
        if (getBooleanProperty(PropertyNames.PROPERTY_FIND_OBJECT_TYPE_CONVERSION) && translator == null && !key.getClass().getName().equals(cmd.getObjectidClass())) {
            // key provided is intended to be the type of the PK member, so provide convenience type conversion to the actual type required
            AbstractMemberMetaData mmd = cmd.getMetaDataForMember(cmd.getPrimaryKeyMemberNames()[0]);
            if (!mmd.getType().isAssignableFrom(key.getClass())) {
                Object convKey = TypeConversionHelper.convertTo(key, mmd.getType());
                if (convKey != null) {
                    key = convKey;
                }
            }
        }
        id = nucCtx.getIdentityManager().getSingleFieldId(clr.classForName(cmd.getObjectidClass()), pcClass, key);
    } else if (key instanceof java.lang.String) {
        // String-based PK (datastore identity or application identity)
        if (cmd.getIdentityType() == IdentityType.APPLICATION) {
            if (Modifier.isAbstract(pcClass.getModifiers()) && cmd.getObjectidClass() != null) {
                try {
                    Constructor c = clr.classForName(cmd.getObjectidClass()).getDeclaredConstructor(new Class[] { java.lang.String.class });
                    id = c.newInstance(new Object[] { (String) key });
                } catch (Exception e) {
                    String msg = Localiser.msg("010030", cmd.getObjectidClass(), cmd.getFullClassName());
                    NucleusLogger.PERSISTENCE.error(msg, e);
                    throw new NucleusUserException(msg);
                }
            } else {
                clr.classForName(pcClass.getName(), true);
                id = nucCtx.getIdentityManager().getApplicationId(pcClass, key);
            }
        } else {
            id = nucCtx.getIdentityManager().getDatastoreId((String) key);
        }
    } else {
        // Key is not a string, and is not SingleFieldIdentity
        throw new NucleusUserException(Localiser.msg("010029", pcClass.getName(), key.getClass().getName()));
    }
    return id;
}
Also used : NoPersistenceInformationException(org.datanucleus.exceptions.NoPersistenceInformationException) NucleusUserException(org.datanucleus.exceptions.NucleusUserException) Constructor(java.lang.reflect.Constructor) AbstractClassMetaData(org.datanucleus.metadata.AbstractClassMetaData) ClassNotDetachableException(org.datanucleus.exceptions.ClassNotDetachableException) NucleusObjectNotFoundException(org.datanucleus.exceptions.NucleusObjectNotFoundException) RollbackStateTransitionException(org.datanucleus.exceptions.RollbackStateTransitionException) NucleusException(org.datanucleus.exceptions.NucleusException) NucleusFatalUserException(org.datanucleus.exceptions.NucleusFatalUserException) ClassNotPersistableException(org.datanucleus.exceptions.ClassNotPersistableException) NoPersistenceInformationException(org.datanucleus.exceptions.NoPersistenceInformationException) TransactionActiveOnCloseException(org.datanucleus.exceptions.TransactionActiveOnCloseException) NucleusUserException(org.datanucleus.exceptions.NucleusUserException) ClassNotResolvedException(org.datanucleus.exceptions.ClassNotResolvedException) NucleusOptimisticException(org.datanucleus.exceptions.NucleusOptimisticException) CommitStateTransitionException(org.datanucleus.exceptions.CommitStateTransitionException) TransactionNotActiveException(org.datanucleus.exceptions.TransactionNotActiveException) ObjectDetachedException(org.datanucleus.exceptions.ObjectDetachedException) IdentityKeyTranslator(org.datanucleus.identity.IdentityKeyTranslator) AbstractMemberMetaData(org.datanucleus.metadata.AbstractMemberMetaData)

Aggregations

Constructor (java.lang.reflect.Constructor)1 ClassNotDetachableException (org.datanucleus.exceptions.ClassNotDetachableException)1 ClassNotPersistableException (org.datanucleus.exceptions.ClassNotPersistableException)1 ClassNotResolvedException (org.datanucleus.exceptions.ClassNotResolvedException)1 CommitStateTransitionException (org.datanucleus.exceptions.CommitStateTransitionException)1 NoPersistenceInformationException (org.datanucleus.exceptions.NoPersistenceInformationException)1 NucleusException (org.datanucleus.exceptions.NucleusException)1 NucleusFatalUserException (org.datanucleus.exceptions.NucleusFatalUserException)1 NucleusObjectNotFoundException (org.datanucleus.exceptions.NucleusObjectNotFoundException)1 NucleusOptimisticException (org.datanucleus.exceptions.NucleusOptimisticException)1 NucleusUserException (org.datanucleus.exceptions.NucleusUserException)1 ObjectDetachedException (org.datanucleus.exceptions.ObjectDetachedException)1 RollbackStateTransitionException (org.datanucleus.exceptions.RollbackStateTransitionException)1 TransactionActiveOnCloseException (org.datanucleus.exceptions.TransactionActiveOnCloseException)1 TransactionNotActiveException (org.datanucleus.exceptions.TransactionNotActiveException)1 IdentityKeyTranslator (org.datanucleus.identity.IdentityKeyTranslator)1 AbstractClassMetaData (org.datanucleus.metadata.AbstractClassMetaData)1 AbstractMemberMetaData (org.datanucleus.metadata.AbstractMemberMetaData)1