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