Search in sources :

Example 21 with ClassDescriptor

use of org.apache.cayenne.reflect.ClassDescriptor in project cayenne by apache.

the class DataObjectDescriptorFactory method createToManyCollectionProperty.

@Override
protected void createToManyCollectionProperty(PersistentDescriptor descriptor, ObjRelationship relationship) {
    ClassDescriptor targetDescriptor = descriptorMap.getDescriptor(relationship.getTargetEntityName());
    descriptor.addDeclaredProperty(new DataObjectToManyProperty(relationship, targetDescriptor, faultFactory.getCollectionFault()));
}
Also used : ClassDescriptor(org.apache.cayenne.reflect.ClassDescriptor)

Example 22 with ClassDescriptor

use of org.apache.cayenne.reflect.ClassDescriptor in project cayenne by apache.

the class DataObjectDescriptorFactory method createToManyMapProperty.

@Override
protected void createToManyMapProperty(PersistentDescriptor descriptor, ObjRelationship relationship) {
    ClassDescriptor targetDescriptor = descriptorMap.getDescriptor(relationship.getTargetEntityName());
    Accessor mapKeyAccessor = createMapKeyAccessor(relationship, targetDescriptor);
    descriptor.addDeclaredProperty(new DataObjectToManyMapProperty(relationship, targetDescriptor, faultFactory.getMapFault(mapKeyAccessor), mapKeyAccessor));
}
Also used : ClassDescriptor(org.apache.cayenne.reflect.ClassDescriptor) Accessor(org.apache.cayenne.reflect.Accessor)

Example 23 with ClassDescriptor

use of org.apache.cayenne.reflect.ClassDescriptor in project cayenne by apache.

the class ObjectStore method processInvalidatedIDs.

/**
 * @since 1.1
 */
void processInvalidatedIDs(Collection<ObjectId> invalidatedIDs) {
    if (invalidatedIDs != null && !invalidatedIDs.isEmpty()) {
        for (ObjectId oid : invalidatedIDs) {
            DataObject object = (DataObject) getNode(oid);
            if (object == null) {
                continue;
            }
            switch(object.getPersistenceState()) {
                case PersistenceState.COMMITTED:
                    object.setPersistenceState(PersistenceState.HOLLOW);
                    break;
                case PersistenceState.MODIFIED:
                    DataContext context = (DataContext) object.getObjectContext();
                    DataRow diff = getSnapshot(oid);
                    // consult delegate if it exists
                    DataContextDelegate delegate = context.nonNullDelegate();
                    if (delegate.shouldMergeChanges(object, diff)) {
                        ClassDescriptor descriptor = context.getEntityResolver().getClassDescriptor(oid.getEntityName());
                        DataRowUtils.forceMergeWithSnapshot(context, descriptor, object, diff);
                        delegate.finishedMergeChanges(object);
                    }
                case PersistenceState.HOLLOW:
                    // do nothing
                    break;
                case PersistenceState.DELETED:
                    // TODO: Do nothing? Or treat as merged?
                    break;
            }
        }
    }
}
Also used : DataObject(org.apache.cayenne.DataObject) ClassDescriptor(org.apache.cayenne.reflect.ClassDescriptor) ObjectId(org.apache.cayenne.ObjectId) DataRow(org.apache.cayenne.DataRow)

Example 24 with ClassDescriptor

use of org.apache.cayenne.reflect.ClassDescriptor in project cayenne by apache.

the class PrefetchProcessorJointNode method buildRowMapping.

/**
 * Configures row columns mapping for this node entity.
 */
private void buildRowMapping() {
    final Map<String, ColumnDescriptor> targetSource = new TreeMap<>();
    // build a DB path .. find parent node that terminates the joint group...
    PrefetchTreeNode jointRoot = this;
    while (jointRoot.getParent() != null && !jointRoot.isDisjointPrefetch() && !jointRoot.isDisjointByIdPrefetch()) {
        jointRoot = jointRoot.getParent();
    }
    final String prefix;
    if (jointRoot != this) {
        Expression objectPath = ExpressionFactory.exp(getPath(jointRoot));
        ASTPath translated = (ASTPath) ((PrefetchProcessorNode) jointRoot).getResolver().getEntity().translateToDbPath(objectPath);
        // make sure we do not include "db:" prefix
        prefix = translated.getOperand(0) + ".";
    } else {
        prefix = "";
    }
    if (getParent() != null && !getParent().isPhantom() && getIncoming() != null && !getIncoming().getRelationship().isFlattened()) {
        DbRelationship r = getIncoming().getRelationship().getDbRelationships().get(0);
        for (final DbJoin join : r.getJoins()) {
            appendColumn(targetSource, join.getTargetName(), prefix + join.getTargetName());
        }
    }
    ClassDescriptor descriptor = resolver.getDescriptor();
    descriptor.visitAllProperties(new PropertyVisitor() {

        public boolean visitAttribute(AttributeProperty property) {
            String target = property.getAttribute().getDbAttributePath();
            if (!property.getAttribute().isLazy()) {
                appendColumn(targetSource, target, prefix + target);
            }
            return true;
        }

        public boolean visitToMany(ToManyProperty property) {
            return visitRelationship(property);
        }

        public boolean visitToOne(ToOneProperty property) {
            return visitRelationship(property);
        }

        private boolean visitRelationship(ArcProperty arc) {
            DbRelationship dbRel = arc.getRelationship().getDbRelationships().get(0);
            for (DbAttribute attribute : dbRel.getSourceAttributes()) {
                String target = attribute.getName();
                appendColumn(targetSource, target, prefix + target);
            }
            return true;
        }
    });
    // append id columns ... (some may have been appended already via relationships)
    for (String pkName : descriptor.getEntity().getPrimaryKeyNames()) {
        appendColumn(targetSource, pkName, prefix + pkName);
    }
    // append inheritance discriminator columns...
    for (ObjAttribute column : descriptor.getDiscriminatorColumns()) {
        String target = column.getDbAttributePath();
        appendColumn(targetSource, target, prefix + target);
    }
    int size = targetSource.size();
    this.rowCapacity = (int) Math.ceil(size / 0.75);
    this.columns = new ColumnDescriptor[size];
    targetSource.values().toArray(columns);
}
Also used : ArcProperty(org.apache.cayenne.reflect.ArcProperty) ClassDescriptor(org.apache.cayenne.reflect.ClassDescriptor) ObjAttribute(org.apache.cayenne.map.ObjAttribute) ColumnDescriptor(org.apache.cayenne.access.jdbc.ColumnDescriptor) DbAttribute(org.apache.cayenne.map.DbAttribute) TreeMap(java.util.TreeMap) AttributeProperty(org.apache.cayenne.reflect.AttributeProperty) ToOneProperty(org.apache.cayenne.reflect.ToOneProperty) ToManyProperty(org.apache.cayenne.reflect.ToManyProperty) Expression(org.apache.cayenne.exp.Expression) PrefetchTreeNode(org.apache.cayenne.query.PrefetchTreeNode) DbRelationship(org.apache.cayenne.map.DbRelationship) DbJoin(org.apache.cayenne.map.DbJoin) ASTPath(org.apache.cayenne.exp.parser.ASTPath) PropertyVisitor(org.apache.cayenne.reflect.PropertyVisitor)

Example 25 with ClassDescriptor

use of org.apache.cayenne.reflect.ClassDescriptor in project cayenne by apache.

the class PrefetchProcessorJointNode method rowFromFlatRow.

/**
 * Returns a DataRow from the flat row.
 */
DataRow rowFromFlatRow(DataRow flatRow) {
    DataRow row = new DataRow(rowCapacity);
    // extract subset of flat row columns, recasting to the target keys
    for (ColumnDescriptor column : columns) {
        row.put(column.getName(), flatRow.get(column.getDataRowKey()));
    }
    // since JDBC row reader won't inject JOINED entity name, we have to detect it here...
    ClassDescriptor descriptor = resolver.getDescriptor();
    ObjEntity entity = descriptor.getEntityInheritanceTree().entityMatchingRow(row);
    row.setEntityName(entity == null ? null : entity.getName());
    return row;
}
Also used : ObjEntity(org.apache.cayenne.map.ObjEntity) ClassDescriptor(org.apache.cayenne.reflect.ClassDescriptor) ColumnDescriptor(org.apache.cayenne.access.jdbc.ColumnDescriptor) DataRow(org.apache.cayenne.DataRow)

Aggregations

ClassDescriptor (org.apache.cayenne.reflect.ClassDescriptor)90 AttributeProperty (org.apache.cayenne.reflect.AttributeProperty)25 PropertyVisitor (org.apache.cayenne.reflect.PropertyVisitor)23 ToManyProperty (org.apache.cayenne.reflect.ToManyProperty)23 ToOneProperty (org.apache.cayenne.reflect.ToOneProperty)23 ObjEntity (org.apache.cayenne.map.ObjEntity)22 ArcProperty (org.apache.cayenne.reflect.ArcProperty)21 Persistent (org.apache.cayenne.Persistent)20 CayenneRuntimeException (org.apache.cayenne.CayenneRuntimeException)16 PropertyDescriptor (org.apache.cayenne.reflect.PropertyDescriptor)15 DbRelationship (org.apache.cayenne.map.DbRelationship)14 ObjRelationship (org.apache.cayenne.map.ObjRelationship)13 DataRow (org.apache.cayenne.DataRow)12 ObjectId (org.apache.cayenne.ObjectId)12 Test (org.junit.Test)12 DbAttribute (org.apache.cayenne.map.DbAttribute)10 DataObject (org.apache.cayenne.DataObject)9 EJBQLException (org.apache.cayenne.ejbql.EJBQLException)9 DbJoin (org.apache.cayenne.map.DbJoin)9 ArrayList (java.util.ArrayList)8