Search in sources :

Example 16 with CayenneRuntimeException

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;
}
Also used : ClassDescriptor(org.apache.cayenne.reflect.ClassDescriptor) ObjectId(org.apache.cayenne.ObjectId) ArrayList(java.util.ArrayList) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException) Persistent(org.apache.cayenne.Persistent) DataRow(org.apache.cayenne.DataRow)

Example 17 with CayenneRuntimeException

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());
    }
}
Also used : CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException)

Example 18 with CayenneRuntimeException

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);
}
Also used : ObjEntity(org.apache.cayenne.map.ObjEntity) ObjectId(org.apache.cayenne.ObjectId) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException) Persistent(org.apache.cayenne.Persistent)

Example 19 with CayenneRuntimeException

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);
    }
}
Also used : ObjRelationship(org.apache.cayenne.map.ObjRelationship) ArcProperty(org.apache.cayenne.reflect.ArcProperty) ToManyProperty(org.apache.cayenne.reflect.ToManyProperty) HashMap(java.util.HashMap) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException) ToOneProperty(org.apache.cayenne.reflect.ToOneProperty)

Example 20 with CayenneRuntimeException

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);
}
Also used : ObjectId(org.apache.cayenne.ObjectId) HashMap(java.util.HashMap) DbAttribute(org.apache.cayenne.map.DbAttribute) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException) DataObject(org.apache.cayenne.DataObject)

Aggregations

CayenneRuntimeException (org.apache.cayenne.CayenneRuntimeException)168 Test (org.junit.Test)25 DbAttribute (org.apache.cayenne.map.DbAttribute)21 DataMap (org.apache.cayenne.map.DataMap)19 ObjectId (org.apache.cayenne.ObjectId)18 ObjEntity (org.apache.cayenne.map.ObjEntity)18 Persistent (org.apache.cayenne.Persistent)17 Expression (org.apache.cayenne.exp.Expression)17 ClassDescriptor (org.apache.cayenne.reflect.ClassDescriptor)17 ArrayList (java.util.ArrayList)14 HashMap (java.util.HashMap)14 DbEntity (org.apache.cayenne.map.DbEntity)14 IOException (java.io.IOException)13 List (java.util.List)12 ObjRelationship (org.apache.cayenne.map.ObjRelationship)12 DbRelationship (org.apache.cayenne.map.DbRelationship)10 DateTestEntity (org.apache.cayenne.testdo.date_time.DateTestEntity)10 File (java.io.File)9 Connection (java.sql.Connection)9 SQLException (java.sql.SQLException)9