Search in sources :

Example 21 with CayenneMapEntry

use of org.apache.cayenne.util.CayenneMapEntry in project cayenne by apache.

the class ObjAttributeInfoDialog method setSelectionPath.

/**
 * Selects path in browser
 */
public void setSelectionPath() {
    List<CayenneMapEntry> list = new ArrayList<>();
    boolean isAttributeLast = false;
    Iterator<CayenneMapEntry> it = attribute.getDbPathIterator();
    while (it.hasNext()) {
        CayenneMapEntry obj = it.next();
        list.add(obj);
        if (obj instanceof DbAttribute && !it.hasNext()) {
            isAttributeLast = true;
        }
    }
    if (isAttributeLast) {
        Object[] path = new Object[list.size() + 1];
        path[0] = getFirstEntity();
        System.arraycopy(list.toArray(), 0, path, 1, list.size());
        view.getPathBrowser().setSelectionPath(new TreePath(path));
        view.getSaveButton().setEnabled(true);
    }
}
Also used : CayenneMapEntry(org.apache.cayenne.util.CayenneMapEntry) TreePath(javax.swing.tree.TreePath) ArrayList(java.util.ArrayList) DbAttribute(org.apache.cayenne.map.DbAttribute)

Example 22 with CayenneMapEntry

use of org.apache.cayenne.util.CayenneMapEntry in project cayenne by apache.

the class ObjAttributeInfoDialog method getFirstEntity.

private Entity getFirstEntity() {
    Iterator<CayenneMapEntry> it = attribute.getDbPathIterator();
    Entity firstEnt = attribute.getDbAttribute().getEntity();
    boolean setEnt = false;
    while (it.hasNext()) {
        Object ob = it.next();
        if (ob instanceof DbRelationship) {
            if (!setEnt) {
                firstEnt = ((DbRelationship) ob).getSourceEntity();
                setEnt = true;
            }
        } else if (ob instanceof DbAttribute) {
            if (!setEnt) {
                firstEnt = ((DbAttribute) ob).getEntity();
            }
        }
    }
    return firstEnt;
}
Also used : ObjEntity(org.apache.cayenne.map.ObjEntity) Entity(org.apache.cayenne.map.Entity) DbEntity(org.apache.cayenne.map.DbEntity) CayenneMapEntry(org.apache.cayenne.util.CayenneMapEntry) DbRelationship(org.apache.cayenne.map.DbRelationship) DbAttribute(org.apache.cayenne.map.DbAttribute)

Example 23 with CayenneMapEntry

use of org.apache.cayenne.util.CayenneMapEntry in project cayenne by apache.

the class SelectQueryMetadata method buildEntityResultForColumn.

/**
 * Collect metadata for column that will be unwrapped to full entity in the final SQL
 * (possibly including joint prefetch).
 * This information will be used to correctly create Persistent object back from raw result.
 *
 * This method is actually repeating logic of
 * {@link org.apache.cayenne.access.translator.select.DefaultSelectTranslator#appendQueryColumns}.
 * Here we don't care about intermediate joins and few other things so it's shorter.
 * Logic of these methods should be unified and simplified, possibly to a single source of metadata,
 * generated only once and used everywhere.
 *
 * @param query original query
 * @param column full object column
 * @param resolver entity resolver to get ObjEntity and ClassDescriptor
 * @return Entity result
 */
private EntityResult buildEntityResultForColumn(SelectQuery<?> query, Property<?> column, EntityResolver resolver) {
    final EntityResult result = new EntityResult(column.getType());
    // Collecting visitor for ObjAttributes and toOne relationships
    PropertyVisitor visitor = new PropertyVisitor() {

        public boolean visitAttribute(AttributeProperty property) {
            ObjAttribute oa = property.getAttribute();
            Iterator<CayenneMapEntry> dbPathIterator = oa.getDbPathIterator();
            while (dbPathIterator.hasNext()) {
                CayenneMapEntry pathPart = dbPathIterator.next();
                if (pathPart instanceof DbAttribute) {
                    result.addDbField(pathPart.getName(), pathPart.getName());
                }
            }
            return true;
        }

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

        public boolean visitToOne(ToOneProperty property) {
            DbRelationship dbRel = property.getRelationship().getDbRelationships().get(0);
            List<DbJoin> joins = dbRel.getJoins();
            for (DbJoin join : joins) {
                if (!join.getSource().isPrimaryKey()) {
                    result.addDbField(join.getSource().getName(), join.getSource().getName());
                }
            }
            return true;
        }
    };
    ObjEntity oe = resolver.getObjEntity(column.getType());
    DbEntity table = oe.getDbEntity();
    // Additionally collect PKs
    for (DbAttribute dba : table.getPrimaryKeys()) {
        result.addDbField(dba.getName(), dba.getName());
    }
    ClassDescriptor descriptor = resolver.getClassDescriptor(oe.getName());
    descriptor.visitAllProperties(visitor);
    // Collection columns for joint prefetch
    if (query.getPrefetchTree() != null) {
        for (PrefetchTreeNode prefetch : query.getPrefetchTree().adjacentJointNodes()) {
            // for each prefetch add columns from the target entity
            Expression prefetchExp = ExpressionFactory.exp(prefetch.getPath());
            ASTDbPath dbPrefetch = (ASTDbPath) oe.translateToDbPath(prefetchExp);
            DbRelationship r = findRelationByPath(table, dbPrefetch);
            if (r == null) {
                throw new CayenneRuntimeException("Invalid joint prefetch '%s' for entity: %s", prefetch, oe.getName());
            }
            // go via target OE to make sure that Java types are mapped correctly...
            ObjRelationship targetRel = (ObjRelationship) prefetchExp.evaluate(oe);
            ObjEntity targetEntity = targetRel.getTargetEntity();
            prefetch.setEntityName(targetRel.getSourceEntity().getName());
            String labelPrefix = dbPrefetch.getPath();
            Set<String> seenNames = new HashSet<>();
            for (ObjAttribute oa : targetEntity.getAttributes()) {
                Iterator<CayenneMapEntry> dbPathIterator = oa.getDbPathIterator();
                while (dbPathIterator.hasNext()) {
                    Object pathPart = dbPathIterator.next();
                    if (pathPart instanceof DbAttribute) {
                        DbAttribute attribute = (DbAttribute) pathPart;
                        if (seenNames.add(attribute.getName())) {
                            result.addDbField(labelPrefix + '.' + attribute.getName(), labelPrefix + '.' + attribute.getName());
                        }
                    }
                }
            }
            // append remaining target attributes such as keys
            DbEntity targetDbEntity = r.getTargetEntity();
            for (DbAttribute attribute : targetDbEntity.getAttributes()) {
                if (seenNames.add(attribute.getName())) {
                    result.addDbField(labelPrefix + '.' + attribute.getName(), labelPrefix + '.' + attribute.getName());
                }
            }
        }
    }
    return result;
}
Also used : ObjRelationship(org.apache.cayenne.map.ObjRelationship) ObjAttribute(org.apache.cayenne.map.ObjAttribute) ClassDescriptor(org.apache.cayenne.reflect.ClassDescriptor) ASTDbPath(org.apache.cayenne.exp.parser.ASTDbPath) DbAttribute(org.apache.cayenne.map.DbAttribute) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException) EntityResult(org.apache.cayenne.map.EntityResult) AttributeProperty(org.apache.cayenne.reflect.AttributeProperty) ToOneProperty(org.apache.cayenne.reflect.ToOneProperty) CayenneMapEntry(org.apache.cayenne.util.CayenneMapEntry) ObjEntity(org.apache.cayenne.map.ObjEntity) ToManyProperty(org.apache.cayenne.reflect.ToManyProperty) DbEntity(org.apache.cayenne.map.DbEntity) Expression(org.apache.cayenne.exp.Expression) DbRelationship(org.apache.cayenne.map.DbRelationship) DbJoin(org.apache.cayenne.map.DbJoin) PropertyVisitor(org.apache.cayenne.reflect.PropertyVisitor) HashSet(java.util.HashSet)

Example 24 with CayenneMapEntry

use of org.apache.cayenne.util.CayenneMapEntry in project cayenne by apache.

the class ASTPath method evaluateEntityNode.

/**
 * Helper method to evaluate path expression with Cayenne Entity.
 */
protected CayenneMapEntry evaluateEntityNode(Entity entity) {
    Iterator<CayenneMapEntry> path = entity.resolvePathComponents(this);
    CayenneMapEntry next = null;
    while (path.hasNext()) {
        next = path.next();
    }
    return next;
}
Also used : CayenneMapEntry(org.apache.cayenne.util.CayenneMapEntry)

Aggregations

CayenneMapEntry (org.apache.cayenne.util.CayenneMapEntry)24 ObjEntity (org.apache.cayenne.map.ObjEntity)8 DbEntity (org.apache.cayenne.map.DbEntity)7 DbRelationship (org.apache.cayenne.map.DbRelationship)7 CayenneRuntimeException (org.apache.cayenne.CayenneRuntimeException)6 Expression (org.apache.cayenne.exp.Expression)6 DbAttribute (org.apache.cayenne.map.DbAttribute)6 ObjRelationship (org.apache.cayenne.map.ObjRelationship)5 ObjAttribute (org.apache.cayenne.map.ObjAttribute)4 ASTDbPath (org.apache.cayenne.exp.parser.ASTDbPath)3 Entity (org.apache.cayenne.map.Entity)3 HashSet (java.util.HashSet)2 ObjectId (org.apache.cayenne.ObjectId)2 ExpressionException (org.apache.cayenne.exp.ExpressionException)2 DbJoin (org.apache.cayenne.map.DbJoin)2 AttributeProperty (org.apache.cayenne.reflect.AttributeProperty)2 ClassDescriptor (org.apache.cayenne.reflect.ClassDescriptor)2 PropertyVisitor (org.apache.cayenne.reflect.PropertyVisitor)2 ToManyProperty (org.apache.cayenne.reflect.ToManyProperty)2 ToOneProperty (org.apache.cayenne.reflect.ToOneProperty)2