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