001// --------------------------------------------------------------------------------
002// Copyright 2002-2025 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.Arrays;
030import java.util.Collections;
031import java.util.List;
032import javax.enterprise.context.RequestScoped;
033
034@RequestScoped
035public class CreateEmploymentCommand
036        extends BaseSimpleCommand<CreateEmploymentForm> {
037    
038    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
039    
040    static {
041        FORM_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
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    /** Creates a new instance of CreateEmploymentCommand */
052    public CreateEmploymentCommand() {
053        super(null, FORM_FIELD_DEFINITIONS, false);
054    }
055    
056    @Override
057    protected BaseResult execute() {
058        var partyControl = Session.getModelController(PartyControl.class);
059        var partyName = form.getPartyName();
060        var party = partyControl.getPartyByName(partyName);
061
062        if(party != null) {
063            var companyName = form.getCompanyName();
064            var partyCompany = partyControl.getPartyCompanyByName(companyName);
065
066            if(partyCompany != null) {
067                var employeeControl = Session.getModelController(EmployeeControl.class);
068                var terminationTypeName = form.getTerminationTypeName();
069                var terminationType = employeeControl.getTerminationTypeByName(terminationTypeName);
070
071                if(terminationTypeName == null || terminationType != null) {
072                    var terminationReasonName = form.getTerminationReasonName();
073                    var terminationReason = employeeControl.getTerminationReasonByName(terminationReasonName);
074
075                    if(terminationReasonName == null || terminationReason != null) {
076                        var startTime = Long.valueOf(form.getStartTime());
077                        var strEndTime = form.getEndTime();
078                        var endTime = strEndTime == null ? null : Long.valueOf(strEndTime);
079
080                        employeeControl.createEmployment(party, partyCompany.getParty(), startTime, endTime, terminationType, terminationReason, getPartyPK());
081                    } else {
082                        addExecutionError(ExecutionErrors.UnknownTerminationReasonName.name(), terminationReasonName);
083                    }
084                } else {
085                    addExecutionError(ExecutionErrors.UnknownTerminationTypeName.name(), terminationTypeName);
086                }
087            } else {
088                addExecutionError(ExecutionErrors.UnknownCompanyName.name(), companyName);
089            }
090        } else {
091            addExecutionError(ExecutionErrors.UnknownPartyName.name(), partyName);
092        }
093        
094        return null;
095    }
096    
097}