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.employee.server.command;
018
019import com.echothree.control.user.employee.common.edit.EmployeeEditFactory;
020import com.echothree.control.user.employee.common.edit.EmploymentEdit;
021import com.echothree.control.user.employee.common.form.EditEmploymentForm;
022import com.echothree.control.user.employee.common.result.EditEmploymentResult;
023import com.echothree.control.user.employee.common.result.EmployeeResultFactory;
024import com.echothree.control.user.employee.common.spec.EmploymentSpec;
025import com.echothree.model.control.employee.server.control.EmployeeControl;
026import com.echothree.model.control.party.server.control.PartyControl;
027import com.echothree.model.data.employee.server.entity.Employment;
028import com.echothree.model.data.employee.server.entity.TerminationReason;
029import com.echothree.model.data.employee.server.entity.TerminationType;
030import com.echothree.model.data.item.server.entity.Item;
031import com.echothree.model.data.item.server.entity.ItemDescriptionType;
032import com.echothree.model.data.party.server.entity.PartyCompany;
033import com.echothree.model.data.user.common.pk.UserVisitPK;
034import com.echothree.util.common.message.ExecutionErrors;
035import com.echothree.util.common.validation.FieldDefinition;
036import com.echothree.util.common.validation.FieldType;
037import com.echothree.util.common.command.EditMode;
038import com.echothree.util.server.control.BaseAbstractEditCommand;
039import com.echothree.util.server.string.DateUtils;
040import java.util.List;
041import javax.enterprise.context.Dependent;
042import javax.inject.Inject;
043
044@Dependent
045public class EditEmploymentCommand
046        extends BaseAbstractEditCommand<EmploymentSpec, EmploymentEdit, EditEmploymentResult, Employment, Employment> {
047    
048    private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS;
049    private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS;
050    
051    static {
052        SPEC_FIELD_DEFINITIONS = List.of(
053                new FieldDefinition("EmploymentName", FieldType.ENTITY_NAME, true, null, null)
054        );
055        
056        EDIT_FIELD_DEFINITIONS = List.of(
057                new FieldDefinition("CompanyName", FieldType.ENTITY_NAME, true, null, null),
058                new FieldDefinition("StartTime", FieldType.DATE_TIME, true, null, null),
059                new FieldDefinition("EndTime", FieldType.DATE_TIME, false, null, null),
060                new FieldDefinition("TerminationTypeName", FieldType.ENTITY_NAME, false, null, null),
061                new FieldDefinition("TerminationReasonName", FieldType.ENTITY_NAME, false, null, null)
062        );
063    }
064
065    @Inject
066    EmployeeControl employeeControl;
067
068    @Inject
069    PartyControl partyControl;
070
071    
072    /** Creates a new instance of EditEmploymentCommand */
073    public EditEmploymentCommand() {
074        super(null, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
075    }
076
077    @Override
078    public EditEmploymentResult getResult() {
079        return EmployeeResultFactory.getEditEmploymentResult();
080    }
081
082    @Override
083    public EmploymentEdit getEdit() {
084        return EmployeeEditFactory.getEmploymentEdit();
085    }
086
087    ItemDescriptionType itemDescriptionType;
088    Item item;
089
090    @Override
091    public Employment getEntity(EditEmploymentResult result) {
092        Employment employment;
093        var employmentName = spec.getEmploymentName();
094
095        if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) {
096            employment = employeeControl.getEmploymentByName(employmentName);
097        } else { // EditMode.UPDATE
098            employment = employeeControl.getEmploymentByNameForUpdate(employmentName);
099        }
100
101        if(employment == null) {
102            addExecutionError(ExecutionErrors.UnknownEmployment.name(), employmentName);
103        }
104
105        return employment;
106    }
107
108    @Override
109    public Employment getLockEntity(Employment employment) {
110        return employment;
111    }
112
113    @Override
114    public void fillInResult(EditEmploymentResult result, Employment employment) {
115        result.setEmployment(employeeControl.getEmploymentTransfer(getUserVisit(), employment));
116    }
117
118    TerminationType terminationType;
119    TerminationReason terminationReason;
120
121    @Override
122    public void doLock(EmploymentEdit edit, Employment employment) {
123        var employmentDetail = employment.getLastDetail();
124        var endTime = employmentDetail.getEndTime();
125
126        terminationType = employmentDetail.getTerminationType();
127        terminationReason = employmentDetail.getTerminationReason();
128
129        edit.setCompanyName(partyControl.getPartyCompany(employmentDetail.getCompanyParty()).getPartyCompanyName());
130        edit.setStartTime(DateUtils.getInstance().formatTypicalDateTime(getUserVisit(), getPreferredDateTimeFormat(), employmentDetail.getStartTime()));
131        edit.setEndTime(endTime == null ? null : DateUtils.getInstance().formatTypicalDateTime(getUserVisit(), getPreferredDateTimeFormat(), endTime));
132        edit.setTerminationTypeName(terminationType == null ? null : terminationType.getLastDetail().getTerminationTypeName());
133        edit.setTerminationReasonName(terminationReason == null ? null : terminationReason.getLastDetail().getTerminationReasonName());
134    }
135
136    PartyCompany partyCompany;
137    
138    @Override
139    public void canUpdate(Employment employment) {
140        var companyName = edit.getCompanyName();
141
142        partyCompany = partyControl.getPartyCompanyByName(companyName);
143
144        if(partyCompany != null) {
145            var terminationTypeName = edit.getTerminationTypeName();
146
147            terminationType = employeeControl.getTerminationTypeByName(terminationTypeName);
148
149            if(terminationTypeName == null || terminationType != null) {
150                var terminationReasonName = edit.getTerminationReasonName();
151
152                terminationReason = employeeControl.getTerminationReasonByName(terminationReasonName);
153
154                if(terminationReasonName != null && terminationReason == null) {
155                    addExecutionError(ExecutionErrors.UnknownTerminationReasonName.name(), terminationReasonName);
156                }
157            } else {
158                addExecutionError(ExecutionErrors.UnknownTerminationTypeName.name(), terminationTypeName);
159            }
160        } else {
161            addExecutionError(ExecutionErrors.UnknownCompanyName.name(), companyName);
162        }
163    }
164
165    @Override
166    public void doUpdate(Employment employment) {
167        var employmentDetailValue = employeeControl.getEmploymentDetailValueForUpdate(employment);
168        var strEndTime = edit.getEndTime();
169
170        employmentDetailValue.setCompanyPartyPK(partyCompany.getPartyPK());
171        employmentDetailValue.setStartTime(Long.valueOf(edit.getStartTime()));
172        employmentDetailValue.setEndTime(strEndTime == null ? null : Long.valueOf(strEndTime));
173        employmentDetailValue.setTerminationTypePK(terminationType == null ? null : terminationType.getPrimaryKey());
174        employmentDetailValue.setTerminationReasonPK(terminationReason == null ? null : terminationReason.getPrimaryKey());
175        
176        employeeControl.updateEmploymentFromValue(employmentDetailValue, getPartyPK());
177    }
178    
179}