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();
}
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));
}
Aggregations