Search in sources :

Example 51 with ObjAttribute

use of org.apache.cayenne.map.ObjAttribute in project cayenne by apache.

the class ASTObjPathIT method testEvaluate_ObjEntity_Outer.

@Test
public void testEvaluate_ObjEntity_Outer() {
    ASTObjPath node = new ASTObjPath("paintingArray+.paintingTitle");
    ObjEntity ae = context.getEntityResolver().getObjEntity(Artist.class);
    Object target = node.evaluate(ae);
    assertTrue(target instanceof ObjAttribute);
}
Also used : ObjEntity(org.apache.cayenne.map.ObjEntity) ObjAttribute(org.apache.cayenne.map.ObjAttribute) Test(org.junit.Test)

Example 52 with ObjAttribute

use of org.apache.cayenne.map.ObjAttribute in project cayenne by apache.

the class DataContext method currentSnapshot.

/**
 * Returns a DataRow reflecting current, possibly uncommitted, object state.
 * <p>
 * <strong>Warning:</strong> This method will return a partial snapshot if
 * an object or one of its related objects that propagate their keys to this
 * object have temporary ids. DO NOT USE this method if you expect a DataRow
 * to represent a complete object state.
 * </p>
 *
 * @since 1.1
 */
public DataRow currentSnapshot(final Persistent object) {
    // for a HOLLOW object return snapshot from cache
    if (object.getPersistenceState() == PersistenceState.HOLLOW && object.getObjectContext() != null) {
        return getObjectStore().getSnapshot(object.getObjectId());
    }
    ObjEntity entity = getEntityResolver().getObjEntity(object);
    final ClassDescriptor descriptor = getEntityResolver().getClassDescriptor(entity.getName());
    final DataRow snapshot = new DataRow(10);
    snapshot.setEntityName(entity.getName());
    descriptor.visitProperties(new PropertyVisitor() {

        public boolean visitAttribute(AttributeProperty property) {
            ObjAttribute objAttr = property.getAttribute();
            // processing compound attributes correctly
            snapshot.put(objAttr.getDbAttributePath(), property.readPropertyDirectly(object));
            return true;
        }

        public boolean visitToMany(ToManyProperty property) {
            // do nothing
            return true;
        }

        public boolean visitToOne(ToOneProperty property) {
            ObjRelationship rel = property.getRelationship();
            // if target doesn't propagates its key value, skip it
            if (rel.isSourceIndependentFromTargetChange()) {
                return true;
            }
            Object targetObject = property.readPropertyDirectly(object);
            if (targetObject == null) {
                return true;
            }
            // to avoid unneeded fault triggering
            if (targetObject instanceof Fault) {
                DataRow storedSnapshot = getObjectStore().getSnapshot(object.getObjectId());
                if (storedSnapshot == null) {
                    throw new CayenneRuntimeException("No matching objects found for ObjectId %s" + ". Object may have been deleted externally.", object.getObjectId());
                }
                DbRelationship dbRel = rel.getDbRelationships().get(0);
                for (DbJoin join : dbRel.getJoins()) {
                    String key = join.getSourceName();
                    snapshot.put(key, storedSnapshot.get(key));
                }
                return true;
            }
            // target is resolved and we have an FK->PK to it,
            // so extract it from target...
            Persistent target = (Persistent) targetObject;
            Map<String, Object> idParts = target.getObjectId().getIdSnapshot();
            // this method.
            if (idParts.isEmpty()) {
                return true;
            }
            DbRelationship dbRel = rel.getDbRelationships().get(0);
            Map<String, Object> fk = dbRel.srcFkSnapshotWithTargetSnapshot(idParts);
            snapshot.putAll(fk);
            return true;
        }
    });
    // process object id map
    // we should ignore any object id values if a corresponding attribute
    // is a part of relationship "toMasterPK", since those values have been
    // set above when db relationships where processed.
    Map<String, Object> thisIdParts = object.getObjectId().getIdSnapshot();
    if (thisIdParts != null) {
        // put only those that do not exist in the map
        for (Map.Entry<String, Object> entry : thisIdParts.entrySet()) {
            String nextKey = entry.getKey();
            if (!snapshot.containsKey(nextKey)) {
                snapshot.put(nextKey, entry.getValue());
            }
        }
    }
    return snapshot;
}
Also used : ObjRelationship(org.apache.cayenne.map.ObjRelationship) ClassDescriptor(org.apache.cayenne.reflect.ClassDescriptor) ObjAttribute(org.apache.cayenne.map.ObjAttribute) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException) Fault(org.apache.cayenne.Fault) Persistent(org.apache.cayenne.Persistent) AttributeProperty(org.apache.cayenne.reflect.AttributeProperty) DataRow(org.apache.cayenne.DataRow) ToOneProperty(org.apache.cayenne.reflect.ToOneProperty) ObjEntity(org.apache.cayenne.map.ObjEntity) ToManyProperty(org.apache.cayenne.reflect.ToManyProperty) DbRelationship(org.apache.cayenne.map.DbRelationship) DbJoin(org.apache.cayenne.map.DbJoin) DataObject(org.apache.cayenne.DataObject) Map(java.util.Map) PropertyVisitor(org.apache.cayenne.reflect.PropertyVisitor)

Example 53 with ObjAttribute

use of org.apache.cayenne.map.ObjAttribute in project cayenne by apache.

the class DataDomainInsertBucket method createPermIds.

void createPermIds(DbEntityClassDescriptor descriptor, Collection<Persistent> objects) {
    if (objects.isEmpty()) {
        return;
    }
    ObjEntity objEntity = descriptor.getEntity();
    DbEntity entity = descriptor.getDbEntity();
    DataNode node = parent.getDomain().lookupDataNode(entity.getDataMap());
    boolean supportsGeneratedKeys = node.getAdapter().supportsGeneratedKeys();
    PkGenerator pkGenerator = node.getAdapter().getPkGenerator();
    for (Persistent object : objects) {
        ObjectId id = object.getObjectId();
        if (id == null || !id.isTemporary()) {
            continue;
        }
        // modify replacement id directly...
        Map<String, Object> idMap = id.getReplacementIdMap();
        boolean autoPkDone = false;
        for (DbAttribute dbAttr : entity.getPrimaryKeys()) {
            String dbAttrName = dbAttr.getName();
            if (idMap.containsKey(dbAttrName)) {
                continue;
            }
            // handle meaningful PK
            ObjAttribute objAttr = objEntity.getAttributeForDbAttribute(dbAttr);
            if (objAttr != null) {
                Object value = descriptor.getClassDescriptor().getProperty(objAttr.getName()).readPropertyDirectly(object);
                if (value != null) {
                    Class<?> javaClass = objAttr.getJavaClass();
                    if (javaClass.isPrimitive() && value instanceof Number && ((Number) value).intValue() == 0) {
                    // primitive 0 has to be treated as NULL, or
                    // otherwise we can't generate PK for POJO's
                    } else {
                        idMap.put(dbAttrName, value);
                        continue;
                    }
                }
            }
            // skip db-generated
            if (supportsGeneratedKeys && dbAttr.isGenerated()) {
                continue;
            }
            // skip propagated
            if (isPropagated(dbAttr)) {
                continue;
            }
            // already in this loop, we must bail out.
            if (autoPkDone) {
                throw new CayenneRuntimeException("Primary Key autogeneration only works for a single attribute.");
            }
            // finally, use database generation mechanism
            try {
                Object pkValue = pkGenerator.generatePk(node, dbAttr);
                idMap.put(dbAttrName, pkValue);
                autoPkDone = true;
            } catch (Exception ex) {
                throw new CayenneRuntimeException("Error generating PK: %s", ex, ex.getMessage());
            }
        }
    }
}
Also used : ObjAttribute(org.apache.cayenne.map.ObjAttribute) ObjectId(org.apache.cayenne.ObjectId) DbAttribute(org.apache.cayenne.map.DbAttribute) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException) Persistent(org.apache.cayenne.Persistent) PkGenerator(org.apache.cayenne.dba.PkGenerator) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException) ObjEntity(org.apache.cayenne.map.ObjEntity) DbEntity(org.apache.cayenne.map.DbEntity)

Example 54 with ObjAttribute

use of org.apache.cayenne.map.ObjAttribute in project cayenne by apache.

the class DataDomainSyncBucket method groupObjEntitiesBySpannedDbEntities.

private void groupObjEntitiesBySpannedDbEntities() {
    dbEntities = new ArrayList<>(objectsByDescriptor.size());
    descriptorsByDbEntity = new HashMap<>(objectsByDescriptor.size() * 2);
    for (ClassDescriptor descriptor : objectsByDescriptor.keySet()) {
        // root DbEntity
        {
            DbEntityClassDescriptor dbEntityDescriptor = new DbEntityClassDescriptor(descriptor);
            DbEntity dbEntity = dbEntityDescriptor.getDbEntity();
            Collection<DbEntityClassDescriptor> descriptors = descriptorsByDbEntity.get(dbEntity);
            if (descriptors == null) {
                descriptors = new ArrayList<>(1);
                dbEntities.add(dbEntity);
                descriptorsByDbEntity.put(dbEntity, descriptors);
            }
            if (!containsClassDescriptor(descriptors, descriptor)) {
                descriptors.add(dbEntityDescriptor);
            }
        }
        // Note that this logic won't allow flattened attributes to span multiple databases...
        for (ObjAttribute objAttribute : descriptor.getEntity().getAttributes()) {
            if (objAttribute.isFlattened()) {
                DbEntityClassDescriptor dbEntityDescriptor = new DbEntityClassDescriptor(descriptor, objAttribute);
                DbEntity dbEntity = dbEntityDescriptor.getDbEntity();
                Collection<DbEntityClassDescriptor> descriptors = descriptorsByDbEntity.get(dbEntity);
                if (descriptors == null) {
                    descriptors = new ArrayList<>(1);
                    dbEntities.add(dbEntity);
                    descriptorsByDbEntity.put(dbEntity, descriptors);
                }
                if (!containsClassDescriptor(descriptors, descriptor)) {
                    descriptors.add(dbEntityDescriptor);
                }
            }
        }
    }
}
Also used : ClassDescriptor(org.apache.cayenne.reflect.ClassDescriptor) DbEntity(org.apache.cayenne.map.DbEntity) ObjAttribute(org.apache.cayenne.map.ObjAttribute) ArrayList(java.util.ArrayList) Collection(java.util.Collection)

Example 55 with ObjAttribute

use of org.apache.cayenne.map.ObjAttribute in project cayenne by apache.

the class MixedResultIncrementalFaultList method buildIdQualifier.

Expression buildIdQualifier(int index, Object[] data) {
    Map<String, Object> map;
    if (data[index] instanceof Map) {
        map = (Map<String, Object>) data[index];
    } else {
        map = new HashMap<>();
        int i = 0;
        for (ObjAttribute attribute : indexToEntity.get(index).getPrimaryKeys()) {
            map.put(attribute.getDbAttributeName(), data[index + i++]);
        }
    }
    return ExpressionFactory.matchAllDbExp(map, Expression.EQUAL_TO);
}
Also used : ObjAttribute(org.apache.cayenne.map.ObjAttribute) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

ObjAttribute (org.apache.cayenne.map.ObjAttribute)81 ObjEntity (org.apache.cayenne.map.ObjEntity)57 DbAttribute (org.apache.cayenne.map.DbAttribute)31 ObjRelationship (org.apache.cayenne.map.ObjRelationship)27 DbEntity (org.apache.cayenne.map.DbEntity)26 Test (org.junit.Test)26 DbRelationship (org.apache.cayenne.map.DbRelationship)19 DbJoin (org.apache.cayenne.map.DbJoin)12 DataMap (org.apache.cayenne.map.DataMap)9 ClassDescriptor (org.apache.cayenne.reflect.ClassDescriptor)9 HashMap (java.util.HashMap)8 CayenneRuntimeException (org.apache.cayenne.CayenneRuntimeException)8 AttributeProperty (org.apache.cayenne.reflect.AttributeProperty)8 PropertyVisitor (org.apache.cayenne.reflect.PropertyVisitor)8 ToManyProperty (org.apache.cayenne.reflect.ToManyProperty)8 ToOneProperty (org.apache.cayenne.reflect.ToOneProperty)8 ArrayList (java.util.ArrayList)7 DataChannelDescriptor (org.apache.cayenne.configuration.DataChannelDescriptor)7 HashSet (java.util.HashSet)6 Expression (org.apache.cayenne.exp.Expression)6