use of org.apache.cayenne.ObjectId in project cayenne by apache.
the class HierarchicalObjectResolverNode method objectsFromDataRows.
@Override
List<Persistent> objectsFromDataRows(List<? extends DataRow> rows) {
if (rows == null || rows.size() == 0) {
return new ArrayList<>(1);
}
List<Persistent> results = new ArrayList<>(rows.size());
for (DataRow row : rows) {
// determine entity to use
ClassDescriptor classDescriptor = descriptorResolutionStrategy.descriptorForRow(row);
// not using DataRow.createObjectId for performance reasons -
// ObjectResolver
// has all needed metadata already cached.
ObjectId anId = createObjectId(row, classDescriptor.getEntity(), null);
Persistent object = objectFromDataRow(row, anId, classDescriptor);
if (object == null) {
throw new CayenneRuntimeException("Can't build Object from row: %s", row);
}
// keep the dupe objects (and data rows) around, as there maybe an
// attached
// joint prefetch...
results.add(object);
node.getParentAttachmentStrategy().linkToParent(row, object);
}
// now deal with snapshots
// TODO: refactoring: dupes will clutter the lists and cause extra
// processing...
// removal of dupes happens only downstream, as we need the objects
// matching
// fetched rows for joint prefetch resolving... maybe pushback unique
// and
// non-unique lists to the "node", instead of returning a single list
// from this
// method
cache.snapshotsUpdatedForObjects(results, rows, refreshObjects);
return results;
}
use of org.apache.cayenne.ObjectId in project cayenne by apache.
the class JoinedIdParentAttachementStrategy method linkToParent.
public void linkToParent(DataRow row, Persistent object) {
Persistent parentObject = null;
for (ObjEntity entity : sourceEntities) {
if (entity.isAbstract()) {
continue;
}
ObjectId id = node.getResolver().createObjectId(row, entity, relatedIdPrefix);
if (id == null) {
throw new CayenneRuntimeException("Can't build ObjectId from row: %s, entity: %s, prefix: %s", row, entity.getName(), relatedIdPrefix);
}
parentObject = (Persistent) graphManager.getNode(id);
if (parentObject != null) {
break;
}
}
node.linkToParent(object, parentObject);
}
use of org.apache.cayenne.ObjectId in project cayenne by apache.
the class ObjectDiff method getArcSnapshotValue.
ObjectId getArcSnapshotValue(String propertyName) {
Object value = arcSnapshot != null ? arcSnapshot.get(propertyName) : null;
if (value instanceof Fault) {
Persistent target = (Persistent) ((Fault) value).resolveFault(object, propertyName);
value = target != null ? target.getObjectId() : null;
arcSnapshot.put(propertyName, value);
}
return (ObjectId) value;
}
use of org.apache.cayenne.ObjectId in project cayenne by apache.
the class ObjectResolver method createObjectId.
ObjectId createObjectId(DataRow dataRow, ObjEntity objEntity, String namePrefix) {
Collection<DbAttribute> pk = objEntity == this.descriptor.getEntity() ? this.primaryKey : objEntity.getDbEntity().getPrimaryKeys();
boolean prefix = namePrefix != null && namePrefix.length() > 0;
if (pk.size() == 1) {
DbAttribute attribute = pk.iterator().next();
String key = (prefix) ? namePrefix + attribute.getName() : attribute.getName();
Object val = dataRow.get(key);
// this is possible when processing left outer joint prefetches
if (val == null) {
if (!dataRow.containsKey(key)) {
throw new CayenneRuntimeException("No PK column '%s' found in data row.", key);
}
return null;
}
// PUT without a prefix
return new ObjectId(objEntity.getName(), attribute.getName(), val);
}
// ... handle generic case - PK.size > 1
Map<String, Object> idMap = new HashMap<>(pk.size() * 2);
for (final DbAttribute attribute : pk) {
String key = (prefix) ? namePrefix + attribute.getName() : attribute.getName();
Object val = dataRow.get(key);
// this is possible when processing left outer joint prefetches
if (val == null) {
if (!dataRow.containsKey(key)) {
throw new CayenneRuntimeException("No PK column '%s' found in data row.", key);
}
return null;
}
// PUT without a prefix
idMap.put(attribute.getName(), val);
}
return new ObjectId(objEntity.getName(), idMap);
}
use of org.apache.cayenne.ObjectId in project cayenne by apache.
the class ObjectStore method processInvalidatedIDs.
/**
* @since 1.1
*/
void processInvalidatedIDs(Collection<ObjectId> invalidatedIDs) {
if (invalidatedIDs != null && !invalidatedIDs.isEmpty()) {
for (ObjectId oid : invalidatedIDs) {
DataObject object = (DataObject) getNode(oid);
if (object == null) {
continue;
}
switch(object.getPersistenceState()) {
case PersistenceState.COMMITTED:
object.setPersistenceState(PersistenceState.HOLLOW);
break;
case PersistenceState.MODIFIED:
DataContext context = (DataContext) object.getObjectContext();
DataRow diff = getSnapshot(oid);
// consult delegate if it exists
DataContextDelegate delegate = context.nonNullDelegate();
if (delegate.shouldMergeChanges(object, diff)) {
ClassDescriptor descriptor = context.getEntityResolver().getClassDescriptor(oid.getEntityName());
DataRowUtils.forceMergeWithSnapshot(context, descriptor, object, diff);
delegate.finishedMergeChanges(object);
}
case PersistenceState.HOLLOW:
// do nothing
break;
case PersistenceState.DELETED:
// TODO: Do nothing? Or treat as merged?
break;
}
}
}
}
Aggregations