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.persistence.Session; 040import com.echothree.util.server.string.DateUtils; 041import java.util.List; 042import javax.enterprise.context.Dependent; 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 /** Creates a new instance of EditEmploymentCommand */ 066 public EditEmploymentCommand() { 067 super(null, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS); 068 } 069 070 @Override 071 public EditEmploymentResult getResult() { 072 return EmployeeResultFactory.getEditEmploymentResult(); 073 } 074 075 @Override 076 public EmploymentEdit getEdit() { 077 return EmployeeEditFactory.getEmploymentEdit(); 078 } 079 080 ItemDescriptionType itemDescriptionType; 081 Item item; 082 083 @Override 084 public Employment getEntity(EditEmploymentResult result) { 085 var employeeControl = Session.getModelController(EmployeeControl.class); 086 Employment employment; 087 var employmentName = spec.getEmploymentName(); 088 089 if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) { 090 employment = employeeControl.getEmploymentByName(employmentName); 091 } else { // EditMode.UPDATE 092 employment = employeeControl.getEmploymentByNameForUpdate(employmentName); 093 } 094 095 if(employment == null) { 096 addExecutionError(ExecutionErrors.UnknownEmployment.name(), employmentName); 097 } 098 099 return employment; 100 } 101 102 @Override 103 public Employment getLockEntity(Employment employment) { 104 return employment; 105 } 106 107 @Override 108 public void fillInResult(EditEmploymentResult result, Employment employment) { 109 var employeeControl = Session.getModelController(EmployeeControl.class); 110 111 result.setEmployment(employeeControl.getEmploymentTransfer(getUserVisit(), employment)); 112 } 113 114 TerminationType terminationType; 115 TerminationReason terminationReason; 116 117 @Override 118 public void doLock(EmploymentEdit edit, Employment employment) { 119 var partyControl = Session.getModelController(PartyControl.class); 120 var employmentDetail = employment.getLastDetail(); 121 var endTime = employmentDetail.getEndTime(); 122 123 terminationType = employmentDetail.getTerminationType(); 124 terminationReason = employmentDetail.getTerminationReason(); 125 126 edit.setCompanyName(partyControl.getPartyCompany(employmentDetail.getCompanyParty()).getPartyCompanyName()); 127 edit.setStartTime(DateUtils.getInstance().formatTypicalDateTime(getUserVisit(), getPreferredDateTimeFormat(), employmentDetail.getStartTime())); 128 edit.setEndTime(endTime == null ? null : DateUtils.getInstance().formatTypicalDateTime(getUserVisit(), getPreferredDateTimeFormat(), endTime)); 129 edit.setTerminationTypeName(terminationType == null ? null : terminationType.getLastDetail().getTerminationTypeName()); 130 edit.setTerminationReasonName(terminationReason == null ? null : terminationReason.getLastDetail().getTerminationReasonName()); 131 } 132 133 PartyCompany partyCompany; 134 135 @Override 136 public void canUpdate(Employment employment) { 137 var partyControl = Session.getModelController(PartyControl.class); 138 var companyName = edit.getCompanyName(); 139 140 partyCompany = partyControl.getPartyCompanyByName(companyName); 141 142 if(partyCompany != null) { 143 var employeeControl = Session.getModelController(EmployeeControl.class); 144 var terminationTypeName = edit.getTerminationTypeName(); 145 146 terminationType = employeeControl.getTerminationTypeByName(terminationTypeName); 147 148 if(terminationTypeName == null || terminationType != null) { 149 var terminationReasonName = edit.getTerminationReasonName(); 150 151 terminationReason = employeeControl.getTerminationReasonByName(terminationReasonName); 152 153 if(terminationReasonName != null && terminationReason == null) { 154 addExecutionError(ExecutionErrors.UnknownTerminationReasonName.name(), terminationReasonName); 155 } 156 } else { 157 addExecutionError(ExecutionErrors.UnknownTerminationTypeName.name(), terminationTypeName); 158 } 159 } else { 160 addExecutionError(ExecutionErrors.UnknownCompanyName.name(), companyName); 161 } 162 } 163 164 @Override 165 public void doUpdate(Employment employment) { 166 var employeeControl = Session.getModelController(EmployeeControl.class); 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}