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.employee.server.command;
018
019import com.echothree.control.user.employee.common.edit.EmployeeEditFactory;
020import com.echothree.control.user.employee.common.edit.TerminationReasonEdit;
021import com.echothree.control.user.employee.common.form.EditTerminationReasonForm;
022import com.echothree.control.user.employee.common.result.EmployeeResultFactory;
023import com.echothree.control.user.employee.common.spec.TerminationReasonSpec;
024import com.echothree.model.control.employee.server.control.EmployeeControl;
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.data.user.common.pk.UserVisitPK;
029import com.echothree.util.common.message.ExecutionErrors;
030import com.echothree.util.common.validation.FieldDefinition;
031import com.echothree.util.common.validation.FieldType;
032import com.echothree.util.common.command.BaseResult;
033import com.echothree.util.common.command.EditMode;
034import com.echothree.util.server.control.BaseEditCommand;
035import com.echothree.util.server.control.CommandSecurityDefinition;
036import com.echothree.util.server.control.PartyTypeDefinition;
037import com.echothree.util.server.control.SecurityRoleDefinition;
038import com.echothree.util.server.persistence.Session;
039import java.util.ArrayList;
040import java.util.Collections;
041import java.util.List;
042import javax.enterprise.context.RequestScoped;
043
044@RequestScoped
045public class EditTerminationReasonCommand
046        extends BaseEditCommand<TerminationReasonSpec, TerminationReasonEdit> {
047
048    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
049    private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS;
050    private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS;
051    
052    static {
053        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of(
054                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
055                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of(
056                        new SecurityRoleDefinition(SecurityRoleGroups.TerminationReason.name(), SecurityRoles.Edit.name())
057                ))
058        ));
059
060        List<FieldDefinition> temp = new ArrayList<>(1);
061        temp.add(new FieldDefinition("TerminationReasonName", FieldType.ENTITY_NAME, true, null, null));
062        SPEC_FIELD_DEFINITIONS = Collections.unmodifiableList(temp);
063        
064        temp = new ArrayList<>(4);
065        temp.add(new FieldDefinition("TerminationReasonName", FieldType.ENTITY_NAME, true, null, null));
066        temp.add(new FieldDefinition("IsDefault", FieldType.BOOLEAN, true, null, null));
067        temp.add(new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null));
068        temp.add(new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L));
069        EDIT_FIELD_DEFINITIONS = Collections.unmodifiableList(temp);
070    }
071    
072    /** Creates a new instance of EditTerminationReasonCommand */
073    public EditTerminationReasonCommand() {
074        super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
075    }
076    
077    @Override
078    protected BaseResult execute() {
079        var employeeControl = Session.getModelController(EmployeeControl.class);
080        var result = EmployeeResultFactory.getEditTerminationReasonResult();
081        
082        if(editMode.equals(EditMode.LOCK)) {
083            var terminationReasonName = spec.getTerminationReasonName();
084            var terminationReason = employeeControl.getTerminationReasonByName(terminationReasonName);
085            
086            if(terminationReason != null) {
087                result.setTerminationReason(employeeControl.getTerminationReasonTransfer(getUserVisit(), terminationReason));
088                
089                if(lockEntity(terminationReason)) {
090                    var terminationReasonDescription = employeeControl.getTerminationReasonDescription(terminationReason, getPreferredLanguage());
091                    var edit = EmployeeEditFactory.getTerminationReasonEdit();
092                    var terminationReasonDetail = terminationReason.getLastDetail();
093                    
094                    result.setEdit(edit);
095                    edit.setTerminationReasonName(terminationReasonDetail.getTerminationReasonName());
096                    edit.setIsDefault(terminationReasonDetail.getIsDefault().toString());
097                    edit.setSortOrder(terminationReasonDetail.getSortOrder().toString());
098                    
099                    if(terminationReasonDescription != null)
100                        edit.setDescription(terminationReasonDescription.getDescription());
101                } else {
102                    addExecutionError(ExecutionErrors.EntityLockFailed.name());
103                }
104                
105                result.setEntityLock(getEntityLockTransfer(terminationReason));
106            } else {
107                addExecutionError(ExecutionErrors.UnknownTerminationReasonName.name(), terminationReasonName);
108            }
109        } else if(editMode.equals(EditMode.UPDATE)) {
110            var terminationReasonName = spec.getTerminationReasonName();
111            var terminationReason = employeeControl.getTerminationReasonByNameForUpdate(terminationReasonName);
112            
113            if(terminationReason != null) {
114                if(lockEntityForUpdate(terminationReason)) {
115                    try {
116                        var partyPK = getPartyPK();
117                        var terminationReasonDetailValue = employeeControl.getTerminationReasonDetailValueForUpdate(terminationReason);
118                        var terminationReasonDescription = employeeControl.getTerminationReasonDescriptionForUpdate(terminationReason, getPreferredLanguage());
119                        var description = edit.getDescription();
120                        
121                        terminationReasonDetailValue.setTerminationReasonName(edit.getTerminationReasonName());
122                        terminationReasonDetailValue.setIsDefault(Boolean.valueOf(edit.getIsDefault()));
123                        terminationReasonDetailValue.setSortOrder(Integer.valueOf(edit.getSortOrder()));
124                        
125                        employeeControl.updateTerminationReasonFromValue(terminationReasonDetailValue, partyPK);
126                        
127                        if(terminationReasonDescription == null && description != null) {
128                            employeeControl.createTerminationReasonDescription(terminationReason, getPreferredLanguage(), description, partyPK);
129                        } else if(terminationReasonDescription != null && description == null) {
130                            employeeControl.deleteTerminationReasonDescription(terminationReasonDescription, partyPK);
131                        } else if(terminationReasonDescription != null && description != null) {
132                            var terminationReasonDescriptionValue = employeeControl.getTerminationReasonDescriptionValue(terminationReasonDescription);
133                            
134                            terminationReasonDescriptionValue.setDescription(description);
135                            employeeControl.updateTerminationReasonDescriptionFromValue(terminationReasonDescriptionValue, partyPK);
136                        }
137                    } finally {
138                        unlockEntity(terminationReason);
139                    }
140                } else {
141                    addExecutionError(ExecutionErrors.EntityLockStale.name());
142                }
143            } else {
144                addExecutionError(ExecutionErrors.UnknownTerminationReasonName.name(), terminationReasonName);
145            }
146        }
147        
148        return result;
149    }
150    
151}