Search in sources :

Example 1 with QueryException

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

the class PropertyRestriction method adaptValueType.

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

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

the class PropertyRestriction method getValueFromFilter.

private Object getValueFromFilter(ValueFilter filter) throws QueryException {
    JpaPropertyDefinition def = linkDefinition.getTargetDefinition();
    Object value;
    if (filter instanceof PropertyValueFilter) {
        value = getValue((PropertyValueFilter) filter);
    } else {
        throw new QueryException("Unknown filter '" + filter + "', can't get value from it.");
    }
    Object adaptedValue = adaptValueType(value, filter);
    if (def.isEnumerated()) {
        return getRepoEnumValue((Enum) adaptedValue, def.getJpaClass());
    } else {
        return adaptedValue;
    }
}
Also used : QueryException(com.evolveum.midpoint.repo.sqlbase.QueryException) JpaPropertyDefinition(com.evolveum.midpoint.repo.sql.query.definition.JpaPropertyDefinition) PropertyValueFilter(com.evolveum.midpoint.prism.query.PropertyValueFilter)

Example 3 with QueryException

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

the class OrgRestriction method interpret.

@Override
public Condition interpret() throws QueryException {
    RootHibernateQuery hibernateQuery = getContext().getHibernateQuery();
    if (filter.isRoot()) {
        // oid in (select descendantOid from ROrgClosure group by descendantOid having count(descendantOid) = 1)
        return hibernateQuery.createIn(getBaseHqlEntity().getHqlPath() + ".oid", "select descendantOid from ROrgClosure group by descendantOid having count(descendantOid) = 1");
    }
    if (filter.getOrgRef() == null) {
        throw new QueryException("No organization reference defined in the search query.");
    }
    if (filter.getOrgRef().getOid() == null) {
        throw new QueryException("No oid specified in organization reference " + filter.getOrgRef().debugDump());
    }
    String orgOidParamName = hibernateQuery.addParameter("orgOid", filter.getOrgRef().getOid());
    String relationParamName = "";
    QName relation = filter.getOrgRef().getRelation();
    if (doesRelationRestrictionExist(relation)) {
        relationParamName = hibernateQuery.addParameter("relation", ReferenceRestriction.getRelationsToTest(relation, context));
    }
    // oid in ...
    String oidQueryText;
    switch(filter.getScope()) {
        case ONE_LEVEL:
            oidQueryText = // TODO distinct(ref.ownerOid) ? (was in original QueryInterpreter)
            "select ref.ownerOid " + "from RObjectReference ref " + "where " + "ref.referenceType = " + RReferenceType.OBJECT_PARENT_ORG.ordinal() + (doesRelationRestrictionExist(relation) ? " and ref.relation in (:" + relationParamName + ")" : "") + " and ref.targetOid = :" + orgOidParamName;
            break;
        case ANCESTORS:
            oidQueryText = "select c.ancestorOid " + "from ROrgClosure c " + "where " + "c.ancestorOid != :" + orgOidParamName + " and " + "c.descendantOid = :" + orgOidParamName;
            break;
        case SUBTREE:
        default:
            oidQueryText = "select ref.ownerOid " + "from RObjectReference ref " + "where " + "ref.referenceType = " + RReferenceType.OBJECT_PARENT_ORG.ordinal() + (doesRelationRestrictionExist(relation) ? " and ref.relation in (:" + relationParamName + ")" : "") + " and ref.targetOid in (" + "select descendantOid from ROrgClosure where ancestorOid = :" + orgOidParamName + ")";
    }
    return hibernateQuery.createIn(getBaseHqlEntity().getHqlPath() + ".oid", oidQueryText);
}
Also used : QueryException(com.evolveum.midpoint.repo.sqlbase.QueryException) RootHibernateQuery(com.evolveum.midpoint.repo.sql.query.hqm.RootHibernateQuery) QName(javax.xml.namespace.QName)

Example 4 with QueryException

use of com.evolveum.midpoint.repo.sqlbase.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 {
    Objects.requireNonNull(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:
            // TODO never used so it seems (never assigned to operation)
            condition = hibernateQuery.createIsNotNull(propertyPath);
            break;
        case NULL:
            // TODO never used so it seems (never assigned to operation)
            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.query.hqm.condition.Condition) QueryException(com.evolveum.midpoint.repo.sqlbase.QueryException)

Example 5 with QueryException

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

the class AuditCustomColumnItemFilterProcessor method process.

@Override
public Predicate process(PropertyValueFilter<AuditEventRecordCustomColumnPropertyType> filter) throws QueryException {
    // This is a tricky situation, if multi-value, each value can have different path (derived
    // from AuditEventRecordCustomColumnPropertyType.getName()), so we can't use this directly.
    ValueFilterValues<AuditEventRecordCustomColumnPropertyType, ?> values = ValueFilterValues.from(filter);
    if (values.isEmpty()) {
        throw new QueryException("Custom column null value is not supported," + " column can't be determined from filter: " + filter);
    }
    if (values.isMultiValue()) {
        Predicate predicate = null;
        for (AuditEventRecordCustomColumnPropertyType propertyType : values.allValuesRaw()) {
            Predicate right = createPredicate(filter, propertyType);
            predicate = predicate != null ? ExpressionUtils.or(predicate, right) : right;
        }
        return predicate;
    }
    AuditEventRecordCustomColumnPropertyType value = values.singleValueRaw();
    assert value != null;
    return createPredicate(filter, value);
}
Also used : AuditEventRecordCustomColumnPropertyType(com.evolveum.midpoint.xml.ns._public.common.audit_3.AuditEventRecordCustomColumnPropertyType) QueryException(com.evolveum.midpoint.repo.sqlbase.QueryException) Predicate(com.querydsl.core.types.Predicate)

Aggregations

QueryException (com.evolveum.midpoint.repo.sqlbase.QueryException)46 PolyString (com.evolveum.midpoint.prism.polystring.PolyString)9 QName (javax.xml.namespace.QName)9 RootHibernateQuery (com.evolveum.midpoint.repo.sql.query.hqm.RootHibernateQuery)8 Predicate (com.querydsl.core.types.Predicate)8 QueryEngine (com.evolveum.midpoint.repo.sql.query.QueryEngine)6 PropertyValueFilter (com.evolveum.midpoint.prism.query.PropertyValueFilter)5 RQuery (com.evolveum.midpoint.repo.sql.query.RQuery)5 ItemPath (com.evolveum.midpoint.prism.path.ItemPath)4 Condition (com.evolveum.midpoint.repo.sql.query.hqm.condition.Condition)4 FilterOperation (com.evolveum.midpoint.repo.sqlbase.filtering.item.FilterOperation)4 SchemaException (com.evolveum.midpoint.util.exception.SchemaException)4 RPolyString (com.evolveum.midpoint.repo.sql.data.common.embedded.RPolyString)3 HqlDataInstance (com.evolveum.midpoint.repo.sql.query.resolution.HqlDataInstance)3 RepositoryException (com.evolveum.midpoint.repo.sqlbase.RepositoryException)3 ItemRelationResolver (com.evolveum.midpoint.repo.sqlbase.mapping.ItemRelationResolver)3 NotNull (org.jetbrains.annotations.NotNull)3 EqualFilter (com.evolveum.midpoint.prism.query.EqualFilter)2 ValueFilter (com.evolveum.midpoint.prism.query.ValueFilter)2 ExtensionProcessor (com.evolveum.midpoint.repo.sqale.ExtensionProcessor)2