001// --------------------------------------------------------------------------------
002// Copyright 2002-2025 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.WorkflowDestinationEdit;
020import com.echothree.control.user.workflow.common.edit.WorkflowEditFactory;
021import com.echothree.control.user.workflow.common.form.EditWorkflowDestinationForm;
022import com.echothree.control.user.workflow.common.result.EditWorkflowDestinationResult;
023import com.echothree.control.user.workflow.common.result.WorkflowResultFactory;
024import com.echothree.control.user.workflow.common.spec.WorkflowDestinationUniversalSpec;
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.workflow.server.control.WorkflowControl;
029import com.echothree.model.control.workflow.server.logic.WorkflowDestinationLogic;
030import com.echothree.model.data.user.common.pk.UserVisitPK;
031import com.echothree.model.data.workflow.server.entity.WorkflowDestination;
032import com.echothree.util.common.message.ExecutionErrors;
033import com.echothree.util.common.validation.FieldDefinition;
034import com.echothree.util.common.validation.FieldType;
035import com.echothree.util.server.control.BaseAbstractEditCommand;
036import com.echothree.util.server.control.CommandSecurityDefinition;
037import com.echothree.util.server.control.PartyTypeDefinition;
038import com.echothree.util.server.control.SecurityRoleDefinition;
039import com.echothree.util.server.persistence.Session;
040import java.util.List;
041import javax.enterprise.context.RequestScoped;
042
043@RequestScoped
044public class EditWorkflowDestinationCommand
045        extends BaseAbstractEditCommand<WorkflowDestinationUniversalSpec, WorkflowDestinationEdit, EditWorkflowDestinationResult, WorkflowDestination, WorkflowDestination> {
046    
047    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
048    private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS;
049    private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS;
050    
051    static {
052        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of(
053                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
054                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of(
055                        new SecurityRoleDefinition(SecurityRoleGroups.WorkflowDestination.name(), SecurityRoles.Edit.name())
056                ))
057        ));
058        
059        SPEC_FIELD_DEFINITIONS = List.of(
060                new FieldDefinition("WorkflowName", FieldType.ENTITY_NAME, false, null, null),
061                new FieldDefinition("WorkflowStepName", FieldType.ENTITY_NAME, false, null, null),
062                new FieldDefinition("WorkflowDestinationName", FieldType.ENTITY_NAME, false, null, null),
063                new FieldDefinition("EntityRef", FieldType.ENTITY_REF, false, null, null),
064                new FieldDefinition("Uuid", FieldType.UUID, false, null, null)
065        );
066        
067        EDIT_FIELD_DEFINITIONS = List.of(
068                new FieldDefinition("WorkflowDestinationName", FieldType.ENTITY_NAME, true, null, null),
069                new FieldDefinition("IsDefault", FieldType.BOOLEAN, true, null, null),
070                new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null),
071                new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L)
072        );
073    }
074    
075    /** Creates a new instance of EditWorkflowDestinationCommand */
076    public EditWorkflowDestinationCommand() {
077        super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
078    }
079
080    @Override
081    public EditWorkflowDestinationResult getResult() {
082        return WorkflowResultFactory.getEditWorkflowDestinationResult();
083    }
084
085    @Override
086    public WorkflowDestinationEdit getEdit() {
087        return WorkflowEditFactory.getWorkflowDestinationEdit();
088    }
089
090    @Override
091    public WorkflowDestination getEntity(EditWorkflowDestinationResult result) {
092        return WorkflowDestinationLogic.getInstance().getWorkflowDestinationByUniversalSpec(this, spec, false, editModeToEntityPermission(editMode));
093    }
094
095    @Override
096    public WorkflowDestination getLockEntity(WorkflowDestination freeOnBoard) {
097        return freeOnBoard;
098    }
099
100    @Override
101    public void fillInResult(EditWorkflowDestinationResult result, WorkflowDestination freeOnBoard) {
102        var workflow = Session.getModelController(WorkflowControl.class);
103
104        result.setWorkflowDestination(workflow.getWorkflowDestinationTransfer(getUserVisit(), freeOnBoard));
105    }
106
107    @Override
108    public void doLock(WorkflowDestinationEdit edit, WorkflowDestination workflowDestination) {
109        var workflowControl = Session.getModelController(WorkflowControl.class);
110        var workflowDestinationDescription = workflowControl.getWorkflowDestinationDescription(workflowDestination, getPreferredLanguage());
111        var workflowDestinationDetail = workflowDestination.getLastDetail();
112
113        edit.setWorkflowDestinationName(workflowDestinationDetail.getWorkflowDestinationName());
114        edit.setIsDefault(workflowDestinationDetail.getIsDefault().toString());
115        edit.setSortOrder(workflowDestinationDetail.getSortOrder().toString());
116
117        if(workflowDestinationDescription != null) {
118            edit.setDescription(workflowDestinationDescription.getDescription());
119        }
120    }
121
122    @Override
123    public void canUpdate(WorkflowDestination workflowDestination) {
124        var workflowControl = Session.getModelController(WorkflowControl.class);
125        var workflowStep = workflowDestination.getLastDetail().getWorkflowStep();
126        var workflowDestinationName = edit.getWorkflowDestinationName();
127        var duplicateWorkflowDestination = workflowControl.getWorkflowDestinationByName(workflowStep, workflowDestinationName);
128
129        if(duplicateWorkflowDestination != null && !workflowDestination.equals(duplicateWorkflowDestination)) {
130            addExecutionError(ExecutionErrors.DuplicateWorkflowDestinationName.name(), workflowDestinationName);
131        }
132    }
133
134    @Override
135    public void doUpdate(WorkflowDestination workflowDestination) {
136        var workflowControl = Session.getModelController(WorkflowControl.class);
137        var partyPK = getPartyPK();
138        var workflowDestinationDetailValue = workflowControl.getWorkflowDestinationDetailValueForUpdate(workflowDestination);
139        var workflowDestinationDescription = workflowControl.getWorkflowDestinationDescriptionForUpdate(workflowDestination, getPreferredLanguage());
140        var description = edit.getDescription();
141
142        workflowDestinationDetailValue.setWorkflowDestinationName(edit.getWorkflowDestinationName());
143        workflowDestinationDetailValue.setIsDefault(Boolean.valueOf(edit.getIsDefault()));
144        workflowDestinationDetailValue.setSortOrder(Integer.valueOf(edit.getSortOrder()));
145
146        workflowControl.updateWorkflowDestinationFromValue(workflowDestinationDetailValue, partyPK);
147
148        if(workflowDestinationDescription == null && description != null) {
149            workflowControl.createWorkflowDestinationDescription(workflowDestination, getPreferredLanguage(), description, partyPK);
150        } else if(workflowDestinationDescription != null && description == null) {
151            workflowControl.deleteWorkflowDestinationDescription(workflowDestinationDescription, partyPK);
152        } else if(workflowDestinationDescription != null && description != null) {
153            var workflowDestinationDescriptionValue = workflowControl.getWorkflowDestinationDescriptionValue(workflowDestinationDescription);
154
155            workflowDestinationDescriptionValue.setDescription(description);
156            workflowControl.updateWorkflowDestinationDescriptionFromValue(workflowDestinationDescriptionValue, partyPK);
157        }
158    }
159    
160}