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.party.server.command;
018
019import com.echothree.control.user.party.common.form.AddEmployeeToDivisionForm;
020import com.echothree.model.control.party.server.logic.CompanyLogic;
021import com.echothree.model.control.party.server.logic.DivisionLogic;
022import com.echothree.model.control.party.server.logic.EmployeeLogic;
023import com.echothree.model.control.party.server.logic.PartyRelationshipLogic;
024import com.echothree.model.data.user.common.pk.UserVisitPK;
025import com.echothree.util.common.validation.FieldDefinition;
026import com.echothree.util.common.validation.FieldType;
027import com.echothree.util.common.command.BaseResult;
028import com.echothree.util.server.control.BaseSimpleCommand;
029import java.util.List;
030import javax.enterprise.context.Dependent;
031import javax.inject.Inject;
032
033@Dependent
034public class AddEmployeeToDivisionCommand
035        extends BaseSimpleCommand<AddEmployeeToDivisionForm> {
036    
037    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
038    
039    static {
040        FORM_FIELD_DEFINITIONS = List.of(
041                new FieldDefinition("DivisionName", FieldType.ENTITY_NAME, true, null, null),
042                new FieldDefinition("EmployeeName", FieldType.ENTITY_NAME, false, null, null),
043                new FieldDefinition("PartyName", FieldType.ENTITY_NAME, false, null, null)
044        );
045    }
046
047    @Inject
048    CompanyLogic companyLogic;
049
050    @Inject
051    DivisionLogic divisionLogic;
052
053    @Inject
054    EmployeeLogic employeeLogic;
055
056    @Inject
057    PartyRelationshipLogic partyRelationshipLogic;
058
059    
060    /** Creates a new instance of AddEmployeeToDivisionCommand */
061    public AddEmployeeToDivisionCommand() {
062        super(null, FORM_FIELD_DEFINITIONS, false);
063    }
064    
065    @Override
066    protected BaseResult execute() {
067        var employeeName = form.getEmployeeName();
068        var partyName = form.getPartyName();
069        var partyEmployee = employeeLogic.getPartyEmployeeByName(this, employeeName, partyName);
070
071        if(!hasExecutionErrors()) {
072            var companyName = form.getCompanyName();
073            var partyCompany = companyLogic.getPartyCompanyByName(this, companyName, null, null, false);
074
075            if(!hasExecutionErrors()) {
076                var divisionName = form.getDivisionName();
077                var partyDivision = divisionLogic.getPartyDivisionByName(this, partyCompany == null ? null : partyCompany.getParty(), divisionName, null, null, true);
078
079                if(!hasExecutionErrors()) {
080                    var division = partyDivision.getParty();
081                    var employee = partyEmployee.getParty();
082
083                    partyRelationshipLogic.addEmployeeToDivision(this, division, employee, getPartyPK());
084                }
085            }
086        }
087
088        return null;
089    }
090    
091}