001// --------------------------------------------------------------------------------
002// Copyright 2002-2024 Echo Three, LLC
003//
004// Licensed under the Apache License, Version 2.0 (the "License");
005// you may not use this file except in compliance with the License.
006// You may obtain a copy of the License at
007//
008//     http://www.apache.org/licenses/LICENSE-2.0
009//
010// Unless required by applicable law or agreed to in writing, software
011// distributed under the License is distributed on an "AS IS" BASIS,
012// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013// See the License for the specific language governing permissions and
014// limitations under the License.
015// --------------------------------------------------------------------------------
016
017package com.echothree.control.user.workflow.server.command;
018
019import com.echothree.control.user.workflow.common.edit.WorkflowEdit;
020import com.echothree.control.user.workflow.common.edit.WorkflowEditFactory;
021import com.echothree.control.user.workflow.common.form.EditWorkflowForm;
022import com.echothree.control.user.workflow.common.result.EditWorkflowResult;
023import com.echothree.control.user.workflow.common.result.WorkflowResultFactory;
024import com.echothree.control.user.workflow.common.spec.WorkflowUniversalSpec;
025import com.echothree.model.control.party.common.PartyTypes;
026import com.echothree.model.control.security.common.SecurityRoleGroups;
027import com.echothree.model.control.security.common.SecurityRoles;
028import com.echothree.model.control.security.server.logic.SecurityRoleGroupLogic;
029import com.echothree.model.control.selector.server.logic.SelectorTypeLogic;
030import com.echothree.model.control.workflow.server.control.WorkflowControl;
031import com.echothree.model.control.workflow.server.logic.WorkflowLogic;
032import com.echothree.model.data.security.server.entity.SecurityRoleGroup;
033import com.echothree.model.data.selector.server.entity.SelectorType;
034import com.echothree.model.data.user.common.pk.UserVisitPK;
035import com.echothree.model.data.workflow.server.entity.Workflow;
036import com.echothree.model.data.workflow.server.entity.WorkflowDescription;
037import com.echothree.model.data.workflow.server.value.WorkflowDescriptionValue;
038import com.echothree.model.data.workflow.server.value.WorkflowDetailValue;
039import com.echothree.util.common.message.ExecutionErrors;
040import com.echothree.util.common.validation.FieldDefinition;
041import com.echothree.util.common.validation.FieldType;
042import com.echothree.util.server.control.BaseAbstractEditCommand;
043import com.echothree.util.server.control.CommandSecurityDefinition;
044import com.echothree.util.server.control.PartyTypeDefinition;
045import com.echothree.util.server.control.SecurityRoleDefinition;
046import com.echothree.util.server.persistence.Session;
047import java.util.Arrays;
048import java.util.Collections;
049import java.util.List;
050
051public class EditWorkflowCommand
052        extends BaseAbstractEditCommand<WorkflowUniversalSpec, WorkflowEdit, EditWorkflowResult, Workflow, Workflow> {
053
054    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
055    private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS;
056    private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS;
057    
058    static {
059        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of(
060                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
061                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of(
062                    new SecurityRoleDefinition(SecurityRoleGroups.Workflow.name(), SecurityRoles.Edit.name())
063            ))
064        ));
065        
066        SPEC_FIELD_DEFINITIONS = List.of(
067                new FieldDefinition("WorkflowName", FieldType.ENTITY_NAME, false, null, null),
068                new FieldDefinition("EntityRef", FieldType.ENTITY_REF, false, null, null),
069                new FieldDefinition("Key", FieldType.KEY, false, null, null),
070                new FieldDefinition("Guid", FieldType.GUID, false, null, null),
071                new FieldDefinition("Ulid", FieldType.ULID, false, null, null)
072        );
073        
074        EDIT_FIELD_DEFINITIONS = List.of(
075                new FieldDefinition("WorkflowName", FieldType.ENTITY_NAME, true, null, null),
076                new FieldDefinition("SelectorKindName", FieldType.ENTITY_NAME, false, null, null),
077                new FieldDefinition("SelectorTypeName", FieldType.ENTITY_NAME, false, null, null),
078                new FieldDefinition("SecurityRoleGroupName", FieldType.ENTITY_NAME, false, null, null),
079                new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null),
080                new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L)
081        );
082    }
083    
084    /** Creates a new instance of EditWorkflowCommand */
085    public EditWorkflowCommand(UserVisitPK userVisitPK, EditWorkflowForm form) {
086        super(userVisitPK, form, COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
087    }
088
089    @Override
090    public EditWorkflowResult getResult() {
091        return WorkflowResultFactory.getEditWorkflowResult();
092    }
093
094    @Override
095    public WorkflowEdit getEdit() {
096        return WorkflowEditFactory.getWorkflowEdit();
097    }
098
099    @Override
100    public Workflow getEntity(EditWorkflowResult result) {
101        return WorkflowLogic.getInstance().getWorkflowByUniversalSpec(this, spec, editModeToEntityPermission(editMode));
102    }
103
104    @Override
105    public Workflow getLockEntity(Workflow freeOnBoard) {
106        return freeOnBoard;
107    }
108
109    @Override
110    public void fillInResult(EditWorkflowResult result, Workflow freeOnBoard) {
111        var workflow = Session.getModelController(WorkflowControl.class);
112
113        result.setWorkflow(workflow.getWorkflowTransfer(getUserVisit(), freeOnBoard));
114    }
115
116    @Override
117    public void doLock(WorkflowEdit edit, Workflow workflow) {
118        var workflowControl = Session.getModelController(WorkflowControl.class);
119        var workflowDescription = workflowControl.getWorkflowDescription(workflow, getPreferredLanguage());
120        var workflowDetail = workflow.getLastDetail();
121        var selectorType = workflowDetail.getSelectorType();
122        var selectorKind = selectorType == null ? null : selectorType.getLastDetail().getSelectorKind();
123        var securityRoleGroup = workflowDetail.getSecurityRoleGroup();
124
125        edit.setWorkflowName(workflowDetail.getWorkflowName());
126        edit.setSelectorKindName(selectorKind == null ? null : selectorKind.getLastDetail().getSelectorKindName());
127        edit.setSelectorTypeName(selectorType == null ? null : selectorType.getLastDetail().getSelectorTypeName());
128        edit.setSecurityRoleGroupName(securityRoleGroup == null ? null : securityRoleGroup.getLastDetail().getSecurityRoleGroupName());
129        edit.setSortOrder(workflowDetail.getSortOrder().toString());
130
131        if(workflowDescription != null) {
132            edit.setDescription(workflowDescription.getDescription());
133        }
134    }
135
136    SelectorType selectorType;
137    SecurityRoleGroup securityRoleGroup;
138
139    @Override
140    public void canUpdate(Workflow workflow) {
141        var workflowControl = Session.getModelController(WorkflowControl.class);
142        var workflowName = edit.getWorkflowName();
143        var duplicateWorkflow = workflowControl.getWorkflowByName(workflowName);
144
145        if(duplicateWorkflow != null && !workflow.equals(duplicateWorkflow)) {
146            addExecutionError(ExecutionErrors.DuplicateWorkflowName.name(), workflowName);
147        } else {
148            var selectorKindName = edit.getSelectorKindName();
149            var selectorTypeName = edit.getSelectorTypeName();
150            var parameterCount = (selectorKindName == null ? 0 : 1) + (selectorTypeName == null ? 0 : 1);
151
152            if(parameterCount == 0 || parameterCount == 2) {
153                selectorType = parameterCount == 0 ? null : SelectorTypeLogic.getInstance().getSelectorTypeByName(this, selectorKindName, selectorTypeName);
154
155                if(!hasExecutionErrors()) {
156                    var securityRoleGroupName = edit.getSecurityRoleGroupName();
157
158                    securityRoleGroup = securityRoleGroupName == null ? null : SecurityRoleGroupLogic.getInstance().getSecurityRoleGroupByName(this, securityRoleGroupName);
159                }
160            } else {
161                addExecutionError(ExecutionErrors.InvalidParameterCount.name());
162            }
163        }
164    }
165
166    @Override
167    public void doUpdate(Workflow workflow) {
168        var workflowControl = Session.getModelController(WorkflowControl.class);
169        var partyPK = getPartyPK();
170        WorkflowDetailValue workflowDetailValue = workflowControl.getWorkflowDetailValueForUpdate(workflow);
171        WorkflowDescription workflowDescription = workflowControl.getWorkflowDescriptionForUpdate(workflow, getPreferredLanguage());
172        String description = edit.getDescription();
173
174        workflowDetailValue.setWorkflowName(edit.getWorkflowName());
175        workflowDetailValue.setSelectorTypePK(selectorType == null ? null : selectorType.getPrimaryKey());
176        workflowDetailValue.setSecurityRoleGroupPK(securityRoleGroup == null ? null : securityRoleGroup.getPrimaryKey());
177        workflowDetailValue.setSortOrder(Integer.valueOf(edit.getSortOrder()));
178
179        workflowControl.updateWorkflowFromValue(workflowDetailValue, partyPK);
180
181        if(workflowDescription == null && description != null) {
182            workflowControl.createWorkflowDescription(workflow, getPreferredLanguage(), description, partyPK);
183        } else if(workflowDescription != null && description == null) {
184            workflowControl.deleteWorkflowDescription(workflowDescription, partyPK);
185        } else if(workflowDescription != null && description != null) {
186            WorkflowDescriptionValue workflowDescriptionValue = workflowControl.getWorkflowDescriptionValue(workflowDescription);
187
188            workflowDescriptionValue.setDescription(description);
189            workflowControl.updateWorkflowDescriptionFromValue(workflowDescriptionValue, partyPK);
190        }
191    }
192    
193}