001// --------------------------------------------------------------------------------
002// Copyright 2002-2026 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.result.EditWorkflowDestinationDescriptionResult;
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.workflow.server.entity.WorkflowDestination;
030import com.echothree.model.data.workflow.server.entity.WorkflowDestinationDescription;
031import com.echothree.util.common.message.ExecutionErrors;
032import com.echothree.util.common.validation.FieldDefinition;
033import com.echothree.util.common.validation.FieldType;
034import com.echothree.util.common.command.EditMode;
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 java.util.List;
040import javax.enterprise.context.Dependent;
041import javax.inject.Inject;
042
043@Dependent
044public class EditWorkflowDestinationDescriptionCommand
045        extends BaseAbstractEditCommand<WorkflowDestinationDescriptionSpec, WorkflowDestinationDescriptionEdit, EditWorkflowDestinationDescriptionResult, WorkflowDestinationDescription, 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.Description.name())
056                ))
057        ));
058        
059        SPEC_FIELD_DEFINITIONS = List.of(
060                new FieldDefinition("WorkflowName", FieldType.ENTITY_NAME, true, null, null),
061                new FieldDefinition("WorkflowStepName", FieldType.ENTITY_NAME, true, null, null),
062                new FieldDefinition("WorkflowDestinationName", FieldType.ENTITY_NAME, true, null, null),
063                new FieldDefinition("LanguageIsoName", FieldType.ENTITY_NAME, true, null, null)
064        );
065        
066        EDIT_FIELD_DEFINITIONS = List.of(
067                new FieldDefinition("Description", FieldType.STRING, true, 1L, 132L)
068        );
069    }
070
071    @Inject
072    PartyControl partyControl;
073
074    @Inject
075    WorkflowControl workflowControl;
076
077    /** Creates a new instance of EditWorkflowDestinationDescriptionCommand */
078    public EditWorkflowDestinationDescriptionCommand() {
079        super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
080    }
081
082    @Override
083    public EditWorkflowDestinationDescriptionResult getResult() {
084        return WorkflowResultFactory.getEditWorkflowDestinationDescriptionResult();
085    }
086
087    @Override
088    public WorkflowDestinationDescriptionEdit getEdit() {
089        return WorkflowEditFactory.getWorkflowDestinationDescriptionEdit();
090    }
091
092    @Override
093    public WorkflowDestinationDescription getEntity(EditWorkflowDestinationDescriptionResult result) {
094        WorkflowDestinationDescription workflowDestinationDescription = null;
095        var workflowName = spec.getWorkflowName();
096        var workflow = workflowControl.getWorkflowByName(workflowName);
097
098        if(workflow != null) {
099            var workflowStepName = spec.getWorkflowStepName();
100            var workflowStep = workflowControl.getWorkflowStepByName(workflow, workflowStepName);
101
102            if(workflowStep != null) {
103                var workflowDestinationName = spec.getWorkflowDestinationName();
104                var workflowDestination = workflowControl.getWorkflowDestinationByName(workflowStep, workflowDestinationName);
105
106                if(workflowDestination != null) {
107                    var languageIsoName = spec.getLanguageIsoName();
108                    var language = partyControl.getLanguageByIsoName(languageIsoName);
109
110                    if(language != null) {
111                        if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) {
112                            workflowDestinationDescription = workflowControl.getWorkflowDestinationDescription(workflowDestination, language);
113                        } else { // EditMode.UPDATE
114                            workflowDestinationDescription = workflowControl.getWorkflowDestinationDescriptionForUpdate(workflowDestination, language);
115                        }
116
117                        if(workflowDestinationDescription == null) {
118                            addExecutionError(ExecutionErrors.UnknownWorkflowDestinationDescription.name(), workflowName, workflowStepName, workflowDestinationName, languageIsoName);
119                        }
120                    } else {
121                        addExecutionError(ExecutionErrors.UnknownLanguageIsoName.name(), languageIsoName);
122                    }
123                } else {
124                    addExecutionError(ExecutionErrors.UnknownWorkflowDestinationName.name(), workflowName, workflowStepName, workflowDestinationName);
125                }
126            } else {
127                addExecutionError(ExecutionErrors.UnknownWorkflowStepName.name(), workflowName, workflowStepName);
128            }
129        } else {
130            addExecutionError(ExecutionErrors.UnknownWorkflowName.name(), workflowName);
131        }
132
133        return workflowDestinationDescription;
134    }
135
136    @Override
137    public WorkflowDestination getLockEntity(WorkflowDestinationDescription workflowDestinationDescription) {
138        return workflowDestinationDescription.getWorkflowDestination();
139    }
140
141    @Override
142    public void fillInResult(EditWorkflowDestinationDescriptionResult result, WorkflowDestinationDescription workflowDestinationDescription) {
143        result.setWorkflowDestinationDescription(workflowControl.getWorkflowDestinationDescriptionTransfer(getUserVisit(), workflowDestinationDescription));
144    }
145
146    @Override
147    public void doLock(WorkflowDestinationDescriptionEdit edit, WorkflowDestinationDescription workflowDestinationDescription) {
148        edit.setDescription(workflowDestinationDescription.getDescription());
149    }
150
151    @Override
152    public void doUpdate(WorkflowDestinationDescription workflowDestinationDescription) {
153        var workflowDestinationDescriptionValue = workflowControl.getWorkflowDestinationDescriptionValue(workflowDestinationDescription);
154
155        workflowDestinationDescriptionValue.setDescription(edit.getDescription());
156
157        workflowControl.updateWorkflowDestinationDescriptionFromValue(workflowDestinationDescriptionValue, getPartyPK());
158    }
159    
160}