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.party.server.command;
018
019import com.echothree.control.user.party.common.form.RemoveEmployeeFromDepartmentForm;
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.employee.server.entity.PartyEmployee;
026import com.echothree.model.data.party.server.entity.Party;
027import com.echothree.model.data.party.server.entity.PartyCompany;
028import com.echothree.model.data.party.server.entity.PartyDepartment;
029import com.echothree.model.data.party.server.entity.PartyDivision;
030import com.echothree.model.data.user.common.pk.UserVisitPK;
031import com.echothree.util.common.validation.FieldDefinition;
032import com.echothree.util.common.validation.FieldType;
033import com.echothree.util.common.command.BaseResult;
034import com.echothree.util.server.control.BaseSimpleCommand;
035import java.util.Arrays;
036import java.util.Collections;
037import java.util.List;
038
039public class RemoveEmployeFromDepartmentCommand
040        extends BaseSimpleCommand<RemoveEmployeeFromDepartmentForm> {
041    
042    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
043    
044    static {
045        FORM_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
046                new FieldDefinition("DepartmentName", FieldType.ENTITY_NAME, true, null, null),
047                new FieldDefinition("EmployeeName", FieldType.ENTITY_NAME, false, null, null),
048                new FieldDefinition("PartyName", FieldType.ENTITY_NAME, false, null, null)
049                ));
050    }
051    
052    /** Creates a new instance of RemoveEmployeFromDepartmentCommand */
053    public RemoveEmployeFromDepartmentCommand(UserVisitPK userVisitPK, RemoveEmployeeFromDepartmentForm form) {
054        super(userVisitPK, form, null, FORM_FIELD_DEFINITIONS, false);
055    }
056    
057    @Override
058    protected BaseResult execute() {
059        String employeeName = form.getEmployeeName();
060        String partyName = form.getPartyName();
061        PartyEmployee partyEmployee = EmployeeLogic.getInstance().getPartyEmployeeByName(this, employeeName, partyName);
062
063        if(!hasExecutionErrors()) {
064            String companyName = form.getCompanyName();
065            PartyCompany partyCompany = CompanyLogic.getInstance().getPartyCompanyByName(this, companyName, null, null, false);
066
067            if(!hasExecutionErrors()) {
068                String divisionName = form.getDivisionName();
069                PartyDivision partyDivision = DivisionLogic.getInstance().getPartyDivisionByName(this, partyCompany == null ? null : partyCompany.getParty(), divisionName, null, null, false);
070
071                if(!hasExecutionErrors()) {
072                    String departmentName = form.getDepartmentName();
073                    PartyDepartment partyDepartment = DepartmentLogic.getInstance().getPartyDepartmentByName(this, partyDivision == null ? null : partyDivision.getParty(), departmentName, null, null, true);
074
075                    if(!hasExecutionErrors()) {
076                        Party departmentParty = partyDepartment.getParty();
077                        Party employeeParty = partyEmployee.getParty();
078
079                        PartyRelationshipLogic.getInstance().removeEmployeeFromDepartment(this, departmentParty, employeeParty, getPartyPK());
080                    }
081                }
082            }
083        }
084
085        return null;
086    }
087    
088}