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.model.control.employee.server.logic;
018
019import com.echothree.control.user.core.common.spec.UniversalEntitySpec;
020import com.echothree.model.control.core.common.ComponentVendors;
021import com.echothree.model.control.core.common.EntityTypes;
022import com.echothree.model.control.core.common.exception.InvalidParameterCountException;
023import com.echothree.model.control.core.server.logic.EntityInstanceLogic;
024import com.echothree.model.control.employee.server.control.EmployeeControl;
025import com.echothree.model.control.party.common.PartyTypes;
026import com.echothree.model.control.party.common.exception.UnknownEmployeeNameException;
027import com.echothree.model.control.party.common.exception.UnknownPartyNameException;
028import com.echothree.model.control.party.server.control.PartyControl;
029import com.echothree.model.control.party.server.logic.PartyLogic;
030import com.echothree.model.data.employee.server.entity.PartyEmployee;
031import com.echothree.util.common.message.ExecutionErrors;
032import com.echothree.util.server.control.BaseLogic;
033import com.echothree.util.server.message.ExecutionErrorAccumulator;
034import com.echothree.util.server.persistence.Session;
035
036public class PartyEmployeeLogic
037        extends BaseLogic {
038
039    private PartyEmployeeLogic() {
040        super();
041    }
042    
043    private static class PartyEmployeeLogicHolder {
044        static PartyEmployeeLogic instance = new PartyEmployeeLogic();
045    }
046    
047    public static PartyEmployeeLogic getInstance() {
048        return PartyEmployeeLogicHolder.instance;
049    }
050
051    public PartyEmployee getPartyEmployeeByName(final ExecutionErrorAccumulator eea, final String partyEmployeeName, final String partyName,
052            final UniversalEntitySpec universalEntitySpec) {
053        var parameterCount = (partyEmployeeName == null ? 0 : 1) + (partyName == null ? 0 : 1) +
054                EntityInstanceLogic.getInstance().countPossibleEntitySpecs(universalEntitySpec);
055        PartyEmployee partyEmployee = null;
056
057        if(parameterCount == 1) {
058            var employeeControl = Session.getModelController(EmployeeControl.class);
059            var partyControl = Session.getModelController(PartyControl.class);
060
061            if(partyEmployeeName != null) {
062                partyEmployee = employeeControl.getPartyEmployeeByName(partyEmployeeName);
063
064                if(partyEmployee == null) {
065                    handleExecutionError(UnknownEmployeeNameException.class, eea, ExecutionErrors.UnknownEmployeeName.name(), partyEmployeeName);
066                }
067            } else if(partyName != null) {
068                var party = partyControl.getPartyByName(partyName);
069
070                if(party != null) {
071                    PartyLogic.getInstance().checkPartyType(eea, party, PartyTypes.EMPLOYEE.name());
072
073                    partyEmployee = employeeControl.getPartyEmployee(party);
074                } else {
075                    handleExecutionError(UnknownPartyNameException.class, eea, ExecutionErrors.UnknownPartyName.name(), partyName);
076                }
077            } else if(universalEntitySpec != null) {
078                var entityInstance = EntityInstanceLogic.getInstance().getEntityInstance(eea, universalEntitySpec,
079                        ComponentVendors.ECHO_THREE.name(), EntityTypes.Party.name());
080
081                if(!eea.hasExecutionErrors()) {
082                    var party = partyControl.getPartyByEntityInstance(entityInstance);
083
084                    PartyLogic.getInstance().checkPartyType(eea, party, PartyTypes.EMPLOYEE.name());
085
086                    partyEmployee = employeeControl.getPartyEmployee(party);
087                }
088            }
089        } else {
090            handleExecutionError(InvalidParameterCountException.class, eea, ExecutionErrors.InvalidParameterCount.name());
091        }
092
093        return partyEmployee;
094    }
095
096}