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.WorkflowEditFactory;
020import com.echothree.control.user.workflow.common.edit.WorkflowEntranceDescriptionEdit;
021import com.echothree.control.user.workflow.common.result.EditWorkflowEntranceDescriptionResult;
022import com.echothree.control.user.workflow.common.result.WorkflowResultFactory;
023import com.echothree.control.user.workflow.common.spec.WorkflowEntranceDescriptionSpec;
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.WorkflowEntrance;
030import com.echothree.model.data.workflow.server.entity.WorkflowEntranceDescription;
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 EditWorkflowEntranceDescriptionCommand
045        extends BaseAbstractEditCommand<WorkflowEntranceDescriptionSpec, WorkflowEntranceDescriptionEdit, EditWorkflowEntranceDescriptionResult, WorkflowEntranceDescription, WorkflowEntrance> {
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.WorkflowEntrance.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("WorkflowEntranceName", FieldType.ENTITY_NAME, true, null, null),
062                new FieldDefinition("LanguageIsoName", FieldType.ENTITY_NAME, true, null, null)
063        );
064        
065        EDIT_FIELD_DEFINITIONS = List.of(
066                new FieldDefinition("Description", FieldType.STRING, true, 1L, 132L)
067        );
068    }
069
070    @Inject
071    PartyControl partyControl;
072
073    @Inject
074    WorkflowControl workflowControl;
075
076    /** Creates a new instance of EditWorkflowEntranceDescriptionCommand */
077    public EditWorkflowEntranceDescriptionCommand() {
078        super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
079    }
080
081    @Override
082    public EditWorkflowEntranceDescriptionResult getResult() {
083        return WorkflowResultFactory.getEditWorkflowEntranceDescriptionResult();
084    }
085
086    @Override
087    public WorkflowEntranceDescriptionEdit getEdit() {
088        return WorkflowEditFactory.getWorkflowEntranceDescriptionEdit();
089    }
090
091    @Override
092    public WorkflowEntranceDescription getEntity(EditWorkflowEntranceDescriptionResult result) {
093        WorkflowEntranceDescription workflowEntranceDescription = null;
094        var workflowName = spec.getWorkflowName();
095        var workflow = workflowControl.getWorkflowByName(workflowName);
096
097        if(workflow != null) {
098            var workflowEntranceName = spec.getWorkflowEntranceName();
099            var workflowEntrance = workflowControl.getWorkflowEntranceByName(workflow, workflowEntranceName);
100
101            if(workflowEntrance != null) {
102                var languageIsoName = spec.getLanguageIsoName();
103                var language = partyControl.getLanguageByIsoName(languageIsoName);
104
105                if(language != null) {
106                    if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) {
107                        workflowEntranceDescription = workflowControl.getWorkflowEntranceDescription(workflowEntrance, language);
108                    } else { // EditMode.UPDATE
109                        workflowEntranceDescription = workflowControl.getWorkflowEntranceDescriptionForUpdate(workflowEntrance, language);
110                    }
111
112                    if(workflowEntranceDescription == null) {
113                        addExecutionError(ExecutionErrors.UnknownWorkflowEntranceDescription.name(), workflowName, workflowEntranceName, languageIsoName);
114                    }
115                } else {
116                    addExecutionError(ExecutionErrors.UnknownLanguageIsoName.name(), languageIsoName);
117                }
118            } else {
119                addExecutionError(ExecutionErrors.UnknownWorkflowEntranceName.name(), workflowName, workflowEntranceName);
120            }
121        } else {
122            addExecutionError(ExecutionErrors.UnknownWorkflowName.name(), workflowName);
123        }
124
125        return workflowEntranceDescription;
126    }
127
128    @Override
129    public WorkflowEntrance getLockEntity(WorkflowEntranceDescription workflowEntranceDescription) {
130        return workflowEntranceDescription.getWorkflowEntrance();
131    }
132
133    @Override
134    public void fillInResult(EditWorkflowEntranceDescriptionResult result, WorkflowEntranceDescription workflowEntranceDescription) {
135        result.setWorkflowEntranceDescription(workflowControl.getWorkflowEntranceDescriptionTransfer(getUserVisit(), workflowEntranceDescription));
136    }
137
138    @Override
139    public void doLock(WorkflowEntranceDescriptionEdit edit, WorkflowEntranceDescription workflowEntranceDescription) {
140        edit.setDescription(workflowEntranceDescription.getDescription());
141    }
142
143    @Override
144    public void doUpdate(WorkflowEntranceDescription workflowEntranceDescription) {
145        var workflowEntranceDescriptionValue = workflowControl.getWorkflowEntranceDescriptionValue(workflowEntranceDescription);
146
147        workflowEntranceDescriptionValue.setDescription(edit.getDescription());
148
149        workflowControl.updateWorkflowEntranceDescriptionFromValue(workflowEntranceDescriptionValue, getPartyPK());
150    }
151    
152}