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