Search in sources :

Example 1 with ASTList

use of org.apache.cayenne.exp.parser.ASTList in project cayenne by apache.

the class DataMapUtils method getParameterNames.

private Map<String, String> getParameterNames(Expression expression, Object root) {
    if (expression != null) {
        Map<String, String> types = new HashMap<>();
        String typeName = "";
        List<String> names = new LinkedList<>();
        for (int i = 0; i < expression.getOperandCount(); i++) {
            Object operand = expression.getOperand(i);
            if (operand instanceof Expression) {
                types.putAll(getParameterNames((Expression) operand, root));
            }
            if (operand instanceof ASTObjPath) {
                PathComponent<ObjAttribute, ObjRelationship> component = ((Entity) root).lastPathComponent((ASTObjPath) operand, null);
                ObjAttribute attribute = component.getAttribute();
                if (attribute != null) {
                    typeName = attribute.getType();
                } else {
                    ObjRelationship relationship = component.getRelationship();
                    if (relationship != null) {
                        typeName = relationship.getTargetEntity().getClassName();
                    } else {
                        typeName = "Object";
                    }
                }
            }
            if (operand instanceof ASTList) {
                Object[] values = (Object[]) ((ASTList) operand).getOperand(0);
                for (Object value : values) {
                    if (value instanceof ExpressionParameter) {
                        names.add(((ExpressionParameter) value).getName());
                    }
                }
            }
            if (operand instanceof ExpressionParameter) {
                names.add(((ExpressionParameter) operand).getName());
            }
        }
        for (String name : names) {
            types.put(Util.underscoredToJava(name, false), typeName);
        }
        return types;
    }
    return Collections.emptyMap();
}
Also used : ASTObjPath(org.apache.cayenne.exp.parser.ASTObjPath) ObjEntity(org.apache.cayenne.map.ObjEntity) Entity(org.apache.cayenne.map.Entity) ObjRelationship(org.apache.cayenne.map.ObjRelationship) ObjAttribute(org.apache.cayenne.map.ObjAttribute) HashMap(java.util.HashMap) LinkedList(java.util.LinkedList) Expression(org.apache.cayenne.exp.Expression) ExpressionParameter(org.apache.cayenne.exp.ExpressionParameter) ASTList(org.apache.cayenne.exp.parser.ASTList)

Example 2 with ASTList

use of org.apache.cayenne.exp.parser.ASTList in project cayenne by apache.

the class Expression_ParamsTest method testNoParams3.

@Test
public void testNoParams3() throws Exception {
    List list = new ArrayList();
    list.add(ExpressionFactory.matchExp("k1", new ExpressionParameter("test1")));
    list.add(ExpressionFactory.matchExp("k2", new ExpressionParameter("test2")));
    list.add(ExpressionFactory.matchExp("k3", new ExpressionParameter("test3")));
    list.add(ExpressionFactory.matchExp("k4", new ExpressionParameter("test4")));
    Expression e1 = ExpressionFactory.joinExp(Expression.OR, list);
    Map params = new HashMap();
    params.put("test4", "123");
    Expression e2 = e1.params(params, true);
    // some expression nodes must be pruned
    assertNotNull(e2);
    assertTrue("List expression: " + e2, !(e2 instanceof ASTList));
    assertEquals(2, e2.getOperandCount());
    assertEquals("123", e2.getOperand(1));
    assertEquals("k4", ((Expression) e2.getOperand(0)).getOperand(0));
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ASTList(org.apache.cayenne.exp.parser.ASTList) ArrayList(java.util.ArrayList) List(java.util.List) ASTList(org.apache.cayenne.exp.parser.ASTList) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Aggregations

HashMap (java.util.HashMap)2 ASTList (org.apache.cayenne.exp.parser.ASTList)2 ArrayList (java.util.ArrayList)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 Map (java.util.Map)1 Expression (org.apache.cayenne.exp.Expression)1 ExpressionParameter (org.apache.cayenne.exp.ExpressionParameter)1 ASTObjPath (org.apache.cayenne.exp.parser.ASTObjPath)1 Entity (org.apache.cayenne.map.Entity)1 ObjAttribute (org.apache.cayenne.map.ObjAttribute)1 ObjEntity (org.apache.cayenne.map.ObjEntity)1 ObjRelationship (org.apache.cayenne.map.ObjRelationship)1 Test (org.junit.Test)1