Search in sources :

Example 1 with MetaAttributePath

use of io.crnk.meta.model.MetaAttributePath in project crnk-framework by crnk-project.

the class JoinRegistry method getEntityAttribute.

public E getEntityAttribute(MetaAttributePath attrPath) {
    MetaAttributePath associationPath = extractAssociationPath(attrPath);
    MetaAttributePath primitivePath = attrPath.subPath(associationPath.length());
    @SuppressWarnings("unchecked") E from = (E) getOrCreateJoin(associationPath);
    if (primitivePath.length() == 0) {
        return from;
    }
    MetaAttributePath currentPath = associationPath;
    E criteriaPath = null;
    for (MetaAttribute pathElement : primitivePath) {
        currentPath = currentPath.concat(pathElement);
        E currentCriteriaPath = criteriaPath != null ? criteriaPath : from;
        if (pathElement instanceof MetaMapAttribute) {
            if (criteriaPath != null)
                throw new IllegalStateException("Cannot join to map");
            criteriaPath = joinMap(currentCriteriaPath, pathElement);
        } else {
            // we may need to downcast if attribute is defined on a subtype
            MetaDataObject parent = pathElement.getParent().asDataObject();
            Class<?> pathType = parent.getImplementationClass();
            Class<?> currentType = backend.getJavaElementType(currentCriteriaPath);
            boolean isSubType = !pathType.isAssignableFrom(currentType);
            if (isSubType) {
                currentCriteriaPath = backend.joinSubType(currentCriteriaPath, pathType);
            }
            criteriaPath = backend.getAttribute(currentCriteriaPath, pathElement);
        }
    }
    return criteriaPath;
}
Also used : MetaMapAttribute(io.crnk.meta.model.MetaMapAttribute) MetaDataObject(io.crnk.meta.model.MetaDataObject) MetaAttribute(io.crnk.meta.model.MetaAttribute) MetaAttributePath(io.crnk.meta.model.MetaAttributePath)

Example 2 with MetaAttributePath

use of io.crnk.meta.model.MetaAttributePath in project crnk-framework by crnk-project.

the class EntityGraphBuilderImpl method applyFetchPaths.

private <T> Subgraph<Object> applyFetchPaths(EntityGraph<T> graph, MetaAttributePath fetchPath) {
    if (fetchPath.length() >= 2) {
        // ensure parent is fetched
        MetaAttributePath parentPath = fetchPath.subPath(0, fetchPath.length() - 1);
        Subgraph<Object> parentGraph = applyFetchPaths(graph, parentPath);
        return parentGraph.addSubgraph(fetchPath.toString());
    } else {
        return graph.addSubgraph(fetchPath.toString());
    }
}
Also used : MetaAttributePath(io.crnk.meta.model.MetaAttributePath)

Example 3 with MetaAttributePath

use of io.crnk.meta.model.MetaAttributePath in project crnk-framework by crnk-project.

the class EntityGraphBuilderImpl method build.

@Override
public <T> void build(EntityManager em, Query criteriaQuery, Class<T> entityClass, Set<MetaAttributePath> fetchPaths) {
    EntityGraph<T> graph = em.createEntityGraph(entityClass);
    for (MetaAttributePath fetchPath : fetchPaths) {
        applyFetchPaths(graph, fetchPath);
    }
    criteriaQuery.setHint("javax.persistence.fetchgraph", graph);
}
Also used : MetaAttributePath(io.crnk.meta.model.MetaAttributePath)

Example 4 with MetaAttributePath

use of io.crnk.meta.model.MetaAttributePath in project crnk-framework by crnk-project.

the class QueryFilterBuilder method filterSimpleOperation.

private P filterSimpleOperation(FilterSpec fs, MetaDataObject rootMeta) {
    Object value = fs.getValue();
    if (value instanceof Set) {
        // HashSet not properly supported in ORM/JDBC, convert to
        // list
        Set<?> set = (Set<?>) value;
        value = new ArrayList<Object>(set);
    }
    MetaAttributePath path = rootMeta.resolvePath(fs.getAttributePath(), attributeFinder);
    path = enhanceAttributePath(path, value);
    return backend.buildPredicate(fs.getOperator(), path, value);
}
Also used : Set(java.util.Set) MetaDataObject(io.crnk.meta.model.MetaDataObject) AnyTypeObject(io.crnk.jpa.query.AnyTypeObject) MetaAttributePath(io.crnk.meta.model.MetaAttributePath)

Example 5 with MetaAttributePath

use of io.crnk.meta.model.MetaAttributePath in project crnk-framework by crnk-project.

the class JoinRegistry method getOrCreateJoin.

private F getOrCreateJoin(MetaAttributePath srcPath, MetaAttribute targetAttr) {
    MetaAttributePath path = srcPath.concat(targetAttr);
    F parent = joinMap.get(srcPath);
    F join = joinMap.get(path);
    if (join == null) {
        JoinType joinType = query.getJoinType(path);
        join = backend.doJoin(targetAttr, joinType, parent);
        joinMap.put(path, join);
    }
    return join;
}
Also used : JoinType(javax.persistence.criteria.JoinType) MetaAttributePath(io.crnk.meta.model.MetaAttributePath)

Aggregations

MetaAttributePath (io.crnk.meta.model.MetaAttributePath)7 MetaDataObject (io.crnk.meta.model.MetaDataObject)3 MetaAttribute (io.crnk.meta.model.MetaAttribute)2 IncludeFieldSpec (io.crnk.core.queryspec.IncludeFieldSpec)1 AnyTypeObject (io.crnk.jpa.query.AnyTypeObject)1 MetaMapAttribute (io.crnk.meta.model.MetaMapAttribute)1 HashMap (java.util.HashMap)1 Set (java.util.Set)1 JoinType (javax.persistence.criteria.JoinType)1