use of com.evolveum.midpoint.xml.ns._public.common.common_3.ExpressionType in project midpoint by Evolveum.
the class ResourceManager method evaluateExpression.
private <T> void evaluateExpression(PrismProperty<T> configurationProperty, PrismObject<ResourceType> resource, Task task, OperationResult result) throws SchemaException, ObjectNotFoundException, ExpressionEvaluationException {
PrismPropertyDefinition<T> propDef = configurationProperty.getDefinition();
String shortDesc = "connector configuration property " + configurationProperty + " in " + resource;
List<PrismPropertyValue<T>> extraValues = new ArrayList<>();
for (PrismPropertyValue<T> configurationPropertyValue : configurationProperty.getValues()) {
ExpressionWrapper expressionWrapper = configurationPropertyValue.getExpression();
if (expressionWrapper == null) {
return;
}
Object expressionObject = expressionWrapper.getExpression();
if (!(expressionObject instanceof ExpressionType)) {
throw new IllegalStateException("Expected that expression in " + configurationPropertyValue + " will be ExpressionType, but it was " + expressionObject);
}
ExpressionType expressionType = (ExpressionType) expressionWrapper.getExpression();
Expression<PrismPropertyValue<T>, PrismPropertyDefinition<T>> expression = expressionFactory.makeExpression(expressionType, propDef, shortDesc, task, result);
ExpressionVariables variables = new ExpressionVariables();
// TODO: populate variables
ExpressionEvaluationContext expressionContext = new ExpressionEvaluationContext(null, variables, shortDesc, task, result);
PrismValueDeltaSetTriple<PrismPropertyValue<T>> expressionOutputTriple = expression.evaluate(expressionContext);
Collection<PrismPropertyValue<T>> expressionOutputValues = expressionOutputTriple.getNonNegativeValues();
if (expressionOutputValues != null && !expressionOutputValues.isEmpty()) {
Iterator<PrismPropertyValue<T>> iterator = expressionOutputValues.iterator();
PrismPropertyValue<T> firstValue = iterator.next();
configurationPropertyValue.setValue(firstValue.getValue());
while (iterator.hasNext()) {
extraValues.add(iterator.next());
}
}
}
for (PrismPropertyValue<T> extraValue : extraValues) {
configurationProperty.add(extraValue);
}
}
use of com.evolveum.midpoint.xml.ns._public.common.common_3.ExpressionType in project midpoint by Evolveum.
the class TestIteration method test200JackAssignAccountDummyPinkConflicting.
@Test
public void test200JackAssignAccountDummyPinkConflicting() throws Exception {
final String TEST_NAME = "test200JackAssignAccountDummyPinkConflicting";
TestUtil.displayTestTile(this, TEST_NAME);
// GIVEN
Task task = taskManager.createTaskInstance(TestIteration.class.getName() + "." + TEST_NAME);
OperationResult result = task.getResult();
dummyAuditService.clear();
// Make sure there is a conflicting account and also a shadow for it
DummyAccount account = new DummyAccount(ACCOUNT_JACK_DUMMY_USERNAME);
account.setEnabled(true);
account.addAttributeValues(DummyResourceContoller.DUMMY_ACCOUNT_ATTRIBUTE_FULLNAME_NAME, "Jack Pinky");
account.addAttributeValues(DummyResourceContoller.DUMMY_ACCOUNT_ATTRIBUTE_LOCATION_NAME, "Red Sea");
dummyResourcePink.addAccount(account);
repoAddObject(createShadow(resourceDummyPink, ACCOUNT_JACK_DUMMY_USERNAME), result);
Collection<ObjectDelta<? extends ObjectType>> deltas = new ArrayList<ObjectDelta<? extends ObjectType>>();
// assignment with weapon := 'pistol' (test for
Collection<ItemDelta<?, ?>> modifications = new ArrayList<>();
AssignmentType assignmentType = createConstructionAssignment(RESOURCE_DUMMY_PINK_OID, ShadowKindType.ACCOUNT, null);
ConstructionType constructionType = assignmentType.getConstruction();
ResourceAttributeDefinitionType attributeDefinitionType = new ResourceAttributeDefinitionType();
attributeDefinitionType.setRef(new ItemPathType(new ItemPath(dummyResourceCtlPink.getAttributeWeaponQName())));
MappingType mappingType = new MappingType();
mappingType.setStrength(MappingStrengthType.STRONG);
ExpressionType expressionType = new ExpressionType();
expressionType.getExpressionEvaluator().add(new ObjectFactory().createValue(RawType.create("pistol", prismContext)));
mappingType.setExpression(expressionType);
attributeDefinitionType.setOutbound(mappingType);
constructionType.getAttribute().add(attributeDefinitionType);
modifications.add(createAssignmentModification(assignmentType, true));
ObjectDelta<UserType> accountAssignmentUserDelta = ObjectDelta.createModifyDelta(USER_JACK_OID, modifications, UserType.class, prismContext);
deltas.add(accountAssignmentUserDelta);
// WHEN
TestUtil.displayWhen(TEST_NAME);
modelService.executeChanges(deltas, null, task, result);
// THEN
TestUtil.displayThen(TEST_NAME);
result.computeStatus();
TestUtil.assertSuccess(result);
PrismObject<UserType> userJack = getUser(USER_JACK_OID);
display("User after change execution", userJack);
assertUserJack(userJack);
assertLinks(userJack, 2);
assertAccount(userJack, RESOURCE_DUMMY_OID);
assertAccount(userJack, RESOURCE_DUMMY_PINK_OID);
String accountPinkOid = getLinkRefOid(userJack, RESOURCE_DUMMY_PINK_OID);
// Check shadow
PrismObject<ShadowType> accountPinkShadow = repositoryService.getObject(ShadowType.class, accountPinkOid, null, result);
assertAccountShadowRepo(accountPinkShadow, accountPinkOid, "jack1", resourceDummyPinkType);
// Check account
PrismObject<ShadowType> accountPinkModel = modelService.getObject(ShadowType.class, accountPinkOid, null, task, result);
assertAccountShadowModel(accountPinkModel, accountPinkOid, "jack1", resourceDummyPinkType);
display("accountPinkModel", accountPinkModel);
PrismAsserts.assertPropertyValue(accountPinkModel, dummyResourceCtlPink.getAttributePath(DummyResourceContoller.DUMMY_ACCOUNT_ATTRIBUTE_WEAPON_NAME), "pistol");
// Check account in dummy resource
assertDefaultDummyAccount(ACCOUNT_JACK_DUMMY_USERNAME, "Jack Sparrow", true);
// The original conflicting account should still remain
assertDummyAccount(RESOURCE_DUMMY_PINK_NAME, ACCOUNT_JACK_DUMMY_USERNAME, "Jack Pinky", true);
// The new account
assertDummyAccount(RESOURCE_DUMMY_PINK_NAME, "jack1", "Jack Sparrow", true);
// Check audit
display("Audit", dummyAuditService);
dummyAuditService.assertRecords(2);
dummyAuditService.assertSimpleRecordSanity();
dummyAuditService.assertAnyRequestDeltas();
dummyAuditService.assertExecutionDeltas(3);
dummyAuditService.assertHasDelta(ChangeType.MODIFY, UserType.class);
dummyAuditService.assertHasDelta(ChangeType.ADD, ShadowType.class);
dummyAuditService.assertExecutionSuccess();
}
use of com.evolveum.midpoint.xml.ns._public.common.common_3.ExpressionType in project midpoint by Evolveum.
the class Expression method processInnerVariables.
private ExpressionVariables processInnerVariables(ExpressionVariables variables, String contextDescription, Task task, OperationResult result) throws SchemaException, ObjectNotFoundException {
if (expressionType == null) {
// shortcut
return variables;
}
ExpressionVariables newVariables = new ExpressionVariables();
// We need to add actor variable before we switch user identity (runAs)
ExpressionUtil.addActorVariable(newVariables, securityEnforcer);
for (Entry<QName, Object> entry : variables.entrySet()) {
newVariables.addVariableDefinition(entry.getKey(), entry.getValue());
}
for (ExpressionVariableDefinitionType variableDefType : expressionType.getVariable()) {
QName varName = variableDefType.getName();
if (varName == null) {
throw new SchemaException("No variable name in expression in " + contextDescription);
}
if (variableDefType.getObjectRef() != null) {
ObjectReferenceType ref = variableDefType.getObjectRef();
ref.setType(prismContext.getSchemaRegistry().qualifyTypeName(ref.getType()));
ObjectType varObject = objectResolver.resolve(ref, ObjectType.class, null, "variable " + varName + " in " + contextDescription, task, result);
newVariables.addVariableDefinition(varName, varObject);
} else if (variableDefType.getValue() != null) {
// Only string is supported now
Object valueObject = variableDefType.getValue();
if (valueObject instanceof String) {
newVariables.addVariableDefinition(varName, valueObject);
} else if (valueObject instanceof Element) {
newVariables.addVariableDefinition(varName, ((Element) valueObject).getTextContent());
} else if (valueObject instanceof RawType) {
newVariables.addVariableDefinition(varName, ((RawType) valueObject).getParsedValue(null, varName));
} else {
throw new SchemaException("Unexpected type " + valueObject.getClass() + " in variable definition " + varName + " in " + contextDescription);
}
} else if (variableDefType.getPath() != null) {
ItemPath itemPath = variableDefType.getPath().getItemPath();
Object resolvedValue = ExpressionUtil.resolvePath(itemPath, variables, null, objectResolver, contextDescription, task, result);
newVariables.addVariableDefinition(varName, resolvedValue);
} else {
throw new SchemaException("No value for variable " + varName + " in " + contextDescription);
}
}
return newVariables;
}
use of com.evolveum.midpoint.xml.ns._public.common.common_3.ExpressionType in project midpoint by Evolveum.
the class ExpressionUtil method createFilterForNoValue.
private static ObjectFilter createFilterForNoValue(ObjectFilter filter, ExpressionType valueExpression) throws ExpressionEvaluationException {
QueryInterpretationOfNoValueType queryInterpretationOfNoValue = valueExpression.getQueryInterpretationOfNoValue();
if (queryInterpretationOfNoValue == null) {
queryInterpretationOfNoValue = QueryInterpretationOfNoValueType.FILTER_EQUAL_NULL;
}
switch(queryInterpretationOfNoValue) {
case FILTER_UNDEFINED:
return UndefinedFilter.createUndefined();
case FILTER_NONE:
return NoneFilter.createNone();
case FILTER_ALL:
return AllFilter.createAll();
case FILTER_EQUAL_NULL:
if (filter instanceof ValueFilter) {
ValueFilter evaluatedFilter = (ValueFilter) filter.clone();
evaluatedFilter.setExpression(null);
return evaluatedFilter;
} else if (filter instanceof InOidFilter) {
return NoneFilter.createNone();
} else if (filter instanceof FullTextFilter) {
// because full text search for 'no value' is meaningless
return NoneFilter.createNone();
} else {
throw new IllegalArgumentException("Unknown filter to evaluate: " + filter);
}
case ERROR:
throw new ExpressionEvaluationException("Expression " + valueExpression + " evaluated to no value");
default:
throw new IllegalArgumentException("Unknown value " + queryInterpretationOfNoValue + " in queryInterpretationOfNoValue in " + valueExpression);
}
}
use of com.evolveum.midpoint.xml.ns._public.common.common_3.ExpressionType in project midpoint by Evolveum.
the class ExpressionUtil method evaluateFilterExpressionsInternal.
private static ObjectFilter evaluateFilterExpressionsInternal(ObjectFilter filter, ExpressionVariables variables, ExpressionFactory expressionFactory, PrismContext prismContext, String shortDesc, Task task, OperationResult result) throws SchemaException, ObjectNotFoundException, ExpressionEvaluationException {
if (filter == null) {
return null;
}
if (filter instanceof InOidFilter) {
ExpressionWrapper expressionWrapper = ((InOidFilter) filter).getExpression();
if (expressionWrapper == null || expressionWrapper.getExpression() == null) {
LOGGER.warn("No valueExpression in filter in {}. Returning original filter", shortDesc);
InOidFilter inOidFilter = (InOidFilter) filter;
if (inOidFilter.getOids() != null && !inOidFilter.getOids().isEmpty()) {
return filter.clone();
}
return NoneFilter.createNone();
}
ExpressionType valueExpression = getExpression(expressionWrapper, shortDesc);
try {
Collection<String> expressionResult = evaluateStringExpression(variables, prismContext, valueExpression, expressionFactory, shortDesc, task, result);
if (expressionResult == null || expressionResult.isEmpty()) {
LOGGER.debug("Result of search filter expression was null or empty. Expression: {}", valueExpression);
return createFilterForNoValue(filter, valueExpression);
}
// TODO: log more context
LOGGER.trace("Search filter expression in the rule for {} evaluated to {}.", new Object[] { shortDesc, expressionResult });
InOidFilter evaluatedFilter = (InOidFilter) filter.clone();
evaluatedFilter.setOids(expressionResult);
evaluatedFilter.setExpression(null);
if (LOGGER.isTraceEnabled()) {
LOGGER.trace("Transformed filter to:\n{}", evaluatedFilter.debugDump());
}
return evaluatedFilter;
} catch (Exception ex) {
throw new ExpressionEvaluationException(ex);
}
} else if (filter instanceof FullTextFilter) {
ExpressionWrapper expressionWrapper = ((FullTextFilter) filter).getExpression();
if (expressionMissing(expressionWrapper, shortDesc)) {
return filter.clone();
}
ExpressionType valueExpression = getExpression(expressionWrapper, shortDesc);
try {
Collection<String> expressionResult = evaluateStringExpression(variables, prismContext, valueExpression, expressionFactory, shortDesc, task, result);
if (expressionResult == null || expressionResult.isEmpty()) {
LOGGER.debug("Result of search filter expression was null or empty. Expression: {}", valueExpression);
return createFilterForNoValue(filter, valueExpression);
}
// TODO: log more context
LOGGER.trace("Search filter expression in the rule for {} evaluated to {}.", shortDesc, expressionResult);
FullTextFilter evaluatedFilter = (FullTextFilter) filter.clone();
evaluatedFilter.setValues(expressionResult);
evaluatedFilter.setExpression(null);
if (LOGGER.isTraceEnabled()) {
LOGGER.trace("Transformed filter to:\n{}", evaluatedFilter.debugDump());
}
return evaluatedFilter;
} catch (Exception ex) {
throw new ExpressionEvaluationException(ex);
}
} else if (filter instanceof LogicalFilter) {
List<ObjectFilter> conditions = ((LogicalFilter) filter).getConditions();
LogicalFilter evaluatedFilter = ((LogicalFilter) filter).cloneEmpty();
for (ObjectFilter condition : conditions) {
ObjectFilter evaluatedSubFilter = evaluateFilterExpressionsInternal(condition, variables, expressionFactory, prismContext, shortDesc, task, result);
evaluatedFilter.addCondition(evaluatedSubFilter);
}
return evaluatedFilter;
} else if (filter instanceof ValueFilter) {
ValueFilter valueFilter = (ValueFilter) filter;
if (valueFilter.getValues() != null && !valueFilter.getValues().isEmpty()) {
// We have value. Nothing to evaluate.
return valueFilter.clone();
}
ExpressionWrapper expressionWrapper = valueFilter.getExpression();
if (expressionMissing(expressionWrapper, shortDesc)) {
return valueFilter.clone();
}
ExpressionType valueExpression = getExpression(expressionWrapper, shortDesc);
try {
PrismValue expressionResult = evaluateExpression(variables, prismContext, valueExpression, filter, expressionFactory, shortDesc, task, result);
if (expressionResult == null || expressionResult.isEmpty()) {
LOGGER.debug("Result of search filter expression was null or empty. Expression: {}", valueExpression);
return createFilterForNoValue(valueFilter, valueExpression);
}
// TODO: log more context
LOGGER.trace("Search filter expression in the rule for {} evaluated to {}.", new Object[] { shortDesc, expressionResult });
ValueFilter evaluatedFilter = valueFilter.clone();
evaluatedFilter.setValue(expressionResult);
evaluatedFilter.setExpression(null);
// }
if (LOGGER.isTraceEnabled()) {
LOGGER.trace("Transformed filter to:\n{}", evaluatedFilter.debugDump());
}
return evaluatedFilter;
} catch (RuntimeException ex) {
LoggingUtils.logException(LOGGER, "Couldn't evaluate expression " + valueExpression + ".", ex);
throw new SystemException("Couldn't evaluate expression" + valueExpression + ": " + ex.getMessage(), ex);
} catch (SchemaException ex) {
LoggingUtils.logException(LOGGER, "Couldn't evaluate expression " + valueExpression + ".", ex);
throw new SchemaException("Couldn't evaluate expression" + valueExpression + ": " + ex.getMessage(), ex);
} catch (ObjectNotFoundException ex) {
LoggingUtils.logException(LOGGER, "Couldn't evaluate expression " + valueExpression + ".", ex);
throw new ObjectNotFoundException("Couldn't evaluate expression" + valueExpression + ": " + ex.getMessage(), ex);
} catch (ExpressionEvaluationException ex) {
LoggingUtils.logException(LOGGER, "Couldn't evaluate expression " + valueExpression + ".", ex);
throw new ExpressionEvaluationException("Couldn't evaluate expression" + valueExpression + ": " + ex.getMessage(), ex);
}
} else if (filter instanceof ExistsFilter) {
ExistsFilter evaluatedFilter = ((ExistsFilter) filter).cloneEmpty();
ObjectFilter evaluatedSubFilter = evaluateFilterExpressionsInternal(((ExistsFilter) filter).getFilter(), variables, expressionFactory, prismContext, shortDesc, task, result);
evaluatedFilter.setFilter(evaluatedSubFilter);
return evaluatedFilter;
} else if (filter instanceof TypeFilter) {
TypeFilter evaluatedFilter = ((TypeFilter) filter).cloneEmpty();
ObjectFilter evaluatedSubFilter = evaluateFilterExpressionsInternal(((TypeFilter) filter).getFilter(), variables, expressionFactory, prismContext, shortDesc, task, result);
evaluatedFilter.setFilter(evaluatedSubFilter);
return evaluatedFilter;
} else if (filter instanceof OrgFilter) {
return filter;
} else if (filter instanceof AllFilter || filter instanceof NoneFilter || filter instanceof UndefinedFilter) {
return filter;
} else {
throw new IllegalStateException("Unsupported filter type: " + filter.getClass());
}
}
Aggregations