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.UnknownDepartmentNameException; 026import com.echothree.model.control.party.common.exception.UnknownPartyNameException; 027import com.echothree.model.control.party.common.exception.UseOfDepartmentNameRequiresDivisionNameException; 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.PartyDepartment; 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 DepartmentLogic 040 extends BaseLogic { 041 042 protected DepartmentLogic() { 043 super(); 044 } 045 046 public static DepartmentLogic getInstance() { 047 return CDI.current().select(DepartmentLogic.class).get(); 048 } 049 050 public PartyDepartment getPartyDepartmentByName(final ExecutionErrorAccumulator eea, final String companyName, 051 final String divisionName, final String departmentName, final String partyName, 052 final UniversalEntitySpec universalEntitySpec, final boolean required) { 053 PartyDepartment partyDepartment = null; 054 Party divisionParty = null; 055 056 if(companyName != null || divisionName != null) { 057 var partyDivision = DivisionLogic.getInstance().getPartyDivisionByName(eea, companyName, 058 divisionName, null, null, true); 059 060 divisionParty = hasExecutionErrors(eea) ? null : partyDivision.getParty(); 061 } 062 063 if(!hasExecutionErrors(eea)) { 064 partyDepartment = getPartyDepartmentByName(eea, divisionParty, departmentName, partyName, universalEntitySpec, 065 required); 066 } 067 068 return partyDepartment; 069 } 070 071 public PartyDepartment getPartyDepartmentByName(final ExecutionErrorAccumulator eea, final Party divisionParty, 072 final String departmentName, final String partyName, final UniversalEntitySpec universalEntitySpec, 073 final boolean required) { 074 var parameterCount = (departmentName == null ? 0 : 1) + (partyName == null ? 0 : 1) + 075 EntityInstanceLogic.getInstance().countPossibleEntitySpecs(universalEntitySpec); 076 PartyDepartment partyDepartment = null; 077 078 if(divisionParty != null) { 079 PartyLogic.getInstance().checkPartyType(eea, divisionParty, PartyTypes.DIVISION.name()); 080 } 081 082 if(!hasExecutionErrors(eea)) { 083 if(parameterCount == 1) { 084 var partyControl = Session.getModelController(PartyControl.class); 085 086 if(departmentName != null) { 087 if(divisionParty == null) { 088 handleExecutionError(UseOfDepartmentNameRequiresDivisionNameException.class, eea, ExecutionErrors.UseOfDepartmentNameRequiresDivisionName.name()); 089 } else { 090 partyDepartment = partyControl.getPartyDepartmentByName(divisionParty, departmentName); 091 092 if(partyDepartment == null) { 093 handleExecutionError(UnknownDepartmentNameException.class, eea, ExecutionErrors.UnknownDepartmentName.name(), departmentName); 094 } 095 } 096 } else { 097 // Use of partyName or universalEntitySpec cannot include a divisionParty. 098 if(divisionParty == null) { 099 if(partyName != null) { 100 var party = partyControl.getPartyByName(partyName); 101 102 if(party != null) { 103 PartyLogic.getInstance().checkPartyType(eea, party, PartyTypes.DEPARTMENT.name()); 104 105 partyDepartment = partyControl.getPartyDepartment(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.DEPARTMENT.name()); 117 118 partyDepartment = partyControl.getPartyDepartment(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 partyDepartment; 133 } 134 135}