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.UnknownDivisionNameException; 026import com.echothree.model.control.party.common.exception.UnknownPartyNameException; 027import com.echothree.model.control.party.common.exception.UseOfDivisionNameRequiresCompanyNameException; 028import com.echothree.model.control.party.server.control.PartyControl; 029import com.echothree.model.data.party.server.entity.Party; 030import com.echothree.model.data.party.server.entity.PartyDivision; 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 DivisionLogic 040 extends BaseLogic { 041 042 protected DivisionLogic() { 043 super(); 044 } 045 046 public static DivisionLogic getInstance() { 047 return CDI.current().select(DivisionLogic.class).get(); 048 } 049 050 public PartyDivision getPartyDivisionByName(final ExecutionErrorAccumulator eea, final String companyName, 051 final String divisionName, final String partyName, final UniversalEntitySpec universalEntitySpec, 052 final boolean required) { 053 PartyDivision partyDivision = null; 054 Party companyParty = null; 055 056 if(companyName != null) { 057 var partyCompany = CompanyLogic.getInstance().getPartyCompanyByName(eea, companyName, null, 058 null, true); 059 060 companyParty = hasExecutionErrors(eea) ? null : partyCompany.getParty(); 061 } 062 063 if(!hasExecutionErrors(eea)) { 064 partyDivision = getPartyDivisionByName(eea, companyParty, divisionName, partyName, universalEntitySpec, required); 065 } 066 067 return partyDivision; 068 } 069 070 public PartyDivision getPartyDivisionByName(final ExecutionErrorAccumulator eea, final Party companyParty, 071 final String divisionName, final String partyName, final UniversalEntitySpec universalEntitySpec, 072 final boolean required) { 073 var parameterCount = (divisionName == null ? 0 : 1) + (partyName == null ? 0 : 1) + 074 EntityInstanceLogic.getInstance().countPossibleEntitySpecs(universalEntitySpec); 075 PartyDivision partyDivision = null; 076 077 if(companyParty != null) { 078 PartyLogic.getInstance().checkPartyType(eea, companyParty, PartyTypes.COMPANY.name()); 079 } 080 081 if(!hasExecutionErrors(eea)) { 082 if(parameterCount == 1) { 083 var partyControl = Session.getModelController(PartyControl.class); 084 085 if(divisionName != null) { 086 // Use of divisionName requires a companyParty. 087 if(companyParty == null) { 088 handleExecutionError(UseOfDivisionNameRequiresCompanyNameException.class, eea, ExecutionErrors.UseOfDivisionNameRequiresCompanyName.name()); 089 } else { 090 partyDivision = partyControl.getPartyDivisionByName(companyParty, divisionName); 091 092 if(partyDivision == null) { 093 handleExecutionError(UnknownDivisionNameException.class, eea, ExecutionErrors.UnknownDivisionName.name(), divisionName); 094 } 095 } 096 } else { 097 // Use of partyName or universalEntitySpec cannot include a companyParty. 098 if(companyParty == null) { 099 if(partyName != null) { 100 var party = partyControl.getPartyByName(partyName); 101 102 if(party != null) { 103 PartyLogic.getInstance().checkPartyType(eea, party, PartyTypes.DIVISION.name()); 104 105 partyDivision = partyControl.getPartyDivision(party); 106 } else { 107 handleExecutionError(UnknownPartyNameException.class, eea, ExecutionErrors.UnknownPartyName.name(), partyName); 108 } 109 } else if(universalEntitySpec != null) { 110 var entityInstance = EntityInstanceLogic.getInstance().getEntityInstance(eea, universalEntitySpec, 111 ComponentVendors.ECHO_THREE.name(), EntityTypes.Party.name()); 112 113 if(!eea.hasExecutionErrors()) { 114 var party = partyControl.getPartyByEntityInstance(entityInstance); 115 116 PartyLogic.getInstance().checkPartyType(eea, party, PartyTypes.DIVISION.name()); 117 118 partyDivision = partyControl.getPartyDivision(party); 119 } 120 } 121 } else { 122 handleExecutionError(InvalidParameterCountException.class, eea, ExecutionErrors.InvalidParameterCount.name()); 123 } 124 } 125 } else { 126 if(required) { 127 handleExecutionError(InvalidParameterCountException.class, eea, ExecutionErrors.InvalidParameterCount.name()); 128 } 129 } 130 } 131 132 return partyDivision; 133 } 134 135}