Package org.datanucleus.api

Examples of org.datanucleus.api.ApiAdapter


            List failures = null;
            try
            {
                // Commit all enlisted StateManagers
                ApiAdapter api = getApiAdapter();
                StateManager[] sms = enlistedSMCache.values().toArray(new StateManager[enlistedSMCache.size()]);
                for (int i = 0; i < sms.length; ++i)
                {
                    try
                    {
                        // Perform any operations required after committing
                        //TODO this if is due to sms that can have lc == null, why?, should not be here then
                        if (sms[i] != null && sms[i].getObject() != null &&
                            (api.isPersistent(sms[i].getObject()) || api.isTransactional(sms[i].getObject())))
                        {
                            sms[i].postCommit(getTransaction());

                            // TODO Change this check so that we remove all objects that are no longer suitable for caching
                            if (getDetachAllOnCommit() && api.isDetachable(sms[i].getObject()))
                            {
                                // "DetachAllOnCommit" - Remove the object from the L1 cache since it is now detached
                                removeStateManager(sms[i]);
                            }
                        }
View Full Code Here


            {
                throw new NucleusUserException("Cannot perform Collection.isEmpty when the collection is being serialised");
            }
            else
            {
                ApiAdapter api = stmt.getRDBMSManager().getApiAdapter();
                Class elementType = clr.classForName(mmd.getCollection().getElementType());
                if (!api.isPersistable(elementType) && mmd.getJoinMetaData() == null)
                {
                    throw new NucleusUserException(
                        "Cannot perform Collection.isEmpty when the collection<Non-Persistable> is not in a join table");
                }
            }
View Full Code Here

            {
                NucleusLogger.PERSISTENCE.debug(LOCALISER.msg("007006", mmd.getFullFieldName()));
            }

            // Check for any persistable keys/values that arent persistent
            ApiAdapter api = ownerOP.getExecutionContext().getApiAdapter();
            Set entries = value.entrySet();
            Iterator iter = entries.iterator();
            while (iter.hasNext())
            {
                Map.Entry entry = (Map.Entry)iter.next();
                if (api.isPersistable(entry.getKey()))
                {
                    if (!api.isPersistent(entry.getKey()) && !api.isDetached(entry.getKey()))
                    {
                        // Key is not persistent so throw exception
                        throw new ReachableObjectNotCascadedException(mmd.getFullFieldName(), entry.getKey());
                    }
                }
                if (api.isPersistable(entry.getValue()))
                {
                    if (!api.isPersistent(entry.getValue()) && !api.isDetached(entry.getValue()))
                    {
                        // Value is not persistent so throw exception
                        throw new ReachableObjectNotCascadedException(mmd.getFullFieldName(), entry.getValue());
                    }
                }
View Full Code Here

                mmd != null ? mmd.getFullFieldName() : "", getType(), value.getClass().getName()));
        }

        if (value != null)
        {
            ApiAdapter api = ec.getApiAdapter();
            ClassLoaderResolver clr = ec.getClassLoaderResolver();

            // Make sure the value is persisted if it is persistable in its own right
            if (!ec.isInserting(value))
            {
                // Object either already exists, or is not yet being inserted.
                Object id = api.getIdForObject(value);

                // Check if the PersistenceCapable exists in this datastore
                boolean requiresPersisting = false;
                if (ec.getApiAdapter().isDetached(value) && ownerSM != null)
                {
                    // Detached object that needs attaching (or persisting if detached from a different datastore)
                    requiresPersisting = true;
                }
                else if (id == null)
                {
                    // Transient object, so we need to persist it
                    requiresPersisting = true;
                }
                else
                {
                    ExecutionContext valueEC = api.getExecutionContext(value);
                    if (valueEC != null && ec != valueEC)
                    {
                        throw new NucleusUserException(LOCALISER.msg("041015"), id);
                    }
                }

                if (requiresPersisting)
                {
                    // The object is either not yet persistent or is detached and so needs attaching
                    Object pcNew = ec.persistObjectInternal(value, null, -1, ObjectProvider.PC);
                    ec.flushInternal(false);
                    id = api.getIdForObject(pcNew);
                    if (ec.getApiAdapter().isDetached(value) && ownerSM != null)
                    {
                        // Update any detached reference to refer to the attached variant
                        ownerSM.replaceFieldMakeDirty(ownerFieldNumber, pcNew);
                        int relationType = mmd.getRelationType(clr);
View Full Code Here

    public Object getObject(ExecutionContext ec, final Object rs, int[] param)
    {
        Object value = super.getObject(ec, rs, param);
        if (value != null)
        {
            ApiAdapter api = ec.getApiAdapter();
            return api.getIdForObject(value);
        }
        return null;
    }
View Full Code Here

        if (myOM == null)
        {
            return;
        }

        ApiAdapter api = myOM.getApiAdapter();
        if (myLC.isDeleted() || api.isDetached(myPC) || ((flags&FLAG_DETACHING)!=0))
        {
            // Already deleted, detached or being detached
            return;
        }

        // Check if detachable ... if so then we detach a copy, otherwise we return a transient copy
        boolean detachable = api.isDetachable(myPC);
        if (detachable)
        {
            if (NucleusLogger.PERSISTENCE.isDebugEnabled())
            {
                NucleusLogger.PERSISTENCE.debug(LOCALISER.msg("010009", StringUtils.toJVMIDString(myPC),
                    "" + state.getCurrentFetchDepth()));
            }

            // Call any "pre-detach" listeners
            getCallbackHandler().preDetach(myPC);
        }

        try
        {
            flags |= FLAG_DETACHING;

            String detachedState = myOM.getNucleusContext().getPersistenceConfiguration().getStringProperty("datanucleus.detachedState");
            if (detachedState.equalsIgnoreCase("all"))
            {
                loadUnloadedFields();
            }
            else if (detachedState.equalsIgnoreCase("loaded"))
            {
                // Do nothing since just using currently loaded fields
            }
            else
            {
                // Using fetch-groups, so honour detachmentOptions for loading/unloading
                if ((myOM.getFetchPlan().getDetachmentOptions() & FetchPlan.DETACH_LOAD_FIELDS) != 0)
                {
                    // Load any unloaded fetch-plan fields
                    loadUnloadedFieldsInFetchPlan();
                }
                if ((myOM.getFetchPlan().getDetachmentOptions() & FetchPlan.DETACH_UNLOAD_FIELDS) != 0)
                {
                    // Unload any loaded fetch-plan fields that aren't in the current fetch plan
                    unloadNonFetchPlanFields();

                    // Remove the values from the detached object - not required by the spec
                    int[] unloadedFields = getFlagsSetTo(loadedFields, getAllFieldNumbers(), false);
                    if (unloadedFields != null && unloadedFields.length > 0)
                    {
                        PersistenceCapable dummyPC = myPC.jdoNewInstance(this);
                        myPC.jdoCopyFields(dummyPC, unloadedFields);
                        replaceStateManager(dummyPC, null);
                    }
                }
            }

            // Detach all (loaded) fields in the FetchPlan
            FieldManager detachFieldManager = new DetachFieldManager(this, getSecondClassMutableFields(),
                myFP, state, false);
            for (int i = 0; i < loadedFields.length; i++)
            {
                if (loadedFields[i])
                {
                    try
                    {
                        // Just fetch the field since we are usually called in postCommit() so dont want to update it
                        detachFieldManager.fetchObjectField(i);
                    }
                    catch (EndOfFetchPlanGraphException eofpge)
                    {
                        Object value = provideField(i);
                        if (api.isPersistable(value))
                        {
                            // PC field beyond end of graph
                            org.datanucleus.state.StateManager valueSM = myOM.findStateManager(value);
                            if (!api.isDetached(value) && !(valueSM != null && valueSM.isDetaching()))
                            {
                                // Field value is not detached or being detached so unload it
                                String fieldName = cmd.getMetaDataForManagedMemberAtAbsolutePosition(i).getName();
                                if (NucleusLogger.PERSISTENCE.isDebugEnabled())
                                {
View Full Code Here

        }

        if (needsObjectidClass)
        {
            // Update "objectid-class" if required yet not specified
            ApiAdapter api = mmgr.getApiAdapter();
            if (no_of_pk_fields == 0)
            {
                NucleusLogger.METADATA.error(LOCALISER.msg("044065", fullName, "" + no_of_pk_fields));
                throw new InvalidMetaDataException(LOCALISER, "044065", fullName, "" + no_of_pk_fields);
            }
            else if (no_of_pk_fields > 1)
            {
                // More than 1 PK yet no objectidClass - maybe added by enhancer later, so log warning
                NucleusLogger.METADATA.warn(LOCALISER.msg("044065", fullName, "" + no_of_pk_fields));
                if (!mmgr.isEnhancing())
                {
                    objectidClass = fullName + GENERATED_PK_SUFFIX;
                    NucleusLogger.METADATA.debug("Class " + fullName + " has " + getNoOfPrimaryKeyMembers() +
                        " primary-key field(s) yet no objectidClass defined. Assumed to be " + objectidClass);
                }
            }
            else
            {
                // Assign associated SingleField identity class
                Class pk_type = mmd_pk.getType();
                if (Byte.class.isAssignableFrom(pk_type) ||
                        byte.class.isAssignableFrom(pk_type))
                {
                    objectidClass = api.getSingleFieldIdentityClassNameForByte();
                }
                else if (Character.class.isAssignableFrom(pk_type) ||
                        char.class.isAssignableFrom(pk_type))
                {
                    objectidClass = api.getSingleFieldIdentityClassNameForChar();
                }
                else if (Integer.class.isAssignableFrom(pk_type) ||
                        int.class.isAssignableFrom(pk_type))
                {
                    objectidClass = api.getSingleFieldIdentityClassNameForInt();
                }
                else if (Long.class.isAssignableFrom(pk_type) ||
                        long.class.isAssignableFrom(pk_type))
                {
                    objectidClass = api.getSingleFieldIdentityClassNameForLong();
                }
                else if (Short.class.isAssignableFrom(pk_type) ||
                        short.class.isAssignableFrom(pk_type))
                {
                    objectidClass = api.getSingleFieldIdentityClassNameForShort();
                }
                else if (String.class.isAssignableFrom(pk_type))
                {
                    objectidClass = api.getSingleFieldIdentityClassNameForString();
                }
                else if (Object.class.isAssignableFrom(pk_type))
                {
                    objectidClass = api.getSingleFieldIdentityClassNameForObject();
                }
                else
                {
                    NucleusLogger.METADATA.error(LOCALISER.msg("044066", fullName, pk_type.getName()));
                    throw new InvalidMetaDataException(LOCALISER, "044066", fullName, pk_type.getName());
View Full Code Here

    {
        SQLStatement stmt = expr1.stmt;
        RDBMSStoreManager storeMgr = stmt.getRDBMSManager();
        SQLExpressionFactory exprFactory = storeMgr.getSQLExpressionFactory();
        ClassLoaderResolver clr = stmt.getClassLoaderResolver();
        ApiAdapter api = storeMgr.getApiAdapter();
        if (expr1 instanceof ObjectLiteral && expr2 instanceof ObjectLiteral)
        {
            // ObjectLiterall == ObjectLiteral
            ObjectLiteral lit1 = (ObjectLiteral)expr1;
            ObjectLiteral lit2 = (ObjectLiteral)expr2;
            return new BooleanLiteral(stmt, expr1.mapping,
                equals ? lit1.getValue().equals(lit2.getValue()) : !lit1.getValue().equals(lit2.getValue()));
        }
        else if (expr1 instanceof ObjectLiteral || expr2 instanceof ObjectLiteral)
        {
            // ObjectExpression == ObjectLiteral, ObjectLiteral == ObjectExpression
            BooleanExpression bExpr = null;
            boolean secondIsLiteral = (expr2 instanceof ObjectLiteral);
            Object value = (!secondIsLiteral ? ((ObjectLiteral)expr1).getValue() : ((ObjectLiteral)expr2).getValue());
            if (value instanceof OID)
            {
                // Object is an OID
                JavaTypeMapping m = storeMgr.getSQLExpressionFactory().getMappingForType(
                    ((OID)value).getKeyValue().getClass(), false);
                SQLExpression oidLit = exprFactory.newLiteral(stmt, m, ((OID)value).getKeyValue());
                if (equals)
                {
                    return (secondIsLiteral ? expr1.subExprs.getExpression(0).eq(oidLit) : expr2.subExprs.getExpression(0).eq(oidLit));
                }
                else
                {
                    return (secondIsLiteral ? expr1.subExprs.getExpression(0).ne(oidLit) : expr2.subExprs.getExpression(0).ne(oidLit));
                }
            }
            else if (api.isSingleFieldIdentity(value))
            {
                // Object is SingleFieldIdentity
                JavaTypeMapping m = storeMgr.getSQLExpressionFactory().getMappingForType(
                    api.getTargetClassForSingleFieldIdentity(value), false);
                SQLExpression oidLit = exprFactory.newLiteral(stmt, m,
                    api.getTargetKeyForSingleFieldIdentity(value));
                if (equals)
                {
                    return (secondIsLiteral ? expr1.subExprs.getExpression(0).eq(oidLit) : expr2.subExprs.getExpression(0).eq(oidLit));
                }
                else
                {
                    return (secondIsLiteral ? expr1.subExprs.getExpression(0).ne(oidLit) : expr2.subExprs.getExpression(0).ne(oidLit));
                }
            }
            else
            {
                AbstractClassMetaData cmd =
                    storeMgr.getNucleusContext().getMetaDataManager().getMetaDataForClass(value.getClass(), clr);
                if (cmd != null)
                {
                    // Value is a persistable object
                    if (cmd.getIdentityType() == IdentityType.APPLICATION)
                    {
                        // Application identity
                        if (api.getIdForObject(value) != null)
                        {
                            // Persistent PC object (FCO)
                            // Cater for composite PKs and parts of PK being PC mappings, and recursion
                            ObjectExpression expr = (secondIsLiteral ? expr1 : expr2);
                            JavaTypeMapping[] pkMappingsApp = new JavaTypeMapping[expr.subExprs.size()];
                            Object[] pkFieldValues = new Object[expr.subExprs.size()];
                            int position = 0;
                            ExecutionContext ec = api.getExecutionContext(value);
                            JavaTypeMapping thisMapping = expr.mapping;
                            if (expr.mapping instanceof ReferenceMapping)
                            {
                                // "InterfaceField == value", so pick an implementation mapping that is castable
                                thisMapping = null;
                                ReferenceMapping refMapping = (ReferenceMapping)expr.mapping;
                                JavaTypeMapping[] implMappings = refMapping.getJavaTypeMapping();
                                for (int i=0;i<implMappings.length;i++)
                                {
                                    Class implType = clr.classForName(implMappings[i].getType());
                                    if (implType.isAssignableFrom(value.getClass()))
                                    {
                                        thisMapping = implMappings[i];
                                        break;
                                    }
                                }
                            }
                            if (thisMapping == null)
                            {
                                // Just return a (1=0) since no implementation castable
                                return exprFactory.newLiteral(stmt, expr1.mapping, false).eq(exprFactory.newLiteral(stmt, expr1.mapping, true));
                            }

                            for (int i=0;i<cmd.getNoOfPrimaryKeyMembers();i++)
                            {
                                AbstractMemberMetaData mmd = cmd.getMetaDataForManagedMemberAtAbsolutePosition(cmd.getPKMemberPositions()[i]);
                                Object fieldValue = ExpressionUtils.getValueForMemberOfObject(ec, mmd, value);
                                JavaTypeMapping mapping = ((PersistableMapping)thisMapping).getJavaTypeMapping()[i];
                                if (mapping instanceof PersistableMapping)
                                {
                                    position = ExpressionUtils.populatePrimaryKeyMappingsValuesForPCMapping(pkMappingsApp,
                                        pkFieldValues, position, (PersistableMapping)mapping,
                                        cmd, mmd, fieldValue, storeMgr, clr);
                                }
                                else
                                {
                                    pkMappingsApp[position] = mapping;
                                    pkFieldValues[position] = fieldValue;
                                    position++;
                                }
                            }

                            for (int i=0; i<expr.subExprs.size(); i++)
                            {
                                SQLExpression source = expr.subExprs.getExpression(i);
                                SQLExpression target =
                                    exprFactory.newLiteral(stmt, pkMappingsApp[i], pkFieldValues[i]);
                                BooleanExpression subExpr = (secondIsLiteral ? source.eq(target) : target.eq(source));
                                if (bExpr == null)
                                {
                                    bExpr = subExpr;
                                }
                                else
                                {
                                    bExpr = bExpr.and(subExpr);
                                }
                            }
                        }
                        else
                        {
                            // PC object with no id (embedded, or transient maybe)
                            if (secondIsLiteral)
                            {
                                for (int i=0; i<expr1.subExprs.size(); i++)
                                {
                                    // Query should return nothing (so just do "(1 = 0)")
                                    NucleusLogger.QUERY.warn(LOCALISER.msg("037003", value));
                                    bExpr = exprFactory.newLiteral(stmt, expr1.mapping, false).eq(exprFactory.newLiteral(stmt, expr1.mapping, true));
                                    // It is arguable that we should compare the id with null (as below)
                                    /*bExpr = expr.eq(new NullLiteral(qs));*/
                                }
                            }
                            else
                            {
                                for (int i=0; i<expr2.subExprs.size(); i++)
                                {
                                    // Query should return nothing (so just do "(1 = 0)")
                                    NucleusLogger.QUERY.warn(LOCALISER.msg("037003", value));
                                    bExpr = exprFactory.newLiteral(stmt, expr2.mapping, false).eq(exprFactory.newLiteral(stmt, expr2.mapping, true));
                                    // It is arguable that we should compare the id with null (as below)
                                    /*bExpr = expr.eq(new NullLiteral(qs));*/
                                }
                            }
                        }
                        // TODO Allow for !equals
                        return bExpr;
                    }
                    else if (cmd.getIdentityType() == IdentityType.DATASTORE)
                    {
                        // Datastore identity
                        SQLExpression source = (secondIsLiteral ? expr1.subExprs.getExpression(0) : expr2.subExprs.getExpression(0));
                        JavaTypeMapping mapping = (secondIsLiteral ? expr1.mapping : expr2.mapping);
                        OID objectId = (OID)api.getIdForObject(value);
                        if (objectId == null)
                        {
                            // PC object with no id (embedded, or transient maybe)
                            // Query should return nothing (so just do "(1 = 0)")
                            NucleusLogger.QUERY.warn(LOCALISER.msg("037003", value));
View Full Code Here

        if (getPersistenceCapableSuperclass() == null)
        {
            // Only check root persistable class PK
            if (objectidClass != null)
            {
                ApiAdapter api = mmgr.getApiAdapter();
                Class obj_cls = null;
                try
                {
                    // Load the class, using the same class loader resolver as this class
                    obj_cls = clr.classForName(objectidClass);
                }
                catch (ClassNotResolvedException cnre)
                {
                    // ObjectIdClass not found
                    throw new InvalidMetaDataException(LOCALISER, "044079", fullName, objectidClass);
                }

                boolean validated = false;
                Set errors = new HashSet();
                try
                {
                    // Check against the API Adapter in use for this MetaData
                    if (api.isValidPrimaryKeyClass(obj_cls, this, clr, getNoOfPopulatedPKMembers(), mmgr))
                    {
                        validated = true;
                    }
                }
                catch (NucleusException ex)
View Full Code Here

                element.type, elementTypeClass.getName()));
            element.type = elementTypeClass.getName();
        }

    // "embedded-element"
        ApiAdapter api = mmgr.getApiAdapter();
        if (element.embedded == null)
        {
            // Assign default for "embedded-element" based on 18.13.1 of JDO 2 spec
            // Note : this fails when using in the enhancer since not yet PC
            if (mmgr.getNucleusContext().getTypeManager().isDefaultEmbeddedType(elementTypeClass))
            {
                element.embedded = Boolean.TRUE;
            }
            else if (api.isPersistable(elementTypeClass) ||
                Object.class.isAssignableFrom(elementTypeClass) ||
                elementTypeClass.isInterface())
            {
                element.embedded = Boolean.FALSE;
            }
            else
            {
                element.embedded = Boolean.TRUE;
            }
        }
        if (Boolean.FALSE.equals(element.embedded))
        {
            // If the user has set a non-PC/non-Interface as not embedded, correct it since not supported.
            // Note : this fails when using in the enhancer since not yet PC
            if (!api.isPersistable(elementTypeClass) && !elementTypeClass.isInterface() &&
                elementTypeClass != java.lang.Object.class)
            {
                element.embedded = Boolean.TRUE;
            }
        }

        ElementMetaData elemmd = ((AbstractMemberMetaData)parent).getElementMetaData();
        if (elemmd != null && elemmd.getEmbeddedMetaData() != null)
        {
            element.embedded = Boolean.TRUE;
        }

        if (Boolean.TRUE.equals(element.dependent))
        {
            // If the user has set a non-PC/non-reference as dependent, correct it since not valid.
            // Note : this fails when using in the enhancer since not yet PC
            if (!api.isPersistable(elementTypeClass) && !elementTypeClass.isInterface() &&
                elementTypeClass != java.lang.Object.class)
            {
                element.dependent = Boolean.FALSE;
            }
        }
View Full Code Here

TOP

Related Classes of org.datanucleus.api.ApiAdapter

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.