Search in sources :

Example 11 with PrismProperty

use of com.evolveum.midpoint.prism.PrismProperty in project midpoint by Evolveum.

the class Jsr223ScriptEvaluator method evaluate.

@Override
public <T, V extends PrismValue> List<V> evaluate(ScriptExpressionEvaluatorType expressionType, ExpressionVariables variables, ItemDefinition outputDefinition, Function<Object, Object> additionalConvertor, ScriptExpressionReturnTypeType suggestedReturnType, ObjectResolver objectResolver, Collection<FunctionLibrary> functions, String contextDescription, Task task, OperationResult result) throws ExpressionEvaluationException, ObjectNotFoundException, ExpressionSyntaxException {
    Bindings bindings = convertToBindings(variables, objectResolver, functions, contextDescription, task, result);
    String codeString = expressionType.getCode();
    if (codeString == null) {
        throw new ExpressionEvaluationException("No script code in " + contextDescription);
    }
    boolean allowEmptyValues = false;
    if (expressionType.isAllowEmptyValues() != null) {
        allowEmptyValues = expressionType.isAllowEmptyValues();
    }
    CompiledScript compiledScript = createCompiledScript(codeString, contextDescription);
    Object evalRawResult;
    try {
        InternalMonitor.recordScriptExecution();
        evalRawResult = compiledScript.eval(bindings);
    } catch (Throwable e) {
        throw new ExpressionEvaluationException(e.getMessage() + " in " + contextDescription, e);
    }
    if (outputDefinition == null) {
        // No outputDefinition means "void" return type, we can return right now
        return null;
    }
    QName xsdReturnType = outputDefinition.getTypeName();
    Class<T> javaReturnType = XsdTypeMapper.toJavaType(xsdReturnType);
    if (javaReturnType == null) {
        javaReturnType = prismContext.getSchemaRegistry().getCompileTimeClass(xsdReturnType);
    }
    if (javaReturnType == null) {
        // TODO quick and dirty hack - because this could be because of enums defined in schema extension (MID-2399)
        // ...and enums (xsd:simpleType) are not parsed into ComplexTypeDefinitions
        javaReturnType = (Class) String.class;
    }
    List<V> pvals = new ArrayList<V>();
    // PrismProperty?
    if (evalRawResult instanceof Collection) {
        for (Object evalRawResultElement : (Collection) evalRawResult) {
            T evalResult = convertScalarResult(javaReturnType, additionalConvertor, evalRawResultElement, contextDescription);
            if (allowEmptyValues || !ExpressionUtil.isEmpty(evalResult)) {
                pvals.add((V) ExpressionUtil.convertToPrismValue(evalResult, outputDefinition, contextDescription, prismContext));
            }
        }
    } else if (evalRawResult instanceof PrismProperty<?>) {
        pvals.addAll((Collection<? extends V>) PrismPropertyValue.cloneCollection(((PrismProperty<T>) evalRawResult).getValues()));
    } else {
        T evalResult = convertScalarResult(javaReturnType, additionalConvertor, evalRawResult, contextDescription);
        if (allowEmptyValues || !ExpressionUtil.isEmpty(evalResult)) {
            pvals.add((V) ExpressionUtil.convertToPrismValue(evalResult, outputDefinition, contextDescription, prismContext));
        }
    }
    return pvals;
}
Also used : CompiledScript(javax.script.CompiledScript) ExpressionEvaluationException(com.evolveum.midpoint.util.exception.ExpressionEvaluationException) QName(javax.xml.namespace.QName) ArrayList(java.util.ArrayList) Bindings(javax.script.Bindings) PrismProperty(com.evolveum.midpoint.prism.PrismProperty) Collection(java.util.Collection)

Example 12 with PrismProperty

use of com.evolveum.midpoint.prism.PrismProperty in project midpoint by Evolveum.

the class ShadowManager method createSearchShadowQuery.

private ObjectQuery createSearchShadowQuery(ProvisioningContext ctx, PrismObject<ShadowType> resourceShadow, PrismContext prismContext, OperationResult parentResult) throws SchemaException, ObjectNotFoundException, CommunicationException, ConfigurationException, ExpressionEvaluationException {
    ResourceAttributeContainer attributesContainer = ShadowUtil.getAttributesContainer(resourceShadow);
    PrismProperty identifier = attributesContainer.getPrimaryIdentifier();
    Collection<PrismPropertyValue<Object>> idValues = identifier.getValues();
    // Only one value is supported for an identifier
    if (idValues.size() > 1) {
        // TODO: This should probably be switched to checked exception later
        throw new IllegalArgumentException("More than one identifier value is not supported");
    }
    if (idValues.size() < 1) {
        // TODO: This should probably be switched to checked exception later
        throw new IllegalArgumentException("The identifier has no value");
    }
    // We have all the data, we can construct the filter now
    try {
        // TODO TODO TODO TODO: set matching rule instead of null
        PrismPropertyDefinition def = identifier.getDefinition();
        return QueryBuilder.queryFor(ShadowType.class, prismContext).itemWithDef(def, ShadowType.F_ATTRIBUTES, def.getName()).eq(getNormalizedValue(identifier, ctx.getObjectClassDefinition())).and().item(ShadowType.F_OBJECT_CLASS).eq(resourceShadow.getPropertyRealValue(ShadowType.F_OBJECT_CLASS, QName.class)).and().item(ShadowType.F_RESOURCE_REF).ref(ctx.getResourceOid()).build();
    } catch (SchemaException e) {
        throw new SchemaException("Schema error while creating search filter: " + e.getMessage(), e);
    }
}
Also used : SchemaException(com.evolveum.midpoint.util.exception.SchemaException) PrismProperty(com.evolveum.midpoint.prism.PrismProperty) PrismPropertyDefinition(com.evolveum.midpoint.prism.PrismPropertyDefinition) QName(javax.xml.namespace.QName) ResourceAttributeContainer(com.evolveum.midpoint.schema.processor.ResourceAttributeContainer) PrismPropertyValue(com.evolveum.midpoint.prism.PrismPropertyValue)

Example 13 with PrismProperty

use of com.evolveum.midpoint.prism.PrismProperty in project midpoint by Evolveum.

the class AbstractBasicDummyTest method test021Configuration.

@Test
public void test021Configuration() throws Exception {
    final String TEST_NAME = "test021Configuration";
    TestUtil.displayTestTile(TEST_NAME);
    // GIVEN
    OperationResult result = new OperationResult(AbstractBasicDummyTest.class.getName() + "." + TEST_NAME);
    // WHEN
    resource = provisioningService.getObject(ResourceType.class, RESOURCE_DUMMY_OID, null, null, result);
    resourceType = resource.asObjectable();
    // THEN
    result.computeStatus();
    display("getObject result", result);
    TestUtil.assertSuccess(result);
    // There may be one parse. Previous test have changed the resource version
    // Schema for this version will not be re-parsed until getObject is tried
    assertResourceSchemaParseCountIncrement(1);
    assertResourceCacheMissesIncrement(1);
    PrismContainer<Containerable> configurationContainer = resource.findContainer(ResourceType.F_CONNECTOR_CONFIGURATION);
    assertNotNull("No configuration container", configurationContainer);
    PrismContainerDefinition confContDef = configurationContainer.getDefinition();
    assertNotNull("No configuration container definition", confContDef);
    PrismContainer confingurationPropertiesContainer = configurationContainer.findContainer(SchemaConstants.CONNECTOR_SCHEMA_CONFIGURATION_PROPERTIES_ELEMENT_QNAME);
    assertNotNull("No configuration properties container", confingurationPropertiesContainer);
    PrismContainerDefinition confPropsDef = confingurationPropertiesContainer.getDefinition();
    assertNotNull("No configuration properties container definition", confPropsDef);
    List<PrismProperty<?>> configurationProperties = confingurationPropertiesContainer.getValue().getItems();
    assertFalse("No configuration properties", configurationProperties.isEmpty());
    for (PrismProperty<?> confProp : configurationProperties) {
        PrismPropertyDefinition confPropDef = confProp.getDefinition();
        assertNotNull("No definition for configuration property " + confProp, confPropDef);
        assertFalse("Configuration property " + confProp + " is raw", confProp.isRaw());
        assertConfigurationProperty(confProp);
    }
    // The useless configuration variables should be reflected to the resource now
    assertEquals("Wrong useless string", "Shiver me timbers!", dummyResource.getUselessString());
    assertEquals("Wrong guarded useless string", "Dead men tell no tales", dummyResource.getUselessGuardedString());
    resource.checkConsistence();
    rememberSchemaMetadata(resource);
    rememberConnectorInstance(resource);
    assertSteadyResource();
}
Also used : PrismProperty(com.evolveum.midpoint.prism.PrismProperty) PrismPropertyDefinition(com.evolveum.midpoint.prism.PrismPropertyDefinition) PrismContainerDefinition(com.evolveum.midpoint.prism.PrismContainerDefinition) PrismContainer(com.evolveum.midpoint.prism.PrismContainer) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) ResourceType(com.evolveum.midpoint.xml.ns._public.common.common_3.ResourceType) Containerable(com.evolveum.midpoint.prism.Containerable) Test(org.testng.annotations.Test)

Example 14 with PrismProperty

use of com.evolveum.midpoint.prism.PrismProperty in project midpoint by Evolveum.

the class AbstractIntegrationTest method assertNoAttribute.

protected void assertNoAttribute(PrismObject<ResourceType> resource, ShadowType shadow, QName attrQname) {
    PrismContainer<?> attributesContainer = shadow.asPrismObject().findContainer(ShadowType.F_ATTRIBUTES);
    if (attributesContainer == null || attributesContainer.isEmpty()) {
        return;
    }
    PrismProperty attribute = attributesContainer.findProperty(attrQname);
    assertNull("Unexpected attribute " + attrQname + " in " + shadow + ": " + attribute, attribute);
}
Also used : PrismProperty(com.evolveum.midpoint.prism.PrismProperty)

Example 15 with PrismProperty

use of com.evolveum.midpoint.prism.PrismProperty in project midpoint by Evolveum.

the class ProvisioningServiceImpl method synchronize.

@SuppressWarnings("rawtypes")
@Override
public int synchronize(ResourceShadowDiscriminator shadowCoordinates, Task task, OperationResult parentResult) throws ObjectNotFoundException, CommunicationException, SchemaException, ConfigurationException, SecurityViolationException, ExpressionEvaluationException {
    Validate.notNull(shadowCoordinates, "Coordinates oid must not be null.");
    String resourceOid = shadowCoordinates.getResourceOid();
    Validate.notNull(resourceOid, "Resource oid must not be null.");
    Validate.notNull(task, "Task must not be null.");
    Validate.notNull(parentResult, "Operation result must not be null.");
    OperationResult result = parentResult.createSubresult(ProvisioningService.class.getName() + ".synchronize");
    result.addParam(OperationResult.PARAM_OID, resourceOid);
    result.addParam(OperationResult.PARAM_TASK, task.toString());
    int processedChanges = 0;
    try {
        // Resolve resource
        PrismObject<ResourceType> resource = getObject(ResourceType.class, resourceOid, null, task, result);
        ResourceType resourceType = resource.asObjectable();
        LOGGER.trace("**PROVISIONING: Start synchronization of resource {} ", resourceType);
        // getting token form task
        PrismProperty tokenProperty = getTokenProperty(shadowCoordinates, task, result);
        if (LOGGER.isTraceEnabled()) {
            LOGGER.trace("**PROVISIONING: Got token property: {} from the task extension.", SchemaDebugUtil.prettyPrint(tokenProperty));
        }
        processedChanges = getShadowCache(Mode.STANDARD).synchronize(shadowCoordinates, tokenProperty, task, result);
        LOGGER.debug("Synchronization of {} done, token {}, {} changes", resource, tokenProperty, processedChanges);
    } catch (ObjectNotFoundException e) {
        ProvisioningUtil.recordFatalError(LOGGER, result, "Synchronization error: object not found: " + e.getMessage(), e);
        throw e;
    } catch (CommunicationException e) {
        ProvisioningUtil.recordFatalError(LOGGER, result, "Synchronization error: communication problem: " + e.getMessage(), e);
        throw e;
    } catch (ObjectAlreadyExistsException e) {
        ProvisioningUtil.recordFatalError(LOGGER, result, "Synchronization error: object already exists problem: " + e.getMessage(), e);
        throw new SystemException(e);
    } catch (GenericFrameworkException e) {
        ProvisioningUtil.recordFatalError(LOGGER, result, "Synchronization error: generic connector framework error: " + e.getMessage(), e);
        throw new GenericConnectorException(e.getMessage(), e);
    } catch (SchemaException e) {
        ProvisioningUtil.recordFatalError(LOGGER, result, "Synchronization error: schema problem: " + e.getMessage(), e);
        throw e;
    } catch (SecurityViolationException e) {
        ProvisioningUtil.recordFatalError(LOGGER, result, "Synchronization error: security violation: " + e.getMessage(), e);
        throw e;
    } catch (ConfigurationException e) {
        ProvisioningUtil.recordFatalError(LOGGER, result, "Synchronization error: configuration problem: " + e.getMessage(), e);
        throw e;
    } catch (RuntimeException e) {
        ProvisioningUtil.recordFatalError(LOGGER, result, "Synchronization error: unexpected problem: " + e.getMessage(), e);
        throw e;
    } catch (ExpressionEvaluationException e) {
        ProvisioningUtil.recordFatalError(LOGGER, result, "Synchronization error: expression error: " + e.getMessage(), e);
        throw e;
    }
    result.recordSuccess();
    result.cleanupResult();
    return processedChanges;
}
Also used : SchemaException(com.evolveum.midpoint.util.exception.SchemaException) ExpressionEvaluationException(com.evolveum.midpoint.util.exception.ExpressionEvaluationException) CommunicationException(com.evolveum.midpoint.util.exception.CommunicationException) GenericFrameworkException(com.evolveum.midpoint.provisioning.ucf.api.GenericFrameworkException) SecurityViolationException(com.evolveum.midpoint.util.exception.SecurityViolationException) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) ResourceType(com.evolveum.midpoint.xml.ns._public.common.common_3.ResourceType) LabeledString(com.evolveum.midpoint.schema.LabeledString) PrismProperty(com.evolveum.midpoint.prism.PrismProperty) SystemException(com.evolveum.midpoint.util.exception.SystemException) GenericConnectorException(com.evolveum.midpoint.provisioning.api.GenericConnectorException) ConfigurationException(com.evolveum.midpoint.util.exception.ConfigurationException) ObjectNotFoundException(com.evolveum.midpoint.util.exception.ObjectNotFoundException) ObjectAlreadyExistsException(com.evolveum.midpoint.util.exception.ObjectAlreadyExistsException)

Aggregations

PrismProperty (com.evolveum.midpoint.prism.PrismProperty)41 PrismObject (com.evolveum.midpoint.prism.PrismObject)14 ItemPath (com.evolveum.midpoint.prism.path.ItemPath)14 SchemaException (com.evolveum.midpoint.util.exception.SchemaException)13 QName (javax.xml.namespace.QName)13 OperationResult (com.evolveum.midpoint.schema.result.OperationResult)12 ArrayList (java.util.ArrayList)11 PolyString (com.evolveum.midpoint.prism.polystring.PolyString)8 PrismContainer (com.evolveum.midpoint.prism.PrismContainer)7 SystemException (com.evolveum.midpoint.util.exception.SystemException)7 Containerable (com.evolveum.midpoint.prism.Containerable)6 PrismPropertyValue (com.evolveum.midpoint.prism.PrismPropertyValue)6 ConfigurationException (com.evolveum.midpoint.util.exception.ConfigurationException)6 ObjectNotFoundException (com.evolveum.midpoint.util.exception.ObjectNotFoundException)6 Test (org.testng.annotations.Test)6 PropertyDelta (com.evolveum.midpoint.prism.delta.PropertyDelta)5 ExpressionEvaluationException (com.evolveum.midpoint.util.exception.ExpressionEvaluationException)5 Item (com.evolveum.midpoint.prism.Item)4 PrismPropertyDefinition (com.evolveum.midpoint.prism.PrismPropertyDefinition)4 Task (com.evolveum.midpoint.task.api.Task)4