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.LeaveReasonEdit;
021import com.echothree.control.user.employee.common.form.EditLeaveReasonForm;
022import com.echothree.control.user.employee.common.result.EditLeaveReasonResult;
023import com.echothree.control.user.employee.common.result.EmployeeResultFactory;
024import com.echothree.control.user.employee.common.spec.LeaveReasonSpec;
025import com.echothree.model.control.employee.server.control.EmployeeControl;
026import com.echothree.model.control.party.common.PartyTypes;
027import com.echothree.model.control.security.common.SecurityRoleGroups;
028import com.echothree.model.control.security.common.SecurityRoles;
029import com.echothree.model.data.employee.server.entity.LeaveReason;
030import com.echothree.model.data.user.common.pk.UserVisitPK;
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 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 EditLeaveReasonCommand
047        extends BaseAbstractEditCommand<LeaveReasonSpec, LeaveReasonEdit, EditLeaveReasonResult, LeaveReason, LeaveReason> {
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.LeaveReason.name(), SecurityRoles.Edit.name())
058                        )))
059                )));
060
061        SPEC_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
062                new FieldDefinition("LeaveReasonName", FieldType.ENTITY_NAME, true, null, null)
063                ));
064
065        EDIT_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
066                new FieldDefinition("LeaveReasonName", FieldType.ENTITY_NAME, true, null, null),
067                new FieldDefinition("IsDefault", FieldType.BOOLEAN, true, null, null),
068                new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null),
069                new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L)
070                ));
071    }
072
073    /** Creates a new instance of EditLeaveReasonCommand */
074    public EditLeaveReasonCommand() {
075        super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
076    }
077
078    @Override
079    public EditLeaveReasonResult getResult() {
080        return EmployeeResultFactory.getEditLeaveReasonResult();
081    }
082
083    @Override
084    public LeaveReasonEdit getEdit() {
085        return EmployeeEditFactory.getLeaveReasonEdit();
086    }
087
088    @Override
089    public LeaveReason getEntity(EditLeaveReasonResult result) {
090        var employeeControl = Session.getModelController(EmployeeControl.class);
091        LeaveReason leaveReason;
092        var leaveReasonName = spec.getLeaveReasonName();
093
094        if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) {
095            leaveReason = employeeControl.getLeaveReasonByName(leaveReasonName);
096        } else { // EditMode.UPDATE
097            leaveReason = employeeControl.getLeaveReasonByNameForUpdate(leaveReasonName);
098        }
099
100        if(leaveReason != null) {
101            result.setLeaveReason(employeeControl.getLeaveReasonTransfer(getUserVisit(), leaveReason));
102        } else {
103            addExecutionError(ExecutionErrors.UnknownLeaveReasonName.name(), leaveReasonName);
104        }
105
106        return leaveReason;
107    }
108
109    @Override
110    public LeaveReason getLockEntity(LeaveReason leaveReason) {
111        return leaveReason;
112    }
113
114    @Override
115    public void fillInResult(EditLeaveReasonResult result, LeaveReason leaveReason) {
116        var employeeControl = Session.getModelController(EmployeeControl.class);
117
118        result.setLeaveReason(employeeControl.getLeaveReasonTransfer(getUserVisit(), leaveReason));
119    }
120
121    @Override
122    public void doLock(LeaveReasonEdit edit, LeaveReason leaveReason) {
123        var employeeControl = Session.getModelController(EmployeeControl.class);
124        var leaveReasonDescription = employeeControl.getLeaveReasonDescription(leaveReason, getPreferredLanguage());
125        var leaveReasonDetail = leaveReason.getLastDetail();
126
127        edit.setLeaveReasonName(leaveReasonDetail.getLeaveReasonName());
128        edit.setIsDefault(leaveReasonDetail.getIsDefault().toString());
129        edit.setSortOrder(leaveReasonDetail.getSortOrder().toString());
130
131        if(leaveReasonDescription != null) {
132            edit.setDescription(leaveReasonDescription.getDescription());
133        }
134    }
135
136    @Override
137    public void canUpdate(LeaveReason leaveReason) {
138        var employeeControl = Session.getModelController(EmployeeControl.class);
139        var leaveReasonName = edit.getLeaveReasonName();
140        var duplicateLeaveReason = employeeControl.getLeaveReasonByName(leaveReasonName);
141
142        if(duplicateLeaveReason != null && !leaveReason.equals(duplicateLeaveReason)) {
143            addExecutionError(ExecutionErrors.DuplicateLeaveReasonName.name(), leaveReasonName);
144        }
145    }
146
147    @Override
148    public void doUpdate(LeaveReason leaveReason) {
149        var employeeControl = Session.getModelController(EmployeeControl.class);
150        var partyPK = getPartyPK();
151        var leaveReasonDetailValue = employeeControl.getLeaveReasonDetailValueForUpdate(leaveReason);
152        var leaveReasonDescription = employeeControl.getLeaveReasonDescriptionForUpdate(leaveReason, getPreferredLanguage());
153        var description = edit.getDescription();
154
155        leaveReasonDetailValue.setLeaveReasonName(edit.getLeaveReasonName());
156        leaveReasonDetailValue.setIsDefault(Boolean.valueOf(edit.getIsDefault()));
157        leaveReasonDetailValue.setSortOrder(Integer.valueOf(edit.getSortOrder()));
158
159        employeeControl.updateLeaveReasonFromValue(leaveReasonDetailValue, partyPK);
160
161        if(leaveReasonDescription == null && description != null) {
162            employeeControl.createLeaveReasonDescription(leaveReason, getPreferredLanguage(), description, partyPK);
163        } else if(leaveReasonDescription != null && description == null) {
164            employeeControl.deleteLeaveReasonDescription(leaveReasonDescription, partyPK);
165        } else if(leaveReasonDescription != null && description != null) {
166            var leaveReasonDescriptionValue = employeeControl.getLeaveReasonDescriptionValue(leaveReasonDescription);
167
168            leaveReasonDescriptionValue.setDescription(description);
169            employeeControl.updateLeaveReasonDescriptionFromValue(leaveReasonDescriptionValue, partyPK);
170        }
171    }
172
173}