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.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 javax.enterprise.context.ApplicationScoped;
035import javax.enterprise.inject.spi.CDI;
036import javax.inject.Inject;
037
038@ApplicationScoped
039public class PartyEmployeeLogic
040        extends BaseLogic {
041
042    @Inject
043    EmployeeControl employeeControl;
044
045    @Inject
046    PartyControl partyControl;
047
048    @Inject
049    EntityInstanceLogic entityInstanceLogic;
050
051    @Inject
052    PartyLogic partyLogic;
053
054    protected PartyEmployeeLogic() {
055        super();
056    }
057
058    public static PartyEmployeeLogic getInstance() {
059        return CDI.current().select(PartyEmployeeLogic.class).get();
060    }
061
062    public PartyEmployee getPartyEmployeeByName(final ExecutionErrorAccumulator eea, final String partyEmployeeName, final String partyName,
063            final UniversalEntitySpec universalEntitySpec) {
064        var parameterCount = (partyEmployeeName == null ? 0 : 1) + (partyName == null ? 0 : 1) +
065                entityInstanceLogic.countPossibleEntitySpecs(universalEntitySpec);
066        PartyEmployee partyEmployee = null;
067
068        if(parameterCount == 1) {
069            if(partyEmployeeName != null) {
070                partyEmployee = employeeControl.getPartyEmployeeByName(partyEmployeeName);
071
072                if(partyEmployee == null) {
073                    handleExecutionError(UnknownEmployeeNameException.class, eea, ExecutionErrors.UnknownEmployeeName.name(), partyEmployeeName);
074                }
075            } else if(partyName != null) {
076                var party = partyControl.getPartyByName(partyName);
077
078                if(party != null) {
079                    partyLogic.checkPartyType(eea, party, PartyTypes.EMPLOYEE.name());
080
081                    partyEmployee = employeeControl.getPartyEmployee(party);
082                } else {
083                    handleExecutionError(UnknownPartyNameException.class, eea, ExecutionErrors.UnknownPartyName.name(), partyName);
084                }
085            } else if(universalEntitySpec != null) {
086                var entityInstance = entityInstanceLogic.getEntityInstance(eea, universalEntitySpec,
087                        ComponentVendors.ECHO_THREE.name(), EntityTypes.Party.name());
088
089                if(eea == null || !eea.hasExecutionErrors()) {
090                    var party = partyControl.getPartyByEntityInstance(entityInstance);
091
092                    partyLogic.checkPartyType(eea, party, PartyTypes.EMPLOYEE.name());
093
094                    partyEmployee = employeeControl.getPartyEmployee(party);
095                }
096            }
097        } else {
098            handleExecutionError(InvalidParameterCountException.class, eea, ExecutionErrors.InvalidParameterCount.name());
099        }
100
101        return partyEmployee;
102    }
103
104}