/** * INTERNAL: * Return the container policy for the mapping. */ protected ContainerPolicy getContainerPolicy() { return this.getCollectionMapping().getContainerPolicy(); }
/** * INTERNAL: * Used during building the backup shallow copy to copy * the vector without re-registering the target objects. */ public Object buildBackupCloneForPartObject(Object attributeValue, Object clone, Object backup, UnitOfWorkImpl unitOfWork) { // Check for null if (attributeValue == null) { return getContainerPolicy().containerInstance(1); } else { return getContainerPolicy().cloneFor(attributeValue); } }
/** * INTERNAL: * Return whether the collection type is appropriate for the indirection policy. * In this case, the type MUST be a Vector (or, in the case of jdk1.2, * Collection or Map). */ protected boolean collectionTypeIsValid(Class collectionType) { return getCollectionMapping().getContainerPolicy().isValidContainerType(collectionType); }
/** * INTERNAL: * This method is used to calculate the differences between two collections. * It is passed to the container policy to calculate the changes. */ public void compareCollectionsForChange(Object oldCollection, Object newCollection, ChangeRecord changeRecord, AbstractSession session) { getContainerPolicy().compareCollectionsForChange(oldCollection, newCollection, (CollectionChangeRecord) changeRecord, session, getReferenceDescriptor()); }
/** * Convenience method. * Return the value of an attribute, unwrapping value holders if necessary. * If the value is null, build a new container. */ public Object getRealCollectionAttributeValueFromObject(Object object, AbstractSession session) throws DescriptorException { Object value = this.getRealAttributeValueFromObject(object, session); if (value == null) { value = this.getContainerPolicy().containerInstance(1); } return value; }
/** * INTERNAL: * Return the value of an attribute, unwrapping value holders if necessary. * Also check to ensure the collection is a vector. */ public Object getRealAttributeValueFromObject(Object object, AbstractSession session) throws DescriptorException { Object value = super.getRealAttributeValueFromObject(object, session); if (value != null) { if (!getContainerPolicy().isValidContainer(value)) { throw DescriptorException.attributeTypeNotValid(this); } } return value; }
/** * INTERNAL: * Initialize the state of mapping. */ public void initialize(AbstractSession session) throws DescriptorException { super.initialize(session); setFields(collectFields()); getContainerPolicy().prepare(getSelectionQuery(), session); // Check that the container policy is correct for the collection type. if ((!usesIndirection()) && (!getAttributeAccessor().getAttributeClass().isAssignableFrom(getContainerPolicy().getContainerClass()))) { throw DescriptorException.incorrectCollectionPolicy(this, getAttributeAccessor().getAttributeClass(), getContainerPolicy().getContainerClass()); } }
/** * INTERNAL: * Copy of the attribute of the object. * This is NOT used for unit of work but for templatizing an object. */ public void buildCopy(Object copy, Object original, ObjectCopyingPolicy policy) { Object attributeValue = getRealCollectionAttributeValueFromObject(original, policy.getSession()); Object valuesIterator = getContainerPolicy().iteratorFor(attributeValue); attributeValue = getContainerPolicy().containerInstance(getContainerPolicy().sizeFor(attributeValue)); while (getContainerPolicy().hasNext(valuesIterator)) { Object originalValue = getContainerPolicy().next(valuesIterator, policy.getSession()); Object copyValue = originalValue; if (policy.shouldCascadeAllParts() || (policy.shouldCascadePrivateParts() && isPrivateOwned())) { copyValue = policy.getSession().copyObject(originalValue, policy); } else { // Check for backrefs to copies. copyValue = policy.getCopies().get(originalValue); if (copyValue == null) { copyValue = originalValue; } } getContainerPolicy().addInto(copyValue, attributeValue, policy.getSession()); } setRealAttributeValueInObject(copy, attributeValue); }
/** * INTERNAL: * Iterate on the attribute value. * The value holder has already been processed. */ public void iterateOnRealAttributeValue(DescriptorIterator iterator, Object realAttributeValue) { if (realAttributeValue == null) { return; } ContainerPolicy cp = getContainerPolicy(); for (Object iter = cp.iteratorFor(realAttributeValue); cp.hasNext(iter);) { iterateOnElement(iterator, cp.next(iter, iterator.getSession())); } }
/** * Compare two objects if their parts are not private owned */ protected boolean compareObjectsWithoutPrivateOwned(Object firstCollection, Object secondCollection, AbstractSession session) { ContainerPolicy cp = getContainerPolicy(); if (cp.sizeFor(firstCollection) != cp.sizeFor(secondCollection)) { return false; } Object firstIter = cp.iteratorFor(firstCollection); Object secondIter = cp.iteratorFor(secondCollection); Vector keyValue = new Vector(); while (cp.hasNext(secondIter)) { Object secondObject = cp.next(secondIter, session); Vector primaryKey = getReferenceDescriptor().getObjectBuilder().extractPrimaryKeyFromObject(secondObject, session); keyValue.addElement(new CacheKey(primaryKey)); } while (cp.hasNext(firstIter)) { Object firstObject = cp.next(firstIter, session); Vector primaryKey = getReferenceDescriptor().getObjectBuilder().extractPrimaryKeyFromObject(firstObject, session); if (!keyValue.contains(new CacheKey(primaryKey))) { return false; } } return true; }
/** * INTERNAL: * Checks if object is deleted from the database or not. */ public boolean verifyDelete(Object object, AbstractSession session) throws DatabaseException { // Row is built for translation if (isReadOnly()) { return true; } if (isPrivateOwned()) { Object objects = getRealCollectionAttributeValueFromObject(object, session); ContainerPolicy containerPolicy = getContainerPolicy(); for (Object iter = containerPolicy.iteratorFor(objects); containerPolicy.hasNext(iter);) { if (!session.verifyDelete(containerPolicy.next(iter, session))) { return false; } } } AbstractRecord row = getDescriptor().getObjectBuilder().buildRowForTranslation(object, session); //cr 3819 added the line below to fix the translationtable to ensure that it // contains the required values prepareTranslationRow(row, object, session); Object value = session.executeQuery(getSelectionQuery(), row); return getContainerPolicy().isEmpty(value); }
ContainerPolicy cp = getContainerPolicy(); if (cp.sizeFor(firstCollection) != cp.sizeFor(secondCollection)) { return false;
/** * ADVANCED: * This method is used to have an object add to a collection once the * changeSet is applied. The referenceKey parameter should only be used for * direct Maps. */ public void simpleAddToCollectionChangeRecord(Object referenceKey, Object changeSetToAdd, ObjectChangeSet changeSet, AbstractSession session) { CollectionChangeRecord collectionChangeRecord = (CollectionChangeRecord)changeSet.getChangesForAttributeNamed(this.getAttributeName()); if (collectionChangeRecord == null) { collectionChangeRecord = new CollectionChangeRecord(changeSet); collectionChangeRecord.setAttribute(getAttributeName()); collectionChangeRecord.setMapping(this); collectionChangeRecord.getAddObjectList().put(changeSetToAdd, changeSetToAdd); collectionChangeRecord.getOrderedAddObjects().add(changeSetToAdd); changeSet.addChange(collectionChangeRecord); } else { getContainerPolicy().recordAddToCollectionInChangeRecord((ObjectChangeSet)changeSetToAdd, collectionChangeRecord); } if (referenceKey != null){ ((ObjectChangeSet)changeSetToAdd).setNewKey(referenceKey); } }
/** * INTERNAL: * Cascade registerNew for Create through mappings that require the cascade */ public void cascadeRegisterNewIfRequired(Object object, UnitOfWorkImpl uow, IdentityHashtable visitedObjects){ Object cloneAttribute = null; cloneAttribute = getAttributeValueFromObject(object); if ((cloneAttribute == null) || (!this.isCascadePersist()) || (!getIndirectionPolicy().objectIsInstantiated(cloneAttribute))) { return; } ContainerPolicy cp = getContainerPolicy(); Object cloneObjectCollection = null; cloneObjectCollection = getRealCollectionAttributeValueFromObject(object, uow); Object cloneIter = cp.iteratorFor(cloneObjectCollection); while (cp.hasNext(cloneIter)) { Object nextObject = cp.next(cloneIter, uow); uow.registerNewObjectForPersist(nextObject, visitedObjects); } }
/** * INTERNAL: * Cascade perform delete through mappings that require the cascade */ public void cascadePerformRemoveIfRequired(Object object, UnitOfWorkImpl uow, IdentityHashtable visitedObjects){ Object cloneAttribute = null; cloneAttribute = getAttributeValueFromObject(object); if ((cloneAttribute == null) || (!this.isCascadeRemove())) { return; } ContainerPolicy cp = getContainerPolicy(); Object cloneObjectCollection = null; cloneObjectCollection = getRealCollectionAttributeValueFromObject(object, uow); Object cloneIter = cp.iteratorFor(cloneObjectCollection); while (cp.hasNext(cloneIter)) { Object nextObject = cp.next(cloneIter, uow); if (nextObject != null && (! visitedObjects.contains(nextObject)) ){ visitedObjects.put(nextObject, nextObject); uow.performRemove(nextObject, visitedObjects); } } }
ContainerPolicy cp = getContainerPolicy(); Object cloneObjectCollection = null; if (cloneAttribute != null) {
/** * INTERNAL: * Require for cloning, the part must be cloned. * Ignore the objects, use the attribute value. */ public Object buildCloneForPartObject(Object attributeValue, Object original, Object clone, UnitOfWorkImpl unitOfWork, boolean isExisting) { ContainerPolicy containerPolicy = getContainerPolicy(); if (attributeValue == null) { Object container = containerPolicy.containerInstance(1); return container; } Object clonedAttributeValue = containerPolicy.containerInstance(containerPolicy.sizeFor(attributeValue)); // I need to synchronize here to prevent the collection from changing while I am cloning it. // This will occur when I am merging into the cache and I am instantiating a UOW valueHolder at the same time // I can not synchronize around the clone, as this will cause deadlocks, so I will need to copy the collection then create the clones // I will use a temporary collection to help speed up the process Object temporaryCollection = null; synchronized (attributeValue) { temporaryCollection = containerPolicy.cloneFor(attributeValue); } for (Object valuesIterator = containerPolicy.iteratorFor(temporaryCollection); containerPolicy.hasNext(valuesIterator);) { Object cloneValue = buildElementClone(containerPolicy.next(valuesIterator, unitOfWork), unitOfWork, isExisting); containerPolicy.addInto(cloneValue, clonedAttributeValue, unitOfWork); } return clonedAttributeValue; }
triggerJoinExpressions(unitOfWork, nestedQuery.getJoinedAttributeManager(), attributeValue); }else { ContainerPolicy cp = ((CollectionMapping)mapping).getContainerPolicy(); Object iterator = cp.iteratorFor(attributeValue); while (cp.hasNext(iterator)){
/** * ADVANCED: * This method is used to have an object removed from a collection once the * changeSet is applied. The referenceKey parameter should only be used for * direct Maps. */ public void simpleRemoveFromCollectionChangeRecord(Object referenceKey, Object changeSetToRemove, ObjectChangeSet changeSet, AbstractSession session) { CollectionChangeRecord collectionChangeRecord = (CollectionChangeRecord)changeSet.getChangesForAttributeNamed(this.getAttributeName()); if (collectionChangeRecord == null) { collectionChangeRecord = new CollectionChangeRecord(changeSet); collectionChangeRecord.setAttribute(getAttributeName()); collectionChangeRecord.setMapping(this); collectionChangeRecord.getRemoveObjectList().put(changeSetToRemove, changeSetToRemove); changeSet.addChange(collectionChangeRecord); } else { getContainerPolicy().recordRemoveFromCollectionInChangeRecord((ObjectChangeSet)changeSetToRemove, collectionChangeRecord); } if (referenceKey != null){ ((ObjectChangeSet)changeSetToRemove).setOldKey(referenceKey); } }
cloneObjectCollection = getRealCollectionAttributeValueFromObject(clone, session); } else { cloneObjectCollection = getContainerPolicy().containerInstance(1);