diff --git a/gui/admin-gui/src/main/java/com/evolveum/midpoint/gui/api/component/ChooseMemberPopup.java b/gui/admin-gui/src/main/java/com/evolveum/midpoint/gui/api/component/ChooseMemberPopup.java index fe36b987287..ee043df90f9 100644 --- a/gui/admin-gui/src/main/java/com/evolveum/midpoint/gui/api/component/ChooseMemberPopup.java +++ b/gui/admin-gui/src/main/java/com/evolveum/midpoint/gui/api/component/ChooseMemberPopup.java @@ -17,13 +17,20 @@ import com.evolveum.midpoint.gui.api.component.tabs.CountablePanelTab; import com.evolveum.midpoint.gui.api.model.LoadableModel; +import com.evolveum.midpoint.gui.api.page.PageBase; import com.evolveum.midpoint.gui.api.util.WebComponentUtil; +import com.evolveum.midpoint.prism.PrismPropertyDefinition; +import com.evolveum.midpoint.prism.PrismReferenceValue; +import com.evolveum.midpoint.prism.PrismValue; import com.evolveum.midpoint.prism.delta.ObjectDelta; import com.evolveum.midpoint.prism.query.ObjectQuery; import com.evolveum.midpoint.schema.constants.ObjectTypes; +import com.evolveum.midpoint.schema.constants.SchemaConstants; import com.evolveum.midpoint.schema.result.OperationResult; import com.evolveum.midpoint.task.api.Task; import com.evolveum.midpoint.task.api.TaskCategory; +import com.evolveum.midpoint.util.DOMUtil; +import com.evolveum.midpoint.util.QNameUtil; import com.evolveum.midpoint.util.exception.SchemaException; import com.evolveum.midpoint.util.logging.LoggingUtils; import com.evolveum.midpoint.util.logging.Trace; @@ -34,7 +41,13 @@ import com.evolveum.midpoint.web.component.util.EnableBehaviour; import com.evolveum.midpoint.web.component.util.SelectableBean; import com.evolveum.midpoint.web.component.util.VisibleEnableBehaviour; +import com.evolveum.midpoint.web.page.admin.roles.MemberOperationsHelper; import com.evolveum.midpoint.xml.ns._public.common.common_3.*; +import com.evolveum.midpoint.xml.ns._public.model.scripting_3.ActionExpressionType; +import com.evolveum.midpoint.xml.ns._public.model.scripting_3.ActionParameterValueType; +import com.evolveum.midpoint.xml.ns._public.model.scripting_3.SearchExpressionType; +import com.evolveum.prism.xml.ns._public.types_3.RawType; + import org.apache.wicket.Component; import org.apache.wicket.ajax.AjaxRequestTarget; import org.apache.wicket.behavior.AttributeAppender; @@ -44,6 +57,7 @@ import org.apache.wicket.model.IModel; import org.apache.wicket.model.StringResourceModel; +import javax.xml.bind.JAXBElement; import javax.xml.namespace.QName; import java.util.ArrayList; import java.util.List; @@ -118,9 +132,9 @@ public void onClick(AjaxRequestTarget target) { if (selectedObjects == null || selectedObjects.size() == 0){ continue; } - executeMemberOperation(memberPanel.getObjectType().getTypeQName(), - createInOidQuery(selectedObjects), - memberPanel.prepareDelta(), target); + executeMemberOperation(memberPanel.getAbstractRoleTypeObject(), getDefaultTargetType(), + createInOidQuery(selectedObjects), memberPanel.getRelationValue(), + memberPanel.getObjectType().getTypeQName(), target, getPageBase()); if (memberPanel.getObjectType().equals(ObjectTypes.ORG)){ orgPanelProcessed = true; } @@ -376,24 +390,12 @@ private boolean isAddButtonEnabled(){ return false; } - protected void executeMemberOperation(QName type, ObjectQuery memberQuery, - ObjectDelta delta, AjaxRequestTarget target) { - - Task operationalTask = getPageBase().createSimpleTask("Add.members"); - OperationResult parentResult = operationalTask.getResult(); - - try { - WebComponentUtil.executeMemberOperation(operationalTask, type, memberQuery, delta, TaskCategory.EXECUTE_CHANGES, parentResult, getPageBase()); - } catch (SchemaException e) { - parentResult.recordFatalError(parentResult.getOperation(), e); - LoggingUtils.logUnexpectedException(LOGGER, - "Failed to execute operation " + parentResult.getOperation(), e); - target.add(getPageBase().getFeedbackPanel()); - } - - target.add(getPageBase().getFeedbackPanel()); + protected void executeMemberOperation(AbstractRoleType targetObject, QName targetType, ObjectQuery query, + QName relation, QName type, AjaxRequestTarget target, PageBase pageBase) { + MemberOperationsHelper.assignMembersPerformed(targetObject, targetType, query, + relation, type, target, pageBase); } - + protected abstract T getAssignmentTargetRefObject(); public int getWidth(){ @@ -421,4 +423,8 @@ public StringResourceModel getTitle(){ public Component getComponent(){ return this; } + + protected QName getDefaultTargetType() { + return RoleType.COMPLEX_TYPE; + } } diff --git a/gui/admin-gui/src/main/java/com/evolveum/midpoint/gui/api/component/ChooseOrgMemberPopup.java b/gui/admin-gui/src/main/java/com/evolveum/midpoint/gui/api/component/ChooseOrgMemberPopup.java index d9beb18140d..20af5b65b6a 100644 --- a/gui/admin-gui/src/main/java/com/evolveum/midpoint/gui/api/component/ChooseOrgMemberPopup.java +++ b/gui/admin-gui/src/main/java/com/evolveum/midpoint/gui/api/component/ChooseOrgMemberPopup.java @@ -72,5 +72,10 @@ public String getCount() { }); return tabs; } + + @Override + protected QName getDefaultTargetType() { + return OrgType.COMPLEX_TYPE; + } } diff --git a/gui/admin-gui/src/main/java/com/evolveum/midpoint/gui/api/util/WebComponentUtil.java b/gui/admin-gui/src/main/java/com/evolveum/midpoint/gui/api/util/WebComponentUtil.java index 2367a3b2073..c04e70b43da 100644 --- a/gui/admin-gui/src/main/java/com/evolveum/midpoint/gui/api/util/WebComponentUtil.java +++ b/gui/admin-gui/src/main/java/com/evolveum/midpoint/gui/api/util/WebComponentUtil.java @@ -658,17 +658,6 @@ public static void executeMemberOperation(Task operationalTask, QName type, Obje } } - public static void executeMemberOperation(Task operationalTask, QName type, ObjectQuery memberQuery, - ObjectDelta delta, String category, OperationResult parentResult, PageBase pageBase) throws SchemaException{ - ModelExecuteOptions options = TaskCategory.EXECUTE_CHANGES.equals(category) - ? ModelExecuteOptions.createReconcile() // This was originally in ExecuteChangesTaskHandler, now it's transferred through task extension. - : null; - TaskType task = WebComponentUtil.createSingleRecurrenceTask(parentResult.getOperation(), type, - memberQuery, delta, options, category, pageBase); - WebModelServiceUtils.runTask(task, operationalTask, parentResult, pageBase); - - } - public static boolean isAuthorized(String... action) { if (action == null || action.length == 0) { return true; diff --git a/gui/admin-gui/src/main/java/com/evolveum/midpoint/web/page/admin/roles/AbstractRoleMemberPanel.java b/gui/admin-gui/src/main/java/com/evolveum/midpoint/web/page/admin/roles/AbstractRoleMemberPanel.java index 8bfc0aa8d86..88c958267c4 100644 --- a/gui/admin-gui/src/main/java/com/evolveum/midpoint/web/page/admin/roles/AbstractRoleMemberPanel.java +++ b/gui/admin-gui/src/main/java/com/evolveum/midpoint/web/page/admin/roles/AbstractRoleMemberPanel.java @@ -792,8 +792,7 @@ private CheckFormGroup getIndirectmembersPanel() { } protected void recomputeMembersPerformed(AjaxRequestTarget target) { - MemberOperationsHelper.recomputeMembersPerformed(getPageBase(), getQueryScope(true), getActionQuery(getQueryScope(true), getSupportedRelations()), getSupportedRelations(), target); - + MemberOperationsHelper.recomputeMembersPerformed(getPageBase(), getQueryScope(true), getActionQuery(getQueryScope(true), getSupportedRelations()), target); } protected ObjectQuery createContentQuery() { diff --git a/gui/admin-gui/src/main/java/com/evolveum/midpoint/web/page/admin/roles/MemberOperationsHelper.java b/gui/admin-gui/src/main/java/com/evolveum/midpoint/web/page/admin/roles/MemberOperationsHelper.java index d6d2b0fa54d..60d6516e596 100644 --- a/gui/admin-gui/src/main/java/com/evolveum/midpoint/web/page/admin/roles/MemberOperationsHelper.java +++ b/gui/admin-gui/src/main/java/com/evolveum/midpoint/web/page/admin/roles/MemberOperationsHelper.java @@ -17,24 +17,17 @@ import com.evolveum.midpoint.gui.api.component.ChooseOrgMemberPopup; import com.evolveum.midpoint.gui.api.page.PageBase; import com.evolveum.midpoint.gui.api.util.WebComponentUtil; -import com.evolveum.midpoint.gui.api.util.WebModelServiceUtils; -import com.evolveum.midpoint.model.api.ModelPublicConstants; -import com.evolveum.midpoint.prism.MutablePrismPropertyDefinition; import com.evolveum.midpoint.prism.PrismContext; -import com.evolveum.midpoint.prism.PrismObject; -import com.evolveum.midpoint.prism.PrismObjectDefinition; import com.evolveum.midpoint.prism.PrismPropertyDefinition; import com.evolveum.midpoint.prism.PrismReferenceValue; import com.evolveum.midpoint.prism.PrismValue; import com.evolveum.midpoint.prism.delta.ObjectDelta; import com.evolveum.midpoint.prism.query.ObjectQuery; import com.evolveum.midpoint.prism.query.builder.S_AtomicFilterExit; -import com.evolveum.midpoint.schema.constants.ObjectTypes; import com.evolveum.midpoint.schema.constants.SchemaConstants; import com.evolveum.midpoint.schema.result.OperationResult; import com.evolveum.midpoint.schema.util.ObjectTypeUtil; import com.evolveum.midpoint.task.api.Task; -import com.evolveum.midpoint.task.api.TaskCategory; import com.evolveum.midpoint.util.DOMUtil; import com.evolveum.midpoint.util.QNameUtil; import com.evolveum.midpoint.util.exception.SchemaException; @@ -42,7 +35,6 @@ import com.evolveum.midpoint.util.logging.Trace; import com.evolveum.midpoint.util.logging.TraceManager; import com.evolveum.midpoint.web.page.admin.dto.ObjectViewDto; -import com.evolveum.midpoint.web.page.admin.roles.AbstractRoleMemberPanel.MemberOperation; import com.evolveum.midpoint.web.page.admin.roles.AbstractRoleMemberPanel.QueryScope; import com.evolveum.midpoint.xml.ns._public.common.common_3.AbstractRoleType; import com.evolveum.midpoint.xml.ns._public.common.common_3.AssignmentType; @@ -50,35 +42,25 @@ import com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectReferenceType; import com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectType; import com.evolveum.midpoint.xml.ns._public.common.common_3.OrgType; -import com.evolveum.midpoint.xml.ns._public.common.common_3.RoleType; -import com.evolveum.midpoint.xml.ns._public.common.common_3.TaskType; import com.evolveum.midpoint.xml.ns._public.model.scripting_3.ActionExpressionType; import com.evolveum.midpoint.xml.ns._public.model.scripting_3.ActionParameterValueType; import com.evolveum.midpoint.xml.ns._public.model.scripting_3.ScriptingExpressionType; import com.evolveum.midpoint.xml.ns._public.model.scripting_3.SearchExpressionType; -import com.evolveum.prism.xml.ns._public.query_3.QueryType; import com.evolveum.prism.xml.ns._public.types_3.RawType; public class MemberOperationsHelper { private static final Trace LOGGER = TraceManager.getTrace(AbstractRoleMemberPanel.class); - private static final String UNASSIGN_OPERATION = "unassign"; - private static final String ROLE_PARAMETER = "role"; - private static final String RELATION_PARAMETER = "relation"; + public static final String UNASSIGN_OPERATION = "unassign"; + public static final String ASSIGN_OPERATION = "assign"; + public static final String DELETE_OPERATION = "delete"; + public static final String RECOMPUTE_OPERATION = "recompute"; + public static final String ROLE_PARAMETER = "role"; + public static final String RELATION_PARAMETER = "relation"; - public static void recomputeMembersPerformed(PageBase modelServiceLocator, QueryScope scope, ObjectQuery query, Collection supportedRelations, AjaxRequestTarget target) { - Task operationalTask = modelServiceLocator.createSimpleTask(getTaskName("Recompute", scope)); - executeMemberOperation(modelServiceLocator, operationalTask, FocusType.COMPLEX_TYPE, query, null, - TaskCategory.RECOMPUTATION, target); - - } - -// public static void unassignOtherOrgMembersPerformed(PageBase pageBase, R targetObject, QueryScope scope, ObjectQuery query, Collection relations, AjaxRequestTarget target) { -// unassignMembersPerformed(pageBase, targetObject, scope, query, relations, ObjectType.COMPLEX_TYPE, target); -// } - - public static void unassignMembersPerformed(PageBase pageBase, R targetObject, QueryScope scope, ObjectQuery query, Collection relations, QName type, AjaxRequestTarget target) { + public static void unassignMembersPerformed(PageBase pageBase, R targetObject, QueryScope scope, + ObjectQuery query, Collection relations, QName type, AjaxRequestTarget target) { Task operationalTask = pageBase.createSimpleTask(getTaskName("Remove", scope)); SearchExpressionType script = new SearchExpressionType(); @@ -117,24 +99,67 @@ public static void unassignMembersPerformed(PageBas } - public static void deleteMembersPerformed(PageBase pageBase, QueryScope scope, ObjectQuery query, QName type, AjaxRequestTarget target) { - Task operationalTask = pageBase.createSimpleTask(getTaskName("Delete", scope)); - OperationResult parentResult = operationalTask.getResult(); + public static void assignMembersPerformed(AbstractRoleType targetObject, QName targetType, ObjectQuery query, + QName relation, QName type, AjaxRequestTarget target, PageBase pageBase) { + Task operationalTask = pageBase.createSimpleTask("Add.members"); + + SearchExpressionType script = new SearchExpressionType(); + script.setType(type); + ActionExpressionType expression = new ActionExpressionType(); + expression.setType(ASSIGN_OPERATION); + + PrismReferenceValue value = pageBase.getPrismContext().itemFactory() + .createReferenceValue(targetObject.getOid(), targetType); + expression.parameter(new ActionParameterValueType().name(ROLE_PARAMETER).value( + new RawType(value, ObjectReferenceType.COMPLEX_TYPE, pageBase.getPrismContext()))); + if(relation != null) { + expression.parameter(new ActionParameterValueType().name(RELATION_PARAMETER).value(QNameUtil.qNameToUri(relation))); + } + script.setScriptingExpression(new JAXBElement(SchemaConstants.S_ACTION, + ActionExpressionType.class, expression)); + try { - TaskType taskType = WebComponentUtil.createSingleRecurrenceTask(parentResult.getOperation(), type, query, null, null, TaskCategory.UTIL, pageBase); - taskType.setHandlerUri(ModelPublicConstants.DELETE_TASK_HANDLER_URI); - - WebModelServiceUtils.runTask(taskType, operationalTask, operationalTask.getResult(), pageBase); + script.setQuery(pageBase.getQueryConverter().createQueryType(query)); } catch (SchemaException e) { - parentResult.recordFatalError(parentResult.getOperation(), e); - LoggingUtils.logUnexpectedException(LOGGER, - "Failed to execute operation " + parentResult.getOperation(), e); - target.add(pageBase.getFeedbackPanel()); + LoggingUtils.logUnexpectedException(LOGGER, "Can not create ObjectQuery from " + query, e); + operationalTask.getResult().recordFatalError("Can not create ObjectQuery from " + query, e); } - target.add(pageBase.getFeedbackPanel()); - //FIXME: temporary hack + executeMemberOperation(pageBase, operationalTask, type, query, script, target); + } + + public static void deleteMembersPerformed(PageBase pageBase, QueryScope scope, + ObjectQuery query, QName type, AjaxRequestTarget target) { + recomputeOrDeleteMembersPerformed(pageBase, scope, query, target, "Delete", DELETE_OPERATION); + } + + public static void recomputeMembersPerformed(PageBase pageBase, QueryScope scope, + ObjectQuery query, AjaxRequestTarget target) { + recomputeOrDeleteMembersPerformed(pageBase, scope, query, target, "Recompute", RECOMPUTE_OPERATION); + } + + private static void recomputeOrDeleteMembersPerformed(PageBase pageBase, QueryScope scope, + ObjectQuery query, AjaxRequestTarget target, String operation, String displayNameOfOperation) { + QName defaultType = FocusType.COMPLEX_TYPE; + Task operationalTask = pageBase.createSimpleTask(getTaskName(displayNameOfOperation, scope)); + + SearchExpressionType script = new SearchExpressionType(); + script.setType(defaultType); + ActionExpressionType expression = new ActionExpressionType(); + expression.setType(operation); + + script.setScriptingExpression(new JAXBElement(SchemaConstants.S_ACTION, + ActionExpressionType.class, expression)); + + try { + script.setQuery(pageBase.getQueryConverter().createQueryType(query)); + } catch (SchemaException e) { + LoggingUtils.logUnexpectedException(LOGGER, "Can not create ObjectQuery from " + query, e); + operationalTask.getResult().recordFatalError("Can not create ObjectQuery from " + query, e); + } + executeMemberOperation(pageBase, operationalTask, defaultType, query, script, target); + } public static void assignMembers(PageBase pageBase, R targetRefObject, AjaxRequestTarget target, List availableRelationList) { @@ -232,79 +257,6 @@ private static String getTaskName(String operation, QueryScope scope, boolean ma return nameBuilder.toString(); } - private static ObjectDelta prepareAssignmentDelta(R targetObject, QName type, Collection relations, MemberOperation operation, PrismContext prismContext, OperationResult result) { - Class classType = WebComponentUtil.qnameToClass(prismContext, type); - ObjectDelta delta = null; - try { - switch (operation) { - case ADD: - delta = getAddAssignmentDelta(targetObject, relations, classType, prismContext); - break; - - case REMOVE: - delta = getDeleteAssignmentDelta(targetObject, relations, classType, prismContext); - break; - } - } catch (SchemaException e) { - LoggingUtils.logUnexpectedException(LOGGER, "Failed to prepare delta for operation " + operation.name(), e); - result.recordFatalError("Failed to prepare delta for operation " + operation.name(), e); - } - return delta; - - } - - public static ObjectDelta prepareObjectTypeDelta(R targetObject, Collection relations, MemberOperation operation, OperationResult result, PrismContext prismContext) { - ObjectDelta delta = null; - try { - switch (operation) { - case ADD: - delta = getAddParentOrgDelta(targetObject, relations, prismContext); - break; - - case REMOVE: - delta = getDeleteParentOrgDelta(targetObject, relations, prismContext); - break; - default: - break; - } - } catch (SchemaException e) { - LoggingUtils.logUnexpectedException(LOGGER, "Failed to prepare delta for operation " + operation.name(), e); - result.recordFatalError("Failed to prepare delta for operation " + operation.name(), e); - } - return delta; - - } - - - - //TODO: why it is not used?? - private static AssignmentType createMemberAssignmentToModify(R targetObject, QName relation, ObjectViewDto tenant, ObjectViewDto project, PrismContext prismContext) throws SchemaException { - AssignmentType assignmentToModify = MemberOperationsHelper.createAssignmentToModify(targetObject, relation, prismContext); - - if (tenant != null && tenant.getObjectType() != null) { - assignmentToModify.setTenantRef(ObjectTypeUtil.createObjectRef(tenant.getObjectType().getOid(), ObjectTypes.ORG)); - } - - if (project != null && project.getObjectType() != null) { - assignmentToModify.setOrgRef(ObjectTypeUtil.createObjectRef(project.getObjectType().getOid(), ObjectTypes.ORG)); - } - - return assignmentToModify; - } - - protected static ObjectDelta getAddAssignmentDelta(R targetObject, Collection relations, Class classType, PrismContext prismContext) throws SchemaException { - ObjectDelta delta = prismContext.deltaFactory().object().createEmptyModifyDelta(classType, "fakeOid"); - if (relations == null || relations.isEmpty()) { - delta.addModificationAddContainer(FocusType.F_ASSIGNMENT, createAssignmentToModify(targetObject, null, prismContext)); - return delta; - } - - for (QName relation : relations) { - delta.addModificationAddContainer(FocusType.F_ASSIGNMENT, createAssignmentToModify(targetObject, relation, prismContext)); - } - return delta; - } - protected static ObjectDelta getDeleteAssignmentDelta(R targetObject, Collection relations, Class classType, PrismContext prismContext) throws SchemaException { if (relations == null || relations.isEmpty()) { return prismContext.deltaFactory().object().createModificationDeleteContainer(classType, "fakeOid", @@ -323,21 +275,6 @@ protected static ObjectDelta getDeleteAssignmentDel } - protected static ObjectDelta getAddParentOrgDelta(R targetObject, Collection relations, PrismContext prismContext) throws SchemaException { - ObjectDelta delta = prismContext.deltaFactory().object().createEmptyModifyDelta(ObjectType.class, "fakeOid" - ); - if (relations == null || relations.isEmpty()) { - delta.addModificationAddReference(ObjectType.F_PARENT_ORG_REF, ObjectTypeUtil.createObjectRef(targetObject, - prismContext).asReferenceValue()); - return delta; - } - - for (QName relation : relations) { - delta.addModificationAddReference(ObjectType.F_PARENT_ORG_REF, createReference(targetObject, relation).asReferenceValue()); - } - return delta; - } - protected static ObjectDelta getDeleteParentOrgDelta(R targetObject, Collection relations, PrismContext prismContext) throws SchemaException { if (relations == null || relations.isEmpty()) { return prismContext.deltaFactory().object().createModificationDeleteReference(ObjectType.class, "fakeOid", @@ -368,23 +305,6 @@ public static ObjectReferenceType createReference(R return ref; } - protected static void executeMemberOperation(PageBase modelServiceLocator, Task operationalTask, QName type, ObjectQuery memberQuery, - ObjectDelta delta, String category, AjaxRequestTarget target) { - - OperationResult parentResult = operationalTask.getResult(); - - try { - WebComponentUtil.executeMemberOperation(operationalTask, type, memberQuery, delta, category, parentResult, modelServiceLocator); - } catch (SchemaException e) { - parentResult.recordFatalError(parentResult.getOperation(), e); - LoggingUtils.logUnexpectedException(LOGGER, - "Failed to execute operation " + parentResult.getOperation(), e); - target.add(modelServiceLocator.getFeedbackPanel()); - } - - target.add(modelServiceLocator.getFeedbackPanel()); - } - protected static void executeMemberOperation(PageBase modelServiceLocator, Task operationalTask, QName type, ObjectQuery memberQuery, ScriptingExpressionType script, AjaxRequestTarget target) { diff --git a/model/model-impl/src/main/java/com/evolveum/midpoint/model/impl/scripting/actions/AssignExecutor.java b/model/model-impl/src/main/java/com/evolveum/midpoint/model/impl/scripting/actions/AssignExecutor.java index c5cdc8e338b..38f6becf8aa 100644 --- a/model/model-impl/src/main/java/com/evolveum/midpoint/model/impl/scripting/actions/AssignExecutor.java +++ b/model/model-impl/src/main/java/com/evolveum/midpoint/model/impl/scripting/actions/AssignExecutor.java @@ -16,126 +16,152 @@ package com.evolveum.midpoint.model.impl.scripting.actions; -import com.evolveum.midpoint.model.api.ModelExecuteOptions; -import com.evolveum.midpoint.model.impl.scripting.PipelineData; -import com.evolveum.midpoint.model.impl.scripting.ExecutionContext; import com.evolveum.midpoint.model.api.ScriptExecutionException; -import com.evolveum.midpoint.model.api.PipelineItem; -import com.evolveum.midpoint.prism.*; import com.evolveum.midpoint.prism.delta.ObjectDelta; -import com.evolveum.midpoint.schema.result.OperationResult; -import com.evolveum.midpoint.util.exception.CommonException; +import com.evolveum.midpoint.schema.constants.RelationTypes; +import com.evolveum.midpoint.util.QNameUtil; import com.evolveum.midpoint.util.exception.SchemaException; import com.evolveum.midpoint.util.logging.Trace; import com.evolveum.midpoint.util.logging.TraceManager; import com.evolveum.midpoint.xml.ns._public.common.common_3.*; -import com.evolveum.midpoint.xml.ns._public.model.scripting_3.ActionExpressionType; -import com.evolveum.midpoint.xml.ns._public.model.scripting_3.ActionParameterValueType; -import org.apache.commons.collections4.CollectionUtils; import org.springframework.stereotype.Component; -import javax.annotation.PostConstruct; - import java.util.ArrayList; import java.util.Collection; +import java.util.Collections; import java.util.List; +import javax.management.relation.RelationType; +import javax.xml.namespace.QName; + /** * @author mederly */ @Component -public class AssignExecutor extends BaseActionExecutor { +public class AssignExecutor extends AssignmentOperationsExecutor { private static final Trace LOGGER = TraceManager.getTrace(AssignExecutor.class); - private static final String NAME = "assign"; - private static final String PARAM_RESOURCE = "resource"; - private static final String PARAM_ROLE = "role"; - - @PostConstruct - public void init() { - scriptingExpressionEvaluator.registerActionExecutor(NAME, this); - } - +// private static final String NAME = "assign"; +// private static final String PARAM_RESOURCE = "resource"; +// private static final String PARAM_ROLE = "role"; +// private static final String PARAM_RELATION = "relation"; + +// @PostConstruct +// public void init() { +// scriptingExpressionEvaluator.registerActionExecutor(NAME, this); +// } + +// @Override +// public PipelineData execute(ActionExpressionType expression, PipelineData input, ExecutionContext context, OperationResult globalResult) throws ScriptExecutionException { +// +// ModelExecuteOptions executionOptions = getOptions(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 relations = expressionHelper.getArgumentValues(expression.getParameter(), PARAM_RELATION, false, false, NAME, input, context, String.class, globalResult); +// +// Collection resources; +// try { +// if (resourceParameterValue != null) { +// PipelineData data = expressionHelper +// .evaluateParameter(resourceParameterValue, null, input, context, globalResult); +// resources = data.getDataAsReferences(ResourceType.COMPLEX_TYPE, ResourceType.class, context, globalResult); +// } else { +// resources = null; +// } +// } catch (CommonException e) { +// throw new ScriptExecutionException("Couldn't evaluate '" + PARAM_RESOURCE + "' parameter of a scripting expression: " + e.getMessage(), e); +// } +// +// Collection roles; +// try { +// if (roleParameterValue != null) { +// PipelineData data = expressionHelper.evaluateParameter(roleParameterValue, null, input, context, globalResult); +// roles = data.getDataAsReferences(RoleType.COMPLEX_TYPE, AbstractRoleType.class, context, globalResult); // if somebody wants to assign Org, he has to use full reference value (including object type) +// } else { +// roles = null; +// } +// } catch (CommonException e) { +// throw new ScriptExecutionException("Couldn't evaluate '" + PARAM_ROLE + "' parameter of a scripting expression: " + e.getMessage(), e); +// } +// +// if (resources == null && roles == null) { +// throw new ScriptExecutionException("Nothing to assign: neither resource nor role specified"); +// } +// +// if (CollectionUtils.isEmpty(resources) && CollectionUtils.isEmpty(roles)) { +// LOGGER.warn("No resources and no roles to assign in a scripting expression"); +// context.println("Warning: no resources and no roles to assign"); // TODO some better handling? +// return input; +// } +// +// 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) { +// @SuppressWarnings({"unchecked", "raw"}) +// PrismObject prismObject = ((PrismObjectValue) value).asPrismObject(); +// ObjectType objectType = prismObject.asObjectable(); +// long started = operationsHelper.recordStart(context, objectType); +// Throwable exception = null; +// try { +// operationsHelper.applyDelta(createDelta(objectType, resources, roles, relations), executionOptions, 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() + optionsSuffix(executionOptions, dryRun) + exceptionSuffix(exception)); +// } else { +// //noinspection ThrowableNotThrown +// processActionException(new ScriptExecutionException("Item is not a PrismObject of FocusType"), NAME, value, context); +// } +// operationsHelper.trimAndCloneResult(result, globalResult, context); +// } +// return input; // TODO updated objects? +// } + @Override - public PipelineData execute(ActionExpressionType expression, PipelineData input, ExecutionContext context, OperationResult globalResult) throws ScriptExecutionException { - - ModelExecuteOptions executionOptions = getOptions(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 resources; - try { - if (resourceParameterValue != null) { - PipelineData data = expressionHelper - .evaluateParameter(resourceParameterValue, null, input, context, globalResult); - resources = data.getDataAsReferences(ResourceType.COMPLEX_TYPE, ResourceType.class, context, globalResult); - } else { - resources = null; - } - } catch (CommonException e) { - throw new ScriptExecutionException("Couldn't evaluate '" + PARAM_RESOURCE + "' parameter of a scripting expression: " + e.getMessage(), e); - } - - Collection roles; - try { - if (roleParameterValue != null) { - PipelineData data = expressionHelper.evaluateParameter(roleParameterValue, null, input, context, globalResult); - roles = data.getDataAsReferences(RoleType.COMPLEX_TYPE, AbstractRoleType.class, context, globalResult); // if somebody wants to assign Org, he has to use full reference value (including object type) - } else { - roles = null; - } - } catch (CommonException e) { - throw new ScriptExecutionException("Couldn't evaluate '" + PARAM_ROLE + "' parameter of a scripting expression: " + e.getMessage(), e); - } - - if (resources == null && roles == null) { - throw new ScriptExecutionException("Nothing to assign: neither resource nor role specified"); - } - - if (CollectionUtils.isEmpty(resources) && CollectionUtils.isEmpty(roles)) { - LOGGER.warn("No resources and no roles to assign in a scripting expression"); - context.println("Warning: no resources and no roles to assign"); // TODO some better handling? - return input; - } - - 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) { - @SuppressWarnings({"unchecked", "raw"}) - PrismObject prismObject = ((PrismObjectValue) value).asPrismObject(); - ObjectType objectType = prismObject.asObjectable(); - long started = operationsHelper.recordStart(context, objectType); - Throwable exception = null; - try { - operationsHelper.applyDelta(createDelta(objectType, resources, roles), executionOptions, 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() + optionsSuffix(executionOptions, dryRun) + exceptionSuffix(exception)); - } else { - //noinspection ThrowableNotThrown - processActionException(new ScriptExecutionException("Item is not a PrismObject of FocusType"), NAME, value, context); - } - operationsHelper.trimAndCloneResult(result, globalResult, context); - } - return input; // TODO updated objects? + protected String getName() { + return AssignmentOperationsExecutor.ASSIGN_NAME; } - private ObjectDelta createDelta(ObjectType objectType, Collection resources, Collection roles) throws ScriptExecutionException { - + @Override + protected ObjectDelta createDelta(AssignmentHolderType objectType, Collection resources, Collection roles, Collection relations) throws ScriptExecutionException { + + String relation = ""; + + if (relations == null || relations.isEmpty()) { + QName defaultRelation = prismContext.getDefaultRelation() != null ? + prismContext.getDefaultRelation() : RelationTypes.MEMBER.getRelation(); + relation = QNameUtil.qNameToUri(defaultRelation); + } else if(relations.size() > 1){ + throw new IllegalArgumentException("Couldn't use relation as multivalue parameter"); + } else { + relation = relations.iterator().next(); + } + List assignments = new ArrayList<>(); if (roles != null) { for (ObjectReferenceType roleRef : roles) { AssignmentType assignmentType = new AssignmentType(); + RelationTypes foundRelation = null; + for (RelationTypes relationType : RelationTypes.values()) { + if (prismContext.relationMatches(QNameUtil.uriToQName(relation, true), + relationType.getRelation())) { + foundRelation = relationType; + break; + } + } + if(foundRelation == null) { + throw new IllegalArgumentException("Relation " + relation + " not found"); + } + roleRef.setRelation(foundRelation.getRelation()); assignmentType.setTargetRef(roleRef); assignments.add(assignmentType); } diff --git a/model/model-impl/src/main/java/com/evolveum/midpoint/model/impl/scripting/actions/AssignmentOperationsExecutor.java b/model/model-impl/src/main/java/com/evolveum/midpoint/model/impl/scripting/actions/AssignmentOperationsExecutor.java new file mode 100644 index 00000000000..7dcf6ca7cc3 --- /dev/null +++ b/model/model-impl/src/main/java/com/evolveum/midpoint/model/impl/scripting/actions/AssignmentOperationsExecutor.java @@ -0,0 +1,143 @@ +/* + * Copyright (c) 2010-2019 Evolveum + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.evolveum.midpoint.model.impl.scripting.actions; + +import com.evolveum.midpoint.model.api.ModelExecuteOptions; +import com.evolveum.midpoint.model.api.PipelineItem; +import com.evolveum.midpoint.model.api.ScriptExecutionException; +import com.evolveum.midpoint.model.impl.scripting.ExecutionContext; +import com.evolveum.midpoint.model.impl.scripting.PipelineData; +import com.evolveum.midpoint.prism.PrismObject; +import com.evolveum.midpoint.prism.PrismObjectValue; +import com.evolveum.midpoint.prism.PrismValue; +import com.evolveum.midpoint.prism.delta.ObjectDelta; +import com.evolveum.midpoint.prism.path.ItemPath; +import com.evolveum.midpoint.schema.constants.RelationTypes; +import com.evolveum.midpoint.schema.result.OperationResult; +import com.evolveum.midpoint.util.QNameUtil; +import com.evolveum.midpoint.util.exception.CommonException; +import com.evolveum.midpoint.util.exception.SchemaException; +import com.evolveum.midpoint.util.logging.Trace; +import com.evolveum.midpoint.util.logging.TraceManager; +import com.evolveum.midpoint.xml.ns._public.common.common_3.*; +import com.evolveum.midpoint.xml.ns._public.model.scripting_3.ActionExpressionType; +import com.evolveum.midpoint.xml.ns._public.model.scripting_3.ActionParameterValueType; +import org.apache.commons.collections4.CollectionUtils; +import org.springframework.stereotype.Component; + +import javax.annotation.PostConstruct; +import javax.xml.namespace.QName; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.List; + +/** + * + */ +public abstract class AssignmentOperationsExecutor extends BaseActionExecutor { + + private static final Trace LOGGER = TraceManager.getTrace(AssignmentOperationsExecutor.class); + + protected static final String UNASSIGN_NAME = "unassign"; + protected static final String ASSIGN_NAME = "assign"; + protected static final String PARAM_RESOURCE = "resource"; + protected static final String PARAM_ROLE = "role"; + protected static final String PARAM_RELATION = "relation"; + + @PostConstruct + public void init() { + scriptingExpressionEvaluator.registerActionExecutor(getName(), this); + } + + protected abstract String getName(); + + @Override + public PipelineData execute(ActionExpressionType expression, PipelineData input, ExecutionContext context, OperationResult globalResult) throws ScriptExecutionException { + + ModelExecuteOptions executionOptions = getOptions(expression, input, context, globalResult); + boolean dryRun = getParamDryRun(expression, input, context, globalResult); + + ActionParameterValueType resourceParameterValue = expressionHelper.getArgument(expression.getParameter(), PARAM_RESOURCE, false, false, getName()); + ActionParameterValueType roleParameterValue = expressionHelper.getArgument(expression.getParameter(), PARAM_ROLE, false, false, getName()); + Collection relations = expressionHelper.getArgumentValues(expression.getParameter(), PARAM_RELATION, false, false, getName(), input, context, String.class, globalResult); + + Collection resources; + try { + if (resourceParameterValue != null) { + PipelineData data = expressionHelper + .evaluateParameter(resourceParameterValue, null, input, context, globalResult); + resources = data.getDataAsReferences(ResourceType.COMPLEX_TYPE, ResourceType.class, context, globalResult); + } else { + resources = null; + } + } catch (CommonException e) { + throw new ScriptExecutionException("Couldn't evaluate '" + PARAM_RESOURCE + "' parameter of a scripting expression: " + e.getMessage(), e); + } + + Collection roles; + try { + if (roleParameterValue != null) { + PipelineData data = expressionHelper.evaluateParameter(roleParameterValue, null, input, context, globalResult); + roles = data.getDataAsReferences(RoleType.COMPLEX_TYPE, AbstractRoleType.class, context, globalResult); // if somebody wants to assign Org, he has to use full reference value (including object type) + } else { + roles = null; + } + } catch (CommonException e) { + throw new ScriptExecutionException("Couldn't evaluate '" + PARAM_ROLE + "' parameter of a scripting expression: " + e.getMessage(), e); + } + + if (resources == null && roles == null) { + throw new ScriptExecutionException("Nothing to " + getName() + ": neither resource nor role specified"); + } + + if (CollectionUtils.isEmpty(resources) && CollectionUtils.isEmpty(roles)) { + LOGGER.warn("No resources and no roles to unassign in a scripting expression"); + context.println("Warning: no resources and no roles to unassign"); // TODO some better handling? + return input; + } + + 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 AssignmentHolderType) { + @SuppressWarnings({"unchecked", "raw"}) + PrismObject prismObject = ((PrismObjectValue) value).asPrismObject(); + AssignmentHolderType objectType = (AssignmentHolderType) prismObject.asObjectable(); + long started = operationsHelper.recordStart(context, objectType); + Throwable exception = null; + try { + operationsHelper.applyDelta(createDelta(objectType, resources, roles, relations), executionOptions, dryRun, context, result); + operationsHelper.recordEnd(context, objectType, started, null); + } catch (Throwable ex) { + operationsHelper.recordEnd(context, objectType, started, ex); + exception = processActionException(ex, getName(), value, context); + } + context.println((exception != null ? "Attempted to modify " : "Modified ") + prismObject.toString() + optionsSuffix(executionOptions, dryRun) + exceptionSuffix(exception)); + } else { + //noinspection ThrowableNotThrown + processActionException(new ScriptExecutionException("Item is not a PrismObject of AssignmentHolderType"), getName(), value, context); + } + operationsHelper.trimAndCloneResult(result, globalResult, context); + } + return input; // TODO updated objects? + } + + protected abstract ObjectDelta createDelta(AssignmentHolderType object, Collection resources, Collection roles, Collection relations) throws ScriptExecutionException; +} diff --git a/model/model-impl/src/main/java/com/evolveum/midpoint/model/impl/scripting/actions/UnassignExecutor.java b/model/model-impl/src/main/java/com/evolveum/midpoint/model/impl/scripting/actions/UnassignExecutor.java index 4f24532909c..01184c504a1 100644 --- a/model/model-impl/src/main/java/com/evolveum/midpoint/model/impl/scripting/actions/UnassignExecutor.java +++ b/model/model-impl/src/main/java/com/evolveum/midpoint/model/impl/scripting/actions/UnassignExecutor.java @@ -51,93 +51,99 @@ * */ @Component -public class UnassignExecutor extends BaseActionExecutor { +public class UnassignExecutor extends AssignmentOperationsExecutor { private static final Trace LOGGER = TraceManager.getTrace(UnassignExecutor.class); - private static final String NAME = "unassign"; - private static final String PARAM_RESOURCE = "resource"; - private static final String PARAM_ROLE = "role"; - private static final String PARAM_RELATION = "relation"; - - @PostConstruct - public void init() { - scriptingExpressionEvaluator.registerActionExecutor(NAME, this); - } - +// private static final String NAME = "unassign"; +// private static final String PARAM_RESOURCE = "resource"; +// private static final String PARAM_ROLE = "role"; +// private static final String PARAM_RELATION = "relation"; + +// @PostConstruct +// public void init() { +// scriptingExpressionEvaluator.registerActionExecutor(NAME, this); +// } + +// @Override +// public PipelineData execute(ActionExpressionType expression, PipelineData input, ExecutionContext context, OperationResult globalResult) throws ScriptExecutionException { +// +// ModelExecuteOptions executionOptions = getOptions(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 relations = expressionHelper.getArgumentValues(expression.getParameter(), PARAM_RELATION, false, false, NAME, input, context, String.class, globalResult); +// +// Collection resources; +// try { +// if (resourceParameterValue != null) { +// PipelineData data = expressionHelper +// .evaluateParameter(resourceParameterValue, null, input, context, globalResult); +// resources = data.getDataAsReferences(ResourceType.COMPLEX_TYPE, ResourceType.class, context, globalResult); +// } else { +// resources = null; +// } +// } catch (CommonException e) { +// throw new ScriptExecutionException("Couldn't evaluate '" + PARAM_RESOURCE + "' parameter of a scripting expression: " + e.getMessage(), e); +// } +// +// Collection roles; +// try { +// if (roleParameterValue != null) { +// PipelineData data = expressionHelper.evaluateParameter(roleParameterValue, null, input, context, globalResult); +// roles = data.getDataAsReferences(RoleType.COMPLEX_TYPE, AbstractRoleType.class, context, globalResult); // if somebody wants to assign Org, he has to use full reference value (including object type) +// } else { +// roles = null; +// } +// } catch (CommonException e) { +// throw new ScriptExecutionException("Couldn't evaluate '" + PARAM_ROLE + "' parameter of a scripting expression: " + e.getMessage(), e); +// } +// +// if (resources == null && roles == null) { +// throw new ScriptExecutionException("Nothing to assign: neither resource nor role specified"); +// } +// +// if (CollectionUtils.isEmpty(resources) && CollectionUtils.isEmpty(roles)) { +// LOGGER.warn("No resources and no roles to unassign in a scripting expression"); +// context.println("Warning: no resources and no roles to unassign"); // TODO some better handling? +// return input; +// } +// +// 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 AssignmentHolderType) { +// @SuppressWarnings({"unchecked", "raw"}) +// PrismObject prismObject = ((PrismObjectValue) value).asPrismObject(); +// AssignmentHolderType objectType = (AssignmentHolderType) prismObject.asObjectable(); +// long started = operationsHelper.recordStart(context, objectType); +// Throwable exception = null; +// try { +// operationsHelper.applyDelta(createDelta(objectType, resources, roles, relations), executionOptions, 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() + optionsSuffix(executionOptions, dryRun) + exceptionSuffix(exception)); +// } else { +// //noinspection ThrowableNotThrown +// processActionException(new ScriptExecutionException("Item is not a PrismObject of AssignmentHolderType"), NAME, value, context); +// } +// operationsHelper.trimAndCloneResult(result, globalResult, context); +// } +// return input; // TODO updated objects? +// } + @Override - public PipelineData execute(ActionExpressionType expression, PipelineData input, ExecutionContext context, OperationResult globalResult) throws ScriptExecutionException { - - ModelExecuteOptions executionOptions = getOptions(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 relations = expressionHelper.getArgumentValues(expression.getParameter(), PARAM_RELATION, false, false, NAME, input, context, String.class, globalResult); - - Collection resources; - try { - if (resourceParameterValue != null) { - PipelineData data = expressionHelper - .evaluateParameter(resourceParameterValue, null, input, context, globalResult); - resources = data.getDataAsReferences(ResourceType.COMPLEX_TYPE, ResourceType.class, context, globalResult); - } else { - resources = null; - } - } catch (CommonException e) { - throw new ScriptExecutionException("Couldn't evaluate '" + PARAM_RESOURCE + "' parameter of a scripting expression: " + e.getMessage(), e); - } - - Collection roles; - try { - if (roleParameterValue != null) { - PipelineData data = expressionHelper.evaluateParameter(roleParameterValue, null, input, context, globalResult); - roles = data.getDataAsReferences(RoleType.COMPLEX_TYPE, AbstractRoleType.class, context, globalResult); // if somebody wants to assign Org, he has to use full reference value (including object type) - } else { - roles = null; - } - } catch (CommonException e) { - throw new ScriptExecutionException("Couldn't evaluate '" + PARAM_ROLE + "' parameter of a scripting expression: " + e.getMessage(), e); - } - - if (resources == null && roles == null) { - throw new ScriptExecutionException("Nothing to assign: neither resource nor role specified"); - } - - if (CollectionUtils.isEmpty(resources) && CollectionUtils.isEmpty(roles)) { - LOGGER.warn("No resources and no roles to unassign in a scripting expression"); - context.println("Warning: no resources and no roles to unassign"); // TODO some better handling? - return input; - } - - 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 AssignmentHolderType) { - @SuppressWarnings({"unchecked", "raw"}) - PrismObject prismObject = ((PrismObjectValue) value).asPrismObject(); - AssignmentHolderType objectType = (AssignmentHolderType) prismObject.asObjectable(); - long started = operationsHelper.recordStart(context, objectType); - Throwable exception = null; - try { - operationsHelper.applyDelta(createDelta(objectType, resources, roles, relations), executionOptions, 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() + optionsSuffix(executionOptions, dryRun) + exceptionSuffix(exception)); - } else { - //noinspection ThrowableNotThrown - processActionException(new ScriptExecutionException("Item is not a PrismObject of AssignmentHolderType"), NAME, value, context); - } - operationsHelper.trimAndCloneResult(result, globalResult, context); - } - return input; // TODO updated objects? + protected String getName() { + return AssignmentOperationsExecutor.UNASSIGN_NAME; } - private ObjectDelta createDelta(AssignmentHolderType object, Collection resources, Collection roles, Collection relations) throws ScriptExecutionException { + @Override + protected ObjectDelta createDelta(AssignmentHolderType object, Collection resources, Collection roles, Collection relations) throws ScriptExecutionException { if (relations == null || relations.isEmpty()) { QName defaultRelation = prismContext.getDefaultRelation() != null ? prismContext.getDefaultRelation() : RelationTypes.MEMBER.getRelation(); diff --git a/model/model-intest/src/test/java/com/evolveum/midpoint/model/intest/scripting/TestScriptingBasic.java b/model/model-intest/src/test/java/com/evolveum/midpoint/model/intest/scripting/TestScriptingBasic.java index b40e5d1166c..fa75b4f42a1 100644 --- a/model/model-intest/src/test/java/com/evolveum/midpoint/model/intest/scripting/TestScriptingBasic.java +++ b/model/model-intest/src/test/java/com/evolveum/midpoint/model/intest/scripting/TestScriptingBasic.java @@ -96,6 +96,7 @@ public class TestScriptingBasic extends AbstractInitializedModelIntegrationTest private static final File UNASSIGN_FROM_WILL_FILE = new File(TEST_DIR, "unassign-from-will.xml"); private static final File UNASSIGN_FROM_WILL_2_FILE = new File(TEST_DIR, "unassign-from-will-2.xml"); private static final File UNASSIGN_FROM_WILL_3_FILE = new File(TEST_DIR, "unassign-from-will-3.xml"); + private static final File ASSIGN_TO_WILL_FILE = new File(TEST_DIR, "assign-to-will.xml"); private static final File PURGE_DUMMY_BLACK_SCHEMA_FILE = new File(TEST_DIR, "purge-dummy-black-schema.xml"); private static final File TEST_DUMMY_RESOURCE_FILE = new File(TEST_DIR, "test-dummy-resource.xml"); private static final File NOTIFICATION_ABOUT_JACK_FILE = new File(TEST_DIR, "notification-about-jack.xml"); @@ -608,8 +609,31 @@ public void test380DisableJackInBackgroundSimple() throws Exception { } @Test - public void test390UnassignFromWill() throws Exception { - final String TEST_NAME = "test390UnassignFromJack"; + public void test390AssignToWill() throws Exception { + final String TEST_NAME = "test390AssignToWill"; + TestUtil.displayTestTitle(this, TEST_NAME); + + // GIVEN + Task task = createTask(DOT_CLASS + TEST_NAME); + OperationResult result = task.getResult(); + PrismProperty expression = parseAnyData(ASSIGN_TO_WILL_FILE); + + // WHEN + ExecutionContext output = scriptingExpressionEvaluator.evaluateExpression(expression.getAnyValue().getValue(), task, result); + + // THEN + dumpOutput(output, result); + assertOutputData(output, 1, OperationResultStatus.SUCCESS); + result.computeStatus(); + TestUtil.assertSuccess(result); + PrismObject will = getUser(USER_WILL_OID); + display("will after assignments creation", will); + MidPointAsserts.assertAssigned(will, "12345678-d34d-b33f-f00d-555555556666", RoleType.COMPLEX_TYPE, RelationTypes.MANAGER.getRelation()); + } + + @Test + public void test391UnassignFromWill() throws Exception { + final String TEST_NAME = "test391UnassignFromJack"; TestUtil.displayTestTitle(this, TEST_NAME); // GIVEN @@ -633,8 +657,8 @@ public void test390UnassignFromWill() throws Exception { } @Test - public void test391UnassignFromWill() throws Exception { - final String TEST_NAME = "test391UnassignFromJack"; + public void test392UnassignFromWill2() throws Exception { + final String TEST_NAME = "test392UnassignFromWill2"; TestUtil.displayTestTitle(this, TEST_NAME); // GIVEN @@ -658,8 +682,8 @@ public void test391UnassignFromWill() throws Exception { } @Test - public void test392UnassignFromWill() throws Exception { - final String TEST_NAME = "test392UnassignFromJack"; + public void test393UnassignFromWill3() throws Exception { + final String TEST_NAME = "test393UnassignFromWill3"; TestUtil.displayTestTitle(this, TEST_NAME); // GIVEN @@ -681,7 +705,7 @@ public void test392UnassignFromWill() throws Exception { MidPointAsserts.assertNotAssigned(will, "12345678-d34d-b33f-f00d-555555556666", RoleType.COMPLEX_TYPE, RelationTypes.MANAGER.getRelation()); MidPointAsserts.assertNotAssignedResource(will, "10000000-0000-0000-0000-000000000004"); } - + @Test public void test400PurgeSchema() throws Exception { final String TEST_NAME = "test400PurgeSchema"; diff --git a/model/model-intest/src/test/resources/common/user-will.xml b/model/model-intest/src/test/resources/common/user-will.xml index 355d3043227..c017d83350d 100644 --- a/model/model-intest/src/test/resources/common/user-will.xml +++ b/model/model-intest/src/test/resources/common/user-will.xml @@ -29,9 +29,6 @@ - - - diff --git a/model/model-intest/src/test/resources/scripting/assign-to-will.xml b/model/model-intest/src/test/resources/scripting/assign-to-will.xml new file mode 100644 index 00000000000..7f691e22afb --- /dev/null +++ b/model/model-intest/src/test/resources/scripting/assign-to-will.xml @@ -0,0 +1,40 @@ + + + + + c:UserType + + + c:name + willTurner + + + + assign + + role + 12345678-d34d-b33f-f00d-555555556666 + + + relation + manager + + + diff --git a/model/model-intest/src/test/resources/scripting/unassign-from-will.xml b/model/model-intest/src/test/resources/scripting/unassign-from-will.xml index 8c9a103506a..e9156a3c1bd 100644 --- a/model/model-intest/src/test/resources/scripting/unassign-from-will.xml +++ b/model/model-intest/src/test/resources/scripting/unassign-from-will.xml @@ -32,9 +32,5 @@ role 12345678-d34d-b33f-f00d-555555556666 - - relation - default -