use of com.evolveum.midpoint.prism.ItemDefinition in project midpoint by Evolveum.
the class MappingTestEvaluator method createInboudMapping.
public <T> Mapping<PrismPropertyValue<T>, PrismPropertyDefinition<T>> createInboudMapping(String filename, String testName, ItemDelta delta, UserType user, ShadowType account, ResourceType resource, final StringPolicyType policy) throws SchemaException, IOException, JAXBException {
MappingType mappingType = PrismTestUtil.parseAtomicValue(new File(TEST_DIR, filename), MappingType.COMPLEX_TYPE);
Mapping.Builder<PrismPropertyValue<T>, PrismPropertyDefinition<T>> builder = mappingFactory.createMappingBuilder(mappingType, testName);
Source<PrismPropertyValue<T>, PrismPropertyDefinition<T>> defaultSource = new Source<>(null, delta, null, ExpressionConstants.VAR_INPUT);
defaultSource.recompute();
builder.setDefaultSource(defaultSource);
builder.setTargetContext(getUserDefinition());
builder.addVariableDefinition(ExpressionConstants.VAR_USER, user);
builder.addVariableDefinition(ExpressionConstants.VAR_FOCUS, user);
builder.addVariableDefinition(ExpressionConstants.VAR_ACCOUNT, account.asPrismObject());
builder.addVariableDefinition(ExpressionConstants.VAR_SHADOW, account.asPrismObject());
StringPolicyResolver stringPolicyResolver = new StringPolicyResolver() {
ItemPath outputPath;
ItemDefinition outputDefinition;
@Override
public void setOutputPath(ItemPath outputPath) {
this.outputPath = outputPath;
}
@Override
public void setOutputDefinition(ItemDefinition outputDefinition) {
this.outputDefinition = outputDefinition;
}
@Override
public StringPolicyType resolve() {
return policy;
}
};
builder.setStringPolicyResolver(stringPolicyResolver);
builder.setOriginType(OriginType.INBOUND);
builder.setOriginObject(resource);
return builder.build();
}
use of com.evolveum.midpoint.prism.ItemDefinition in project midpoint by Evolveum.
the class SceneItemImpl method debugDumpCommon.
@NotNull
protected StringBuilder debugDumpCommon(int indent) {
StringBuilder sb = new StringBuilder();
DebugUtil.indentDebugDump(sb, indent);
sb.append("Item: ").append(name).append(" [rel-path: ").append(sourceRelPath).append("]");
if (sourceItem != null) {
sb.append(" ITEM");
final ItemDefinition def = sourceItem.getDefinition();
if (def != null) {
sb.append(" DEF(").append(def.getName().getLocalPart()).append("/").append(def.getDisplayName()).append(":").append(def.getDisplayOrder()).append(")");
}
}
if (operational) {
sb.append(" OPER");
}
return sb;
}
use of com.evolveum.midpoint.prism.ItemDefinition in project midpoint by Evolveum.
the class XPathHolder method toCanonicalPath.
public String toCanonicalPath(Class objectType, PrismContext prismContext) {
StringBuilder sb = new StringBuilder("\\");
boolean first = true;
PrismObjectDefinition objDef = null;
if (objectType != null) {
objDef = prismContext.getSchemaRegistry().findObjectDefinitionByCompileTimeClass(objectType);
}
ItemDefinition def = null;
for (XPathSegment seg : segments) {
if (seg.isIdValueFilter()) {
//for now, we don't want to save concrete id, just the path
continue;
} else {
QName qname = seg.getQName();
if (!first) {
sb.append("\\");
if (StringUtils.isBlank(qname.getNamespaceURI()) && objDef != null) {
if (def instanceof PrismContainerDefinition) {
PrismContainerDefinition containerDef = (PrismContainerDefinition) def;
def = containerDef.findItemDefinition(qname);
}
if (def != null) {
qname = def.getName();
}
}
} else {
if (StringUtils.isBlank(qname.getNamespaceURI()) && objDef != null) {
def = objDef.findItemDefinition(qname);
if (def != null) {
qname = def.getName();
}
}
first = false;
}
sb.append(QNameUtil.qNameToUri(qname));
}
}
return sb.toString();
}
use of com.evolveum.midpoint.prism.ItemDefinition in project midpoint by Evolveum.
the class TestParseScriptingExpression method testParseToXNode.
@Test
public void testParseToXNode() throws Exception {
displayTestTitle("testParseToXNode");
ItemDefinition sequenceDef = getPrismContext().getSchemaRegistry().findItemDefinitionByElementName(SchemaConstants.S_SEQUENCE);
System.out.println("sequence.substitutionHead = " + sequenceDef.getSubstitutionHead());
System.out.println("sequence.heterogeneousListItem = " + sequenceDef.isHeterogeneousListItem());
ComplexTypeDefinition sequenceCtd = getPrismContext().getSchemaRegistry().findComplexTypeDefinitionByType(sequenceDef.getTypeName());
System.out.println("ExpressionSequenceType.list = " + sequenceCtd.isListMarker());
System.out.println("\n\n-----------------------------------\n");
String file = MiscUtil.readFile(getFile());
System.out.println("Original text:\n" + file);
RootXNode xnode = getPrismContext().parserFor(file).parseToXNode();
System.out.println("XNode:\n" + xnode.debugDump());
System.out.println("source -> XNode -> JSON:\n" + getPrismContext().jsonSerializer().serialize(xnode));
System.out.println("source -> XNode -> YAML:\n" + getPrismContext().yamlSerializer().serialize(xnode));
System.out.println("source -> XNode -> XML:\n" + getPrismContext().xmlSerializer().serialize(xnode));
}
use of com.evolveum.midpoint.prism.ItemDefinition in project midpoint by Evolveum.
the class MappingEvaluator method evaluateMappingSetProjection.
public <V extends PrismValue, D extends ItemDefinition, T extends ObjectType, F extends FocusType> void evaluateMappingSetProjection(MappingEvaluatorParams<V, D, T, F> params, Task task, OperationResult result) throws ExpressionEvaluationException, ObjectNotFoundException, SchemaException, CommunicationException, ConfigurationException, SecurityViolationException {
String mappingDesc = params.getMappingDesc();
LensElementContext<T> targetContext = params.getTargetContext();
PrismObjectDefinition<T> targetObjectDefinition = targetContext.getObjectDefinition();
ItemPath defaultTargetItemPath = params.getDefaultTargetItemPath();
Map<ItemPath, MappingOutputStruct<V>> outputTripleMap = new HashMap<>();
XMLGregorianCalendar nextRecomputeTime = null;
Collection<MappingType> mappingTypes = params.getMappingTypes();
Collection<Mapping<V, D>> mappings = new ArrayList<>(mappingTypes.size());
for (MappingType mappingType : mappingTypes) {
Mapping.Builder<V, D> mappingBuilder = mappingFactory.createMappingBuilder(mappingType, mappingDesc);
String mappingName = null;
if (mappingType.getName() != null) {
mappingName = mappingType.getName();
}
if (!mappingBuilder.isApplicableToChannel(params.getContext().getChannel())) {
LOGGER.trace("Mapping {} not applicable to channel, skipping {}", mappingName, params.getContext().getChannel());
continue;
}
mappingBuilder.now(params.getNow());
if (defaultTargetItemPath != null && targetObjectDefinition != null) {
D defaultTargetItemDef = targetObjectDefinition.findItemDefinition(defaultTargetItemPath);
mappingBuilder.defaultTargetDefinition(defaultTargetItemDef);
mappingBuilder.defaultTargetPath(defaultTargetItemPath);
} else {
mappingBuilder.defaultTargetDefinition(params.getTargetItemDefinition());
mappingBuilder.defaultTargetPath(defaultTargetItemPath);
}
mappingBuilder.targetContext(targetObjectDefinition);
if (params.getSourceContext() != null) {
mappingBuilder.sourceContext(params.getSourceContext());
}
// Initialize mapping (using Inversion of Control)
mappingBuilder = params.getInitializer().initialize(mappingBuilder);
Mapping<V, D> mapping = mappingBuilder.build();
Boolean timeConstraintValid = mapping.evaluateTimeConstraintValid(task, result);
if (params.getEvaluateCurrent() != null) {
if (params.getEvaluateCurrent() && !timeConstraintValid) {
LOGGER.trace("Mapping {} is non-current, but evulating current mappings, skipping {}", mappingName, params.getContext().getChannel());
continue;
}
if (!params.getEvaluateCurrent() && timeConstraintValid) {
LOGGER.trace("Mapping {} is current, but evulating non-current mappings, skipping {}", mappingName, params.getContext().getChannel());
continue;
}
}
mappings.add(mapping);
}
boolean hasFullTargetObject = params.hasFullTargetObject();
PrismObject<T> aPrioriTargetObject = params.getAPrioriTargetObject();
LOGGER.trace("Going to process {} mappings for {}", mappings.size(), mappingDesc);
for (Mapping<V, D> mapping : mappings) {
if (mapping.getStrength() == MappingStrengthType.WEAK) {
// Evaluate weak mappings in a second run.
continue;
}
ItemPath mappingOutputPath = mapping.getOutputPath();
if (params.isFixTarget() && mappingOutputPath != null && defaultTargetItemPath != null && !mappingOutputPath.equivalent(defaultTargetItemPath)) {
throw new ExpressionEvaluationException("Target cannot be overridden in " + mappingDesc);
}
if (params.getAPrioriTargetDelta() != null && mappingOutputPath != null) {
ItemDelta<?, ?> aPrioriItemDelta = params.getAPrioriTargetDelta().findItemDelta(mappingOutputPath);
if (mapping.getStrength() != MappingStrengthType.STRONG) {
if (aPrioriItemDelta != null && !aPrioriItemDelta.isEmpty()) {
continue;
}
}
}
evaluateMapping(mapping, params.getContext(), task, result);
PrismValueDeltaSetTriple<V> mappingOutputTriple = mapping.getOutputTriple();
if (LOGGER.isTraceEnabled()) {
LOGGER.trace("Output triple of mapping {}\n{}", mapping.getContextDescription(), mappingOutputTriple == null ? null : mappingOutputTriple.debugDump(1));
}
if (mappingOutputTriple != null) {
MappingOutputStruct<V> mappingOutputStruct = outputTripleMap.get(mappingOutputPath);
if (mappingOutputStruct == null) {
mappingOutputStruct = new MappingOutputStruct<>();
outputTripleMap.put(mappingOutputPath, mappingOutputStruct);
}
if (mapping.getStrength() == MappingStrengthType.STRONG) {
mappingOutputStruct.setStrongMappingWasUsed(true);
// if (!hasFullTargetObject && params.getTargetLoader() != null) {
// if (!params.getTargetLoader().isLoaded()) {
// aPrioriTargetObject = params.getTargetLoader().load(task, result);
// LOGGER.trace("Loaded object because of strong mapping: {}", aPrioriTargetObject);
// hasFullTargetObject = true;
// }
// }
}
PrismValueDeltaSetTriple<V> outputTriple = mappingOutputStruct.getOutputTriple();
if (outputTriple == null) {
mappingOutputStruct.setOutputTriple(mappingOutputTriple);
} else {
outputTriple.merge(mappingOutputTriple);
}
}
}
if (params.isEvaluateWeak()) {
// Second pass, evaluate only weak mappings
for (Mapping<V, D> mapping : mappings) {
if (mapping.getStrength() != MappingStrengthType.WEAK) {
continue;
}
ItemPath mappingOutputPath = mapping.getOutputPath();
if (params.isFixTarget() && mappingOutputPath != null && defaultTargetItemPath != null && !mappingOutputPath.equivalent(defaultTargetItemPath)) {
throw new ExpressionEvaluationException("Target cannot be overridden in " + mappingDesc);
}
MappingOutputStruct<V> mappingOutputStruct = outputTripleMap.get(mappingOutputPath);
if (mappingOutputStruct == null) {
mappingOutputStruct = new MappingOutputStruct<>();
outputTripleMap.put(mappingOutputPath, mappingOutputStruct);
}
PrismValueDeltaSetTriple<V> outputTriple = mappingOutputStruct.getOutputTriple();
if (outputTriple != null) {
// MID-3847
continue;
}
Item<V, D> aPrioriTargetItem = null;
if (aPrioriTargetObject != null && mappingOutputPath != null) {
aPrioriTargetItem = aPrioriTargetObject.findItem(mappingOutputPath);
}
if (hasNoValue(aPrioriTargetItem)) {
mappingOutputStruct.setWeakMappingWasUsed(true);
evaluateMapping(mapping, params.getContext(), task, result);
PrismValueDeltaSetTriple<V> mappingOutputTriple = mapping.getOutputTriple();
if (mappingOutputTriple != null) {
// This is all not right. See MID-3847
if (!hasFullTargetObject && params.getTargetLoader() != null) {
if (!params.getTargetLoader().isLoaded()) {
aPrioriTargetObject = params.getTargetLoader().load("weak mapping", task, result);
LOGGER.trace("Loaded object because of weak mapping: {}", aPrioriTargetObject);
hasFullTargetObject = true;
}
}
if (aPrioriTargetObject != null && mappingOutputPath != null) {
aPrioriTargetItem = aPrioriTargetObject.findItem(mappingOutputPath);
}
if (!hasNoValue(aPrioriTargetItem)) {
continue;
}
if (outputTriple == null) {
mappingOutputStruct.setOutputTriple(mappingOutputTriple);
} else {
outputTriple.merge(mappingOutputTriple);
}
}
}
}
}
MappingOutputProcessor<V> processor = params.getProcessor();
for (Entry<ItemPath, MappingOutputStruct<V>> outputTripleMapEntry : outputTripleMap.entrySet()) {
ItemPath mappingOutputPath = outputTripleMapEntry.getKey();
MappingOutputStruct<V> mappingOutputStruct = outputTripleMapEntry.getValue();
PrismValueDeltaSetTriple<V> outputTriple = mappingOutputStruct.getOutputTriple();
boolean defaultProcessing = true;
if (processor != null) {
LOGGER.trace("Executing processor to process mapping evaluation results: {}", processor);
defaultProcessing = processor.process(mappingOutputPath, mappingOutputStruct);
}
if (defaultProcessing) {
if (outputTriple == null) {
LOGGER.trace("{} expression resulted in null triple for {}, skipping", mappingDesc, targetContext);
continue;
}
ItemDefinition targetItemDefinition = null;
if (mappingOutputPath != null) {
targetItemDefinition = targetObjectDefinition.findItemDefinition(mappingOutputPath);
if (targetItemDefinition == null) {
throw new SchemaException("No definition for item " + mappingOutputPath + " in " + targetObjectDefinition);
}
} else {
targetItemDefinition = params.getTargetItemDefinition();
}
ItemDelta<V, D> targetItemDelta = targetItemDefinition.createEmptyDelta(mappingOutputPath);
Item<V, D> aPrioriTargetItem = null;
if (aPrioriTargetObject != null) {
aPrioriTargetItem = aPrioriTargetObject.findItem(mappingOutputPath);
}
if (targetContext.isAdd()) {
Collection<V> nonNegativeValues = outputTriple.getNonNegativeValues();
if (nonNegativeValues == null || nonNegativeValues.isEmpty()) {
LOGGER.trace("{} resulted in null or empty value for {}, skipping", mappingDesc, targetContext);
continue;
}
targetItemDelta.setValuesToReplace(PrismValue.cloneCollection(nonNegativeValues));
} else {
// if we have fresh information (full shadow) AND the mapping used to derive the information was strong,
// we will consider all values (zero & plus sets) -- otherwise, we take only the "plus" (i.e. changed) set
// the first case is necessary, because in some situations (e.g. when mapping is changed)
// the evaluator sees no differences w.r.t. real state, even if there is a difference
// - and we must have a way to push new information onto the resource
Collection<V> valuesToReplace;
if (hasFullTargetObject && mappingOutputStruct.isStrongMappingWasUsed()) {
valuesToReplace = outputTriple.getNonNegativeValues();
} else {
valuesToReplace = outputTriple.getPlusSet();
}
if (LOGGER.isTraceEnabled()) {
LOGGER.trace("{}: hasFullTargetObject={}, isStrongMappingWasUsed={}, valuesToReplace={}", new Object[] { mappingDesc, hasFullTargetObject, mappingOutputStruct.isStrongMappingWasUsed(), valuesToReplace });
}
if (valuesToReplace != null && !valuesToReplace.isEmpty()) {
if (hasFullTargetObject && targetContext.isFresh() && aPrioriTargetItem != null) {
Collection<V> valuesPresent = aPrioriTargetItem.getValues();
if (PrismValue.equalsRealValues(valuesPresent, valuesToReplace)) {
LOGGER.trace("{} resulted in existing values for {}, skipping creation of a delta", mappingDesc, targetContext);
continue;
}
}
targetItemDelta.setValuesToReplace(PrismValue.cloneCollection(valuesToReplace));
} else if (outputTriple.hasMinusSet()) {
LOGGER.trace("{} resulted in null or empty value for {} and there is a minus set, resetting it (replace with empty)", mappingDesc, targetContext);
targetItemDelta.setValueToReplace();
} else {
LOGGER.trace("{} resulted in null or empty value for {}, skipping", mappingDesc, targetContext);
}
}
if (targetItemDelta.isEmpty()) {
continue;
}
LOGGER.trace("{} adding new delta for {}: {}", mappingDesc, targetContext, targetItemDelta);
targetContext.swallowToSecondaryDelta(targetItemDelta);
}
}
for (Mapping<V, D> mapping : mappings) {
XMLGregorianCalendar mappingNextRecomputeTime = mapping.getNextRecomputeTime();
if (mappingNextRecomputeTime != null) {
if (nextRecomputeTime == null || nextRecomputeTime.compare(mappingNextRecomputeTime) == DatatypeConstants.GREATER) {
nextRecomputeTime = mappingNextRecomputeTime;
}
}
}
if (nextRecomputeTime != null) {
boolean alreadyHasTrigger = false;
if (params.getAPrioriTargetObject() != null) {
for (TriggerType trigger : params.getAPrioriTargetObject().asObjectable().getTrigger()) {
if (RecomputeTriggerHandler.HANDLER_URI.equals(trigger.getHandlerUri()) && nextRecomputeTime.equals(trigger.getTimestamp())) {
alreadyHasTrigger = true;
break;
}
}
}
if (!alreadyHasTrigger) {
PrismContainerDefinition<TriggerType> triggerContDef = targetObjectDefinition.findContainerDefinition(ObjectType.F_TRIGGER);
ContainerDelta<TriggerType> triggerDelta = triggerContDef.createEmptyDelta(new ItemPath(ObjectType.F_TRIGGER));
PrismContainerValue<TriggerType> triggerCVal = triggerContDef.createValue();
triggerDelta.addValueToAdd(triggerCVal);
TriggerType triggerType = triggerCVal.asContainerable();
triggerType.setTimestamp(nextRecomputeTime);
triggerType.setHandlerUri(RecomputeTriggerHandler.HANDLER_URI);
targetContext.swallowToSecondaryDelta(triggerDelta);
}
}
}
Aggregations