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);
}
}
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;
}
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());
}
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));
}
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);
}
}
Aggregations