Search in sources :

Example 1 with EqualFilter

use of com.evolveum.midpoint.prism.query.EqualFilter in project midpoint by Evolveum.

the class ShadowManager method processQueryMatchingRuleFilter.

private <T> void processQueryMatchingRuleFilter(ObjectFilter filter, RefinedObjectClassDefinition objectClassDef) throws SchemaException {
    if (!(filter instanceof EqualFilter)) {
        return;
    }
    EqualFilter<T> eqFilter = (EqualFilter) filter;
    ItemPath parentPath = eqFilter.getParentPath();
    if (parentPath == null || !parentPath.equivalent(SchemaConstants.PATH_ATTRIBUTES)) {
        return;
    }
    QName attrName = eqFilter.getElementName();
    RefinedAttributeDefinition rAttrDef = objectClassDef.findAttributeDefinition(attrName);
    if (rAttrDef == null) {
        throw new SchemaException("Unknown attribute " + attrName + " in filter " + filter);
    }
    QName matchingRuleQName = rAttrDef.getMatchingRuleQName();
    if (matchingRuleQName == null) {
        return;
    }
    Class<?> valueClass = null;
    MatchingRule<T> matchingRule = matchingRuleRegistry.getMatchingRule(matchingRuleQName, rAttrDef.getTypeName());
    List<PrismValue> newValues = new ArrayList<PrismValue>();
    if (eqFilter.getValues() != null) {
        for (PrismPropertyValue<T> ppval : eqFilter.getValues()) {
            T normalizedRealValue = matchingRule.normalize(ppval.getValue());
            PrismPropertyValue<T> newPPval = ppval.clone();
            newPPval.setValue(normalizedRealValue);
            newValues.add(newPPval);
            if (normalizedRealValue != null) {
                valueClass = normalizedRealValue.getClass();
            }
        }
        eqFilter.getValues().clear();
        eqFilter.getValues().addAll((Collection) newValues);
        LOGGER.trace("Replacing values for attribute {} in search filter with normalized values because there is a matching rule, normalized values: {}", attrName, newValues);
    }
    if (eqFilter.getMatchingRule() == null) {
        QName supportedMatchingRule = valueClass != null ? repositoryService.getApproximateSupportedMatchingRule(valueClass, matchingRuleQName) : matchingRuleQName;
        eqFilter.setMatchingRule(supportedMatchingRule);
        LOGGER.trace("Setting matching rule to {} (supported by repo as a replacement for {} to search for {})", supportedMatchingRule, matchingRuleQName, valueClass);
    }
}
Also used : SchemaException(com.evolveum.midpoint.util.exception.SchemaException) QName(javax.xml.namespace.QName) ArrayList(java.util.ArrayList) PrismValue(com.evolveum.midpoint.prism.PrismValue) RefinedAttributeDefinition(com.evolveum.midpoint.common.refinery.RefinedAttributeDefinition) EqualFilter(com.evolveum.midpoint.prism.query.EqualFilter) ItemPath(com.evolveum.midpoint.prism.path.ItemPath)

Example 2 with EqualFilter

use of com.evolveum.midpoint.prism.query.EqualFilter in project midpoint by Evolveum.

the class QueryHelper method applyMatchingRules.

/**
 * Visit the query and normalize values (or set matching rules) as needed
 */
@Contract("null, _ -> null; !null, _ -> !null")
ObjectQuery applyMatchingRules(ObjectQuery originalQuery, ResourceObjectDefinition objectDef) {
    if (originalQuery == null) {
        return null;
    }
    ObjectQuery processedQuery = originalQuery.clone();
    ObjectFilter filter = processedQuery.getFilter();
    Visitor visitor = f -> {
        try {
            if (f instanceof EqualFilter) {
                applyMatchingRuleToEqFilter((EqualFilter<?>) f, objectDef);
            }
        } catch (SchemaException e) {
            throw new SystemException(e);
        }
    };
    filter.accept(visitor);
    return processedQuery;
}
Also used : ResourceAttributeDefinition(com.evolveum.midpoint.schema.processor.ResourceAttributeDefinition) ResourceObjectDefinition(com.evolveum.midpoint.schema.processor.ResourceObjectDefinition) EqualFilter(com.evolveum.midpoint.prism.query.EqualFilter) SchemaConstants(com.evolveum.midpoint.schema.constants.SchemaConstants) Autowired(org.springframework.beans.factory.annotation.Autowired) SchemaException(com.evolveum.midpoint.util.exception.SchemaException) PrismPropertyValue(com.evolveum.midpoint.prism.PrismPropertyValue) MiscUtil(com.evolveum.midpoint.util.MiscUtil) Trace(com.evolveum.midpoint.util.logging.Trace) MatchingRuleRegistry(com.evolveum.midpoint.prism.match.MatchingRuleRegistry) ObjectFilter(com.evolveum.midpoint.prism.query.ObjectFilter) ArrayList(java.util.ArrayList) List(java.util.List) Contract(org.jetbrains.annotations.Contract) Component(org.springframework.stereotype.Component) MatchingRule(com.evolveum.midpoint.prism.match.MatchingRule) Visitor(com.evolveum.midpoint.prism.query.Visitor) SystemException(com.evolveum.midpoint.util.exception.SystemException) QName(javax.xml.namespace.QName) ObjectQuery(com.evolveum.midpoint.prism.query.ObjectQuery) NotNull(org.jetbrains.annotations.NotNull) TraceManager(com.evolveum.midpoint.util.logging.TraceManager) SchemaException(com.evolveum.midpoint.util.exception.SchemaException) Visitor(com.evolveum.midpoint.prism.query.Visitor) SystemException(com.evolveum.midpoint.util.exception.SystemException) EqualFilter(com.evolveum.midpoint.prism.query.EqualFilter) ObjectFilter(com.evolveum.midpoint.prism.query.ObjectFilter) ObjectQuery(com.evolveum.midpoint.prism.query.ObjectQuery) Contract(org.jetbrains.annotations.Contract)

Example 3 with EqualFilter

use of com.evolveum.midpoint.prism.query.EqualFilter in project midpoint by Evolveum.

the class TestParseObjectTemplate method testAccessObjectTemplateMultithreaded.

@Test
public void testAccessObjectTemplateMultithreaded() throws Exception {
    // GIVEN
    PrismContext prismContext = getPrismContext();
    int THREADS = 50;
    // WHEN
    PrismObject<ObjectTemplateType> template = prismContext.parseObject(OBJECT_TEMPLATE_FILE);
    // this is necessary in order to eliminate thread-unsafe DOM value parsers
    template.freeze();
    MappingType mapping = template.asObjectable().getMapping().stream().filter(m -> "Access role assignment".equals(m.getName())).findAny().orElse(null);
    assertNotNull("The mapping was not found", mapping);
    AssignmentTargetSearchExpressionEvaluatorType evaluator = (AssignmentTargetSearchExpressionEvaluatorType) mapping.getExpression().getExpressionEvaluator().get(0).getValue();
    AtomicInteger errors = new AtomicInteger(0);
    List<Thread> threads = new ArrayList<>(THREADS);
    for (int i = 0; i < THREADS; i++) {
        Thread thread = new Thread(() -> {
            try {
                ObjectFilter filter = prismContext.getQueryConverter().createObjectFilter(RoleType.class, evaluator.getFilter());
                EqualFilter equalFilter = (EqualFilter) filter;
                assertNotNull(equalFilter.getExpression());
                ExpressionType expression = (ExpressionType) equalFilter.getExpression().getExpression();
                ScriptExpressionEvaluatorType script = (ScriptExpressionEvaluatorType) expression.getExpressionEvaluator().get(0).getValue();
                String code = script.getCode();
                assertNotNull("No code", code);
                assertEquals("Wrong code", "return memberOf.split(\";\", -1)[0]", code.trim());
            } catch (Throwable t) {
                errors.incrementAndGet();
                throw new AssertionError("Got exception: " + t.getMessage(), t);
            }
        });
        thread.setName("Executor #" + i);
        thread.start();
        threads.add(thread);
    }
    // THEN
    waitForCompletion(threads, 20000);
    assertEquals("Wrong # of errors", 0, errors.get());
    // TODO some asserts on correct parsing maybe
    System.out.println("Frozen object:\n" + template.debugDump());
}
Also used : PrismTestUtil.getPrismContext(com.evolveum.midpoint.prism.util.PrismTestUtil.getPrismContext) ArrayList(java.util.ArrayList) ObjectFilter(com.evolveum.midpoint.prism.query.ObjectFilter) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) EqualFilter(com.evolveum.midpoint.prism.query.EqualFilter) Test(org.testng.annotations.Test)

Example 4 with EqualFilter

use of com.evolveum.midpoint.prism.query.EqualFilter in project midpoint by Evolveum.

the class TestRefinedSchema method assertProtectedAccount.

private void assertProtectedAccount(String message, ResourceObjectPattern protectedPattern, String identifierValue, ResourceObjectTypeDefinition rAccount) throws SchemaException {
    ObjectFilter filter = protectedPattern.getObjectFilter();
    assertNotNull("Null objectFilter in " + message, filter);
    assertTrue("Wrong filter class " + filter.getClass().getSimpleName() + " in " + message, filter instanceof EqualFilter);
    assertNotNull("Null filter path in " + message, ((EqualFilter) filter).getPath());
    assertEquals("Wrong filter value in " + message, identifierValue, ((EqualFilter<String>) filter).getValues().iterator().next().getValue());
    // Try matching
    PrismObject<ShadowType> shadow = rAccount.getPrismObjectDefinition().instantiate();
    ResourceAttributeContainer attributesContainer = ShadowUtil.getOrCreateAttributesContainer(shadow, rAccount);
    ResourceAttribute<String> confusingAttr1 = createStringAttribute(new QName("http://whatever.com", "confuseMe"), "HowMuchWoodWouldWoodchuckChuckIfWoodchuckCouldChudkWood");
    attributesContainer.add(confusingAttr1);
    ResourceAttribute<String> nameAttr = createStringAttribute(SchemaTestConstants.ICFS_NAME, identifierValue);
    attributesContainer.add(nameAttr);
    ResourceAttribute<String> confusingAttr2 = createStringAttribute(new QName("http://whatever.com", "confuseMeAgain"), "WoodchuckWouldChuckNoWoodAsWoodchuckCannotChuckWood");
    attributesContainer.add(confusingAttr2);
    assertTrue("Test attr not matched in " + message, protectedPattern.matches(shadow, matchingRuleRegistry, relationRegistry));
    nameAttr.setRealValue("huhulumululul");
    assertFalse("Test attr nonsense was matched in " + message, protectedPattern.matches(shadow, matchingRuleRegistry, relationRegistry));
}
Also used : QName(javax.xml.namespace.QName) EqualFilter(com.evolveum.midpoint.prism.query.EqualFilter) ObjectFilter(com.evolveum.midpoint.prism.query.ObjectFilter)

Example 5 with EqualFilter

use of com.evolveum.midpoint.prism.query.EqualFilter in project midpoint by Evolveum.

the class PropertyRestriction method createPropertyVsPropertyCondition.

protected Condition createPropertyVsPropertyCondition(String leftPropertyValuePath) throws QueryException {
    HqlDataInstance rightItem = getItemPathResolver().resolveItemPath(filter.getRightHandSidePath(), filter.getRightHandSideDefinition(), getBaseHqlEntityForChildren(), true);
    String rightHqlPath = rightItem.getHqlPath();
    RootHibernateQuery hibernateQuery = context.getHibernateQuery();
    if (filter instanceof EqualFilter) {
        // left = right OR (left IS NULL AND right IS NULL)
        Condition condition = hibernateQuery.createCompareXY(leftPropertyValuePath, rightHqlPath, "=", false);
        OrCondition orCondition = hibernateQuery.createOr(condition, hibernateQuery.createAnd(hibernateQuery.createIsNull(leftPropertyValuePath), hibernateQuery.createIsNull(rightHqlPath)));
        return orCondition;
    } else if (filter instanceof ComparativeFilter) {
        ItemRestrictionOperation operation = findOperationForFilter(filter);
        Condition condition = hibernateQuery.createCompareXY(leftPropertyValuePath, rightHqlPath, operation.symbol(), false);
        return condition;
    } else {
        throw new QueryException("Right-side ItemPath is supported currently only for EqualFilter or ComparativeFilter, not for " + filter);
    }
}
Also used : Condition(com.evolveum.midpoint.repo.sql.query2.hqm.condition.Condition) OrCondition(com.evolveum.midpoint.repo.sql.query2.hqm.condition.OrCondition) HqlDataInstance(com.evolveum.midpoint.repo.sql.query2.resolution.HqlDataInstance) QueryException(com.evolveum.midpoint.repo.sql.query.QueryException) RootHibernateQuery(com.evolveum.midpoint.repo.sql.query2.hqm.RootHibernateQuery) ComparativeFilter(com.evolveum.midpoint.prism.query.ComparativeFilter) EqualFilter(com.evolveum.midpoint.prism.query.EqualFilter) PolyString(com.evolveum.midpoint.prism.polystring.PolyString) OrCondition(com.evolveum.midpoint.repo.sql.query2.hqm.condition.OrCondition)

Aggregations

EqualFilter (com.evolveum.midpoint.prism.query.EqualFilter)14 ItemPath (com.evolveum.midpoint.prism.path.ItemPath)5 ObjectFilter (com.evolveum.midpoint.prism.query.ObjectFilter)5 ArrayList (java.util.ArrayList)5 List (java.util.List)4 PolyString (com.evolveum.midpoint.prism.polystring.PolyString)3 QName (javax.xml.namespace.QName)3 PrismValue (com.evolveum.midpoint.prism.PrismValue)2 ComparativeFilter (com.evolveum.midpoint.prism.query.ComparativeFilter)2 QueryFactory (com.evolveum.midpoint.prism.query.QueryFactory)2 QueryException (com.evolveum.midpoint.repo.sql.query.QueryException)2 QueryException (com.evolveum.midpoint.repo.sqlbase.QueryException)2 SearchResultList (com.evolveum.midpoint.schema.SearchResultList)2 SchemaException (com.evolveum.midpoint.util.exception.SchemaException)2 UserType (com.evolveum.midpoint.xml.ns._public.common.common_3.UserType)2 RestartResponseException (org.apache.wicket.RestartResponseException)2 RefinedAttributeDefinition (com.evolveum.midpoint.common.refinery.RefinedAttributeDefinition)1 PrismObject (com.evolveum.midpoint.prism.PrismObject)1 PrismPropertyValue (com.evolveum.midpoint.prism.PrismPropertyValue)1 MatchingRule (com.evolveum.midpoint.prism.match.MatchingRule)1