Search in sources :

Example 21 with RawType

use of com.evolveum.prism.xml.ns._public.types_3.RawType 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;
}
Also used : JAXBElement(javax.xml.bind.JAXBElement) Element(org.w3c.dom.Element) ItemName(com.evolveum.midpoint.prism.path.ItemName) VariablesMap(com.evolveum.midpoint.schema.expression.VariablesMap) RawType(com.evolveum.prism.xml.ns._public.types_3.RawType) TypedValue(com.evolveum.midpoint.schema.expression.TypedValue) ItemPath(com.evolveum.midpoint.prism.path.ItemPath)

Example 22 with RawType

use of com.evolveum.prism.xml.ns._public.types_3.RawType in project midpoint by Evolveum.

the class PropertyRestriction method adaptValueType.

private Object adaptValueType(Object value, ValueFilter filter) throws QueryException {
    Class<?> expectedType = linkDefinition.getTargetDefinition().getJaxbClass();
    if (expectedType == null || value == null) {
        // nothing to check here
        return value;
    }
    Class<?> expectedWrappedType;
    if (expectedType.isPrimitive()) {
        expectedWrappedType = ClassUtils.primitiveToWrapper(expectedType);
    } else {
        expectedWrappedType = expectedType;
    }
    Object adaptedValue;
    // attempt to fix value type for polystring (if it was string in filter we create polystring from it)
    if (PolyString.class.equals(expectedWrappedType) && value instanceof String) {
        LOGGER.debug("Trying to query PolyString value but filter contains String '{}'.", filter);
        String orig = (String) value;
        adaptedValue = new PolyString(orig, context.getPrismContext().getDefaultPolyStringNormalizer().normalize(orig));
    } else if (PolyString.class.equals(expectedWrappedType) && value instanceof PolyStringType) {
        // attempt to fix value type for polystring (if it was polystring type in filter we create polystring from it)
        LOGGER.debug("Trying to query PolyString value but filter contains PolyStringType '{}'.", filter);
        PolyStringType type = (PolyStringType) value;
        adaptedValue = new PolyString(type.getOrig(), type.getNorm());
    } else if (String.class.equals(expectedWrappedType) && value instanceof QName) {
        // eg. shadow/objectClass
        adaptedValue = RUtil.qnameToString((QName) value);
    } else if (value instanceof RawType) {
        // MID-3850: but it's quite a workaround. Maybe we should treat RawType's earlier than this.
        try {
            adaptedValue = ((RawType) value).getParsedRealValue(expectedWrappedType);
        } catch (SchemaException e) {
            throw new QueryException("Couldn't parse value " + value + " as " + expectedWrappedType + ": " + e.getMessage(), e);
        }
    } else {
        adaptedValue = value;
    }
    if (expectedWrappedType.isAssignableFrom(adaptedValue.getClass())) {
        return adaptedValue;
    } else {
        throw new QueryException("Value should be type of '" + expectedWrappedType + "' but it's '" + value.getClass() + "', filter '" + filter + "'.");
    }
}
Also used : PolyStringType(com.evolveum.prism.xml.ns._public.types_3.PolyStringType) SchemaException(com.evolveum.midpoint.util.exception.SchemaException) QueryException(com.evolveum.midpoint.repo.sqlbase.QueryException) QName(javax.xml.namespace.QName) PolyString(com.evolveum.midpoint.prism.polystring.PolyString) PolyString(com.evolveum.midpoint.prism.polystring.PolyString) RawType(com.evolveum.prism.xml.ns._public.types_3.RawType)

Example 23 with RawType

use of com.evolveum.prism.xml.ns._public.types_3.RawType in project midpoint by Evolveum.

the class TestConsistencyMechanism method test511AssignAccountMorgan.

/**
 * assign account to the user morgan. Account with the same 'uid' (not dn, nut other secondary identifier already exists)
 * account should be linked to the user.
 */
@Test
public void test511AssignAccountMorgan() throws Exception {
    // GIVEN
    openDJController.assumeRunning();
    Task task = getTestTask();
    OperationResult result = task.getResult();
    dummyAuditService.clear();
    // prepare new OU in opendj
    openDJController.addEntryFromLdifFile(LDIF_CREATE_USERS_OU_FILE);
    PrismObject<UserType> user = repositoryService.getObject(UserType.class, USER_MORGAN_OID, null, result);
    display("User Morgan: ", user);
    ExpressionType expression = new ExpressionType();
    ObjectFactory of = new ObjectFactory();
    RawType raw = new RawType(prismContext.xnodeFactory().primitive("uid=morgan,ou=users,dc=example,dc=com").frozen(), prismContext);
    JAXBElement<?> val = of.createValue(raw);
    expression.getExpressionEvaluator().add(val);
    MappingType mapping = new MappingType();
    mapping.setExpression(expression);
    ResourceAttributeDefinitionType attrDefType = new ResourceAttributeDefinitionType();
    attrDefType.setRef(new ItemPathType(ItemPath.create(getOpenDjSecondaryIdentifierQName())));
    attrDefType.setOutbound(mapping);
    ConstructionType construction = new ConstructionType();
    construction.getAttribute().add(attrDefType);
    construction.setResourceRef(ObjectTypeUtil.createObjectRef(resourceTypeOpenDjrepo, prismContext));
    AssignmentType assignment = new AssignmentType();
    assignment.setConstruction(construction);
    // noinspection unchecked
    ObjectDelta<UserType> userDelta = prismContext.deltaFactory().object().createModificationAddContainer(UserType.class, USER_MORGAN_OID, UserType.F_ASSIGNMENT, assignment.asPrismContainerValue());
    Collection<ObjectDelta<? extends ObjectType>> deltas = MiscSchemaUtil.createCollection(userDelta);
    // WHEN
    when();
    modelService.executeChanges(deltas, null, task, result);
    // THEN
    then();
    result.computeStatus();
    // assertEquals("Expected handled error but got: " + result.getStatus(), OperationResultStatus.HANDLED_ERROR, result.getStatus());
    PrismObject<UserType> userMorgan = modelService.getObject(UserType.class, USER_MORGAN_OID, null, task, result);
    display("User morgan after", userMorgan);
    UserType userMorganType = userMorgan.asObjectable();
    assertEquals("Unexpected number of accountRefs", 1, userMorganType.getLinkRef().size());
    String accountOid = userMorganType.getLinkRef().iterator().next().getOid();
    // Check shadow
    PrismObject<ShadowType> accountShadow = repositoryService.getObject(ShadowType.class, accountOid, null, result);
    provisioningService.applyDefinition(accountShadow, task, result);
    assertShadowRepo(accountShadow, accountOid, "uid=morgan,ou=users,dc=example,dc=com", resourceTypeOpenDjrepo, RESOURCE_OPENDJ_ACCOUNT_OBJECTCLASS);
    // Check account
    PrismObject<ShadowType> accountModel = modelService.getObject(ShadowType.class, accountOid, null, task, result);
    assertShadowModel(accountModel, accountOid, "uid=morgan,ou=users,dc=example,dc=com", resourceTypeOpenDjrepo, RESOURCE_OPENDJ_ACCOUNT_OBJECTCLASS);
    ResourceAttribute<?> attributes = ShadowUtil.getAttribute(accountModel, new QName(MidPointConstants.NS_RI, "uid"));
    assertEquals("morgan", attributes.getAnyRealValue());
// TODO: check OpenDJ Account
}
Also used : Task(com.evolveum.midpoint.task.api.Task) ItemPathType(com.evolveum.prism.xml.ns._public.types_3.ItemPathType) QName(javax.xml.namespace.QName) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) PolyString(com.evolveum.midpoint.prism.polystring.PolyString) ObjectFactory(com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectFactory) RawType(com.evolveum.prism.xml.ns._public.types_3.RawType) Test(org.testng.annotations.Test) AbstractModelIntegrationTest(com.evolveum.midpoint.model.test.AbstractModelIntegrationTest)

Example 24 with RawType

use of com.evolveum.prism.xml.ns._public.types_3.RawType in project midpoint by Evolveum.

the class RAnyConverter method extractValue.

@NotNull
private <T> T extractValue(PrismPropertyValue value, Class<T> returnType) throws SchemaException {
    ItemDefinition definition = value.getParent().getDefinition();
    // todo raw types
    Object object = value.getValue();
    if (object instanceof Element) {
        object = getRealRepoValue(definition, (Element) object, prismContext);
    } else if (object instanceof RawType) {
        RawType raw = (RawType) object;
        // todo this can return null!
        object = raw.getParsedRealValue(returnType);
    } else {
        object = getAggregatedRepoObject(object);
    }
    if (returnType.isAssignableFrom(object.getClass())) {
        // noinspection unchecked
        return (T) object;
    }
    throw new IllegalStateException("Can't extract value for saving from prism property value " + value + " expected return type " + returnType + ", actual type " + (object == null ? null : object.getClass()));
}
Also used : Element(org.w3c.dom.Element) RObject(com.evolveum.midpoint.repo.sql.data.common.RObject) RawType(com.evolveum.prism.xml.ns._public.types_3.RawType) NotNull(org.jetbrains.annotations.NotNull)

Example 25 with RawType

use of com.evolveum.prism.xml.ns._public.types_3.RawType in project midpoint by Evolveum.

the class TestAbstractRestService method extractItemProcessingResults.

private <X extends OperationSpecificData> List<ItemProcessingResult<X>> extractItemProcessingResults(ExecuteScriptResponseType response, Function<Object, X> operationDataExtractor) throws SchemaException {
    List<PipelineItemType> outputItems = response.getOutput().getDataOutput().getItem();
    List<ItemProcessingResult<X>> extractedResults = new ArrayList<>(outputItems.size());
    for (PipelineItemType outputItem : outputItems) {
        ItemProcessingResult<X> extractedResult = new ItemProcessingResult<>();
        Object value = outputItem.getValue();
        if (value instanceof RawType) {
            value = ((RawType) value).getParsedRealValue(Object.class);
        }
        if (value instanceof ObjectType) {
            ObjectType object = (ObjectType) value;
            extractedResult.oid = object.getOid();
            extractedResult.name = PolyString.getOrig(object.getName());
            if (operationDataExtractor != null) {
                extractedResult.data = operationDataExtractor.apply(value);
            }
        } else if (value instanceof Referencable) {
            extractedResult.oid = ((Referencable) value).getOid();
        } else {
            throw new IllegalStateException("Unexpected item value: " + value);
        }
        if (outputItem.getResult() != null) {
            extractedResult.status = outputItem.getResult().getStatus();
            extractedResult.message = outputItem.getResult().getMessage();
        }
        extractedResults.add(extractedResult);
    }
    return extractedResults;
}
Also used : Referencable(com.evolveum.midpoint.prism.Referencable) ArrayList(java.util.ArrayList) PipelineItemType(com.evolveum.midpoint.xml.ns._public.model.scripting_3.PipelineItemType) PrismObject(com.evolveum.midpoint.prism.PrismObject) RawType(com.evolveum.prism.xml.ns._public.types_3.RawType)

Aggregations

RawType (com.evolveum.prism.xml.ns._public.types_3.RawType)62 QName (javax.xml.namespace.QName)22 Test (org.testng.annotations.Test)19 JAXBElement (javax.xml.bind.JAXBElement)18 OperationResult (com.evolveum.midpoint.schema.result.OperationResult)16 ItemPath (com.evolveum.midpoint.prism.path.ItemPath)13 SchemaException (com.evolveum.midpoint.util.exception.SchemaException)10 ItemPathType (com.evolveum.prism.xml.ns._public.types_3.ItemPathType)10 AbstractModelIntegrationTest (com.evolveum.midpoint.model.test.AbstractModelIntegrationTest)9 ItemDeltaType (com.evolveum.prism.xml.ns._public.types_3.ItemDeltaType)9 ObjectDeltaType (com.evolveum.prism.xml.ns._public.types_3.ObjectDeltaType)9 ArrayList (java.util.ArrayList)9 PolyString (com.evolveum.midpoint.prism.polystring.PolyString)8 Task (com.evolveum.midpoint.task.api.Task)8 UserType (com.evolveum.midpoint.xml.ns._public.common.common_3.UserType)7 ProtectedStringType (com.evolveum.prism.xml.ns._public.types_3.ProtectedStringType)7 PrismAsserts.assertEqualsPolyString (com.evolveum.midpoint.prism.util.PrismAsserts.assertEqualsPolyString)6 PrimitiveXNode (com.evolveum.midpoint.prism.xnode.PrimitiveXNode)6 TaskType (com.evolveum.midpoint.xml.ns._public.common.common_3.TaskType)6 PrismObject (com.evolveum.midpoint.prism.PrismObject)5