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