use of org.apache.cayenne.access.flush.operation.DbRowOpType in project cayenne by apache.
the class ArcValuesCreationHandler method processFlattenedPath.
ObjectId processFlattenedPath(ObjectId id, ObjectId finalTargetId, DbEntity entity, String dbPath, boolean add) {
Iterator<CayenneMapEntry> dbPathIterator = entity.resolvePathComponents(dbPath);
StringBuilder path = new StringBuilder();
ObjectId srcId = id;
ObjectId targetId = null;
while (dbPathIterator.hasNext()) {
CayenneMapEntry entry = dbPathIterator.next();
if (path.length() > 0) {
path.append('.');
}
path.append(entry.getName());
if (entry instanceof DbRelationship) {
DbRelationship relationship = (DbRelationship) entry;
// intermediate db entity to be inserted
DbEntity target = relationship.getTargetEntity();
// if ID is present, just use it, otherwise create new
String flattenedPath = path.toString();
// if this is last segment and it's a relationship, use known target id from arc creation
if (!dbPathIterator.hasNext()) {
targetId = finalTargetId;
} else {
if (!relationship.isToMany()) {
targetId = factory.getStore().getFlattenedId(id, flattenedPath);
} else {
targetId = null;
}
}
if (targetId == null) {
// should insert, regardless of original operation (insert/update)
targetId = ObjectId.of(ASTDbPath.DB_PREFIX + target.getName());
if (!relationship.isToMany()) {
factory.getStore().markFlattenedPath(id, flattenedPath, targetId);
}
DbRowOpType type;
if (relationship.isToMany()) {
type = add ? DbRowOpType.INSERT : DbRowOpType.DELETE;
factory.getOrCreate(target, targetId, type);
} else {
type = add ? DbRowOpType.INSERT : DbRowOpType.UPDATE;
factory.<DbRowOpWithValues>getOrCreate(target, targetId, type).getValues().addFlattenedId(flattenedPath, targetId);
}
} else if (dbPathIterator.hasNext()) {
// should update existing DB row
factory.getOrCreate(target, targetId, add ? DbRowOpType.UPDATE : defaultType);
}
processRelationship(relationship, srcId, targetId, add);
// use target as next source..
srcId = targetId;
}
}
return targetId;
}
Aggregations