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