use of org.apache.cayenne.reflect.AttributeProperty in project cayenne by apache.
the class DataObjectDescriptorFactory_InheritanceMapsIT method testVisitProperties_IterationOrder.
@Test
public void testVisitProperties_IterationOrder() {
DataObjectDescriptorFactory factory = new DataObjectDescriptorFactory(resolver.getClassDescriptorMap(), new SingletonFaultFactory());
for (ObjEntity e : resolver.getObjEntities()) {
ClassDescriptor descriptor = factory.getDescriptor(e.getName());
final PropertyDescriptor[] lastProcessed = new PropertyDescriptor[1];
PropertyVisitor visitor = new PropertyVisitor() {
public boolean visitToOne(ToOneProperty property) {
DataObjectDescriptorFactoryIT.assertPropertiesAreInOrder(lastProcessed[0], property);
lastProcessed[0] = property;
return true;
}
public boolean visitToMany(ToManyProperty property) {
DataObjectDescriptorFactoryIT.assertPropertiesAreInOrder(lastProcessed[0], property);
lastProcessed[0] = property;
return true;
}
public boolean visitAttribute(AttributeProperty property) {
DataObjectDescriptorFactoryIT.assertPropertiesAreInOrder(lastProcessed[0], property);
lastProcessed[0] = property;
return true;
}
};
descriptor.visitProperties(visitor);
}
}
use of org.apache.cayenne.reflect.AttributeProperty in project cayenne by apache.
the class ObjectContextDeleteAction method processDeleteRules.
private void processDeleteRules(final Persistent object, int oldState) throws DeleteDenyException {
ClassDescriptor descriptor = context.getEntityResolver().getClassDescriptor(object.getObjectId().getEntityName());
for (final ObjRelationship relationship : descriptor.getEntity().getRelationships()) {
boolean processFlattened = relationship.isFlattened() && relationship.isToDependentEntity() && !relationship.isReadOnly();
// first check for no action... bail out if no flattened processing is needed
if (relationship.getDeleteRule() == DeleteRule.NO_ACTION && !processFlattened) {
continue;
}
ArcProperty property = (ArcProperty) descriptor.getProperty(relationship.getName());
final Collection<Persistent> relatedObjects = toCollection(property.readProperty(object));
// no related object, bail out
if (relatedObjects.size() == 0) {
continue;
}
// process DENY rule first...
if (relationship.getDeleteRule() == DeleteRule.DENY) {
object.setPersistenceState(oldState);
String message = relatedObjects.size() == 1 ? "1 related object" : relatedObjects.size() + " related objects";
throw new DeleteDenyException(object, relationship.getName(), message);
}
// object graph
if (processFlattened) {
for (Persistent relatedObject : relatedObjects) {
context.getGraphManager().arcDeleted(object.getObjectId(), relatedObject.getObjectId(), relationship.getName());
}
}
// process remaining rules
switch(relationship.getDeleteRule()) {
case DeleteRule.NO_ACTION:
break;
case DeleteRule.NULLIFY:
ArcProperty reverseArc = property.getComplimentaryReverseArc();
if (reverseArc == null) {
// nothing we can do here
break;
}
reverseArc.visit(new PropertyVisitor() {
public boolean visitAttribute(AttributeProperty property) {
return false;
}
public boolean visitToMany(ToManyProperty property) {
for (Persistent relatedObject : relatedObjects) {
property.removeTarget(relatedObject, object, true);
}
return false;
}
public boolean visitToOne(ToOneProperty property) {
// nullify the reverse relationship
for (Persistent relatedObject : relatedObjects) {
property.setTarget(relatedObject, null, true);
}
return false;
}
});
break;
case DeleteRule.CASCADE:
// Delete all related objects
for (Persistent relatedObject : relatedObjects) {
performDelete(relatedObject);
}
break;
default:
object.setPersistenceState(oldState);
throw new CayenneRuntimeException("Invalid delete rule %s", relationship.getDeleteRule());
}
}
}
use of org.apache.cayenne.reflect.AttributeProperty in project cayenne by apache.
the class DataContext method currentSnapshot.
/**
* Returns a DataRow reflecting current, possibly uncommitted, object state.
* <p>
* <strong>Warning:</strong> This method will return a partial snapshot if
* an object or one of its related objects that propagate their keys to this
* object have temporary ids. DO NOT USE this method if you expect a DataRow
* to represent a complete object state.
* </p>
*
* @since 1.1
*/
public DataRow currentSnapshot(final Persistent object) {
// for a HOLLOW object return snapshot from cache
if (object.getPersistenceState() == PersistenceState.HOLLOW && object.getObjectContext() != null) {
return getObjectStore().getSnapshot(object.getObjectId());
}
ObjEntity entity = getEntityResolver().getObjEntity(object);
final ClassDescriptor descriptor = getEntityResolver().getClassDescriptor(entity.getName());
final DataRow snapshot = new DataRow(10);
snapshot.setEntityName(entity.getName());
descriptor.visitProperties(new PropertyVisitor() {
public boolean visitAttribute(AttributeProperty property) {
ObjAttribute objAttr = property.getAttribute();
// processing compound attributes correctly
snapshot.put(objAttr.getDbAttributePath(), property.readPropertyDirectly(object));
return true;
}
public boolean visitToMany(ToManyProperty property) {
// do nothing
return true;
}
public boolean visitToOne(ToOneProperty property) {
ObjRelationship rel = property.getRelationship();
// if target doesn't propagates its key value, skip it
if (rel.isSourceIndependentFromTargetChange()) {
return true;
}
Object targetObject = property.readPropertyDirectly(object);
if (targetObject == null) {
return true;
}
// to avoid unneeded fault triggering
if (targetObject instanceof Fault) {
DataRow storedSnapshot = getObjectStore().getSnapshot(object.getObjectId());
if (storedSnapshot == null) {
throw new CayenneRuntimeException("No matching objects found for ObjectId %s" + ". Object may have been deleted externally.", object.getObjectId());
}
DbRelationship dbRel = rel.getDbRelationships().get(0);
for (DbJoin join : dbRel.getJoins()) {
String key = join.getSourceName();
snapshot.put(key, storedSnapshot.get(key));
}
return true;
}
// target is resolved and we have an FK->PK to it,
// so extract it from target...
Persistent target = (Persistent) targetObject;
Map<String, Object> idParts = target.getObjectId().getIdSnapshot();
// this method.
if (idParts.isEmpty()) {
return true;
}
DbRelationship dbRel = rel.getDbRelationships().get(0);
Map<String, Object> fk = dbRel.srcFkSnapshotWithTargetSnapshot(idParts);
snapshot.putAll(fk);
return true;
}
});
// process object id map
// we should ignore any object id values if a corresponding attribute
// is a part of relationship "toMasterPK", since those values have been
// set above when db relationships where processed.
Map<String, Object> thisIdParts = object.getObjectId().getIdSnapshot();
if (thisIdParts != null) {
// put only those that do not exist in the map
for (Map.Entry<String, Object> entry : thisIdParts.entrySet()) {
String nextKey = entry.getKey();
if (!snapshot.containsKey(nextKey)) {
snapshot.put(nextKey, entry.getValue());
}
}
}
return snapshot;
}
use of org.apache.cayenne.reflect.AttributeProperty in project cayenne by apache.
the class DataContextMergeHandler method arcChanged.
// works the same for add and remove as long as we don't get too smart per TODO below.
private void arcChanged(Object nodeId, Object targetNodeId, Object arcId) {
final Persistent source = (Persistent) context.getGraphManager().getNode(nodeId);
if (source != null && source.getPersistenceState() != PersistenceState.HOLLOW) {
final int state = source.getPersistenceState();
PropertyDescriptor p = propertyForId(nodeId, arcId.toString());
p.visit(new PropertyVisitor() {
public boolean visitAttribute(AttributeProperty property) {
return false;
}
public boolean visitToMany(ToManyProperty property) {
if (state == PersistenceState.COMMITTED) {
property.invalidate(source);
}
return false;
}
public boolean visitToOne(ToOneProperty property) {
if (state == PersistenceState.COMMITTED) {
property.invalidate(source);
}
// of dirty objects. See DataRowUtils for details.
return false;
}
});
}
}
use of org.apache.cayenne.reflect.AttributeProperty in project cayenne by apache.
the class DataDomainSyncBucket method postprocess.
void postprocess() {
if (!objectsByDescriptor.isEmpty()) {
CompoundDiff result = parent.getResultDiff();
Map<ObjectId, DataRow> modifiedSnapshots = parent.getResultModifiedSnapshots();
Collection<ObjectId> deletedIds = parent.getResultDeletedIds();
for (Map.Entry<ClassDescriptor, List<Persistent>> entry : objectsByDescriptor.entrySet()) {
ClassDescriptor descriptor = entry.getKey();
for (Persistent object : entry.getValue()) {
ObjectId id = object.getObjectId();
ObjectId finalId;
// record id change and update attributes for generated ids
if (id.isReplacementIdAttached()) {
Map<String, Object> replacement = id.getReplacementIdMap();
for (AttributeProperty property : descriptor.getIdProperties()) {
Object value = replacement.get(property.getAttribute().getDbAttributeName());
// if the id wasn't generated. We may need to optimize it...
if (value != null) {
property.writePropertyDirectly(object, null, value);
}
}
ObjectId replacementId = id.createReplacementId();
result.add(new NodeIdChangeOperation(id, replacementId));
// DataRowCache has no notion of replaced id...
if (!id.isTemporary()) {
deletedIds.add(id);
}
finalId = replacementId;
} else if (id.isTemporary()) {
throw new CayenneRuntimeException("Temporary ID hasn't been replaced on commit: %s", object);
} else {
finalId = id;
}
// do not take the snapshot until generated columns are processed (see code above)
DataRow dataRow = parent.getContext().currentSnapshot(object);
if (object instanceof DataObject) {
DataObject dataObject = (DataObject) object;
dataRow.setReplacesVersion(dataObject.getSnapshotVersion());
dataObject.setSnapshotVersion(dataRow.getVersion());
}
modifiedSnapshots.put(finalId, dataRow);
// update Map reverse relationships
for (ArcProperty arc : descriptor.getMapArcProperties()) {
ToManyMapProperty reverseArc = (ToManyMapProperty) arc.getComplimentaryReverseArc();
// must resolve faults... hopefully for to-one this will not cause extra fetches...
Object source = arc.readProperty(object);
if (source != null && !reverseArc.isFault(source)) {
remapTarget(reverseArc, source, object);
}
}
}
}
}
}
Aggregations