001// --------------------------------------------------------------------------------
002// Copyright 2002-2024 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.Party;
029import com.echothree.model.data.party.server.entity.PartyCompany;
030import com.echothree.util.common.message.ExecutionErrors;
031import com.echothree.util.server.control.BaseLogic;
032import com.echothree.util.server.message.ExecutionErrorAccumulator;
033import com.echothree.util.server.persistence.Session;
034
035public class CompanyLogic
036        extends BaseLogic {
037    
038    private CompanyLogic() {
039        super();
040    }
041    
042    private static class CompanyLogicHolder {
043        static CompanyLogic instance = new CompanyLogic();
044    }
045    
046    public static CompanyLogic getInstance() {
047        return CompanyLogicHolder.instance;
048    }
049
050    public PartyCompany getPartyCompanyByName(final ExecutionErrorAccumulator eea, final String companyName,
051            final String partyName, final UniversalEntitySpec universalEntitySpec, final boolean required) {
052        var parameterCount = (companyName == null ? 0 : 1) + (partyName == null ? 0 : 1) +
053                EntityInstanceLogic.getInstance().countPossibleEntitySpecs(universalEntitySpec);
054        PartyCompany partyCompany = null;
055
056        if(parameterCount == 1) {
057            var partyControl = Session.getModelController(PartyControl.class);
058
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                Party party = partyControl.getPartyByName(partyName);
067
068                if(party != null) {
069                    PartyLogic.getInstance().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.getInstance().getEntityInstance(eea, universalEntitySpec,
077                        ComponentVendors.ECHO_THREE.name(), EntityTypes.Party.name());
078
079                if(!eea.hasExecutionErrors()) {
080                    var party = partyControl.getPartyByEntityInstance(entityInstance);
081
082                    PartyLogic.getInstance().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}