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.WorkflowDestinationDescriptionEdit;
020import com.echothree.control.user.workflow.common.edit.WorkflowEditFactory;
021import com.echothree.control.user.workflow.common.form.EditWorkflowDestinationDescriptionForm;
022import com.echothree.control.user.workflow.common.result.WorkflowResultFactory;
023import com.echothree.control.user.workflow.common.spec.WorkflowDestinationDescriptionSpec;
024import com.echothree.model.control.party.common.PartyTypes;
025import com.echothree.model.control.party.server.control.PartyControl;
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.data.user.common.pk.UserVisitPK;
030import com.echothree.util.common.message.ExecutionErrors;
031import com.echothree.util.common.validation.FieldDefinition;
032import com.echothree.util.common.validation.FieldType;
033import com.echothree.util.common.command.BaseResult;
034import com.echothree.util.common.command.EditMode;
035import com.echothree.util.server.control.BaseEditCommand;
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.Arrays;
041import java.util.Collections;
042import java.util.List;
043import javax.enterprise.context.RequestScoped;
044
045@RequestScoped
046public class EditWorkflowDestinationDescriptionCommand
047        extends BaseEditCommand<WorkflowDestinationDescriptionSpec, WorkflowDestinationDescriptionEdit> {
048    
049    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
050    private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS;
051    private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS;
052    
053    static {
054        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(Collections.unmodifiableList(Arrays.asList(
055                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
056                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), Collections.unmodifiableList(Arrays.asList(
057                        new SecurityRoleDefinition(SecurityRoleGroups.WorkflowDestination.name(), SecurityRoles.Description.name())
058                        )))
059                )));
060        
061        SPEC_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
062                new FieldDefinition("WorkflowName", FieldType.ENTITY_NAME, true, null, null),
063                new FieldDefinition("WorkflowStepName", FieldType.ENTITY_NAME, true, null, null),
064                new FieldDefinition("WorkflowDestinationName", FieldType.ENTITY_NAME, true, null, null),
065                new FieldDefinition("LanguageIsoName", FieldType.ENTITY_NAME, true, null, null)
066                ));
067        
068        EDIT_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
069                new FieldDefinition("Description", FieldType.STRING, true, 1L, 132L)
070                ));
071    }
072    
073    /** Creates a new instance of EditWorkflowDestinationDescriptionCommand */
074    public EditWorkflowDestinationDescriptionCommand() {
075        super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
076    }
077    
078    @Override
079    protected BaseResult execute() {
080        var workflowControl = Session.getModelController(WorkflowControl.class);
081        var result = WorkflowResultFactory.getEditWorkflowDestinationDescriptionResult();
082        var workflowName = spec.getWorkflowName();
083        var workflow = workflowControl.getWorkflowByName(workflowName);
084        
085        if(workflow != null) {
086            var workflowStepName = spec.getWorkflowStepName();
087            var workflowStep = workflowControl.getWorkflowStepByName(workflow, workflowStepName);
088            
089            if(workflowStep != null) {
090                var workflowDestinationName = spec.getWorkflowDestinationName();
091                var workflowDestination = workflowControl.getWorkflowDestinationByName(workflowStep, workflowDestinationName);
092                
093                if(workflowDestination != null) {
094                    var partyControl = Session.getModelController(PartyControl.class);
095                    var languageIsoName = spec.getLanguageIsoName();
096                    var language = partyControl.getLanguageByIsoName(languageIsoName);
097                    
098                    if(language != null) {
099                        if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) {
100                            var workflowDestinationDescription = workflowControl.getWorkflowDestinationDescription(workflowDestination, language);
101                            
102                            if(workflowDestinationDescription != null) {
103                                if(editMode.equals(EditMode.LOCK)) {
104                                    result.setWorkflowDestinationDescription(workflowControl.getWorkflowDestinationDescriptionTransfer(getUserVisit(), workflowDestinationDescription));
105
106                                    if(lockEntity(workflowDestination)) {
107                                        var edit = WorkflowEditFactory.getWorkflowDestinationDescriptionEdit();
108
109                                        result.setEdit(edit);
110                                        edit.setDescription(workflowDestinationDescription.getDescription());
111                                    } else {
112                                        addExecutionError(ExecutionErrors.EntityLockFailed.name());
113                                    }
114
115                                    result.setEntityLock(getEntityLockTransfer(workflowDestination));
116                                } else { // EditMode.ABANDON
117                                    unlockEntity(workflowDestination);
118                                }
119                            } else {
120                                addExecutionError(ExecutionErrors.UnknownWorkflowDestinationDescription.name());
121                            }
122                        } else if(editMode.equals(EditMode.UPDATE)) {
123                            var workflowDestinationDescriptionValue = workflowControl.getWorkflowDestinationDescriptionValueForUpdate(workflowDestination, language);
124                            
125                            if(workflowDestinationDescriptionValue != null) {
126                                if(lockEntityForUpdate(workflowDestination)) {
127                                    try {
128                                        var description = edit.getDescription();
129                                        
130                                        workflowDestinationDescriptionValue.setDescription(description);
131                                        
132                                        workflowControl.updateWorkflowDestinationDescriptionFromValue(workflowDestinationDescriptionValue, getPartyPK());
133                                    } finally {
134                                        unlockEntity(workflowDestination);
135                                    }
136                                } else {
137                                    addExecutionError(ExecutionErrors.EntityLockStale.name());
138                                }
139                            } else {
140                                addExecutionError(ExecutionErrors.UnknownWorkflowDestinationDescription.name());
141                            }
142                        }
143                    } else {
144                        addExecutionError(ExecutionErrors.UnknownLanguageIsoName.name(), languageIsoName);
145                    }
146                } else {
147                    addExecutionError(ExecutionErrors.UnknownWorkflowDestinationName.name(), workflowDestinationName);
148                }
149            } else {
150                addExecutionError(ExecutionErrors.UnknownWorkflowStepName.name(), workflowStepName);
151            }
152        } else {
153            addExecutionError(ExecutionErrors.UnknownWorkflowName.name(), workflowName);
154        }
155        
156        return result;
157    }
158    
159}