use of org.apache.cayenne.CayenneRuntimeException 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.CayenneRuntimeException in project cayenne by apache.
the class IncrementalFaultList method checkPageResultConsistency.
/**
* @since 3.0
*/
void checkPageResultConsistency(List<?> objects, List<?> ids) {
if (objects.size() < ids.size()) {
// find missing ids
StringBuilder buffer = new StringBuilder();
buffer.append("Some ObjectIds are missing from the database. ");
buffer.append("Expected ").append(ids.size()).append(", fetched ").append(objects.size());
boolean first = true;
for (Object id : ids) {
boolean found = false;
for (Object object : objects) {
if (helper.replacesObject(object, id)) {
found = true;
break;
}
}
if (!found) {
if (first) {
first = false;
} else {
buffer.append(", ");
}
buffer.append(id.toString());
}
}
throw new CayenneRuntimeException(buffer.toString());
} else if (objects.size() > ids.size()) {
throw new CayenneRuntimeException("Expected %d objects, retrieved %d", ids.size(), objects.size());
}
}
use of org.apache.cayenne.CayenneRuntimeException 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.CayenneRuntimeException in project cayenne by apache.
the class ObjectDiff method addDiff.
void addDiff(NodeDiff diff, ObjectStore parent) {
boolean addDiff = true;
if (diff instanceof ArcOperation) {
ArcOperation arcDiff = (ArcOperation) diff;
Object targetId = arcDiff.getTargetNodeId();
String arcId = arcDiff.getArcId().toString();
ArcProperty property = (ArcProperty) getClassDescriptor().getProperty(arcId);
if (property == null && arcId.startsWith(ASTDbPath.DB_PREFIX)) {
addPhantomFkDiff(arcDiff);
addDiff = false;
} else if (property instanceof ToManyProperty) {
// record flattened op changes
ObjRelationship relationship = property.getRelationship();
if (relationship.isFlattened()) {
if (flatIds == null) {
flatIds = new HashMap<>();
}
ArcOperation oldOp = flatIds.put(arcDiff, arcDiff);
// "delete" cancels "create" and vice versa...
if (oldOp != null && oldOp.isDelete() != arcDiff.isDelete()) {
addDiff = false;
flatIds.remove(arcDiff);
if (otherDiffs != null) {
otherDiffs.remove(oldOp);
}
}
} else if (property.getComplimentaryReverseArc() == null) {
// register complimentary arc diff
String arc = ASTDbPath.DB_PREFIX + property.getComplimentaryReverseDbRelationshipPath();
ArcOperation complimentartyOp = new ArcOperation(targetId, arcDiff.getNodeId(), arc, arcDiff.isDelete());
parent.registerDiff(targetId, complimentartyOp);
}
} else if (property instanceof ToOneProperty) {
if (currentArcSnapshot == null) {
currentArcSnapshot = new HashMap<>();
}
currentArcSnapshot.put(arcId, targetId);
} else {
String message = (property == null) ? "No property for arcId " + arcId : "Unrecognized property for arcId " + arcId + ": " + property;
throw new CayenneRuntimeException(message);
}
}
if (addDiff) {
if (otherDiffs == null) {
otherDiffs = new ArrayList<>(3);
}
otherDiffs.add(diff);
}
}
use of org.apache.cayenne.CayenneRuntimeException 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);
}
Aggregations