Search in sources :

Example 1 with QueryException

use of com.evolveum.midpoint.repo.sql.query.QueryException 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 2 with QueryException

use of com.evolveum.midpoint.repo.sql.query.QueryException in project midpoint by Evolveum.

the class JpaAnyContainerDefinition method nextLinkDefinition.

@Override
public DataSearchResult nextLinkDefinition(ItemPath path, ItemDefinition itemDefinition, PrismContext prismContext) throws QueryException {
    if (ItemPath.asSingleName(path) == null) {
        throw new QueryException("Couldn't resolve paths other than those in the form of single name in extension/attributes container: " + path);
    }
    if (itemDefinition == null) {
        throw new QueryException("Couldn't resolve dynamically defined item path '" + path + "' without proper definition");
    }
    CollectionSpecification collSpec = itemDefinition.isSingleValue() ? null : new CollectionSpecification();
    // longs, strings, ...
    String jpaName;
    JpaDataNodeDefinition jpaNodeDefinition;
    if (itemDefinition instanceof PrismPropertyDefinition) {
        try {
            jpaName = RAnyConverter.getAnySetType(itemDefinition, prismContext);
        } catch (SchemaException e) {
            throw new QueryException(e.getMessage(), e);
        }
        // TODO
        jpaNodeDefinition = new JpaAnyPropertyDefinition(Object.class, null);
    } else if (itemDefinition instanceof PrismReferenceDefinition) {
        jpaName = "references";
        jpaNodeDefinition = new JpaAnyReferenceDefinition(Object.class, RObject.class);
    } else {
        throw new QueryException("Unsupported 'any' item: " + itemDefinition);
    }
    JpaLinkDefinition<?> linkDefinition = new JpaAnyItemLinkDefinition(itemDefinition.getName(), jpaName, collSpec, getOwnerType(), jpaNodeDefinition);
    return new DataSearchResult<>(linkDefinition, ItemPath.EMPTY_PATH);
}
Also used : SchemaException(com.evolveum.midpoint.util.exception.SchemaException) QueryException(com.evolveum.midpoint.repo.sql.query.QueryException) DataSearchResult(com.evolveum.midpoint.repo.sql.query2.resolution.DataSearchResult) RObject(com.evolveum.midpoint.repo.sql.data.common.RObject)

Example 3 with QueryException

use of com.evolveum.midpoint.repo.sql.query.QueryException in project midpoint by Evolveum.

the class PropertyRestriction method checkValueType.

private Object checkValueType(Object value, ValueFilter filter) throws QueryException {
    Class expectedType = linkDefinition.getTargetDefinition().getJaxbClass();
    if (expectedType == null || value == null) {
        // nothing to check here
        return value;
    }
    if (expectedType.isPrimitive()) {
        expectedType = ClassUtils.primitiveToWrapper(expectedType);
    }
    //attempt to fix value type for polystring (if it was string in filter we create polystring from it)
    if (PolyString.class.equals(expectedType) && (value instanceof String)) {
        LOGGER.debug("Trying to query PolyString value but filter contains String '{}'.", filter);
        String orig = (String) value;
        value = new PolyString(orig, context.getPrismContext().getDefaultPolyStringNormalizer().normalize(orig));
    }
    //attempt to fix value type for polystring (if it was polystringtype in filter we create polystring from it)
    if (PolyString.class.equals(expectedType) && (value instanceof PolyStringType)) {
        LOGGER.debug("Trying to query PolyString value but filter contains PolyStringType '{}'.", filter);
        PolyStringType type = (PolyStringType) value;
        value = new PolyString(type.getOrig(), type.getNorm());
    }
    if (String.class.equals(expectedType) && (value instanceof QName)) {
        //eg. shadow/objectClass
        value = RUtil.qnameToString((QName) value);
    }
    if (value instanceof RawType) {
        // MID-3850: but it's quite a workaround. Maybe we should treat RawType's earlier than this.
        try {
            return ((RawType) value).getParsedRealValue(expectedType);
        } catch (SchemaException e) {
            throw new QueryException("Couldn't parse value " + value + " as " + expectedType + ": " + e.getMessage(), e);
        }
    }
    if (!expectedType.isAssignableFrom(value.getClass())) {
        throw new QueryException("Value should be type of '" + expectedType + "' but it's '" + value.getClass() + "', filter '" + filter + "'.");
    }
    return value;
}
Also used : PolyStringType(com.evolveum.prism.xml.ns._public.types_3.PolyStringType) SchemaException(com.evolveum.midpoint.util.exception.SchemaException) QueryException(com.evolveum.midpoint.repo.sql.query.QueryException) QName(javax.xml.namespace.QName) PolyString(com.evolveum.midpoint.prism.polystring.PolyString) PolyString(com.evolveum.midpoint.prism.polystring.PolyString) RawType(com.evolveum.prism.xml.ns._public.types_3.RawType)

Example 4 with QueryException

use of com.evolveum.midpoint.repo.sql.query.QueryException in project midpoint by Evolveum.

the class ReferenceRestriction method interpretInternal.

@Override
public Condition interpretInternal() throws QueryException {
    String hqlPath = hqlDataInstance.getHqlPath();
    LOGGER.trace("interpretInternal starting with hqlPath = {}", hqlPath);
    RootHibernateQuery hibernateQuery = context.getHibernateQuery();
    List<PrismReferenceValue> values = filter.getValues();
    if (CollectionUtils.isEmpty(values)) {
        return hibernateQuery.createIsNull(hqlDataInstance.getHqlPath());
    }
    Set<String> oids = new HashSet<>();
    Set<QName> relations = new HashSet<>();
    Set<QName> targetTypes = new HashSet<>();
    for (PrismReferenceValue value : values) {
        if (value.getOid() == null) {
            throw new QueryException("Null OID is not allowed in the reference query. Use empty reference list if needed.");
        }
        oids.add(value.getOid());
        if (value.getRelation() == null) {
            relations.add(SchemaConstants.ORG_DEFAULT);
        } else {
            // we intentionally don't normalize relations namespaces, to be able to do namespace-insensitive searches
            // so the caller is responsible to unify namespaces if he needs to optimize queries (use IN instead of OR)
            relations.add(value.getRelation());
        }
        targetTypes.add(qualifyTypeName(value.getTargetType()));
    }
    if (relations.size() > 1 || targetTypes.size() > 1) {
        // we must use 'OR' clause
        OrCondition rootOr = hibernateQuery.createOr();
        values.forEach(prv -> rootOr.add(createRefCondition(hibernateQuery, Collections.singleton(prv.getOid()), prv.getRelation(), prv.getTargetType())));
        return rootOr;
    } else {
        return createRefCondition(hibernateQuery, oids, MiscUtil.extractSingleton(relations), MiscUtil.extractSingleton(targetTypes));
    }
}
Also used : QueryException(com.evolveum.midpoint.repo.sql.query.QueryException) PrismReferenceValue(com.evolveum.midpoint.prism.PrismReferenceValue) RootHibernateQuery(com.evolveum.midpoint.repo.sql.query2.hqm.RootHibernateQuery) QName(javax.xml.namespace.QName) RUtil.qnameToString(com.evolveum.midpoint.repo.sql.util.RUtil.qnameToString) OrCondition(com.evolveum.midpoint.repo.sql.query2.hqm.condition.OrCondition)

Example 5 with QueryException

use of com.evolveum.midpoint.repo.sql.query.QueryException in project midpoint by Evolveum.

the class Matcher method basicMatch.

protected Condition basicMatch(RootHibernateQuery hibernateQuery, ItemRestrictionOperation operation, String propertyPath, Object value, boolean ignoreCase) throws QueryException {
    Validate.notNull(hibernateQuery, "hibernateQuery");
    if (ignoreCase && !(value instanceof String)) {
        LOGGER.warn("Ignoring ignoreCase setting for non-string value of {}", value);
        ignoreCase = false;
    }
    Condition condition;
    switch(operation) {
        case EQ:
            if (value == null) {
                condition = hibernateQuery.createIsNull(propertyPath);
            } else {
                condition = hibernateQuery.createEq(propertyPath, value, ignoreCase);
            }
            break;
        case GT:
        case GE:
        case LT:
        case LE:
            condition = hibernateQuery.createSimpleComparisonCondition(propertyPath, value, operation.symbol(), ignoreCase);
            break;
        case NOT_NULL:
            condition = hibernateQuery.createIsNotNull(propertyPath);
            break;
        case NULL:
            condition = hibernateQuery.createIsNull(propertyPath);
            break;
        case STARTS_WITH:
            condition = hibernateQuery.createLike(propertyPath, (String) value, MatchMode.START, ignoreCase);
            break;
        case ENDS_WITH:
            condition = hibernateQuery.createLike(propertyPath, (String) value, MatchMode.END, ignoreCase);
            break;
        case SUBSTRING:
            condition = hibernateQuery.createLike(propertyPath, (String) value, MatchMode.ANYWHERE, ignoreCase);
            break;
        default:
            throw new QueryException("Unknown operation '" + operation + "'.");
    }
    return condition;
}
Also used : Condition(com.evolveum.midpoint.repo.sql.query2.hqm.condition.Condition) QueryException(com.evolveum.midpoint.repo.sql.query.QueryException)

Aggregations

QueryException (com.evolveum.midpoint.repo.sql.query.QueryException)26 RQuery (com.evolveum.midpoint.repo.sql.query.RQuery)5 QueryEngine2 (com.evolveum.midpoint.repo.sql.query2.QueryEngine2)5 RootHibernateQuery (com.evolveum.midpoint.repo.sql.query2.hqm.RootHibernateQuery)5 PolyString (com.evolveum.midpoint.prism.polystring.PolyString)4 Condition (com.evolveum.midpoint.repo.sql.query2.hqm.condition.Condition)4 SchemaException (com.evolveum.midpoint.util.exception.SchemaException)4 QName (javax.xml.namespace.QName)4 ItemPath (com.evolveum.midpoint.prism.path.ItemPath)3 JpaEntityDefinition (com.evolveum.midpoint.repo.sql.query2.definition.JpaEntityDefinition)3 OrCondition (com.evolveum.midpoint.repo.sql.query2.hqm.condition.OrCondition)3 HqlDataInstance (com.evolveum.midpoint.repo.sql.query2.resolution.HqlDataInstance)3 EqualFilter (com.evolveum.midpoint.prism.query.EqualFilter)2 QueryDefinitionRegistry2 (com.evolveum.midpoint.repo.sql.query2.QueryDefinitionRegistry2)2 JpaPropertyDefinition (com.evolveum.midpoint.repo.sql.query2.definition.JpaPropertyDefinition)2 AndCondition (com.evolveum.midpoint.repo.sql.query2.hqm.condition.AndCondition)2 DataSearchResult (com.evolveum.midpoint.repo.sql.query2.resolution.DataSearchResult)2 ItemDefinition (com.evolveum.midpoint.prism.ItemDefinition)1 PrismReferenceValue (com.evolveum.midpoint.prism.PrismReferenceValue)1 IdItemPathSegment (com.evolveum.midpoint.prism.path.IdItemPathSegment)1