Search in sources :

Example 1 with TraversalHelper

use of org.apache.cayenne.exp.TraversalHelper in project cayenne by apache.

the class PersistentDescriptorFactory method indexQualifiers.

protected void indexQualifiers(final PersistentDescriptor descriptor, EntityInheritanceTree inheritanceTree) {
    Expression qualifier;
    if (inheritanceTree != null) {
        qualifier = inheritanceTree.qualifierForEntityAndSubclasses();
    } else {
        qualifier = descriptor.getEntity().getDeclaredQualifier();
    }
    if (qualifier != null) {
        // using map instead of a Set to collect attributes, as
        // ObjEntity.getAttribute may return a decorator for attribute on
        // each call, resulting in dupes
        final Map<String, ObjAttribute> attributes = new HashMap<>();
        final DbEntity dbEntity = descriptor.getEntity().getDbEntity();
        qualifier.traverse(new TraversalHelper() {

            @Override
            public void startNode(Expression node, Expression parentNode) {
                if (node.getType() == Expression.DB_PATH) {
                    String path = node.getOperand(0).toString();
                    final DbAttribute attribute = dbEntity.getAttribute(path);
                    if (attribute != null) {
                        ObjAttribute objectAttribute = descriptor.getEntity().getAttributeForDbAttribute(attribute);
                        if (objectAttribute == null) {
                            objectAttribute = new ObjAttribute(attribute.getName()) {

                                @Override
                                public DbAttribute getDbAttribute() {
                                    return attribute;
                                }
                            };
                            // we semi-officially DO NOT support inheritance
                            // descriptors based on related entities, so
                            // here we
                            // assume that DbAttribute is rooted in the root
                            // DbEntity, and no relationship is involved.
                            objectAttribute.setDbAttributePath(attribute.getName());
                            objectAttribute.setType(TypesMapping.getJavaBySqlType(attribute.getType()));
                        }
                        attributes.put(objectAttribute.getName(), objectAttribute);
                    }
                } else if (node.getType() == Expression.OBJ_PATH) {
                    String path = node.getOperand(0).toString();
                    ObjAttribute attribute = descriptor.getEntity().getAttribute(path);
                    attributes.put(path, attribute);
                }
            }
        });
        descriptor.setDiscriminatorColumns(attributes.values());
        descriptor.setEntityQualifier(qualifier);
    }
}
Also used : ObjAttribute(org.apache.cayenne.map.ObjAttribute) DbEntity(org.apache.cayenne.map.DbEntity) Expression(org.apache.cayenne.exp.Expression) HashMap(java.util.HashMap) TraversalHelper(org.apache.cayenne.exp.TraversalHelper) DbAttribute(org.apache.cayenne.map.DbAttribute)

Example 2 with TraversalHelper

use of org.apache.cayenne.exp.TraversalHelper in project cayenne by apache.

the class DefaultSelectTranslator method isAggregate.

private boolean isAggregate(Property<?> property) {
    final boolean[] isAggregate = new boolean[1];
    Expression exp = property.getExpression();
    exp.traverse(new TraversalHelper() {

        @Override
        public void startNode(Expression node, Expression parentNode) {
            if (node instanceof ASTAggregateFunctionCall) {
                isAggregate[0] = true;
            }
        }
    });
    return isAggregate[0];
}
Also used : Expression(org.apache.cayenne.exp.Expression) TraversalHelper(org.apache.cayenne.exp.TraversalHelper) ASTAggregateFunctionCall(org.apache.cayenne.exp.parser.ASTAggregateFunctionCall)

Aggregations

Expression (org.apache.cayenne.exp.Expression)2 TraversalHelper (org.apache.cayenne.exp.TraversalHelper)2 HashMap (java.util.HashMap)1 ASTAggregateFunctionCall (org.apache.cayenne.exp.parser.ASTAggregateFunctionCall)1 DbAttribute (org.apache.cayenne.map.DbAttribute)1 DbEntity (org.apache.cayenne.map.DbEntity)1 ObjAttribute (org.apache.cayenne.map.ObjAttribute)1