Package org.datanucleus

Examples of org.datanucleus.ObjectManager


        if (oldValue != value)
        {
            // Value changed so update the map
            try
            {
                ObjectManager om = sm.getObjectManager();
                ManagedConnection mconn = storeMgr.getConnection(om);
                try
                {
                    if (exists)
                    {
View Full Code Here


        {
            oldValue = null;
            exists = false;
        }

        ObjectManager om = sm.getObjectManager();
        if (exists)
        {
            removeInternal(sm, key);
        }

        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);
            }
        }

        return oldValue;
    }
View Full Code Here

     * Method to be called after any update of the owner class element.
     * @param sm StateManager of the owner
     */
    public void postUpdate(StateManager sm)
    {
        ObjectManager om = sm.getObjectManager();
        java.util.Map value = (java.util.Map) sm.provideField(mmd.getAbsoluteFieldNumber());
        if (containerIsStoredInSingleColumn())
        {
            // Do nothing when serialised since we are handled in the main request
            if (value != null)
            {
                // Make sure the keys/values are ok for proceeding
                SCOUtils.validateObjectsForWriting(om, value.keySet());
                SCOUtils.validateObjectsForWriting(om, value.values());
            }
            return;
        }

        if (value == null)
        {
            // replace null map with empty SCO wrapper
            replaceFieldWithWrapper(sm, null, false, false);
            return;
        }

        if (value instanceof SCOContainer)
        {
            SCOContainer sco = (SCOContainer) value;

            if (sm.getObject() == sco.getOwner() && fieldName.equals(sco.getFieldName()))
            {
                // Flush any outstanding updates
                sco.flush();

                return;
            }

            if (sco.getOwner() != null)
            {
                throw new NucleusException("Owned second-class object was somehow assigned to a field other than its owner's").setFatal();
            }
        }

        if (!mmd.isCascadeUpdate())
        {
            // User doesnt want to update by reachability
            if (NucleusLogger.REACHABILITY.isDebugEnabled())
            {
                NucleusLogger.REACHABILITY.debug(LOCALISER.msg("007008", mmd.getFullFieldName()));
            }
            return;
        }
        if (NucleusLogger.REACHABILITY.isDebugEnabled())
        {
            NucleusLogger.REACHABILITY.debug(LOCALISER.msg("007009", mmd.getFullFieldName()));
        }

        // Update the datastore with this value of map (clear old entries and add new ones)
        // This method could be called in two situations
        // 1). Update a map field of an object, so UpdateRequest is called, which calls here
        // 2). Persist a new object, and it needed to wait til the element was inserted so
        //     goes into dirty state and then flush() triggers UpdateRequest, which comes here
        MapStore store = ((MapStore) storeMgr.getBackingStoreForField(
            om.getClassLoaderResolver(), mmd, value.getClass()));

        // TODO Consider making this more efficient picking the ones to remove/add
        // e.g use an update() method on the backing store like for CollectionStore
        store.clear(sm);
        store.putAll(sm, value);
View Full Code Here

    public boolean add(StateManager sm, Object element, int size)
    {
        validateElementForWriting(sm, element, null);

        boolean modified = false;
        ObjectManager om = sm.getObjectManager();

        try
        {
            ManagedConnection mconn = storeMgr.getConnection(om);
            try
View Full Code Here

        while (iter.hasNext())
        {
            validateElementForWriting(sm, iter.next(), null);
        }

        ObjectManager om = sm.getObjectManager();
        ManagedConnection mconn = storeMgr.getConnection(om);
        try
        {
            // Loop through all elements to be added
            iter = elements.iterator();
View Full Code Here

            NucleusLogger.DATASTORE.debug("Attempt to remove element=" + StringUtils.toJVMIDString(element) + " but doesn't exist in this Set.");
            return false;
        }

        Object elementToRemove = element;
        ObjectManager om = sm.getObjectManager();
        if (om.getApiAdapter().isDetached(element))
        {
            // Element passed in is detached so find attached version (DON'T attach this object)
            elementToRemove = om.findObject(om.getApiAdapter().getIdForObject(element), true, false,
                element.getClass().getName());
        }

        boolean modified = getSpecialization().remove(sm, elementToRemove, size, this);
        CollectionMetaData collmd = ownerMemberMetaData.getCollection();
View Full Code Here

            }
        }

        try
        {
            ObjectManager om = sm.getObjectManager();
            ManagedConnection mconn = storeMgr.getConnection(om);
            try
            {
                Object element = null;
                iter = elements.iterator();
View Full Code Here

                {
                    // Key is stored in the value and the value has changed so remove the old value
                    removeValue(sm, newKey, oldValue);
                }

                ObjectManager om = sm.getObjectManager();
                final Object newOwner = sm.getObject();

                if (om.getApiAdapter().isPersistent(newValue))
                {
                    /*
                     * The new value is already persistent.
                     *
                     * "Put" the new value in the map by updating its owner and key
                     * fields to the appropriate values.  This is done with the same
                     * methods the PC itself would use if the application code
                     * modified the fields.  It should result in no actual database
                     * activity if the fields were already set to the right values.
                     */
                    PersistenceCapable newValuePC = (PersistenceCapable)newValue;
                    if (om != om.getApiAdapter().getObjectManager(newValue))
                    {
                        throw new NucleusUserException(LOCALISER.msg("RDBMS.SCO.Map.WriteValudInvalidWithDifferentPM"), newValuePC.jdoGetObjectId());
                    }

                    StateManager vsm = om.findStateManager(newValue);
                   
                    // Ensure the current owner field is loaded, and replace with new value
                    if (ownerFieldNumber >= 0)
                    {
                        om.getApiAdapter().isLoaded(vsm, ownerFieldNumber);
                        Object oldOwner = vsm.provideField(ownerFieldNumber);
                        vsm.setObjectField(newValuePC, ownerFieldNumber, oldOwner, newOwner);
                    }
                    else
                    {
                        updateValueFk(sm, newValue, newOwner);
                    }
                   
                    // Ensure the current key field is loaded, and replace with new value
                    om.getApiAdapter().isLoaded(vsm, keyFieldNumber);
                    Object oldKey = vsm.provideField(keyFieldNumber);
                    vsm.setObjectField(newValuePC, keyFieldNumber, oldKey, newKey);
                }
                else
                {                 
                    /*
                     * The new value is not yet persistent.
                     *
                     * Update its owner and key fields to the appropriate values and
                     * *then* make it persistent.  Making the changes before DB
                     * insertion avoids an unnecessary UPDATE allows the owner
                     * and/or key fields to be non-nullable.
                     */
                    om.persistObjectInternal(newValue, new FieldValues()
                        {
                        public void fetchFields(StateManager vsm)
                        {
                            if (ownerFieldNumber >= 0)
                            {
                                vsm.replaceField(ownerFieldNumber, newOwner, true);
                            }
                            vsm.replaceField(keyFieldNumber, newKey, true);
                        }
                        public void fetchNonLoadedFields(StateManager sm)
                        {
                        }
                        public FetchPlan getFetchPlanForLoading()
                        {
                            return null;
                        }
                        }, null, -1, StateManager.PC);
                    if (ownerFieldNumber < 0)
                    {
                        updateValueFk(sm, newValue, newOwner);
                    }
                }
            }
            else
            {
                // Value is stored in the key
                ObjectManager om = sm.getObjectManager();
                PersistenceCapable pcNewKey = (PersistenceCapable)newKey;
                final Object newOwner = sm.getObject();

                if (om.getApiAdapter().isPersistent(pcNewKey))
                {
                    /*
                     * The new key is already persistent.
                     *
                     * "Put" the new key in the map by updating its owner and value
                     * fields to the appropriate values. This is done with the same
                     * methods the PC itself would use if the application code
                     * modified the fields. It should result in no actual database
                     * activity if the fields were already set to the right values.
                     */
                    if (om != om.getApiAdapter().getObjectManager(pcNewKey))
                    {
                        throw new NucleusUserException(LOCALISER.msg("056060"),
                            om.getApiAdapter().getIdForObject(pcNewKey));
                    }

                    StateManager vsm = om.findStateManager(pcNewKey);

                    // Ensure the current owner field is loaded, and replace with new key
                    if (ownerFieldNumber >= 0)
                    {
                        om.getApiAdapter().isLoaded(vsm, ownerFieldNumber);
                        Object oldOwner = vsm.provideField(ownerFieldNumber);
                        vsm.setObjectField(pcNewKey, ownerFieldNumber, oldOwner, newOwner);
                    }
                    else
                    {
                        updateKeyFk(sm, pcNewKey, newOwner);
                    }

                    // Ensure the current value field is loaded, and replace with new value
                    om.getApiAdapter().isLoaded(vsm, valueFieldNumber);
                    oldValue = vsm.provideField(valueFieldNumber); // TODO Should we update the local variable ?
                    vsm.setObjectField(pcNewKey, valueFieldNumber, oldValue, newValue);
                }
                else
                {
                    /*
                     * The new key is not yet persistent.
                     *
                     * Update its owner and key fields to the appropriate values and
                     * *then* make it persistent.  Making the changes before DB
                     * insertion avoids an unnecessary UPDATE allows the owner
                     * and/or key fields to be non-nullable.
                     */
                    final Object newValueObj = newValue;
                    om.persistObjectInternal(newKey, new FieldValues()
                        {
                        public void fetchFields(StateManager vsm)
                        {
                            if (ownerFieldNumber >= 0)
                            {
View Full Code Here

     * @param key Key of the object
     * @param oldValue Value to remove
     */
    private void removeValue(StateManager sm, Object key, Object oldValue)
    {
        ObjectManager om = sm.getObjectManager();
       
        // Null out the key and owner fields if they are nullable
        if (keyMapping.isNullable())
        {
            PersistenceCapable pcOldValue = (PersistenceCapable)oldValue;
            StateManager vsm = om.findStateManager(pcOldValue);
           
            // Null the key field
            vsm.setObjectField(pcOldValue, keyFieldNumber, key, null);
            vsm.replaceField(keyFieldNumber, null, true);
           
            // Null the owner field
            if (ownerFieldNumber >= 0)
            {
                Object oldOwner = vsm.provideField(ownerFieldNumber);
                vsm.setObjectField(pcOldValue, ownerFieldNumber, oldOwner, null);
                vsm.replaceField(ownerFieldNumber, null, true);
            }
            else
            {
                updateValueFk(sm, pcOldValue, null);
            }
        }
        // otherwise just delete the item
        else
        {
            om.deleteObjectInternal(oldValue);
        }
    }
View Full Code Here

     * @param key Key of the object
     * @param oldValue Value to remove
     */
    public void clearKeyOfValue(StateManager sm, Object key, Object oldValue)
    {
        ObjectManager om = sm.getObjectManager();

        // Null out the key and owner fields if they are nullable
        if (keyMapping.isNullable())
        {
            StateManager vsm = om.findStateManager(oldValue);

            // Check that the value hasn't already been deleted due to being removed from the map
            PersistenceCapable oldValuePC = (PersistenceCapable)oldValue;
            if (!om.getApiAdapter().isDeleted(oldValuePC))
            {
                // Null the key field
                vsm.setObjectField(oldValuePC, keyFieldNumber, key, null);
                vsm.replaceField(keyFieldNumber, null, true);
                vsm.makeDirty(keyFieldNumber);
            }
        }
        // otherwise just delete the item
        else
        {
            om.deleteObjectInternal(oldValue);
        }
    }
View Full Code Here

TOP

Related Classes of org.datanucleus.ObjectManager

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.