Search in sources :

Example 1 with ExpressionException

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

the class DataMapUtils method isValidParameterNames.

public Boolean isValidParameterNames(SelectQueryDescriptor query) {
    if (query.getQualifier() == null) {
        return true;
    }
    Map<String, String> queryParameters = queriesMap.get(query.getName());
    if (queryParameters == null) {
        try {
            queryParameters = getParameterNames(query.getQualifier(), query.getRoot());
        } catch (Exception e) {
            // if we have wrong path in queryParameters return false.
            return false;
        }
    }
    for (Ordering ordering : query.getOrderings()) {
        // validate paths in ordering
        String path = ordering.getSortSpecString();
        Iterator<CayenneMapEntry> it = ((ObjEntity) query.getRoot()).resolvePathComponents(path);
        while (it.hasNext()) {
            try {
                it.next();
            } catch (ExpressionException e) {
                // if we have wrong path in orderings return false.
                return false;
            }
        }
    }
    return true;
}
Also used : CayenneMapEntry(org.apache.cayenne.util.CayenneMapEntry) ObjEntity(org.apache.cayenne.map.ObjEntity) Ordering(org.apache.cayenne.query.Ordering) ExpressionException(org.apache.cayenne.exp.ExpressionException) ExpressionException(org.apache.cayenne.exp.ExpressionException)

Example 2 with ExpressionException

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

the class DefaultSelectTranslatorIT method testCreateSqlString13.

@Test
public void testCreateSqlString13() throws Exception {
    // query with invalid joint prefetches
    SelectQuery q = new SelectQuery(Painting.class);
    q.addPrefetch("invalid.invalid").setSemantics(PrefetchTreeNode.JOINT_PREFETCH_SEMANTICS);
    try {
        new DefaultSelectTranslator(q, dataNode.getAdapter(), dataNode.getEntityResolver()).getSql();
        fail("Invalid jointPrefetch must have thrown...");
    } catch (ExpressionException e) {
    // expected
    }
}
Also used : SelectQuery(org.apache.cayenne.query.SelectQuery) ExpressionException(org.apache.cayenne.exp.ExpressionException) Test(org.junit.Test)

Example 3 with ExpressionException

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

the class EOModelProcessor method makeFlatRelationships.

/**
 * Create Flattened ObjRelationships of the specified entity.
 */
protected void makeFlatRelationships(EOModelHelper helper, ObjEntity e) {
    Map info = helper.entityPListMap(e.getName());
    List rinfo = (List) info.get("relationships");
    if (rinfo == null) {
        return;
    }
    Iterator it = rinfo.iterator();
    while (it.hasNext()) {
        Map relMap = (Map) it.next();
        String targetPath = (String) relMap.get("definition");
        // ignore normal relationships
        if (targetPath == null) {
            continue;
        }
        ObjRelationship flatRel = new ObjRelationship();
        flatRel.setName((String) relMap.get("name"));
        flatRel.setSourceEntity(e);
        try {
            flatRel.setDbRelationshipPath(targetPath);
        } catch (ExpressionException ex) {
            logger.warn("Invalid relationship: " + targetPath);
            continue;
        }
        // find target entity
        Map entityInfo = info;
        StringTokenizer toks = new StringTokenizer(targetPath, ".");
        while (toks.hasMoreTokens() && entityInfo != null) {
            String pathComponent = toks.nextToken();
            // get relationship info and reset entityInfo, so that we could
            // use
            // entityInfo state as an indicator of valid flat relationship
            // enpoint
            // outside the loop
            Collection relationshipInfo = (Collection) entityInfo.get("relationships");
            entityInfo = null;
            if (relationshipInfo == null) {
                break;
            }
            Iterator rit = relationshipInfo.iterator();
            while (rit.hasNext()) {
                Map pathRelationship = (Map) rit.next();
                if (pathComponent.equals(pathRelationship.get("name"))) {
                    String targetName = (String) pathRelationship.get("destination");
                    entityInfo = helper.entityPListMap(targetName);
                    break;
                }
            }
        }
        if (entityInfo != null) {
            flatRel.setTargetEntityName((String) entityInfo.get("name"));
        }
        e.addRelationship(flatRel);
    }
}
Also used : ObjRelationship(org.apache.cayenne.map.ObjRelationship) StringTokenizer(java.util.StringTokenizer) Iterator(java.util.Iterator) Collection(java.util.Collection) ArrayList(java.util.ArrayList) List(java.util.List) Map(java.util.Map) DataMap(org.apache.cayenne.map.DataMap) ExpressionException(org.apache.cayenne.exp.ExpressionException)

Example 4 with ExpressionException

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

the class ObjRelationship method getValidRelationshipPath.

/**
 * Returns dot-separated path over DbRelationships, only including
 * components that have valid DbRelationships.
 */
String getValidRelationshipPath() {
    String path = getDbRelationshipPath();
    if (path == null) {
        return null;
    }
    ObjEntity entity = (ObjEntity) getSourceEntity();
    if (entity == null) {
        throw new CayenneRuntimeException("Can't resolve DbRelationships, null source ObjEntity");
    }
    DbEntity dbEntity = entity.getDbEntity();
    if (dbEntity == null) {
        return null;
    }
    StringBuilder validPath = new StringBuilder();
    try {
        for (PathComponent<DbAttribute, DbRelationship> pathComponent : dbEntity.resolvePath(new ASTDbPath(path), Collections.emptyMap())) {
            if (validPath.length() > 0) {
                validPath.append(Entity.PATH_SEPARATOR);
            }
            validPath.append(pathComponent.getName());
        }
    } catch (ExpressionException ex) {
    }
    return validPath.toString();
}
Also used : ToStringBuilder(org.apache.cayenne.util.ToStringBuilder) ASTDbPath(org.apache.cayenne.exp.parser.ASTDbPath) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException) ExpressionException(org.apache.cayenne.exp.ExpressionException)

Example 5 with ExpressionException

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

the class ObjRelationship method refreshFromPath.

/**
 * Rebuild a list of relationships if String relationshipPath has changed.
 */
final void refreshFromPath(String dbRelationshipPath, boolean stripInvalid) {
    // remove existing relationships
    dbRelationships.clear();
    if (dbRelationshipPath != null) {
        ObjEntity entity = (ObjEntity) getSourceEntity();
        if (entity == null) {
            throw new CayenneRuntimeException("Can't resolve DbRelationships, null source ObjEntity");
        }
        try {
            // add new relationships from path
            Iterator<CayenneMapEntry> it = entity.resolvePathComponents(new ASTDbPath(dbRelationshipPath));
            while (it.hasNext()) {
                DbRelationship relationship = (DbRelationship) it.next();
                dbRelationships.add(relationship);
            }
        } catch (ExpressionException ex) {
            if (!stripInvalid) {
                throw ex;
            }
        }
    }
    recalculateToManyValue();
    recalculateReadOnlyValue();
}
Also used : CayenneMapEntry(org.apache.cayenne.util.CayenneMapEntry) ASTDbPath(org.apache.cayenne.exp.parser.ASTDbPath) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException) 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