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