use of com.evolveum.midpoint.prism.path.ItemPath in project midpoint by Evolveum.
the class PathExpressionEvaluator method evaluate.
/* (non-Javadoc)
* @see com.evolveum.midpoint.common.expression.ExpressionEvaluator#evaluate(java.util.Collection, java.util.Map, boolean, java.lang.String, com.evolveum.midpoint.schema.result.OperationResult)
*/
@Override
public PrismValueDeltaSetTriple<V> evaluate(ExpressionEvaluationContext context) throws SchemaException, ExpressionEvaluationException, ObjectNotFoundException {
ItemDeltaItem<?, ?> resolveContext = null;
if (context.getSources() != null && context.getSources().size() == 1) {
Source<?, ?> source = context.getSources().iterator().next();
if (path.isEmpty()) {
PrismValueDeltaSetTriple<V> outputTriple = (PrismValueDeltaSetTriple<V>) source.toDeltaSetTriple();
return outputTriple.clone();
}
resolveContext = source;
}
Map<QName, Object> variablesAndSources = ExpressionUtil.compileVariablesAndSources(context);
ItemPath resolvePath = path;
ItemPathSegment first = path.first();
if (first instanceof NameItemPathSegment && first.isVariable()) {
QName variableName = ((NameItemPathSegment) first).getName();
Object variableValue;
if (variablesAndSources.containsKey(variableName)) {
variableValue = variablesAndSources.get(variableName);
} else if (QNameUtil.matchAny(variableName, variablesAndSources.keySet())) {
QName fullVariableName = QNameUtil.resolveNs(variableName, variablesAndSources.keySet());
variableValue = variablesAndSources.get(fullVariableName);
} else {
throw new ExpressionEvaluationException("No variable with name " + variableName + " in " + context.getContextDescription());
}
if (variableValue == null) {
return null;
}
if (variableValue instanceof Item || variableValue instanceof ItemDeltaItem<?, ?>) {
resolveContext = ExpressionUtil.toItemDeltaItem(variableValue, objectResolver, "path expression in " + context.getContextDescription(), context.getResult());
} else if (variableValue instanceof PrismPropertyValue<?>) {
PrismValueDeltaSetTriple<V> outputTriple = new PrismValueDeltaSetTriple<>();
outputTriple.addToZeroSet((V) variableValue);
return ExpressionUtil.toOutputTriple(outputTriple, outputDefinition, context.getAdditionalConvertor(), null, protector, prismContext);
} else {
throw new ExpressionEvaluationException("Unexpected variable value " + variableValue + " (" + variableValue.getClass() + ")");
}
resolvePath = path.rest();
}
if (resolveContext == null) {
return null;
}
while (!resolvePath.isEmpty()) {
if (resolveContext.isContainer()) {
resolveContext = resolveContext.findIdi(resolvePath.head());
resolvePath = resolvePath.tail();
if (resolveContext == null) {
throw new ExpressionEvaluationException("Cannot find item using path " + path + " in " + context.getContextDescription());
}
} else if (resolveContext.isStructuredProperty()) {
// The output path does not really matter. The delta will be converted to triple anyway
// But the path cannot be null, oherwise the code will die
resolveContext = resolveContext.resolveStructuredProperty(resolvePath, (PrismPropertyDefinition) outputDefinition, new ItemPath());
break;
} else if (resolveContext.isNull()) {
break;
} else {
throw new ExpressionEvaluationException("Cannot resolve path " + resolvePath + " on " + resolveContext + " in " + context.getContextDescription());
}
}
PrismValueDeltaSetTriple<V> outputTriple = ItemDelta.toDeltaSetTriple((Item<V, D>) resolveContext.getItemOld(), (ItemDelta<V, D>) resolveContext.getDelta());
if (outputTriple == null) {
return null;
}
return ExpressionUtil.toOutputTriple(outputTriple, outputDefinition, context.getAdditionalConvertor(), null, protector, prismContext);
}
use of com.evolveum.midpoint.prism.path.ItemPath in project midpoint by Evolveum.
the class PathExpressionEvaluatorFactory method createEvaluator.
/* (non-Javadoc)
* @see com.evolveum.midpoint.common.expression.ExpressionEvaluatorFactory#createEvaluator(javax.xml.bind.JAXBElement, com.evolveum.midpoint.prism.ItemDefinition, com.evolveum.midpoint.prism.PrismContext)
*/
@Override
public <V extends PrismValue, D extends ItemDefinition> ExpressionEvaluator<V, D> createEvaluator(Collection<JAXBElement<?>> evaluatorElements, D outputDefinition, String contextDescription, Task task, OperationResult result) throws SchemaException {
Validate.notNull(outputDefinition, "output definition must be specified for path expression evaluator");
if (evaluatorElements.size() > 1) {
throw new SchemaException("More than one evaluator specified in " + contextDescription);
}
JAXBElement<?> evaluatorElement = evaluatorElements.iterator().next();
Object evaluatorElementObject = evaluatorElement.getValue();
if (!(evaluatorElementObject instanceof ItemPathType)) {
throw new IllegalArgumentException("Path expression cannot handle elements of type " + evaluatorElementObject.getClass().getName() + " in " + contextDescription);
}
ItemPath path = ((ItemPathType) evaluatorElementObject).getItemPath();
return new PathExpressionEvaluator<>(path, objectResolver, outputDefinition, protector, prismContext);
}
use of com.evolveum.midpoint.prism.path.ItemPath in project midpoint by Evolveum.
the class Mapping method parseSource.
private <IV extends PrismValue, ID extends ItemDefinition> Source<IV, ID> parseSource(VariableBindingDefinitionType sourceType, Task task, OperationResult result) throws SchemaException, ObjectNotFoundException, ExpressionEvaluationException {
ItemPathType itemPathType = sourceType.getPath();
if (itemPathType == null) {
throw new SchemaException("No path in source definition in " + getMappingContextDescription());
}
ItemPath path = itemPathType.getItemPath();
if (path.isEmpty()) {
throw new SchemaException("Empty source path in " + getMappingContextDescription());
}
QName name = sourceType.getName();
if (name == null) {
name = ItemPath.getName(path.last());
}
ItemPath resolvePath = path;
Object sourceObject = ExpressionUtil.resolvePath(path, variables, sourceContext, objectResolver, "source definition in " + getMappingContextDescription(), task, result);
Item<IV, ID> itemOld = null;
ItemDelta<IV, ID> delta = null;
Item<IV, ID> itemNew = null;
ItemPath residualPath = null;
Collection<? extends ItemDelta<?, ?>> subItemDeltas = null;
if (sourceObject != null) {
if (sourceObject instanceof ItemDeltaItem<?, ?>) {
itemOld = ((ItemDeltaItem<IV, ID>) sourceObject).getItemOld();
delta = ((ItemDeltaItem<IV, ID>) sourceObject).getDelta();
itemNew = ((ItemDeltaItem<IV, ID>) sourceObject).getItemNew();
residualPath = ((ItemDeltaItem<IV, ID>) sourceObject).getResidualPath();
resolvePath = ((ItemDeltaItem<IV, ID>) sourceObject).getResolvePath();
subItemDeltas = ((ItemDeltaItem<IV, ID>) sourceObject).getSubItemDeltas();
} else if (sourceObject instanceof Item<?, ?>) {
itemOld = (Item<IV, ID>) sourceObject;
itemNew = (Item<IV, ID>) sourceObject;
} else {
throw new IllegalStateException("Unknown resolve result " + sourceObject);
}
}
// apply domain
ValueSetDefinitionType domainSetType = sourceType.getSet();
if (domainSetType != null) {
ValueSetDefinition setDef = new ValueSetDefinition(domainSetType, name, "domain of " + name.getLocalPart() + " in " + getMappingContextDescription(), task, result);
setDef.init(expressionFactory);
try {
if (itemOld != null) {
itemOld = itemOld.clone();
itemOld.filterValues(val -> setDef.containsTunnel(val));
}
if (itemNew != null) {
itemNew = itemNew.clone();
itemNew.filterValues(val -> setDef.containsTunnel(val));
}
if (delta != null) {
delta = delta.clone();
delta.filterValues(val -> setDef.containsTunnel(val));
}
} catch (TunnelException te) {
Throwable cause = te.getCause();
if (cause instanceof SchemaException) {
throw (SchemaException) cause;
} else if (cause instanceof ExpressionEvaluationException) {
throw (ExpressionEvaluationException) cause;
} else if (cause instanceof ObjectNotFoundException) {
throw (ObjectNotFoundException) cause;
}
}
}
Source<IV, ID> source = new Source<>(itemOld, delta, itemNew, name);
source.setResidualPath(residualPath);
source.setResolvePath(resolvePath);
source.setSubItemDeltas(subItemDeltas);
return source;
}
use of com.evolveum.midpoint.prism.path.ItemPath in project midpoint by Evolveum.
the class BasicExpressionFunctions method getPropertyValues.
public <T> Collection<T> getPropertyValues(ObjectType object, String path) {
if (object == null) {
return null;
}
ScriptExpressionEvaluationContext scriptContext = ScriptExpressionEvaluationContext.getThreadLocal();
ScriptExpression scriptExpression = scriptContext.getScriptExpression();
ItemPath itemPath = scriptExpression.parsePath(path);
PrismProperty property = object.asPrismObject().findProperty(itemPath);
if (property == null) {
return new ArrayList<T>(0);
}
return property.getRealValues();
}
use of com.evolveum.midpoint.prism.path.ItemPath in project midpoint by Evolveum.
the class ObjectWrapper method createAddingObjectDelta.
private ObjectDelta createAddingObjectDelta() throws SchemaException {
PrismObject object = this.object.clone();
List<ContainerWrapper<? extends Containerable>> containers = getContainers();
// sort containers by path size
Collections.sort(containers, new PathSizeComparator());
for (ContainerWrapper containerWrapper : getContainers()) {
if (containerWrapper.getItemDefinition().getName().equals(ShadowType.F_ASSOCIATION)) {
PrismContainer associationContainer = object.findOrCreateContainer(ShadowType.F_ASSOCIATION);
List<AssociationWrapper> associationItemWrappers = (List<AssociationWrapper>) containerWrapper.getItems();
for (AssociationWrapper associationItemWrapper : associationItemWrappers) {
List<ValueWrapper> assocValueWrappers = associationItemWrapper.getValues();
for (ValueWrapper assocValueWrapper : assocValueWrappers) {
PrismContainerValue<ShadowAssociationType> assocValue = (PrismContainerValue<ShadowAssociationType>) assocValueWrapper.getValue();
associationContainer.add(assocValue.clone());
}
}
continue;
}
if (!containerWrapper.hasChanged()) {
continue;
}
PrismContainer container = containerWrapper.getItem();
ItemPath path = containerWrapper.getPath();
if (containerWrapper.getPath() != null) {
container = container.clone();
if (path.size() > 1) {
ItemPath parentPath = path.allExceptLast();
PrismContainer parent = object.findOrCreateContainer(parentPath);
parent.add(container);
} else {
PrismContainer existing = object.findContainer(container.getElementName());
if (existing == null) {
object.add(container);
} else {
continue;
}
}
} else {
container = object;
}
for (ItemWrapper itemWrapper : (List<ItemWrapper>) containerWrapper.getItems()) {
if (!itemWrapper.hasChanged()) {
continue;
}
if (container.findItem(itemWrapper.getName()) != null) {
continue;
}
Item updatedItem = ((PropertyOrReferenceWrapper) itemWrapper).getUpdatedItem(object.getPrismContext());
if (!updatedItem.isEmpty()) {
container.add(updatedItem);
}
}
}
// cleanup empty containers
cleanupEmptyContainers(object);
ObjectDelta delta = ObjectDelta.createAddDelta(object);
// returning container to previous order
Collections.sort(containers, new ItemWrapperComparator());
if (LOGGER.isTraceEnabled()) {
LOGGER.trace("Creating delta from wrapper {}: adding object, creating complete ADD delta:\n{}", this, delta.debugDump());
}
if (InternalsConfig.consistencyChecks) {
delta.checkConsistence(true, true, true, ConsistencyCheckScope.THOROUGH);
}
return delta;
}
Aggregations