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