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.form.CreateEmploymentForm;
020import com.echothree.model.control.employee.server.control.EmployeeControl;
021import com.echothree.model.control.party.server.control.PartyControl;
022import com.echothree.model.data.user.common.pk.UserVisitPK;
023import com.echothree.util.common.message.ExecutionErrors;
024import com.echothree.util.common.validation.FieldDefinition;
025import com.echothree.util.common.validation.FieldType;
026import com.echothree.util.common.command.BaseResult;
027import com.echothree.util.server.control.BaseSimpleCommand;
028import java.util.List;
029import javax.enterprise.context.Dependent;
030import javax.inject.Inject;
031
032@Dependent
033public class CreateEmploymentCommand
034        extends BaseSimpleCommand<CreateEmploymentForm> {
035    
036    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
037    
038    static {
039        FORM_FIELD_DEFINITIONS = List.of(
040                new FieldDefinition("PartyName", FieldType.ENTITY_NAME, true, null, null),
041                new FieldDefinition("CompanyName", FieldType.ENTITY_NAME, true, null, null),
042                new FieldDefinition("StartTime", FieldType.DATE_TIME, true, null, null),
043                new FieldDefinition("EndTime", FieldType.DATE_TIME, false, null, null),
044                new FieldDefinition("TerminationTypeName", FieldType.ENTITY_NAME, false, null, null),
045                new FieldDefinition("TerminationReasonName", FieldType.ENTITY_NAME, false, null, null)
046        );
047    }
048
049    @Inject
050    EmployeeControl employeeControl;
051
052    @Inject
053    PartyControl partyControl;
054
055    
056    /** Creates a new instance of CreateEmploymentCommand */
057    public CreateEmploymentCommand() {
058        super(null, FORM_FIELD_DEFINITIONS, false);
059    }
060    
061    @Override
062    protected BaseResult execute() {
063        var partyName = form.getPartyName();
064        var party = partyControl.getPartyByName(partyName);
065
066        if(party != null) {
067            var companyName = form.getCompanyName();
068            var partyCompany = partyControl.getPartyCompanyByName(companyName);
069
070            if(partyCompany != null) {
071                var terminationTypeName = form.getTerminationTypeName();
072                var terminationType = employeeControl.getTerminationTypeByName(terminationTypeName);
073
074                if(terminationTypeName == null || terminationType != null) {
075                    var terminationReasonName = form.getTerminationReasonName();
076                    var terminationReason = employeeControl.getTerminationReasonByName(terminationReasonName);
077
078                    if(terminationReasonName == null || terminationReason != null) {
079                        var startTime = Long.valueOf(form.getStartTime());
080                        var strEndTime = form.getEndTime();
081                        var endTime = strEndTime == null ? null : Long.valueOf(strEndTime);
082
083                        employeeControl.createEmployment(party, partyCompany.getParty(), startTime, endTime, terminationType, terminationReason, getPartyPK());
084                    } else {
085                        addExecutionError(ExecutionErrors.UnknownTerminationReasonName.name(), terminationReasonName);
086                    }
087                } else {
088                    addExecutionError(ExecutionErrors.UnknownTerminationTypeName.name(), terminationTypeName);
089                }
090            } else {
091                addExecutionError(ExecutionErrors.UnknownCompanyName.name(), companyName);
092            }
093        } else {
094            addExecutionError(ExecutionErrors.UnknownPartyName.name(), partyName);
095        }
096        
097        return null;
098    }
099    
100}