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.party.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.party.common.PartyTypes;
025import com.echothree.model.control.party.common.exception.UnknownCompanyNameException;
026import com.echothree.model.control.party.common.exception.UnknownPartyNameException;
027import com.echothree.model.control.party.server.control.PartyControl;
028import com.echothree.model.data.party.server.entity.PartyCompany;
029import com.echothree.util.common.message.ExecutionErrors;
030import com.echothree.util.server.control.BaseLogic;
031import com.echothree.util.server.message.ExecutionErrorAccumulator;
032import javax.enterprise.context.ApplicationScoped;
033import javax.inject.Inject;
034
035@ApplicationScoped
036public class CompanyLogic
037        extends BaseLogic {
038
039    @Inject
040    PartyControl partyControl;
041
042    @Inject
043    EntityInstanceLogic entityInstanceLogic;
044
045    @Inject
046    PartyLogic partyLogic;
047
048    protected CompanyLogic() {
049        super();
050    }
051
052    public PartyCompany getPartyCompanyByName(final ExecutionErrorAccumulator eea, final String companyName,
053            final String partyName, final UniversalEntitySpec universalEntitySpec, final boolean required) {
054        var parameterCount = (companyName == null ? 0 : 1) + (partyName == null ? 0 : 1) +
055                entityInstanceLogic.countPossibleEntitySpecs(universalEntitySpec);
056        PartyCompany partyCompany = null;
057
058        if(parameterCount == 1) {
059            if(companyName != null) {
060                partyCompany = partyControl.getPartyCompanyByName(companyName);
061
062                if(partyCompany == null) {
063                    handleExecutionError(UnknownCompanyNameException.class, eea, ExecutionErrors.UnknownCompanyName.name(), companyName);
064                }
065            } else if(partyName != null) {
066                var party = partyControl.getPartyByName(partyName);
067
068                if(party != null) {
069                    partyLogic.checkPartyType(eea, party, PartyTypes.COMPANY.name());
070
071                    partyCompany = partyControl.getPartyCompany(party);
072                } else {
073                    handleExecutionError(UnknownPartyNameException.class, eea, ExecutionErrors.UnknownPartyName.name(), partyName);
074                }
075            } else if(universalEntitySpec != null) {
076                var entityInstance = entityInstanceLogic.getEntityInstance(eea, universalEntitySpec,
077                        ComponentVendors.ECHO_THREE.name(), EntityTypes.Party.name());
078
079                if(eea == null || !eea.hasExecutionErrors()) {
080                    var party = partyControl.getPartyByEntityInstance(entityInstance);
081
082                    partyLogic.checkPartyType(eea, party, PartyTypes.COMPANY.name());
083
084                    partyCompany = partyControl.getPartyCompany(party);
085                }
086            }
087        } else {
088            if(required) {
089                handleExecutionError(InvalidParameterCountException.class, eea, ExecutionErrors.InvalidParameterCount.name());
090            }
091        }
092
093        return partyCompany;
094    }
095
096}