Search in sources :

Example 21 with RelationshipManager

use of org.datanucleus.state.RelationshipManager in project datanucleus-core by datanucleus.

the class HashSet method clear.

/**
 * Method to clear the HashSet
 */
public void clear() {
    if (ownerOP != null && ownerOP.getExecutionContext().getManageRelations()) {
        // Relationship management
        Iterator iter = delegate.iterator();
        RelationshipManager relMgr = ownerOP.getExecutionContext().getRelationshipManager(ownerOP);
        while (iter.hasNext()) {
            relMgr.relationRemove(ownerMmd.getAbsoluteFieldNumber(), iter.next());
        }
    }
    if (ownerOP != null && !delegate.isEmpty()) {
        // Cascade delete
        if (SCOUtils.useQueuedUpdate(ownerOP)) {
            // Queue any cascade delete
            Iterator iter = delegate.iterator();
            while (iter.hasNext()) {
                ownerOP.getExecutionContext().addOperationToQueue(new CollectionRemoveOperation(ownerOP, ownerMmd.getAbsoluteFieldNumber(), iter.next(), true));
            }
        } else if (SCOUtils.hasDependentElement(ownerMmd)) {
            // Perform the cascade delete
            Iterator iter = delegate.iterator();
            while (iter.hasNext()) {
                ownerOP.getExecutionContext().deleteObjectInternal(iter.next());
            }
        }
    }
    delegate.clear();
    makeDirty();
    if (ownerOP != null && !ownerOP.getExecutionContext().getTransaction().isActive()) {
        ownerOP.getExecutionContext().processNontransactionalUpdate();
    }
}
Also used : RelationshipManager(org.datanucleus.state.RelationshipManager) Iterator(java.util.Iterator) SCOCollectionIterator(org.datanucleus.store.types.SCOCollectionIterator) CollectionRemoveOperation(org.datanucleus.flush.CollectionRemoveOperation)

Example 22 with RelationshipManager

use of org.datanucleus.state.RelationshipManager in project datanucleus-rdbms by datanucleus.

the class FKSetStore method add.

/**
 * Method to add an object to the relationship at the collection end.
 * @param ownerOP ObjectProvider for the owner.
 * @param element Element to be added
 * @return Success indicator
 */
public boolean add(final ObjectProvider ownerOP, E element, int size) {
    if (element == null) {
        // Sets allow no duplicates
        throw new NucleusUserException(Localiser.msg("056039"));
    }
    // Make sure that the element is persisted in the datastore (reachability)
    final Object newOwner = ownerOP.getObject();
    final ExecutionContext ec = ownerOP.getExecutionContext();
    // Find the (element) table storing the FK back to the owner
    boolean isPersistentInterface = storeMgr.getNucleusContext().getMetaDataManager().isPersistentInterface(elementType);
    DatastoreClass elementTable = null;
    if (isPersistentInterface) {
        elementTable = storeMgr.getDatastoreClass(storeMgr.getNucleusContext().getMetaDataManager().getImplementationNameForPersistentInterface(elementType), clr);
    } else {
        Class elementTypeCls = clr.classForName(elementType);
        if (elementTypeCls.isInterface()) {
            // Set<interface> so use type of element passed in and get its table
            elementTable = storeMgr.getDatastoreClass(element.getClass().getName(), clr);
        } else {
            // Use table for element type
            elementTable = storeMgr.getDatastoreClass(elementType, clr);
        }
    }
    if (elementTable == null) {
        // "subclass-table", persisted into table of other class
        AbstractClassMetaData[] managingCmds = storeMgr.getClassesManagingTableForClass(elementCmd, clr);
        if (managingCmds != null && managingCmds.length > 0) {
            // Find which of these subclasses is appropriate for this element
            for (int i = 0; i < managingCmds.length; i++) {
                Class tblCls = clr.classForName(managingCmds[i].getFullClassName());
                if (tblCls.isAssignableFrom(element.getClass())) {
                    elementTable = storeMgr.getDatastoreClass(managingCmds[i].getFullClassName(), clr);
                    break;
                }
            }
        }
    }
    final DatastoreClass elementTbl = elementTable;
    boolean inserted = validateElementForWriting(ec, element, new FieldValues() {

        public void fetchFields(ObjectProvider elementOP) {
            if (elementTbl != null) {
                JavaTypeMapping externalFKMapping = elementTbl.getExternalMapping(ownerMemberMetaData, MappingType.EXTERNAL_FK);
                if (externalFKMapping != null) {
                    // The element has an external FK mapping so set the value it needs to use in the INSERT
                    elementOP.setAssociatedValue(externalFKMapping, ownerOP.getObject());
                }
                if (relationDiscriminatorMapping != null) {
                    // Element type has a shared FK so set the discriminator value for this relation
                    elementOP.setAssociatedValue(relationDiscriminatorMapping, relationDiscriminatorValue);
                }
            }
            int fieldNumInElement = getFieldNumberInElementForBidirectional(elementOP);
            if (fieldNumInElement >= 0) {
                // TODO Move this into RelationshipManager
                // Managed Relations : 1-N bidir, so make sure owner is correct at persist
                Object currentOwner = elementOP.provideField(fieldNumInElement);
                if (currentOwner == null) {
                    // No owner, so correct it
                    NucleusLogger.PERSISTENCE.info(Localiser.msg("056037", ownerOP.getObjectAsPrintable(), ownerMemberMetaData.getFullFieldName(), StringUtils.toJVMIDString(elementOP.getObject())));
                    elementOP.replaceFieldMakeDirty(fieldNumInElement, newOwner);
                } else if (currentOwner != newOwner) {
                    // Check for owner change
                    Object ownerId1 = ec.getApiAdapter().getIdForObject(currentOwner);
                    Object ownerId2 = ec.getApiAdapter().getIdForObject(newOwner);
                    if (ownerId1 != null && ownerId2 != null && ownerId1.equals(ownerId2)) {
                        // Must be attaching
                        if (!ec.getApiAdapter().isDetached(newOwner)) {
                            // Attaching, so make sure we set to the attached owner
                            elementOP.replaceField(fieldNumInElement, newOwner);
                        }
                    } else if (ownerOP.getReferencedPC() == null) {
                        // Not being attached so must be inconsistent owner, so throw exception
                        throw new NucleusUserException(Localiser.msg("056038", ownerOP.getObjectAsPrintable(), ownerMemberMetaData.getFullFieldName(), StringUtils.toJVMIDString(elementOP.getObject()), StringUtils.toJVMIDString(currentOwner)));
                    }
                }
            }
        }

        public void fetchNonLoadedFields(ObjectProvider op) {
        }

        public FetchPlan getFetchPlanForLoading() {
            return null;
        }
    });
    if (inserted) {
        // Element has just been persisted so the FK will be set
        return true;
    }
    // Element was already persistent so make sure the FK is in place
    // TODO This is really "ManagedRelationships" so needs to go in RelationshipManager
    ObjectProvider elementOP = ec.findObjectProvider(element);
    if (elementOP == null) {
        // Element is likely being attached and this is the detached element; lookup the attached element via the id
        Object elementId = ec.getApiAdapter().getIdForObject(element);
        if (elementId != null) {
            element = (E) ec.findObject(elementId, false, false, element.getClass().getName());
            if (element != null) {
                elementOP = ec.findObjectProvider(element);
            }
        }
    }
    int fieldNumInElement = getFieldNumberInElementForBidirectional(elementOP);
    if (fieldNumInElement >= 0 && elementOP != null) {
        // Managed Relations : 1-N bidir, so update the owner of the element
        // Ensure is loaded
        elementOP.isLoaded(fieldNumInElement);
        Object oldOwner = elementOP.provideField(fieldNumInElement);
        if (oldOwner != newOwner) {
            if (NucleusLogger.PERSISTENCE.isDebugEnabled()) {
                NucleusLogger.PERSISTENCE.debug(Localiser.msg("055009", ownerOP.getObjectAsPrintable(), ownerMemberMetaData.getFullFieldName(), StringUtils.toJVMIDString(element)));
            }
            elementOP.replaceFieldMakeDirty(fieldNumInElement, newOwner);
            if (ec.getManageRelations()) {
                // Managed Relationships - add the change we've made here to be analysed at flush
                RelationshipManager relationshipManager = ec.getRelationshipManager(elementOP);
                relationshipManager.relationChange(fieldNumInElement, oldOwner, newOwner);
                if (ec.isFlushing()) {
                    // When already flushing process the changes right away to make them effective during the current flush
                    relationshipManager.process();
                }
            }
            if (ec.isFlushing()) {
                elementOP.flush();
            }
        }
        return oldOwner != newOwner;
    }
    // 1-N unidir so update the FK if not set to be contained in the set
    boolean contained = contains(ownerOP, element);
    return (contained ? false : updateElementFk(ownerOP, element, newOwner));
}
Also used : JavaTypeMapping(org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping) NucleusUserException(org.datanucleus.exceptions.NucleusUserException) FetchPlan(org.datanucleus.FetchPlan) AbstractClassMetaData(org.datanucleus.metadata.AbstractClassMetaData) ExecutionContext(org.datanucleus.ExecutionContext) RelationshipManager(org.datanucleus.state.RelationshipManager) DatastoreClass(org.datanucleus.store.rdbms.table.DatastoreClass) ObjectProvider(org.datanucleus.state.ObjectProvider) DatastoreClass(org.datanucleus.store.rdbms.table.DatastoreClass) FieldValues(org.datanucleus.store.FieldValues)

Example 23 with RelationshipManager

use of org.datanucleus.state.RelationshipManager in project datanucleus-core by datanucleus.

the class ManagedRelationsHandler method getRelationshipManagerForObjectProvider.

/**
 * Method to return the RelationshipManager for the specified ObjectProvider.
 * If none is currently present will create one
 * @param op ObjectProvider
 * @return The RelationshipManager for this object
 */
public RelationshipManager getRelationshipManagerForObjectProvider(ObjectProvider op) {
    RelationshipManager relMgr = managedRelationDetails.get(op);
    if (relMgr == null) {
        relMgr = new RelationshipManagerImpl(op);
        managedRelationDetails.put(op, relMgr);
    }
    return relMgr;
}
Also used : RelationshipManager(org.datanucleus.state.RelationshipManager) RelationshipManagerImpl(org.datanucleus.state.RelationshipManagerImpl)

Example 24 with RelationshipManager

use of org.datanucleus.state.RelationshipManager in project datanucleus-core by datanucleus.

the class Collection method addAll.

/**
 * Method to add a collection of elements.
 * @param c The collection of elements to add.
 * @return Whether they were added successfully.
 */
public boolean addAll(java.util.Collection c) {
    if (useCache) {
        loadFromStore();
    }
    if (ownerOP != null && ownerOP.getExecutionContext().getManageRelations() && !initialising) {
        // Relationship management
        Iterator iter = c.iterator();
        RelationshipManager relMgr = ownerOP.getExecutionContext().getRelationshipManager(ownerOP);
        while (iter.hasNext()) {
            relMgr.relationAdd(ownerMmd.getAbsoluteFieldNumber(), iter.next());
        }
    }
    boolean backingSuccess = true;
    if (backingStore != null) {
        if (SCOUtils.useQueuedUpdate(ownerOP)) {
            for (Object element : c) {
                ownerOP.getExecutionContext().addOperationToQueue(new CollectionAddOperation(ownerOP, backingStore, element));
            }
        } else {
            try {
                backingSuccess = backingStore.addAll(ownerOP, c, useCache ? delegate.size() : -1);
            } catch (NucleusDataStoreException dse) {
                throw new IllegalArgumentException(Localiser.msg("023013", "addAll", ownerMmd.getName(), dse), dse);
            }
        }
    }
    // Only make it dirty after adding the element(s) to the datastore so we give it time
    // to be inserted - otherwise jdoPreStore on this object would have been called before completing the addition
    makeDirty();
    boolean delegateSuccess = delegate.addAll(c);
    if (ownerOP != null && !ownerOP.getExecutionContext().getTransaction().isActive()) {
        ownerOP.getExecutionContext().processNontransactionalUpdate();
    }
    return backingStore != null ? backingSuccess : delegateSuccess;
}
Also used : CollectionAddOperation(org.datanucleus.flush.CollectionAddOperation) NucleusDataStoreException(org.datanucleus.exceptions.NucleusDataStoreException) RelationshipManager(org.datanucleus.state.RelationshipManager) Iterator(java.util.Iterator) SCOCollectionIterator(org.datanucleus.store.types.SCOCollectionIterator)

Example 25 with RelationshipManager

use of org.datanucleus.state.RelationshipManager in project datanucleus-core by datanucleus.

the class HashSet method addAll.

/**
 * Method to add a collection to the HashSet.
 * @param c The collection
 * @return Whether it was added ok.
 */
public boolean addAll(Collection c) {
    if (useCache) {
        loadFromStore();
    }
    if (ownerOP != null && ownerOP.getExecutionContext().getManageRelations() && !initialising) {
        // Relationship management
        Iterator iter = c.iterator();
        RelationshipManager relMgr = ownerOP.getExecutionContext().getRelationshipManager(ownerOP);
        while (iter.hasNext()) {
            relMgr.relationAdd(ownerMmd.getAbsoluteFieldNumber(), iter.next());
        }
    }
    boolean backingSuccess = true;
    if (backingStore != null) {
        if (SCOUtils.useQueuedUpdate(ownerOP)) {
            for (Object element : c) {
                ownerOP.getExecutionContext().addOperationToQueue(new CollectionAddOperation(ownerOP, backingStore, element));
            }
        } else {
            try {
                backingSuccess = backingStore.addAll(ownerOP, c, useCache ? delegate.size() : -1);
            } catch (NucleusDataStoreException dse) {
                throw new IllegalArgumentException(Localiser.msg("023013", "addAll", ownerMmd.getName(), dse), dse);
            }
        }
    }
    // Only make it dirty after adding the element(s) to the datastore so we give it time
    // to be inserted - otherwise jdoPreStore on this object would have been called before completing the addition
    makeDirty();
    boolean delegateSuccess = delegate.addAll(c);
    if (ownerOP != null && !ownerOP.getExecutionContext().getTransaction().isActive()) {
        ownerOP.getExecutionContext().processNontransactionalUpdate();
    }
    return backingStore != null ? backingSuccess : delegateSuccess;
}
Also used : CollectionAddOperation(org.datanucleus.flush.CollectionAddOperation) NucleusDataStoreException(org.datanucleus.exceptions.NucleusDataStoreException) RelationshipManager(org.datanucleus.state.RelationshipManager) Iterator(java.util.Iterator) SCOCollectionIterator(org.datanucleus.store.types.SCOCollectionIterator)

Aggregations

RelationshipManager (org.datanucleus.state.RelationshipManager)47 Iterator (java.util.Iterator)44 CollectionRemoveOperation (org.datanucleus.flush.CollectionRemoveOperation)28 SCOCollectionIterator (org.datanucleus.store.types.SCOCollectionIterator)24 ListIterator (java.util.ListIterator)20 SCOListIterator (org.datanucleus.store.types.SCOListIterator)20 NucleusDataStoreException (org.datanucleus.exceptions.NucleusDataStoreException)12 CollectionAddOperation (org.datanucleus.flush.CollectionAddOperation)11 Collection (java.util.Collection)5 ListAddAtOperation (org.datanucleus.flush.ListAddAtOperation)5 SCOCollection (org.datanucleus.store.types.SCOCollection)5 SCOList (org.datanucleus.store.types.SCOList)5 ObjectProvider (org.datanucleus.state.ObjectProvider)2 AbstractList (java.util.AbstractList)1 Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 ExecutionContext (org.datanucleus.ExecutionContext)1 FetchPlan (org.datanucleus.FetchPlan)1 NucleusUserException (org.datanucleus.exceptions.NucleusUserException)1 AbstractClassMetaData (org.datanucleus.metadata.AbstractClassMetaData)1