Search in sources :

Example 1 with DbRowOpType

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;
}
Also used : CayenneMapEntry(org.apache.cayenne.util.CayenneMapEntry) DbEntity(org.apache.cayenne.map.DbEntity) ObjectId(org.apache.cayenne.ObjectId) DbRowOpType(org.apache.cayenne.access.flush.operation.DbRowOpType) DbRelationship(org.apache.cayenne.map.DbRelationship) DbRowOpWithValues(org.apache.cayenne.access.flush.operation.DbRowOpWithValues)

Aggregations

ObjectId (org.apache.cayenne.ObjectId)1 DbRowOpType (org.apache.cayenne.access.flush.operation.DbRowOpType)1 DbRowOpWithValues (org.apache.cayenne.access.flush.operation.DbRowOpWithValues)1 DbEntity (org.apache.cayenne.map.DbEntity)1 DbRelationship (org.apache.cayenne.map.DbRelationship)1 CayenneMapEntry (org.apache.cayenne.util.CayenneMapEntry)1