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