Search in sources :

Example 1 with ActionExpressionType

use of com.evolveum.midpoint.xml.ns._public.model.scripting_3.ActionExpressionType in project midpoint by Evolveum.

the class ScriptExecutor method execute.

@Override
public PipelineData execute(ActionExpressionType expression, PipelineData input, ExecutionContext context, OperationResult globalResult) throws ScriptExecutionException {
    checkRootAuthorization(globalResult, NAME);
    ScriptExpressionEvaluatorType script = expressionHelper.getSingleArgumentValue(expression.getParameter(), PARAM_SCRIPT, true, true, NAME, input, context, ScriptExpressionEvaluatorType.class, globalResult);
    String outputItem = expressionHelper.getSingleArgumentValue(expression.getParameter(), PARAM_OUTPUT_ITEM, false, false, NAME, input, context, String.class, globalResult);
    ItemDefinition<?> outputDefinition = getItemDefinition(outputItem);
    ScriptExpression scriptExpression;
    try {
        scriptExpression = scriptExpressionFactory.createScriptExpression(script, outputDefinition, "script");
    } catch (ExpressionSyntaxException e) {
        throw new ScriptExecutionException("Couldn't parse script expression: " + e.getMessage(), e);
    }
    PipelineData output = PipelineData.createEmpty();
    for (PipelineItem item : input.getData()) {
        PrismValue value = item.getValue();
        OperationResult result = operationsHelper.createActionResult(item, this, context, globalResult);
        context.checkTaskStop();
        String valueDescription;
        long started;
        if (value instanceof PrismObjectValue) {
            started = operationsHelper.recordStart(context, asObjectType(value));
            valueDescription = asObjectType(value).asPrismObject().toString();
        } else {
            started = 0;
            valueDescription = value.toHumanReadableString();
        }
        Throwable exception = null;
        try {
            Object outObject = executeScript(scriptExpression, value, context, result);
            if (outObject != null) {
                addToData(outObject, item.getResult(), output);
            } else {
                output.add(item);
            }
            if (value instanceof PrismObjectValue) {
                operationsHelper.recordEnd(context, asObjectType(value), started, null);
            }
        } catch (Throwable ex) {
            if (value instanceof PrismObjectValue) {
                operationsHelper.recordEnd(context, asObjectType(value), started, ex);
            }
            exception = processActionException(ex, NAME, value, context);
        }
        context.println((exception != null ? "Attempted to execute " : "Executed ") + "script on " + valueDescription + exceptionSuffix(exception));
        operationsHelper.trimAndCloneResult(result, globalResult, context);
    }
    return output;
}
Also used : ScriptExecutionException(com.evolveum.midpoint.model.api.ScriptExecutionException) PipelineData(com.evolveum.midpoint.model.impl.scripting.PipelineData) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) ScriptExpressionEvaluatorType(com.evolveum.midpoint.xml.ns._public.common.common_3.ScriptExpressionEvaluatorType) ScriptExpression(com.evolveum.midpoint.model.common.expression.script.ScriptExpression) ExpressionSyntaxException(com.evolveum.midpoint.repo.common.expression.ExpressionSyntaxException) PipelineItem(com.evolveum.midpoint.model.api.PipelineItem)

Example 2 with ActionExpressionType

use of com.evolveum.midpoint.xml.ns._public.model.scripting_3.ActionExpressionType in project midpoint by Evolveum.

the class ValidateExecutor method execute.

@Override
public PipelineData execute(ActionExpressionType expression, PipelineData input, ExecutionContext context, OperationResult globalResult) throws ScriptExecutionException {
    PipelineData output = PipelineData.createEmpty();
    for (PipelineItem item : input.getData()) {
        PrismValue value = item.getValue();
        OperationResult result = operationsHelper.createActionResult(item, this, context, globalResult);
        context.checkTaskStop();
        if (value instanceof PrismObjectValue && ((PrismObjectValue) value).asObjectable() instanceof ResourceType) {
            PrismObject<ResourceType> resourceTypePrismObject = ((PrismObjectValue) value).asPrismObject();
            ResourceType resourceType = resourceTypePrismObject.asObjectable();
            long started = operationsHelper.recordStart(context, resourceType);
            try {
                ValidationResult validationResult = resourceValidator.validate(resourceTypePrismObject, Scope.THOROUGH, null, context.getTask(), result);
                PrismContainer pc = prismContext.getSchemaRegistry().findContainerDefinitionByElementName(SchemaConstantsGenerated.C_VALIDATION_RESULT).instantiate();
                pc.add(validationResult.toValidationResultType().asPrismContainerValue());
                context.println("Validated " + resourceTypePrismObject + ": " + validationResult.getIssues().size() + " issue(s)");
                operationsHelper.recordEnd(context, resourceType, started, null);
                output.add(new PipelineItem(pc.getValue(), item.getResult()));
            } catch (SchemaException | RuntimeException e) {
                operationsHelper.recordEnd(context, resourceType, started, e);
                context.println("Error validation " + resourceTypePrismObject + ": " + e.getMessage());
                //noinspection ThrowableNotThrown
                processActionException(e, NAME, value, context);
                output.add(item);
            }
        } else {
            //noinspection ThrowableNotThrown
            processActionException(new ScriptExecutionException("Item is not a PrismObject<ResourceType>"), NAME, value, context);
        }
        operationsHelper.trimAndCloneResult(result, globalResult, context);
    }
    return output;
}
Also used : ScriptExecutionException(com.evolveum.midpoint.model.api.ScriptExecutionException) PipelineData(com.evolveum.midpoint.model.impl.scripting.PipelineData) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) ResourceType(com.evolveum.midpoint.xml.ns._public.common.common_3.ResourceType) ValidationResult(com.evolveum.midpoint.model.api.validator.ValidationResult) PipelineItem(com.evolveum.midpoint.model.api.PipelineItem)

Example 3 with ActionExpressionType

use of com.evolveum.midpoint.xml.ns._public.model.scripting_3.ActionExpressionType in project midpoint by Evolveum.

the class AssignExecutor method execute.

@Override
public PipelineData execute(ActionExpressionType expression, PipelineData input, ExecutionContext context, OperationResult globalResult) throws ScriptExecutionException {
    boolean raw = getParamRaw(expression, input, context, globalResult);
    boolean dryRun = getParamDryRun(expression, input, context, globalResult);
    ActionParameterValueType resourceParameterValue = expressionHelper.getArgument(expression.getParameter(), PARAM_RESOURCE, false, false, NAME);
    ActionParameterValueType roleParameterValue = expressionHelper.getArgument(expression.getParameter(), PARAM_ROLE, false, false, NAME);
    Collection<ObjectReferenceType> resources;
    if (resourceParameterValue != null) {
        PipelineData data = expressionHelper.evaluateParameter(resourceParameterValue, null, input, context, globalResult);
        resources = data.getDataAsReferences(ResourceType.COMPLEX_TYPE);
    } else {
        resources = null;
    }
    Collection<ObjectReferenceType> roles;
    if (roleParameterValue != null) {
        PipelineData data = expressionHelper.evaluateParameter(roleParameterValue, null, input, context, globalResult);
        roles = data.getDataAsReferences(RoleType.COMPLEX_TYPE);
    } else {
        roles = null;
    }
    if (resources == null && roles == null) {
        throw new ScriptExecutionException("Nothing to assign: neither resource nor role specified");
    }
    for (PipelineItem item : input.getData()) {
        PrismValue value = item.getValue();
        OperationResult result = operationsHelper.createActionResult(item, this, context, globalResult);
        context.checkTaskStop();
        if (value instanceof PrismObjectValue && ((PrismObjectValue) value).asObjectable() instanceof FocusType) {
            PrismObject<? extends ObjectType> prismObject = ((PrismObjectValue) value).asPrismObject();
            ObjectType objectType = prismObject.asObjectable();
            long started = operationsHelper.recordStart(context, objectType);
            Throwable exception = null;
            try {
                operationsHelper.applyDelta(createDelta(objectType, resources, roles), operationsHelper.createExecutionOptions(raw), dryRun, context, result);
                operationsHelper.recordEnd(context, objectType, started, null);
            } catch (Throwable ex) {
                operationsHelper.recordEnd(context, objectType, started, ex);
                exception = processActionException(ex, NAME, value, context);
            }
            context.println((exception != null ? "Attempted to modify " : "Modified ") + prismObject.toString() + rawDrySuffix(raw, dryRun) + exceptionSuffix(exception));
        } else {
            //noinspection ThrowableNotThrown
            processActionException(new ScriptExecutionException("Item is not a PrismObject of FocusType"), NAME, value, context);
        }
        operationsHelper.trimAndCloneResult(result, globalResult, context);
    }
    // TODO updated objects?
    return input;
}
Also used : ScriptExecutionException(com.evolveum.midpoint.model.api.ScriptExecutionException) PipelineData(com.evolveum.midpoint.model.impl.scripting.PipelineData) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) ActionParameterValueType(com.evolveum.midpoint.xml.ns._public.model.scripting_3.ActionParameterValueType) ObjectType(com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectType) ObjectReferenceType(com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectReferenceType) PipelineItem(com.evolveum.midpoint.model.api.PipelineItem) FocusType(com.evolveum.midpoint.xml.ns._public.common.common_3.FocusType)

Example 4 with ActionExpressionType

use of com.evolveum.midpoint.xml.ns._public.model.scripting_3.ActionExpressionType in project midpoint by Evolveum.

the class DeleteExecutor method execute.

@Override
public PipelineData execute(ActionExpressionType expression, PipelineData input, ExecutionContext context, OperationResult globalResult) throws ScriptExecutionException {
    boolean raw = getParamRaw(expression, input, context, globalResult);
    boolean dryRun = getParamDryRun(expression, input, context, globalResult);
    for (PipelineItem item : input.getData()) {
        PrismValue value = item.getValue();
        OperationResult result = operationsHelper.createActionResult(item, this, context, globalResult);
        context.checkTaskStop();
        if (value instanceof PrismObjectValue) {
            PrismObject<? extends ObjectType> prismObject = ((PrismObjectValue) value).asPrismObject();
            ObjectType objectType = prismObject.asObjectable();
            long started = operationsHelper.recordStart(context, objectType);
            Throwable exception = null;
            try {
                operationsHelper.applyDelta(createDeleteDelta(objectType), operationsHelper.createExecutionOptions(raw), dryRun, context, result);
                operationsHelper.recordEnd(context, objectType, started, null);
            } catch (Throwable ex) {
                operationsHelper.recordEnd(context, objectType, started, ex);
                exception = processActionException(ex, NAME, value, context);
            }
            context.println((exception != null ? "Attempted to delete " : "Deleted ") + prismObject.toString() + rawDrySuffix(raw, dryRun) + exceptionSuffix(exception));
        } else {
            //noinspection ThrowableNotThrown
            processActionException(new ScriptExecutionException("Item is not a PrismObject"), NAME, value, context);
        }
        operationsHelper.trimAndCloneResult(result, globalResult, context);
    }
    return input;
}
Also used : ObjectType(com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectType) PrismObjectValue(com.evolveum.midpoint.prism.PrismObjectValue) PipelineItem(com.evolveum.midpoint.model.api.PipelineItem) ScriptExecutionException(com.evolveum.midpoint.model.api.ScriptExecutionException) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) PrismValue(com.evolveum.midpoint.prism.PrismValue)

Example 5 with ActionExpressionType

use of com.evolveum.midpoint.xml.ns._public.model.scripting_3.ActionExpressionType in project midpoint by Evolveum.

the class EnableDisableExecutor method execute.

@Override
public PipelineData execute(ActionExpressionType expression, PipelineData input, ExecutionContext context, OperationResult globalResult) throws ScriptExecutionException {
    boolean isEnable = NAME_ENABLE.equals(expression.getType());
    boolean raw = getParamRaw(expression, input, context, globalResult);
    boolean dryRun = getParamDryRun(expression, input, context, globalResult);
    for (PipelineItem item : input.getData()) {
        PrismValue value = item.getValue();
        OperationResult result = operationsHelper.createActionResult(item, this, context, globalResult);
        result.addParam("operation", expression.getType());
        context.checkTaskStop();
        if (value instanceof PrismObjectValue) {
            PrismObject<? extends ObjectType> prismObject = ((PrismObjectValue) value).asPrismObject();
            ObjectType objectType = prismObject.asObjectable();
            long started = operationsHelper.recordStart(context, objectType);
            Throwable exception = null;
            try {
                if (objectType instanceof FocusType) {
                    operationsHelper.applyDelta(createEnableDisableDelta((FocusType) objectType, isEnable), operationsHelper.createExecutionOptions(raw), dryRun, context, result);
                } else if (objectType instanceof ShadowType) {
                    operationsHelper.applyDelta(createEnableDisableDelta((ShadowType) objectType, isEnable), context, result);
                } else {
                    throw new ScriptExecutionException("Item is not a FocusType nor ShadowType: " + value.toString());
                }
                operationsHelper.recordEnd(context, objectType, started, null);
            } catch (Throwable ex) {
                operationsHelper.recordEnd(context, objectType, started, ex);
                exception = processActionException(ex, expression.getType(), value, context);
            }
            context.println((exception != null ? "Attempted to " + expression.getType() : (isEnable ? "Enabled " : "Disabled ")) + prismObject.toString() + rawDrySuffix(raw, dryRun) + exceptionSuffix(exception));
        } else {
            //noinspection ThrowableNotThrown
            processActionException(new ScriptExecutionException("Item is not a PrismObject"), expression.getType(), value, context);
        }
        operationsHelper.trimAndCloneResult(result, globalResult, context);
    }
    return input;
}
Also used : ObjectType(com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectType) PrismObjectValue(com.evolveum.midpoint.prism.PrismObjectValue) PipelineItem(com.evolveum.midpoint.model.api.PipelineItem) ScriptExecutionException(com.evolveum.midpoint.model.api.ScriptExecutionException) ShadowType(com.evolveum.midpoint.xml.ns._public.common.common_3.ShadowType) FocusType(com.evolveum.midpoint.xml.ns._public.common.common_3.FocusType) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) PrismValue(com.evolveum.midpoint.prism.PrismValue)

Aggregations

PipelineItem (com.evolveum.midpoint.model.api.PipelineItem)13 OperationResult (com.evolveum.midpoint.schema.result.OperationResult)13 ScriptExecutionException (com.evolveum.midpoint.model.api.ScriptExecutionException)12 PipelineData (com.evolveum.midpoint.model.impl.scripting.PipelineData)7 PrismValue (com.evolveum.midpoint.prism.PrismValue)6 ObjectType (com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectType)6 PrismObjectValue (com.evolveum.midpoint.prism.PrismObjectValue)5 FocusType (com.evolveum.midpoint.xml.ns._public.common.common_3.FocusType)3 ResourceType (com.evolveum.midpoint.xml.ns._public.common.common_3.ResourceType)3 ObjectNotFoundException (com.evolveum.midpoint.util.exception.ObjectNotFoundException)2 ActionExpressionType (com.evolveum.midpoint.xml.ns._public.model.scripting_3.ActionExpressionType)2 ActionParameterValueType (com.evolveum.midpoint.xml.ns._public.model.scripting_3.ActionParameterValueType)2 ExpressionPipelineType (com.evolveum.midpoint.xml.ns._public.model.scripting_3.ExpressionPipelineType)2 ExpressionSequenceType (com.evolveum.midpoint.xml.ns._public.model.scripting_3.ExpressionSequenceType)2 SearchExpressionType (com.evolveum.midpoint.xml.ns._public.model.scripting_3.SearchExpressionType)2 JAXBElement (javax.xml.bind.JAXBElement)2 QName (javax.xml.namespace.QName)2 ValidationResult (com.evolveum.midpoint.model.api.validator.ValidationResult)1 ScriptExpression (com.evolveum.midpoint.model.common.expression.script.ScriptExpression)1 CustomEvent (com.evolveum.midpoint.notifications.api.events.CustomEvent)1