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 com.echothree.util.server.persistence.Session;
029import java.util.List;
030import javax.enterprise.context.Dependent;
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    /** Creates a new instance of CreateEmploymentCommand */
050    public CreateEmploymentCommand() {
051        super(null, FORM_FIELD_DEFINITIONS, false);
052    }
053    
054    @Override
055    protected BaseResult execute() {
056        var partyControl = Session.getModelController(PartyControl.class);
057        var partyName = form.getPartyName();
058        var party = partyControl.getPartyByName(partyName);
059
060        if(party != null) {
061            var companyName = form.getCompanyName();
062            var partyCompany = partyControl.getPartyCompanyByName(companyName);
063
064            if(partyCompany != null) {
065                var employeeControl = Session.getModelController(EmployeeControl.class);
066                var terminationTypeName = form.getTerminationTypeName();
067                var terminationType = employeeControl.getTerminationTypeByName(terminationTypeName);
068
069                if(terminationTypeName == null || terminationType != null) {
070                    var terminationReasonName = form.getTerminationReasonName();
071                    var terminationReason = employeeControl.getTerminationReasonByName(terminationReasonName);
072
073                    if(terminationReasonName == null || terminationReason != null) {
074                        var startTime = Long.valueOf(form.getStartTime());
075                        var strEndTime = form.getEndTime();
076                        var endTime = strEndTime == null ? null : Long.valueOf(strEndTime);
077
078                        employeeControl.createEmployment(party, partyCompany.getParty(), startTime, endTime, terminationType, terminationReason, getPartyPK());
079                    } else {
080                        addExecutionError(ExecutionErrors.UnknownTerminationReasonName.name(), terminationReasonName);
081                    }
082                } else {
083                    addExecutionError(ExecutionErrors.UnknownTerminationTypeName.name(), terminationTypeName);
084                }
085            } else {
086                addExecutionError(ExecutionErrors.UnknownCompanyName.name(), companyName);
087            }
088        } else {
089            addExecutionError(ExecutionErrors.UnknownPartyName.name(), partyName);
090        }
091        
092        return null;
093    }
094    
095}