use of com.evolveum.midpoint.schema.expression.TypedValue in project midpoint by Evolveum.
the class Expression method processInnerVariables.
private VariablesMap processInnerVariables(VariablesMap variables, String contextDescription, Task task, OperationResult result) throws SchemaException, ObjectNotFoundException, CommunicationException, ConfigurationException, SecurityViolationException, ExpressionEvaluationException {
if (expressionType == null) {
// shortcut
return variables;
}
VariablesMap newVariables = new VariablesMap();
// We need to add actor variable before we switch user identity (runAs)
ExpressionUtil.addActorVariable(newVariables, securityContextManager, prismContext);
boolean actorDefined = newVariables.get(ExpressionConstants.VAR_ACTOR) != null;
for (Entry<String, TypedValue> entry : variables.entrySet()) {
String key = entry.getKey();
if (ExpressionConstants.VAR_ACTOR.equals(key) && actorDefined) {
// avoid pointless warning about redefined value of actor
continue;
}
newVariables.put(key, entry.getValue());
if (variables.isAlias(key)) {
newVariables.registerAlias(key, variables.getAliasResolution(key));
}
}
for (ExpressionVariableDefinitionType variableDefType : expressionType.getVariable()) {
String varName = variableDefType.getName().getLocalPart();
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, varObject.asPrismObject().getDefinition());
} else if (variableDefType.getValue() != null) {
// Only string is supported now
Object valueObject = variableDefType.getValue();
if (valueObject instanceof String) {
MutablePrismPropertyDefinition<Object> def = prismContext.definitionFactory().createPropertyDefinition(new ItemName(SchemaConstants.NS_C, varName), PrimitiveType.STRING.getQname());
newVariables.addVariableDefinition(varName, valueObject, def);
} else if (valueObject instanceof Element) {
MutablePrismPropertyDefinition<Object> def = prismContext.definitionFactory().createPropertyDefinition(new ItemName(SchemaConstants.NS_C, varName), PrimitiveType.STRING.getQname());
newVariables.addVariableDefinition(varName, ((Element) valueObject).getTextContent(), def);
} else if (valueObject instanceof RawType) {
ItemName varQName = new ItemName(SchemaConstants.NS_C, varName);
MutablePrismPropertyDefinition<Object> def = prismContext.definitionFactory().createPropertyDefinition(varQName, PrimitiveType.STRING.getQname());
newVariables.addVariableDefinition(varName, ((RawType) valueObject).getParsedValue(null, varQName), def);
} else {
throw new SchemaException("Unexpected type " + valueObject.getClass() + " in variable definition " + varName + " in " + contextDescription);
}
} else if (variableDefType.getPath() != null) {
ItemPath itemPath = variableDefType.getPath().getItemPath();
TypedValue resolvedValueAndDefinition = ExpressionUtil.resolvePathGetTypedValue(itemPath, variables, false, null, objectResolver, prismContext, contextDescription, task, result);
newVariables.put(varName, resolvedValueAndDefinition);
} else {
throw new SchemaException("No value for variable " + varName + " in " + contextDescription);
}
}
return newVariables;
}
use of com.evolveum.midpoint.schema.expression.TypedValue in project midpoint by Evolveum.
the class ExpressionUtil method convertItemToRealValues.
private static TypedValue<?> convertItemToRealValues(TypedValue<?> typedValue, PrismContext prismContext) {
Object value = typedValue.getValue();
if (value instanceof PrismObject<?>) {
typedValue.setValue(((PrismObject<?>) value).asObjectable());
return typedValue;
} else if (value instanceof PrismProperty<?>) {
PrismProperty<?> prop = (PrismProperty<?>) value;
PrismPropertyDefinition<?> def = prop.getDefinition();
if (def != null) {
if (def.isSingleValue()) {
return new TypedValue<>(prop.getRealValue(), def);
} else {
return new TypedValue<>(prop.getRealValues(), def);
}
} else {
// Guess, but we may be wrong
PrismPropertyDefinition<?> fakeDef = prismContext.definitionFactory().createPropertyDefinition(prop.getElementName(), PrimitiveType.STRING.getQname());
return new TypedValue<>(prop.getRealValues(), fakeDef);
}
} else if (value instanceof PrismReference) {
PrismReference ref = (PrismReference) value;
PrismReferenceDefinition def = ref.getDefinition();
if (def != null) {
if (def.isSingleValue()) {
return new TypedValue<>(ref.getRealValue(), def);
} else {
return new TypedValue<>(ref.getRealValues(), def);
}
} else {
PrismReferenceDefinition fakeDef = prismContext.definitionFactory().createReferenceDefinition(ref.getElementName(), ObjectType.COMPLEX_TYPE);
return new TypedValue<>(ref.getRealValues(), fakeDef);
}
} else if (value instanceof PrismContainer<?>) {
PrismContainer<?> container = (PrismContainer<?>) value;
PrismContainerDefinition<?> def = container.getDefinition();
Class<?> containerCompileTimeClass = container.getCompileTimeClass();
if (containerCompileTimeClass == null) {
// Dynamic schema. We do not have anything to convert to. Leave it as PrismContainer
if (def != null) {
return new TypedValue<>(container, def);
} else {
return new TypedValue<>(container, PrismContainer.class);
}
} else {
if (def != null) {
if (def.isSingleValue()) {
return new TypedValue<>(container.getRealValue(), def);
} else {
return new TypedValue<>(container.getRealValues(), def);
}
} else {
if (container.size() == 1) {
PrismContainerValue<?> cval = container.getValue();
// will this always work?
Containerable containerable = cval.asContainerable();
return new TypedValue<>(container.getRealValues(), containerable.getClass());
} else {
return new TypedValue<>(container.getRealValues(), Object.class);
}
}
}
} else {
// huh?
return typedValue;
}
}
use of com.evolveum.midpoint.schema.expression.TypedValue in project midpoint by Evolveum.
the class PathExpressionResolver method determineTypedValue.
@NotNull
private TypedValue<?> determineTypedValue(PrismContainer<?> rootContainer, boolean objectAlreadyFetched, OperationResult result) throws SchemaException, ObjectNotFoundException, SecurityViolationException, CommunicationException, ConfigurationException, ExpressionEvaluationException {
Object value;
PartiallyResolvedItem<PrismValue, ItemDefinition<?>> partiallyResolvedItem = rootContainer.findPartial(relativePath);
if (partiallyResolvedItem == null) {
value = null;
} else {
if (partiallyResolvedItem.getResidualPath() == null) {
value = partiallyResolvedItem.getItem();
} else {
Object parentValue = partiallyResolvedItem.getItem().getRealValue();
if (parentValue instanceof Structured) {
value = ((Structured) parentValue).resolve(partiallyResolvedItem.getResidualPath());
} else {
throw new SchemaException("No subpath " + partiallyResolvedItem.getResidualPath() + " in " + partiallyResolvedItem.getItem());
}
}
}
if (value instanceof Item && ((Item<?, ?>) value).isIncomplete()) {
if (objectAlreadyFetched) {
LOGGER.warn("Referencing incomplete item {} in {} but it is marked as incomplete even if the object was fully fetched", value, rootContainer);
} else if (!(rootContainer instanceof PrismObject)) {
LOGGER.warn("Unable to resolve incomplete item {} in {} because the root is not a prism object", value, rootContainer);
} else {
PrismObject<?> rootObject = (PrismObject<?>) rootContainer;
LOGGER.debug("Fetching {} because of incomplete item {}", rootObject, value);
// noinspection unchecked
Class<? extends ObjectType> type = (Class<? extends ObjectType>) rootObject.asObjectable().getClass();
// Let's retrieve everything (at least for now). In the future we could ask just for the single item.
Collection<SelectorOptions<GetOperationOptions>> options = SelectorOptions.createCollection(GetOperationOptions.createRetrieve());
ObjectType object = objectResolver.getObject(type, rootObject.getOid(), options, task, result);
return determineTypedValue(object.asPrismObject(), true, result);
}
}
ItemDefinition<?> def = determineItemDefinition(rootContainer.getDefinition(), relativePath);
if (def == null) {
throw new IllegalArgumentException("Cannot determine definition for '" + relativePath + "' from " + rootContainer + ", value: " + value);
}
return new TypedValue<>(value, def);
}
use of com.evolveum.midpoint.schema.expression.TypedValue in project midpoint by Evolveum.
the class Search method getFilterVariables.
public VariablesMap getFilterVariables(VariablesMap defaultVariables, PageBase pageBase) {
VariablesMap variables = defaultVariables == null ? new VariablesMap() : defaultVariables;
for (FilterSearchItem item : getFilterItems()) {
SearchFilterParameterType functionParameter = item.getPredefinedFilter().getParameter();
if (functionParameter != null && functionParameter.getType() != null) {
TypedValue value;
if (item.getInput() == null || item.getInput().getValue() == null) {
Class<?> inputClass = pageBase.getPrismContext().getSchemaRegistry().determineClassForType(functionParameter.getType());
value = new TypedValue(null, inputClass);
} else {
value = new TypedValue(item.getInput().getValue(), item.getInput().getValue().getClass());
}
variables.put(functionParameter.getName(), value);
}
}
return variables;
}
use of com.evolveum.midpoint.schema.expression.TypedValue in project midpoint by Evolveum.
the class PathExpressionEvaluation method getInitialResolveContextFromVariable.
private ResolutionContext getInitialResolveContextFromVariable() throws ExpressionEvaluationException {
String variableName = ItemPath.toVariableName(pathToResolve.first()).getLocalPart();
pathToResolve = pathToResolve.rest();
TypedValue variableValueAndDefinition = evaluator.findInSourcesAndVariables(context, variableName);
if (variableValueAndDefinition == null) {
throw new ExpressionEvaluationException("No variable with name " + variableName + " in " + context.getContextDescription());
}
Object variableValue = variableValueAndDefinition.getValue();
if (variableValue == null) {
return null;
} else if (variableValue instanceof Item || variableValue instanceof ItemDeltaItem<?, ?>) {
return IdiResolutionContext.fromAnyObject(variableValue);
} else if (variableValue instanceof PrismValue) {
return new ValueResolutionContext((PrismValue) variableValue, context.getContextDescription());
} else if (variableValueAndDefinition.getTypeClass().isAssignableFrom(variableValue.getClass())) {
return ValueResolutionContext.fromRealValue(variableValue, context.getContextDescription(), evaluator.getPrismContext());
} else {
throw new ExpressionEvaluationException("Unexpected variable value " + variableValue + " (" + variableValue.getClass() + ")");
}
}
Aggregations