/** * Method to add a collection to the TreeSet. * * @param elements The collection * @return Whether it was added ok. */ public boolean addAll(Collection elements) { if (useCache) { loadFromStore(); } if (ownerOP != null && ownerOP.getExecutionContext().getManageRelations()) { // Relationship management Iterator iter = elements.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(queued, ownerOP)) { for (Object element : elements) { ownerOP .getExecutionContext() .addOperationToQueue(new CollectionAddOperation(ownerOP, backingStore, element)); } } else { try { backingSuccess = backingStore.addAll(ownerOP, elements, (useCache ? delegate.size() : -1)); } catch (NucleusDataStoreException dse) { NucleusLogger.PERSISTENCE.warn( LOCALISER.msg("023013", "addAll", ownerMmd.getName(), dse)); backingSuccess = false; } } } // Only make it dirty after adding the field 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(elements); if (ownerOP != null && !ownerOP.getExecutionContext().getTransaction().isActive()) { ownerOP.getExecutionContext().processNontransactionalUpdate(); } return (backingStore != null ? backingSuccess : delegateSuccess); }
/** * Method to remove all elements from the collection from the LinkedHashSet. * * @param elements The collection of elements to remove * @return Whether it was removed ok. */ public boolean removeAll(java.util.Collection elements) { boolean success = delegate.removeAll(elements); if (ownerOP != null && ownerOP.getExecutionContext().getManageRelations()) { // Relationship management Iterator iter = elements.iterator(); RelationshipManager relMgr = ownerOP.getExecutionContext().getRelationshipManager(ownerOP); while (iter.hasNext()) { relMgr.relationRemove(ownerMmd.getAbsoluteFieldNumber(), iter.next()); } } if (ownerOP != null && elements != null && !elements.isEmpty()) { // Cascade delete if (SCOUtils.useQueuedUpdate(ownerOP)) { // Queue the cascade delete Iterator iter = elements.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 = elements.iterator(); while (iter.hasNext()) { ownerOP.getExecutionContext().deleteObjectInternal(iter.next()); } } } if (success) { makeDirty(); if (ownerOP != null && !ownerOP.getExecutionContext().getTransaction().isActive()) { ownerOP.getExecutionContext().processNontransactionalUpdate(); } } return success; }
/** * Method to remove all elements from the collection from the TreeSet. * * @param elements The collection of elements to remove * @return Whether it was removed ok. */ public boolean removeAll(java.util.Collection elements) { makeDirty(); if (useCache) { loadFromStore(); } int size = (useCache ? delegate.size() : -1); Collection contained = null; if (backingStore != null && SCOUtils.useQueuedUpdate(queued, ownerOP)) { // Check which are contained before updating the delegate contained = new java.util.HashSet(); for (Object elem : elements) { if (contains(elem)) { contained.add(elem); } } } boolean delegateSuccess = delegate.removeAll(elements); if (ownerOP != null && ownerOP.getExecutionContext().getManageRelations()) { // Relationship management Iterator iter = elements.iterator(); RelationshipManager relMgr = ownerOP.getExecutionContext().getRelationshipManager(ownerOP); while (iter.hasNext()) { relMgr.relationRemove(ownerMmd.getAbsoluteFieldNumber(), iter.next()); } } if (backingStore != null) { boolean backingSuccess = true; if (SCOUtils.useQueuedUpdate(queued, ownerOP)) { backingSuccess = false; for (Object element : contained) { backingSuccess = true; ownerOP .getExecutionContext() .addOperationToQueue( new CollectionRemoveOperation(ownerOP, backingStore, element, true)); } } else { try { backingSuccess = backingStore.removeAll(ownerOP, elements, size); } catch (NucleusDataStoreException dse) { NucleusLogger.PERSISTENCE.warn( LOCALISER.msg("023013", "removeAll", ownerMmd.getName(), dse)); backingSuccess = false; } } if (ownerOP != null && !ownerOP.getExecutionContext().getTransaction().isActive()) { ownerOP.getExecutionContext().processNontransactionalUpdate(); } return backingSuccess; } else { if (ownerOP != null && !ownerOP.getExecutionContext().getTransaction().isActive()) { ownerOP.getExecutionContext().processNontransactionalUpdate(); } return delegateSuccess; } }
/** * Method to initialise the SCO from an existing value. * * @param o The object to set from * @param forInsert Whether the object needs inserting in the datastore with this value * @param forUpdate Whether to update the datastore with this value */ public void initialise(Object o, boolean forInsert, boolean forUpdate) { Collection c = (Collection) o; if (c != null) { // Check for the case of serialised PC elements, and assign ObjectProviders to the elements // without if (SCOUtils.collectionHasSerialisedElements(ownerMmd) && ownerMmd.getCollection().elementIsPersistent()) { ExecutionContext ec = ownerOP.getExecutionContext(); Iterator iter = c.iterator(); while (iter.hasNext()) { Object pc = iter.next(); ObjectProvider objSM = ec.findObjectProvider(pc); if (objSM == null) { objSM = ec.newObjectProviderForEmbedded( pc, false, ownerOP, ownerMmd.getAbsoluteFieldNumber()); } } } if (backingStore != null && useCache && !isCacheLoaded) { // Mark as loaded isCacheLoaded = true; } if (forInsert) { if (NucleusLogger.PERSISTENCE.isDebugEnabled()) { NucleusLogger.PERSISTENCE.debug( LOCALISER.msg( "023007", ownerOP.getObjectAsPrintable(), ownerMmd.getName(), "" + c.size())); } if (useCache) { loadFromStore(); } if (ownerOP != null && ownerOP.getExecutionContext().getManageRelations()) { // Relationship management Iterator iter = c.iterator(); RelationshipManager relMgr = ownerOP.getExecutionContext().getRelationshipManager(ownerOP); while (iter.hasNext()) { relMgr.relationAdd(ownerMmd.getAbsoluteFieldNumber(), iter.next()); } } if (backingStore != null) { if (SCOUtils.useQueuedUpdate(queued, ownerOP)) { for (Object element : c) { ownerOP .getExecutionContext() .addOperationToQueue(new CollectionAddOperation(ownerOP, backingStore, element)); } } else { try { backingStore.addAll(ownerOP, c, (useCache ? delegate.size() : -1)); } catch (NucleusDataStoreException dse) { NucleusLogger.PERSISTENCE.warn( LOCALISER.msg("023013", "addAll", ownerMmd.getName(), dse)); } } } delegate.addAll(c); makeDirty(); } else if (forUpdate) { if (NucleusLogger.PERSISTENCE.isDebugEnabled()) { NucleusLogger.PERSISTENCE.debug( LOCALISER.msg( "023008", ownerOP.getObjectAsPrintable(), ownerMmd.getName(), "" + c.size())); } // Detect which objects are added and which are deleted if (useCache) { isCacheLoaded = false; // Mark as false since need to load the old collection loadFromStore(); for (Object elem : c) { if (!delegate.contains(elem)) { add(elem); } } java.util.TreeSet delegateCopy = new java.util.TreeSet(delegate); for (Object elem : delegateCopy) { if (!c.contains(elem)) { remove(elem); } } } else { for (Object elem : c) { if (!contains(elem)) { add(elem); } } Iterator iter = iterator(); while (iter.hasNext()) { Object elem = iter.next(); if (!c.contains(elem)) { remove(elem); } } } } else { if (NucleusLogger.PERSISTENCE.isDebugEnabled()) { NucleusLogger.PERSISTENCE.debug( LOCALISER.msg( "023007", ownerOP.getObjectAsPrintable(), ownerMmd.getName(), "" + c.size())); } delegate.clear(); delegate.addAll(c); } } }