Package org.jpox.api

Examples of org.jpox.api.ApiAdapter


    public void storeObjectField(int fieldNumber, Object value)
    {
        if (value != null)
        {
            AbstractMemberMetaData fmd = sm.getClassMetaData().getMetaDataForManagedMemberAtAbsolutePosition(fieldNumber);
            ApiAdapter api = sm.getObjectManager().getApiAdapter();
            if (api.isPersistable(value))
            {
                // Process PC fields
                if (fmd.isDependent())
                {
                    processPersistable(value);
                }
            }
            else if (value instanceof Collection)
            {
                // Process all elements of the Collection that are PC
                if (fmd.hasCollection() && fmd.getCollection().isDependentElement())
                {
                    // Process any elements that are PersistenceCapable
                    Collection coll = (Collection)value;
                    Iterator iter = coll.iterator();
                    while (iter.hasNext())
                    {
                        Object element = iter.next();
                        if (api.isPersistable(element))
                        {
                            processPersistable(element);
                        }
                    }
                }
            }
            else if (value instanceof Map)
            {
                // Process all keys, values of the Map that are PC
                Map map = (Map)value;
                if (fmd.hasMap() && fmd.getMap().isDependentKey())
                {
                    // Process any keys that are PersistenceCapable
                    Set keys = map.keySet();
                    Iterator iter = keys.iterator();
                    while (iter.hasNext())
                    {
                        Object mapKey = iter.next();
                        if (api.isPersistable(mapKey))
                        {
                            processPersistable(mapKey);
                        }
                    }
                }
                if (fmd.hasMap() && fmd.getMap().isDependentValue())
                {
                    // Process any values that are PersistenceCapable
                    Collection values = map.values();
                    Iterator iter = values.iterator();
                    while (iter.hasNext())
                    {
                        Object mapValue = iter.next();
                        if (api.isPersistable(mapValue))
                        {
                            processPersistable(mapValue);
                        }
                    }
                }
            }
            else if (value instanceof Object[])
            {
                // Process all array elements that are PC
                if (fmd.hasArray() && fmd.getArray().isDependentElement())
                {
                    // Process any array elements that are PersistenceCapable
                    for (int i=0;i<Array.getLength(value);i++)
                    {
                        Object element = Array.get(value, i);
                        if (api.isPersistable(element))
                        {
                            processPersistable(element);
                        }
                    }
                }
View Full Code Here


     */
    public static void attachCopyForMap(StateManager ownerSM, Set detachedEntries,
            Map attached, boolean keysWithoutIdentity, boolean valuesWithoutIdentity)
    {
        Iterator iter = detachedEntries.iterator();
        ApiAdapter api = ownerSM.getObjectManager().getApiAdapter();
        while (iter.hasNext())
        {
            Map.Entry entry = (Map.Entry) iter.next();
            Object val = entry.getValue();
            Object key = entry.getKey();
            if (api.isPersistable(val) && api.isDetachable(val))
            {
                val = ownerSM.getObjectManager().attachObjectCopy(val, valuesWithoutIdentity);
            }
            if (api.isPersistable(key) && api.isDetachable(key))
            {
                key = ownerSM.getObjectManager().attachObjectCopy(key, keysWithoutIdentity);
            }
            attached.put(key, val);
        }
View Full Code Here

    private void setObjectAsValue(ObjectManager om, Object ps, int[] param, Object value, StateManager ownerSM,
            int ownerFieldNumber)
    {
        Object id;

        ApiAdapter api = om.getApiAdapter();
        if (!api.isPersistable(value))
        {
            throw new JPOXException(LOCALISER.msg("041016",
                value.getClass(), value)).setFatal();
        }

        StateManager sm = om.findStateManager(value);

        try
        {
            ClassLoaderResolver clr = om.getClassLoaderResolver();

            // Check if the field is attributed in the datastore
            boolean hasDatastoreAttributedPrimaryKeyValues = hasDatastoreAttributedPrimaryKeyValues(
                om.getMetaDataManager(), om.getStoreManager(), clr);

            boolean inserted = false;
            if (ownerFieldNumber >= 0)
            {
                // Field mapping : is this field of the related object present in the datastore?
                inserted = om.isInserted(value, ownerFieldNumber);
            }
            else if (fmd == null)
            {
                // Identity mapping : is the object inserted far enough to be considered of this mapping type?
                inserted = om.isInserted(value, type);
            }

            if (sm != null)
            {
                if (om.getApiAdapter().isDetached(value) && sm.getReferencedPC() != null && ownerSM != null && fmd != null)
                {
                    // 1-1, N-1 mapping
                    // The value is really still detached but has a temporary StateManager whilst attaching so
                    // replace this field reference to be for the newly attached object
                    // Note that we have "fmd != null" here hence omitting any M-N relations where this is a join table
                    // mapping
                    ownerSM.replaceField(ownerFieldNumber, sm.getReferencedPC(), true);
                }

                if (sm.isWaitingToBeFlushedToDatastore())
                {
                    // Related object is not yet flushed to the datastore so flush it so we can set the FK
                    sm.flush();
                }
            }

            // we can execute this block when
            // 1) the pc has been inserted; OR
            // 2) is not in process of being inserted; OR
            // 3) is being inserted yet is inserted enough to use this mapping; OR
            // 4) the PC PK values are not attributed by the database and this mapping is for a PK field (compound identity)
            if (inserted || !om.isInserting(value) ||
                (!hasDatastoreAttributedPrimaryKeyValues && (this.fmd != null && this.fmd.isPrimaryKey())))
            {
                // The PC is either already inserted, or inserted down to the level we need, or not inserted at all,
                // or the field is a PK and identity not attributed by the datastore

                // Object either already exists, or is not yet being inserted.
                id = api.getIdForObject(value);

                // Check if the PersistenceCapable exists in this datastore
                boolean requiresPersisting = false;
                if (om.getApiAdapter().isDetached(value) && ownerSM != null)
                {
                    // Detached object so needs attaching
                    if (ownerSM.isInserting())
                    {
                        // Inserting other object, and this object is detached but if detached from this datastore
                        // we can just return the value now and attach later (in InsertRequest)
                        if (!om.getOMFContext().getPersistenceConfiguration().getBooleanProperty("org.jpox.attachSameDatastore"))
                        {
                            if (om.getObjectFromCache(api.getIdForObject(value)) != null)
                            {
                                // Object is in cache so exists for this datastore, so no point checking
                            }
                            else
                            {
                                try
                                {
                                    Object obj = om.findObject(api.getIdForObject(value), true, false,
                                        value.getClass().getName());
                                    if (obj != null)
                                    {
                                        // Make sure this object is not retained in cache etc
                                        StateManager objSM = om.findStateManager(obj);
                                        if (objSM != null)
                                        {
                                            om.evictFromTransaction(objSM);
                                        }
                                        om.removeObjectFromCache(value, api.getIdForObject(value), true, true);
                                    }
                                }
                                catch (JPOXObjectNotFoundException onfe)
                                {
                                    // Object doesnt yet exist
                                    requiresPersisting = true;
                                }
                            }
                        }
                    }
                    else
                    {
                        requiresPersisting = true;
                    }
                }
                else if (id == null)
                {
                    // Transient object, so we need to persist it
                    requiresPersisting = true;
                }
                else
                {
                    ObjectManager pcPM = ObjectManagerHelper.getObjectManager(value);
                    if (pcPM != null && om != pcPM)
                    {
                        throw new JPOXUserException(LOCALISER.msg("041015"), id);
                    }
                }

                if (requiresPersisting)
                {
                    // PERSISTENCE-BY-REACHABILITY
                    // This PC object needs persisting (new or detached) to do the "set"
                    if (fmd != null && !fmd.isCascadePersist() && !om.getApiAdapter().isDetached(value))
                    {
                        // Related PC object not persistent, but cant do cascade-persist so throw exception
                        if (JPOXLogger.REACHABILITY.isDebugEnabled())
                        {
                            JPOXLogger.REACHABILITY.debug(LOCALISER.msg("007006",
                                fmd.getFullFieldName()));
                        }
                        throw new ReachableObjectNotCascadedException(fmd.getFullFieldName(), value);
                    }

                    if (JPOXLogger.REACHABILITY.isDebugEnabled())
                    {
                        JPOXLogger.REACHABILITY.debug(LOCALISER.msg("007007",
                            fmd != null ? fmd.getFullFieldName() : null));
                    }

                    try
                    {
                        Object pcNew = om.persistObjectInternal(value, null, null, -1, StateManager.PC);
                        if (hasDatastoreAttributedPrimaryKeyValues)
                        {
                            om.flushInternal(false);
                        }
                        id = api.getIdForObject(pcNew);
                        if (om.getApiAdapter().isDetached(value) && ownerSM != null)
                        {
                            // Update any detached reference to refer to the attached variant
                            ownerSM.replaceField(ownerFieldNumber, pcNew, true);
                            int relationType = fmd.getRelationType(clr);
View Full Code Here

     * @return Whether the object was persisted during this call
     */
    public static boolean validateObjectForWriting(ObjectManager om, Object object, FieldValues fieldValues)
    {
        boolean persisted = false;
        ApiAdapter api = om.getApiAdapter();
        if (api.isPersistable(object))
        {
            ObjectManager objectOM = ObjectManagerHelper.getObjectManager(object);
            if (objectOM != null && om != objectOM)
            {
                throw new JPOXUserException(
                    LOCALISER.msg("023009", StringUtils.toJVMIDString(object)),
                    api.getIdForObject(object));
            }
            else if (!api.isPersistent(object))
            {
                // Not persistent, so either is detached, or needs persisting for first time
                boolean exists = false;
                if (api.isDetached(object))
                {
                    if (om.getOMFContext().getPersistenceConfiguration().getBooleanProperty("org.jpox.attachSameDatastore"))
                    {
                        // Assume that it is detached from this datastore
                        exists = true;
                    }
                    else
                    {
                        // Check if the (attached) object exists in this datastore
                        try
                        {
                            Object obj = om.findObject(api.getIdForObject(object), true, false, object.getClass().getName());
                            if (obj != null)
                            {
                                // PM.getObjectById creates a dummy object to represent this object and automatically
                                // enlists it in the txn. Evict it to avoid issues with reachability
                                StateManager objSM = om.findStateManager(obj);
View Full Code Here

        // Datastore Identity - retrieve the OID for the class.
        // Note that this is a temporary OID that is simply formed from the type of base class in the relationship
        // and the id stored in the FK. The real OID for the object may be of a different class.
        // For that reason we get the object by checking the inheritance (final param in getObjectById())
        Object oid = super.getObject(om, rs, param);
        ApiAdapter api = om.getApiAdapter();
        if (api.isPersistable(oid)) //why check this?
        {
          return oid;
        }
        return oid == null ? null : om.findObject(oid, false, true, null);
    }
View Full Code Here

        // Only include classes which have a table.
        List classTree = new ArrayList();

        String targetClassName = null;
        AbstractClassMetaData cmd = null;
        ApiAdapter api = getApiAdapter();

        if (id instanceof OID)
        {
            // Object is an OID
            OID oid = (OID) id;
            cmd = getMetaDataManager().getMetaDataForClass(oid.getPcClass(), clr);
            if( cmd.getDiscriminatorStrategy() == null)
            {
                // Using SingleFieldIdentity so can assume that object is of the target class or a subclass
                List subclasses = new ArrayList(this.getSubClassesForClass(oid.getPcClass(), true, clr));
                if (subclasses.size() < 1)
                {
                    JPOXLogger.PERSISTENCE.debug("1) Id \""+id+"\" has been determined to be the id of class "+oid.getPcClass());
                    // No subclasses so must be an instance of the OID class
                    return oid.getPcClass();
                }
                targetClassName = oid.getPcClass();
            }
        }
        else if (api.isSingleFieldIdentity(id))
        {
            // Using SingleFieldIdentity so can assume that object is of the target class or a subclass
            targetClassName = api.getTargetClassNameForSingleFieldIdentity(id);

            cmd = getMetaDataManager().getMetaDataForClass(targetClassName, clr);
            if (cmd.getIdentityType() != IdentityType.APPLICATION || !cmd.getObjectidClass().equals(id.getClass().getName()))
            {
                throw new JPOXUserException(LOCALISER_RDBMS.msg("050022", id, cmd.getFullClassName()));
View Full Code Here

                throw new JPOXDataStoreException(LOCALISER.msg("056012",removeStmt),e);
            }
        }

        MapMetaData mapmd = ownerMemberMetaData.getMap();
        ApiAdapter api = om.getApiAdapter();
        if (mapmd.isDependentKey() && !mapmd.isEmbeddedKey() && api.isPersistable(key))
        {
            // Delete the key if it is dependent
            om.deleteObjectInternal(key);
        }

        if (mapmd.isDependentValue() && !mapmd.isEmbeddedValue() && api.isPersistable(oldValue))
        {
            if (!containsValue(sm, oldValue))
            {
                // Delete the value if it is dependent and is not keyed by another key
                om.deleteObjectInternal(oldValue);
View Full Code Here

        Collection dependentElements = null;
        if (ownerMemberMetaData.getMap().isDependentKey() || ownerMemberMetaData.getMap().isDependentValue())
        {
            // Retain the PC dependent keys/values that need deleting after clearing
            dependentElements = new HashSet();
            ApiAdapter api = ownerSM.getObjectManager().getApiAdapter();
            Iterator iter = entrySetStore().iterator(ownerSM);
            while (iter.hasNext())
            {
                Map.Entry entry = (Map.Entry)iter.next();
                MapMetaData mapmd = ownerMemberMetaData.getMap();
                if (api.isPersistable(entry.getKey()) && mapmd.isDependentKey() && !mapmd.isEmbeddedKey())
                {
                    dependentElements.add(entry.getKey());
                }
                if (api.isPersistable(entry.getValue()) && mapmd.isDependentValue() && !mapmd.isEmbeddedValue())
                {
                    dependentElements.add(entry.getValue());
                }
            }
        }
View Full Code Here

        }
       
        defaultArgs = cmd.getDefaultArgs();

        // populate options
        ApiAdapter adapter = omfContext.getApiAdapter();
        Map map = adapter.getDefaultFactoryProperties();
        setOptions(map);
        if (cmd.hasOption("persistenceUnit"))
        {
            setProperty("org.jpox.PersistenceUnitName", cmd.getOptionArg("persistenceUnit"));
        }
View Full Code Here

                {
                    // Using explicit parameters
                    explicitParameters = true;
                    if (parameters != null)
                    {
                        ApiAdapter api = om.getApiAdapter();
                        if (parameters.size() != parameterTypesByName.size())
                        {
                            // Number of explicit parameters doesn't match the number of parameter values
                            throw new JPOXQueryInvalidParametersException(LOCALISER.msg("021025",
                                "" + parameterTypesByName.size(), "" + parameters.size()));
                        }

                        for (Iterator it = parameterTypesByName.entrySet().iterator(); it.hasNext(); )
                        {
                            Map.Entry entry = (Entry) it.next();
                            Object key = entry.getKey();
                            Object value = parameters.get(key);
                            if (value == null)
                            {
                                // primitive parameter can't be null
                                if (((Class)entry.getValue()).isPrimitive())
                                {
                                    throw new JPOXQueryInvalidParametersException(LOCALISER.msg("021026",
                                        entry.getKey(), ((Class)entry.getValue()).getName()));
                                }
                            }
                            else
                            {
                                if (api.isPersistable(value))
                                {
                                    // parameters bound to a different PM can't be used with this PM
                                    ObjectManager valueOM = ObjectManagerHelper.getObjectManager(value);
                                    if (valueOM != null && om != valueOM)
                                    {
                                        throw new JPOXUserException(LOCALISER.msg("021068", key));
                                    }
                                }
                            }
                        }
                    }
                }
                else
                {
                    // Using implicit parameters
                    // TODO Need to check the number of implicit params in the query (only do-able after compiling)
                    if (parameters != null)
                    {
                        ApiAdapter api = om.getApiAdapter();
                        Iterator parameterEntryIter = parameters.entrySet().iterator();
                        while (parameterEntryIter.hasNext())
                        {
                            Map.Entry entry = (Map.Entry)parameterEntryIter.next();
                            Object value = entry.getValue();
                            if (api.isPersistable(value))
                            {
                                // parameters bound to a different PM can't be used with this PM
                                ObjectManager valueOM = ObjectManagerHelper.getObjectManager(value);
                                if (valueOM != null && om != valueOM)
                                {
View Full Code Here

TOP

Related Classes of org.jpox.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.