Search in sources :

Example 6 with ExpressionException

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

the class Ordering method compare.

/**
 * Comparable interface implementation. Can compare two Java Beans based on
 * the stored expression.
 */
@Override
public int compare(Object o1, Object o2) {
    Expression exp = getSortSpec();
    Object value1 = null;
    Object value2 = null;
    try {
        value1 = exp.evaluate(o1);
    } catch (ExpressionException e) {
        if (pathExceptionSuppressed && e.getCause() instanceof org.apache.cayenne.reflect.UnresolvablePathException) {
        // do nothing, we expect this
        } else {
            // re-throw
            throw e;
        }
    }
    try {
        value2 = exp.evaluate(o2);
    } catch (ExpressionException e) {
        if (pathExceptionSuppressed && e.getCause() instanceof org.apache.cayenne.reflect.UnresolvablePathException) {
        // do nothing, we expect this
        } else {
            // rethrow
            throw e;
        }
    }
    if (value1 == null && value2 == null) {
        return 0;
    } else if (value1 == null) {
        return nullSortedFirst ? -1 : 1;
    } else if (value2 == null) {
        return nullSortedFirst ? 1 : -1;
    }
    if (isCaseInsensitive()) {
        // TODO: to upper case should probably be defined as a separate
        // expression
        // type
        value1 = ConversionUtil.toUpperCase(value1);
        value2 = ConversionUtil.toUpperCase(value2);
    }
    int compareResult = ConversionUtil.toComparable(value1).compareTo(ConversionUtil.toComparable(value2));
    return (isAscending()) ? compareResult : -compareResult;
}
Also used : Expression(org.apache.cayenne.exp.Expression) ExpressionException(org.apache.cayenne.exp.ExpressionException)

Example 7 with ExpressionException

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

the class ASTNamedParameter method setValue.

@Override
public void setValue(Object value) {
    if (value == null) {
        throw new ExpressionException("Null Parameter value");
    }
    String name = value.toString().trim();
    if (name.length() == 0) {
        throw new ExpressionException("Empty Parameter value");
    }
    super.setValue(new ExpressionParameter(name));
}
Also used : ExpressionParameter(org.apache.cayenne.exp.ExpressionParameter) ExpressionException(org.apache.cayenne.exp.ExpressionException)

Example 8 with ExpressionException

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

the class PrefetchModel method isToMany.

private boolean isToMany(String prefetch) {
    // totally invalid path would result in ExpressionException
    try {
        Expression exp = ExpressionFactory.exp(prefetch);
        Object object = exp.evaluate(root);
        return object instanceof Relationship && ((Relationship) object).isToMany();
    } catch (ExpressionException e) {
        return false;
    }
}
Also used : Expression(org.apache.cayenne.exp.Expression) Relationship(org.apache.cayenne.map.Relationship) ExpressionException(org.apache.cayenne.exp.ExpressionException)

Example 9 with ExpressionException

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

the class EOQuery method initBindings.

private void initBindings(Map bindings, Entity entity, Map qualifier) {
    if (qualifier == null) {
        return;
    }
    if ("EOKeyValueQualifier".equals(qualifier.get("class"))) {
        String key = (String) qualifier.get("key");
        if (key == null) {
            return;
        }
        Object value = qualifier.get("value");
        if (!(value instanceof Map)) {
            return;
        }
        Map valueMap = (Map) value;
        if (!"EOQualifierVariable".equals(valueMap.get("class")) || !valueMap.containsKey("_key")) {
            return;
        }
        String name = (String) valueMap.get("_key");
        String className = null;
        // so we will use Object type for all DB path...
        try {
            Object lastObject = new ASTObjPath(key).evaluate(entity);
            if (lastObject instanceof ObjAttribute) {
                className = ((ObjAttribute) lastObject).getType();
            } else if (lastObject instanceof ObjRelationship) {
                ObjEntity target = ((ObjRelationship) lastObject).getTargetEntity();
                if (target != null) {
                    className = target.getClassName();
                }
            }
        } catch (ExpressionException ex) {
            className = "java.lang.Object";
        }
        if (className == null) {
            className = "java.lang.Object";
        }
        bindings.put(name, className);
        return;
    }
    List children = (List) qualifier.get("qualifiers");
    if (children != null) {
        Iterator it = children.iterator();
        while (it.hasNext()) {
            initBindings(bindings, entity, (Map) it.next());
        }
    }
}
Also used : ASTObjPath(org.apache.cayenne.exp.parser.ASTObjPath) ObjRelationship(org.apache.cayenne.map.ObjRelationship) ObjEntity(org.apache.cayenne.map.ObjEntity) ObjAttribute(org.apache.cayenne.map.ObjAttribute) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) ExpressionException(org.apache.cayenne.exp.ExpressionException)

Aggregations

ExpressionException (org.apache.cayenne.exp.ExpressionException)9 ArrayList (java.util.ArrayList)2 Iterator (java.util.Iterator)2 List (java.util.List)2 Map (java.util.Map)2 CayenneRuntimeException (org.apache.cayenne.CayenneRuntimeException)2 Expression (org.apache.cayenne.exp.Expression)2 ASTDbPath (org.apache.cayenne.exp.parser.ASTDbPath)2 ObjEntity (org.apache.cayenne.map.ObjEntity)2 ObjRelationship (org.apache.cayenne.map.ObjRelationship)2 CayenneMapEntry (org.apache.cayenne.util.CayenneMapEntry)2 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 StringTokenizer (java.util.StringTokenizer)1 ExpressionParameter (org.apache.cayenne.exp.ExpressionParameter)1 ASTObjPath (org.apache.cayenne.exp.parser.ASTObjPath)1 DataMap (org.apache.cayenne.map.DataMap)1 ObjAttribute (org.apache.cayenne.map.ObjAttribute)1 Relationship (org.apache.cayenne.map.Relationship)1 Ordering (org.apache.cayenne.query.Ordering)1