use of org.eclipse.persistence.internal.queries.ContainerPolicy in project eclipselink by eclipse-ee4j.
the class EISOneToManyMapping method writeFromObjectIntoRow.
/**
* INTERNAL:
* Get the appropriate attribute value from the object
* and put it in the appropriate field of the database row.
* Loop through the reference objects and extract the
* primary keys and put them in the vector of "nested" rows.
*/
@Override
public void writeFromObjectIntoRow(Object object, AbstractRecord row, AbstractSession session, WriteType writeType) {
if (!isForeignKeyRelationship) {
return;
}
if (((getSourceForeignKeysToTargetKeys()) == null) || (getSourceForeignKeysToTargetKeys().size() == 0)) {
return;
}
if (this.isReadOnly()) {
return;
}
AbstractRecord referenceRow = this.getIndirectionPolicy().extractReferenceRow(this.getAttributeValueFromObject(object));
if (referenceRow != null) {
// the reference objects have not been instantiated - use the value from the original row
if (getForeignKeyGroupingElement() != null) {
row.put(this.getForeignKeyGroupingElement(), referenceRow.getValues(this.getForeignKeyGroupingElement()));
} else if (getSourceForeignKeyFields().size() > 0) {
DatabaseField foreignKeyField = getSourceForeignKeyFields().get(0);
row.put(foreignKeyField, referenceRow.getValues(foreignKeyField));
}
return;
}
ContainerPolicy cp = this.getContainerPolicy();
// extract the keys from the objects
Object attributeValue = this.getRealCollectionAttributeValueFromObject(object, session);
if (getForeignKeyGroupingElement() != null) {
Vector<AbstractRecord> nestedRows = new Vector<>(cp.sizeFor(attributeValue));
for (Object iter = cp.iteratorFor(attributeValue); cp.hasNext(iter); ) {
AbstractRecord nestedRow = extractKeyRowFromReferenceObject(cp.next(iter, session), session, row);
nestedRows.add(nestedRow);
}
row.add(this.getForeignKeyGroupingElement(), nestedRows);
} else {
DatabaseField singleField = getSourceForeignKeyFields().get(0);
DatabaseField pkField = getSourceForeignKeysToTargetKeys().get(singleField);
List<Object> foreignKeys = new ArrayList<>(cp.sizeFor(attributeValue));
for (Object iter = cp.iteratorFor(attributeValue); cp.hasNext(iter); ) {
Object singleValue = getReferenceDescriptor().getObjectBuilder().extractValueFromObjectForField(cp.next(iter, session), pkField, session);
foreignKeys.add(singleValue);
}
row.add(singleField, foreignKeys);
}
}
use of org.eclipse.persistence.internal.queries.ContainerPolicy in project eclipselink by eclipse-ee4j.
the class EISOneToManyMapping method postDelete.
/**
* INTERNAL:
* Delete the reference objects.
*/
@Override
public void postDelete(DeleteObjectQuery query) throws DatabaseException, OptimisticLockException {
if (!isForeignKeyRelationship()) {
return;
}
if (!this.shouldObjectModifyCascadeToParts(query)) {
return;
}
Object referenceObjects = this.getRealCollectionAttributeValueFromObject(query.getObject(), query.getSession());
// otherwise, delete the reference objects one by one
if (this.hasCustomDeleteAllQuery()) {
this.deleteAll(query, referenceObjects);
} else {
ContainerPolicy cp = this.getContainerPolicy();
for (Object iter = cp.iteratorFor(referenceObjects); cp.hasNext(iter); ) {
DeleteObjectQuery deleteQuery = new DeleteObjectQuery();
deleteQuery.setIsExecutionClone(true);
deleteQuery.setObject(cp.next(iter, query.getSession()));
deleteQuery.setCascadePolicy(query.getCascadePolicy());
query.getSession().executeQuery(deleteQuery);
}
if (!query.getSession().isUnitOfWork()) {
// This deletes any objects on the database, as the collection in memory may have been changed.
// This is not required for unit of work, as the update would have already deleted these objects,
// and the backup copy will include the same objects, causing double deletes.
this.deleteReferenceObjectsLeftOnDatabase(query);
}
}
}
use of org.eclipse.persistence.internal.queries.ContainerPolicy in project eclipselink by eclipse-ee4j.
the class EISOneToManyMapping method verifyDelete.
/**
* INTERNAL:
* Used to verify whether the specified object is deleted or not.
*/
@Override
public boolean verifyDelete(Object object, AbstractSession session) throws DatabaseException {
if (this.isPrivateOwned()) {
Object objects = this.getRealCollectionAttributeValueFromObject(object, session);
ContainerPolicy containerPolicy = getContainerPolicy();
for (Object iter = containerPolicy.iteratorFor(objects); containerPolicy.hasNext(iter); ) {
if (!session.verifyDelete(containerPolicy.next(iter, session))) {
return false;
}
}
}
return true;
}
use of org.eclipse.persistence.internal.queries.ContainerPolicy in project eclipselink by eclipse-ee4j.
the class EISOneToManyQueryBasedValueHolder method instantiate.
@Override
protected T instantiate(AbstractSession session) throws DatabaseException {
Vector<AbstractRecord> rows = this.mapping.getForeignKeyRows(this.getRow(), session);
int size = rows.size();
ContainerPolicy cp = ((ReadAllQuery) this.getQuery()).getContainerPolicy();
@SuppressWarnings({ "unchecked" }) T returnValue = (T) cp.containerInstance(size);
for (int i = 0; i < size; i++) {
AbstractRecord nextRow = rows.get(i);
Object results = session.executeQuery(getQuery(), nextRow);
if (results instanceof Collection) {
Iterator<?> iter = ((Collection<?>) results).iterator();
while (iter.hasNext()) {
cp.addInto(iter.next(), returnValue, session);
}
} else if (results instanceof java.util.Map) {
Iterator<?> iter = ((java.util.Map<?, ?>) results).values().iterator();
while (iter.hasNext()) {
cp.addInto(iter.next(), returnValue, session);
}
} else {
cp.addInto(results, returnValue, session);
}
}
return returnValue;
}
use of org.eclipse.persistence.internal.queries.ContainerPolicy in project eclipselink by eclipse-ee4j.
the class CollectionChangeRecord method internalRecreateOriginalCollection.
/**
* Recreates the original state of currentCollection.
*/
@Override
public void internalRecreateOriginalCollection(Object currentCollection, AbstractSession session) {
ContainerPolicy cp = this.mapping.getContainerPolicy();
if (orderedChangeObjectList == null || orderedChangeObjectList.isEmpty()) {
if (this.removeObjectList != null) {
Iterator<ObjectChangeSet> it = this.removeObjectList.keySet().iterator();
while (it.hasNext()) {
ObjectChangeSet changeSet = it.next();
cp.addInto(changeSet.getUnitOfWorkClone(), currentCollection, session);
}
}
if (this.addObjectList != null) {
Iterator<ObjectChangeSet> it = this.addObjectList.keySet().iterator();
while (it.hasNext()) {
ObjectChangeSet changeSet = it.next();
cp.removeFrom(changeSet.getUnitOfWorkClone(), currentCollection, session);
}
}
} else {
List originalList = (List) currentCollection;
for (int i = this.orderedChangeObjectList.size() - 1; i >= 0; i--) {
OrderedChangeObject orderedChange = this.orderedChangeObjectList.get(i);
Object obj = orderedChange.getAddedOrRemovedObject();
Integer index = orderedChange.getIndex();
int changeType = orderedChange.getChangeType();
if (changeType == CollectionChangeEvent.ADD) {
// the object was added - remove the corresponding index
if (index == null) {
originalList.remove(originalList.size() - 1);
} else {
originalList.remove(index.intValue());
}
} else if (changeType == CollectionChangeEvent.REMOVE) {
// the object was removed - add its index in the new list
if (index == null) {
throw ValidationException.collectionRemoveEventWithNoIndex(getMapping());
} else {
originalList.add(index, obj);
}
}
}
}
}
Aggregations