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.AddEmployeeToDepartmentForm;
020import com.echothree.model.control.party.server.logic.CompanyLogic;
021import com.echothree.model.control.party.server.logic.DepartmentLogic;
022import com.echothree.model.control.party.server.logic.DivisionLogic;
023import com.echothree.model.control.party.server.logic.EmployeeLogic;
024import com.echothree.model.control.party.server.logic.PartyRelationshipLogic;
025import com.echothree.model.data.user.common.pk.UserVisitPK;
026import com.echothree.util.common.validation.FieldDefinition;
027import com.echothree.util.common.validation.FieldType;
028import com.echothree.util.common.command.BaseResult;
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 AddEmployeeToDepartmentCommand
036        extends BaseSimpleCommand<AddEmployeeToDepartmentForm> {
037    
038    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
039    
040    static {
041        FORM_FIELD_DEFINITIONS = List.of(
042                new FieldDefinition("DepartmentName", FieldType.ENTITY_NAME, true, null, null),
043                new FieldDefinition("EmployeeName", FieldType.ENTITY_NAME, false, null, null),
044                new FieldDefinition("PartyName", FieldType.ENTITY_NAME, false, null, null)
045        );
046    }
047
048    @Inject
049    CompanyLogic companyLogic;
050
051    @Inject
052    DepartmentLogic departmentLogic;
053
054    @Inject
055    DivisionLogic divisionLogic;
056
057    @Inject
058    EmployeeLogic employeeLogic;
059
060    @Inject
061    PartyRelationshipLogic partyRelationshipLogic;
062
063    
064    /** Creates a new instance of AddEmployeeToDepartmentCommand */
065    public AddEmployeeToDepartmentCommand() {
066        super(null, FORM_FIELD_DEFINITIONS, false);
067    }
068    
069    @Override
070    protected BaseResult execute() {
071        var employeeName = form.getEmployeeName();
072        var partyName = form.getPartyName();
073        var partyEmployee = employeeLogic.getPartyEmployeeByName(this, employeeName, partyName);
074
075        if(!hasExecutionErrors()) {
076            var companyName = form.getCompanyName();
077            var partyCompany = companyLogic.getPartyCompanyByName(this, companyName, null, null, false);
078
079            if(!hasExecutionErrors()) {
080                var divisionName = form.getDivisionName();
081                var partyDivision = divisionLogic.getPartyDivisionByName(this, partyCompany == null ? null : partyCompany.getParty(), divisionName, null, null, false);
082
083                if(!hasExecutionErrors()) {
084                    var departmentName = form.getDepartmentName();
085                    var partyDepartment = departmentLogic.getPartyDepartmentByName(this, partyDivision == null ? null : partyDivision.getParty(), departmentName, null, null, true);
086
087                    if(!hasExecutionErrors()) {
088                        var departmentParty = partyDepartment.getParty();
089                        var employeeParty = partyEmployee.getParty();
090
091                        partyRelationshipLogic.addEmployeeToDepartment(this, departmentParty, employeeParty, getPartyPK());
092                    }
093                }
094            }
095        }
096
097        return null;
098    }
099    
100}