Search in sources :

Example 81 with VariablesMap

use of com.evolveum.midpoint.schema.expression.VariablesMap in project midpoint by Evolveum.

the class TestExpressionProfileSafe method test160ScriptJavaScript.

@Test
@Override
public void test160ScriptJavaScript() throws Exception {
    skipIfEcmaScriptEngineNotSupported();
    given();
    OperationResult result = createOperationResult();
    rememberScriptExecutionCount();
    ExpressionType expressionType = parseExpression(EXPRESSION_SCRIPT_JAVASCRIPT_FILE);
    Collection<Source<?, ?>> sources = prepareStringSources();
    VariablesMap variables = prepareBasicVariables();
    ExpressionEvaluationContext expressionContext = new ExpressionEvaluationContext(sources, variables, getTestNameShort(), null);
    when();
    evaluatePropertyExpressionRestricted(expressionType, PrimitiveType.STRING, expressionContext, result);
    then();
    assertScriptExecutionIncrement(0);
}
Also used : ExpressionEvaluationContext(com.evolveum.midpoint.repo.common.expression.ExpressionEvaluationContext) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) VariablesMap(com.evolveum.midpoint.schema.expression.VariablesMap) ExpressionType(com.evolveum.midpoint.xml.ns._public.common.common_3.ExpressionType) Source(com.evolveum.midpoint.repo.common.expression.Source) Test(org.testng.annotations.Test)

Example 82 with VariablesMap

use of com.evolveum.midpoint.schema.expression.VariablesMap in project midpoint by Evolveum.

the class TestExpressionProfileSafe method test154ScriptGroovySystemDeny.

@Test
@Override
public void test154ScriptGroovySystemDeny() throws Exception {
    given();
    OperationResult result = createOperationResult();
    rememberScriptExecutionCount();
    ExpressionType expressionType = parseExpression(EXPRESSION_SCRIPT_GROOVY_SYSTEM_DENY_FILE);
    Collection<Source<?, ?>> sources = prepareStringSources();
    VariablesMap variables = prepareBasicVariables();
    ExpressionEvaluationContext expressionContext = new ExpressionEvaluationContext(sources, variables, getTestNameShort(), null);
    when();
    evaluatePropertyExpressionRestricted(expressionType, PrimitiveType.STRING, expressionContext, result);
    then();
    assertScriptExecutionIncrement(0);
}
Also used : ExpressionEvaluationContext(com.evolveum.midpoint.repo.common.expression.ExpressionEvaluationContext) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) VariablesMap(com.evolveum.midpoint.schema.expression.VariablesMap) ExpressionType(com.evolveum.midpoint.xml.ns._public.common.common_3.ExpressionType) Source(com.evolveum.midpoint.repo.common.expression.Source) Test(org.testng.annotations.Test)

Example 83 with VariablesMap

use of com.evolveum.midpoint.schema.expression.VariablesMap in project midpoint by Evolveum.

the class RunReportPopupPanel method runConfirmPerformed.

private void runConfirmPerformed(AjaxRequestTarget target) {
    PrismContainerValue<ReportParameterType> reportParamValue;
    @NotNull PrismContainer<ReportParameterType> parameterContainer;
    try {
        PrismContainerDefinition<ReportParameterType> paramContainerDef = getPrismContext().getSchemaRegistry().findContainerDefinitionByElementName(ReportConstants.REPORT_PARAMS_PROPERTY_NAME);
        parameterContainer = paramContainerDef.instantiate();
        ReportParameterType reportParam = new ReportParameterType();
        reportParamValue = reportParam.asPrismContainerValue();
        reportParamValue.revive(getPrismContext());
        parameterContainer.add(reportParamValue);
    } catch (SchemaException e) {
        LOGGER.error("Couldn't create container for report parameters");
        return;
    }
    VariablesMap variablesMap = getTable().getReportVariables();
    for (SearchFilterParameterType parameter : getModelObject().getObjectCollection().getParameter()) {
        if (variablesMap.get(parameter.getName()) != null && variablesMap.getValue(parameter.getName()) != null) {
            Class<?> clazz = getPrismContext().getSchemaRegistry().determineClassForType(parameter.getType());
            QName type = getPrismContext().getSchemaRegistry().determineTypeForClass(clazz);
            if (Containerable.class.isAssignableFrom(clazz)) {
                LOGGER.error("Couldn't create container item for parameter " + parameter);
                continue;
            }
            MutableItemDefinition def;
            String namespaceUri = ReportConstants.NS_EXTENSION + "/" + AbstractReportWorkDefinitionType.F_REPORT_PARAM;
            if (Referencable.class.isAssignableFrom(clazz)) {
                def = getPrismContext().definitionFactory().createReferenceDefinition(new QName(namespaceUri, parameter.getName()), type);
                ((MutablePrismReferenceDefinition) def).setTargetTypeName(parameter.getTargetType());
            } else {
                List values = WebComponentUtil.getAllowedValues(parameter, getPageBase());
                if (CollectionUtils.isNotEmpty(values)) {
                    def = getPrismContext().definitionFactory().createPropertyDefinition(new QName(namespaceUri, parameter.getName()), type, values, null).toMutable();
                } else {
                    def = getPrismContext().definitionFactory().createPropertyDefinition(new QName(namespaceUri, parameter.getName()), type);
                }
            }
            def.setDynamic(true);
            def.setRuntimeSchema(true);
            def.setMaxOccurs(1);
            def.setMinOccurs(0);
            if (parameter.getDisplay() != null) {
                String displayName = WebComponentUtil.getTranslatedPolyString(parameter.getDisplay().getLabel());
                def.setDisplayName(displayName);
                String help = WebComponentUtil.getTranslatedPolyString(parameter.getDisplay().getHelp());
                def.setHelp(help);
            }
            if (parameter.getAllowedValuesLookupTable() != null) {
                def.setValueEnumerationRef(parameter.getAllowedValuesLookupTable().asReferenceValue());
            }
            try {
                Item item = def.instantiate();
                if (item instanceof PrismReference) {
                    ObjectReferenceType ref = (ObjectReferenceType) variablesMap.getValue(parameter.getName());
                    item.add(ref.asReferenceValue());
                } else {
                    PrismPropertyValue<Object> value = getPrismContext().itemFactory().createPropertyValue();
                    value.setValue(variablesMap.getValue(parameter.getName()));
                    item.add(value);
                }
                reportParamValue.add(item);
            } catch (SchemaException e) {
                LOGGER.error("Couldn't create item for parameter " + parameter);
            }
        }
    }
    runConfirmPerformed(target, getModelObject().asPrismObject(), parameterContainer);
}
Also used : SchemaException(com.evolveum.midpoint.util.exception.SchemaException) QName(javax.xml.namespace.QName) NotNull(org.jetbrains.annotations.NotNull) VariablesMap(com.evolveum.midpoint.schema.expression.VariablesMap)

Example 84 with VariablesMap

use of com.evolveum.midpoint.schema.expression.VariablesMap in project midpoint by Evolveum.

the class ReportObjectsListPanel method evaluateExpression.

@Override
protected Collection evaluateExpression(C rowValue, com.evolveum.midpoint.prism.Item<?, ?> columnItem, ExpressionType expression, GuiObjectColumnType customColumn) {
    Task task = getPageBase().createSimpleTask(OPERATION_EVALUATE_EXPRESSION);
    OperationResult result = task.getResult();
    try {
        VariablesMap variablesMap = new VariablesMap();
        if (columnItem == null) {
            variablesMap.put(ExpressionConstants.VAR_INPUT, null, String.class);
        } else {
            variablesMap.put(ExpressionConstants.VAR_INPUT, columnItem, columnItem.getDefinition());
        }
        processVariables(variablesMap);
        if (!variablesMap.containsKey(ExpressionConstants.VAR_OBJECT)) {
            variablesMap.put(ExpressionConstants.VAR_OBJECT, rowValue, rowValue.asPrismContainerValue().getDefinition());
        }
        Object object = getPageBase().getReportManager().evaluateScript(getReport().asPrismObject(), expression, variablesMap, "evaluate column expression", task, result);
        if (object instanceof Collection) {
            return (Collection) object;
        }
        return Collections.singletonList(object);
    } catch (Exception e) {
        LOGGER.error("Couldn't execute expression for {} column. Reason: {}", customColumn, e.getMessage(), e);
        result.recomputeStatus();
        OperationResultStatusPresentationProperties props = OperationResultStatusPresentationProperties.parseOperationalResultStatus(result.getStatus());
        // TODO: this is not entirely correct
        return Collections.singletonList(getPageBase().createStringResource(props.getStatusLabelKey()).getString());
    }
}
Also used : Task(com.evolveum.midpoint.task.api.Task) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) VariablesMap(com.evolveum.midpoint.schema.expression.VariablesMap) OperationResultStatusPresentationProperties(com.evolveum.midpoint.web.page.admin.server.dto.OperationResultStatusPresentationProperties)

Example 85 with VariablesMap

use of com.evolveum.midpoint.schema.expression.VariablesMap in project midpoint by Evolveum.

the class ReportObjectsListPanel method createProvider.

@Override
protected ISelectableDataProvider<C, SelectableBean<C>> createProvider() {
    SelectableBeanContainerDataProvider<C> provider = new SelectableBeanContainerDataProvider<>(this, getSearchModel(), null, false) {

        @Override
        public List<SelectableBean<C>> createDataObjectWrappers(Class<? extends C> type, ObjectQuery query, Collection<SelectorOptions<GetOperationOptions>> options, Task task, OperationResult result) throws CommunicationException, ObjectNotFoundException, SchemaException, SecurityViolationException, ConfigurationException, ExpressionEvaluationException {
            Collection<SelectorOptions<GetOperationOptions>> defaultOptions = DefaultColumnUtils.createOption(getObjectCollectionView().getTargetClass(getPrismContext()), getSchemaService());
            QName qNameType = WebComponentUtil.containerClassToQName(getPrismContext(), type);
            VariablesMap variables = new VariablesMap();
            if (getSearchModel().getObject() != null) {
                variables.putAll(getSearchModel().getObject().getFilterVariables(getVariables(), getPageBase()));
                processReferenceVariables(variables);
            }
            List<C> list = (List<C>) getModelInteractionService().searchObjectsFromCollection(getReport().getObjectCollection().getCollection(), qNameType, defaultOptions, query.getPaging(), variables, task, result);
            if (LOGGER.isTraceEnabled()) {
                LOGGER.trace("Query {} resulted in {} objects", type.getSimpleName(), list.size());
            }
            List<SelectableBean<C>> data = new ArrayList<SelectableBean<C>>();
            for (C object : list) {
                data.add(createDataObjectWrapper(object));
            }
            return data;
        }

        @Override
        protected Integer countObjects(Class<? extends C> type, ObjectQuery query, Collection<SelectorOptions<GetOperationOptions>> currentOptions, Task task, OperationResult result) throws CommunicationException, ObjectNotFoundException, SchemaException, SecurityViolationException, ConfigurationException, ExpressionEvaluationException {
            Collection<SelectorOptions<GetOperationOptions>> defaultOptions = DefaultColumnUtils.createOption(getObjectCollectionView().getTargetClass(getPrismContext()), getSchemaService());
            QName qNameType = WebComponentUtil.containerClassToQName(getPrismContext(), type);
            VariablesMap variables = new VariablesMap();
            if (getSearchModel().getObject() != null) {
                variables.putAll(getSearchModel().getObject().getFilterVariables(getVariables(), getPageBase()));
                processReferenceVariables(variables);
            }
            return getModelInteractionService().countObjectsFromCollection(getReport().getObjectCollection().getCollection(), qNameType, defaultOptions, null, variables, task, result);
        }

        @Override
        protected boolean isUseObjectCounting() {
            return !isDisableCounting();
        }

        @Override
        public boolean isOrderingDisabled() {
            return isDisableSorting();
        }

        @Override
        public ObjectQuery getQuery() {
            // fake query because of we need paging in method createDataObjectWrappers
            return getPrismContext().queryFor(ObjectType.class).build();
        }

        @Override
        @NotNull
        protected List<ObjectOrdering> createObjectOrderings(SortParam<String> sortParam) {
            if (AuditEventRecordType.class.equals(getDefaultType()) && sortParam != null && sortParam.getProperty() != null) {
                OrderDirection order = sortParam.isAscending() ? OrderDirection.ASCENDING : OrderDirection.DESCENDING;
                return Collections.singletonList(getPrismContext().queryFactory().createOrdering(ItemPath.create(new QName(AuditEventRecordType.COMPLEX_TYPE.getNamespaceURI(), sortParam.getProperty())), order));
            }
            return super.createObjectOrderings(sortParam);
        }
    };
    if (provider.getSort() == null && hasView()) {
        if (ObjectType.class.isAssignableFrom(getDefaultType())) {
            provider.setSort("name", SortOrder.ASCENDING);
        } else if (AuditEventRecordType.class.isAssignableFrom(getDefaultType())) {
            provider.setSort("timestamp", SortOrder.ASCENDING);
        }
    }
    return provider;
}
Also used : Task(com.evolveum.midpoint.task.api.Task) QName(javax.xml.namespace.QName) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) ObjectOrdering(com.evolveum.midpoint.prism.query.ObjectOrdering) ObjectQuery(com.evolveum.midpoint.prism.query.ObjectQuery) SortParam(org.apache.wicket.extensions.markup.html.repeater.util.SortParam) GetOperationOptions(com.evolveum.midpoint.schema.GetOperationOptions) SelectorOptions(com.evolveum.midpoint.schema.SelectorOptions) AuditEventRecordType(com.evolveum.midpoint.xml.ns._public.common.audit_3.AuditEventRecordType) SelectableBeanContainerDataProvider(com.evolveum.midpoint.web.component.data.SelectableBeanContainerDataProvider) SelectableBean(com.evolveum.midpoint.web.component.util.SelectableBean) VariablesMap(com.evolveum.midpoint.schema.expression.VariablesMap) OrderDirection(com.evolveum.midpoint.prism.query.OrderDirection)

Aggregations

VariablesMap (com.evolveum.midpoint.schema.expression.VariablesMap)166 OperationResult (com.evolveum.midpoint.schema.result.OperationResult)48 ExpressionEvaluationContext (com.evolveum.midpoint.repo.common.expression.ExpressionEvaluationContext)30 Test (org.testng.annotations.Test)28 Task (com.evolveum.midpoint.task.api.Task)23 NotNull (org.jetbrains.annotations.NotNull)23 QName (javax.xml.namespace.QName)15 AbstractInternalModelIntegrationTest (com.evolveum.midpoint.model.impl.AbstractInternalModelIntegrationTest)12 Source (com.evolveum.midpoint.repo.common.expression.Source)12 AbstractModelCommonTest (com.evolveum.midpoint.model.common.AbstractModelCommonTest)11 UserType (com.evolveum.midpoint.xml.ns._public.common.common_3.UserType)11 ExpressionFactory (com.evolveum.midpoint.repo.common.expression.ExpressionFactory)10 Trace (com.evolveum.midpoint.util.logging.Trace)10 TraceManager (com.evolveum.midpoint.util.logging.TraceManager)10 ObjectFilter (com.evolveum.midpoint.prism.query.ObjectFilter)9 ObjectQuery (com.evolveum.midpoint.prism.query.ObjectQuery)9 com.evolveum.midpoint.xml.ns._public.common.common_3 (com.evolveum.midpoint.xml.ns._public.common.common_3)9 PrismPropertyValue (com.evolveum.midpoint.prism.PrismPropertyValue)8 ExpressionType (com.evolveum.midpoint.xml.ns._public.common.common_3.ExpressionType)8 ExpressionEvaluationException (com.evolveum.midpoint.util.exception.ExpressionEvaluationException)7