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.enterprise.inject.spi.CDI; 034import javax.inject.Inject; 035 036@ApplicationScoped 037public class CompanyLogic 038 extends BaseLogic { 039 040 @Inject 041 PartyControl partyControl; 042 043 @Inject 044 EntityInstanceLogic entityInstanceLogic; 045 046 @Inject 047 PartyLogic partyLogic; 048 049 protected CompanyLogic() { 050 super(); 051 } 052 053 public static CompanyLogic getInstance() { 054 return CDI.current().select(CompanyLogic.class).get(); 055 } 056 057 public PartyCompany getPartyCompanyByName(final ExecutionErrorAccumulator eea, final String companyName, 058 final String partyName, final UniversalEntitySpec universalEntitySpec, final boolean required) { 059 var parameterCount = (companyName == null ? 0 : 1) + (partyName == null ? 0 : 1) + 060 entityInstanceLogic.countPossibleEntitySpecs(universalEntitySpec); 061 PartyCompany partyCompany = null; 062 063 if(parameterCount == 1) { 064 if(companyName != null) { 065 partyCompany = partyControl.getPartyCompanyByName(companyName); 066 067 if(partyCompany == null) { 068 handleExecutionError(UnknownCompanyNameException.class, eea, ExecutionErrors.UnknownCompanyName.name(), companyName); 069 } 070 } else if(partyName != null) { 071 var party = partyControl.getPartyByName(partyName); 072 073 if(party != null) { 074 partyLogic.checkPartyType(eea, party, PartyTypes.COMPANY.name()); 075 076 partyCompany = partyControl.getPartyCompany(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.COMPANY.name()); 088 089 partyCompany = partyControl.getPartyCompany(party); 090 } 091 } 092 } else { 093 if(required) { 094 handleExecutionError(InvalidParameterCountException.class, eea, ExecutionErrors.InvalidParameterCount.name()); 095 } 096 } 097 098 return partyCompany; 099 } 100 101}