Search in sources :

Example 1 with JpaEntityDefinition

use of com.evolveum.midpoint.repo.sql.query2.definition.JpaEntityDefinition in project midpoint by Evolveum.

the class QueryInterpreter2 method findAndCreateRestriction.

private <T extends ObjectFilter> Restriction findAndCreateRestriction(@NotNull T filter, @NotNull InterpretationContext context, Restriction parent) throws QueryException {
    LOGGER.trace("Determining restriction for filter {}", filter);
    ItemPathResolver helper = context.getItemPathResolver();
    JpaEntityDefinition baseEntityDefinition;
    if (parent != null) {
        baseEntityDefinition = parent.getBaseHqlEntityForChildren().getJpaDefinition();
    } else {
        baseEntityDefinition = context.getRootEntityDefinition();
    }
    Restriction restriction = findAndCreateRestrictionInternal(filter, context, parent, helper, baseEntityDefinition);
    LOGGER.trace("Restriction for {} is {}", filter.getClass().getSimpleName(), restriction);
    return restriction;
}
Also used : ItemPathResolver(com.evolveum.midpoint.repo.sql.query2.resolution.ItemPathResolver)

Example 2 with JpaEntityDefinition

use of com.evolveum.midpoint.repo.sql.query2.definition.JpaEntityDefinition in project midpoint by Evolveum.

the class QueryInterpreter2 method addOrdering.

private void addOrdering(InterpretationContext context, ObjectOrdering ordering) throws QueryException {
    ItemPath orderByPath = ordering.getOrderBy();
    // TODO if we'd like to have order-by extension properties, we'd need to provide itemDefinition for them
    ProperDataSearchResult<JpaDataNodeDefinition> result = context.getItemPathResolver().findProperDataDefinition(context.getRootEntityDefinition(), orderByPath, null, JpaDataNodeDefinition.class, context.getPrismContext());
    if (result == null) {
        LOGGER.error("Unknown path '" + orderByPath + "', couldn't find definition for it, " + "list will not be ordered by it.");
        return;
    }
    JpaDataNodeDefinition targetDefinition = result.getLinkDefinition().getTargetDefinition();
    if (targetDefinition instanceof JpaAnyContainerDefinition) {
        throw new QueryException("Sorting based on extension item or attribute is not supported yet: " + orderByPath);
    } else if (targetDefinition instanceof JpaReferenceDefinition) {
        throw new QueryException("Sorting based on reference is not supported: " + orderByPath);
    } else if (result.getLinkDefinition().isMultivalued()) {
        throw new QueryException("Sorting based on multi-valued item is not supported: " + orderByPath);
    } else if (targetDefinition instanceof JpaEntityDefinition) {
        throw new QueryException("Sorting based on entity is not supported: " + orderByPath);
    } else if (!(targetDefinition instanceof JpaPropertyDefinition)) {
        throw new IllegalStateException("Unknown item definition type: " + result.getClass());
    }
    JpaEntityDefinition baseEntityDefinition = result.getEntityDefinition();
    JpaPropertyDefinition orderByDefinition = (JpaPropertyDefinition) targetDefinition;
    String hqlPropertyPath = context.getItemPathResolver().resolveItemPath(orderByPath, null, context.getPrimaryEntityAlias(), baseEntityDefinition, true).getHqlPath();
    if (RPolyString.class.equals(orderByDefinition.getJpaClass())) {
        hqlPropertyPath += ".orig";
    }
    RootHibernateQuery hibernateQuery = context.getHibernateQuery();
    if (ordering.getDirection() != null) {
        switch(ordering.getDirection()) {
            case ASCENDING:
                hibernateQuery.addOrdering(hqlPropertyPath, OrderDirection.ASCENDING);
                break;
            case DESCENDING:
                hibernateQuery.addOrdering(hqlPropertyPath, OrderDirection.DESCENDING);
                break;
        }
    } else {
        hibernateQuery.addOrdering(hqlPropertyPath, OrderDirection.ASCENDING);
    }
}
Also used : QueryException(com.evolveum.midpoint.repo.sql.query.QueryException) RootHibernateQuery(com.evolveum.midpoint.repo.sql.query2.hqm.RootHibernateQuery) PolyString(com.evolveum.midpoint.prism.polystring.PolyString) RPolyString(com.evolveum.midpoint.repo.sql.data.common.embedded.RPolyString) ItemPath(com.evolveum.midpoint.prism.path.ItemPath)

Example 3 with JpaEntityDefinition

use of com.evolveum.midpoint.repo.sql.query2.definition.JpaEntityDefinition in project midpoint by Evolveum.

the class ItemPathResolver method findRestrictedEntityDefinition.

/**
     * Given existing entity definition and a request for narrowing it, tries to find refined definition.
     */
public JpaEntityDefinition findRestrictedEntityDefinition(JpaEntityDefinition baseEntityDefinition, QName specificTypeName) throws QueryException {
    QueryDefinitionRegistry2 registry = QueryDefinitionRegistry2.getInstance();
    JpaEntityDefinition specificEntityDefinition = registry.findEntityDefinition(specificTypeName);
    if (!baseEntityDefinition.isAssignableFrom(specificEntityDefinition)) {
        throw new QueryException("Entity " + baseEntityDefinition + " cannot be restricted to " + specificEntityDefinition);
    }
    return specificEntityDefinition;
}
Also used : QueryException(com.evolveum.midpoint.repo.sql.query.QueryException) QueryDefinitionRegistry2(com.evolveum.midpoint.repo.sql.query2.QueryDefinitionRegistry2) JpaEntityDefinition(com.evolveum.midpoint.repo.sql.query2.definition.JpaEntityDefinition)

Example 4 with JpaEntityDefinition

use of com.evolveum.midpoint.repo.sql.query2.definition.JpaEntityDefinition in project midpoint by Evolveum.

the class ExistsRestriction method interpret.

@Override
public Condition interpret() throws QueryException {
    HqlDataInstance dataInstance = getItemPathResolver().resolveItemPath(filter.getFullPath(), filter.getDefinition(), getBaseHqlEntity(), false);
    boolean isAll = filter.getFilter() == null || filter.getFilter() instanceof AllFilter;
    JpaDataNodeDefinition jpaDefinition = dataInstance.getJpaDefinition();
    if (!isAll) {
        if (!(jpaDefinition instanceof JpaEntityDefinition)) {
            // partially checked already (for non-null-ness)
            throw new QueryException("ExistsRestriction with non-empty subfilter points to non-entity node: " + jpaDefinition);
        }
        setHqlDataInstance(dataInstance);
        QueryInterpreter2 interpreter = context.getInterpreter();
        return interpreter.interpretFilter(context, filter.getFilter(), this);
    } else if (jpaDefinition instanceof JpaPropertyDefinition && (((JpaPropertyDefinition) jpaDefinition).isCount())) {
        RootHibernateQuery hibernateQuery = context.getHibernateQuery();
        return hibernateQuery.createSimpleComparisonCondition(dataInstance.getHqlPath(), 0, ">");
    } else {
        // TODO support exists also for other properties (single valued or multi valued)
        throw new UnsupportedOperationException("Exists filter with 'all' subfilter is currently not supported");
    }
}
Also used : JpaDataNodeDefinition(com.evolveum.midpoint.repo.sql.query2.definition.JpaDataNodeDefinition) HqlDataInstance(com.evolveum.midpoint.repo.sql.query2.resolution.HqlDataInstance) QueryException(com.evolveum.midpoint.repo.sql.query.QueryException) AllFilter(com.evolveum.midpoint.prism.query.AllFilter) RootHibernateQuery(com.evolveum.midpoint.repo.sql.query2.hqm.RootHibernateQuery) JpaEntityDefinition(com.evolveum.midpoint.repo.sql.query2.definition.JpaEntityDefinition) JpaPropertyDefinition(com.evolveum.midpoint.repo.sql.query2.definition.JpaPropertyDefinition) QueryInterpreter2(com.evolveum.midpoint.repo.sql.query2.QueryInterpreter2)

Example 5 with JpaEntityDefinition

use of com.evolveum.midpoint.repo.sql.query2.definition.JpaEntityDefinition in project midpoint by Evolveum.

the class QueryDefinitionRegistry2 method findEntityDefinition.

public JpaEntityDefinition findEntityDefinition(QName typeName) {
    Validate.notNull(typeName, "Type name must not be null.");
    JpaEntityDefinition def = QNameUtil.getKey(definitions, typeName);
    if (def == null) {
        throw new IllegalStateException("Type " + typeName + " couldn't be found in type registry");
    }
    return def;
}
Also used : JpaEntityDefinition(com.evolveum.midpoint.repo.sql.query2.definition.JpaEntityDefinition)

Aggregations

JpaEntityDefinition (com.evolveum.midpoint.repo.sql.query2.definition.JpaEntityDefinition)7 QueryException (com.evolveum.midpoint.repo.sql.query.QueryException)5 ItemPath (com.evolveum.midpoint.prism.path.ItemPath)2 QueryDefinitionRegistry2 (com.evolveum.midpoint.repo.sql.query2.QueryDefinitionRegistry2)2 RootHibernateQuery (com.evolveum.midpoint.repo.sql.query2.hqm.RootHibernateQuery)2 ArrayList (java.util.ArrayList)2 ItemDefinition (com.evolveum.midpoint.prism.ItemDefinition)1 PolyString (com.evolveum.midpoint.prism.polystring.PolyString)1 AllFilter (com.evolveum.midpoint.prism.query.AllFilter)1 RPolyString (com.evolveum.midpoint.repo.sql.data.common.embedded.RPolyString)1 QueryInterpreter2 (com.evolveum.midpoint.repo.sql.query2.QueryInterpreter2)1 JpaDataNodeDefinition (com.evolveum.midpoint.repo.sql.query2.definition.JpaDataNodeDefinition)1 JpaPropertyDefinition (com.evolveum.midpoint.repo.sql.query2.definition.JpaPropertyDefinition)1 HqlDataInstance (com.evolveum.midpoint.repo.sql.query2.resolution.HqlDataInstance)1 ItemPathResolver (com.evolveum.midpoint.repo.sql.query2.resolution.ItemPathResolver)1 ProperDataSearchResult (com.evolveum.midpoint.repo.sql.query2.resolution.ProperDataSearchResult)1