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