001// -------------------------------------------------------------------------------- 002// Copyright 2002-2024 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.exception.InvalidPartyTypeException; 025import com.echothree.model.control.party.common.exception.UnknownPartyNameException; 026import com.echothree.model.control.party.common.exception.UnknownPartyTypeNameException; 027import com.echothree.model.control.party.server.control.PartyControl; 028import com.echothree.model.data.core.server.entity.EntityInstance; 029import com.echothree.model.data.party.common.pk.PartyPK; 030import com.echothree.model.data.party.server.entity.Party; 031import com.echothree.model.data.party.server.entity.PartyType; 032import com.echothree.model.data.party.server.factory.PartyFactory; 033import com.echothree.util.common.message.ExecutionErrors; 034import com.echothree.util.server.control.BaseLogic; 035import com.echothree.util.server.message.ExecutionErrorAccumulator; 036import com.echothree.util.server.persistence.EntityPermission; 037import com.echothree.util.server.persistence.Session; 038 039public class PartyLogic 040 extends BaseLogic { 041 042 private PartyLogic() { 043 super(); 044 } 045 046 private static class PartyLogicHolder { 047 static PartyLogic instance = new PartyLogic(); 048 } 049 050 public static PartyLogic getInstance() { 051 return PartyLogicHolder.instance; 052 } 053 054 /** Assume that the entityInstance passed to this function is an ECHO_THREE.Party. */ 055 public Party getPartyFromEntityInstance(final EntityInstance entityInstance) { 056 PartyPK pk = new PartyPK(entityInstance.getEntityUniqueId()); 057 058 return PartyFactory.getInstance().getEntityFromPK(EntityPermission.READ_ONLY, pk); 059 } 060 061 public boolean isPartyType(final Party party, final String ... partyTypeNames) { 062 String partyTypeName = party.getLastDetail().getPartyType().getPartyTypeName(); 063 boolean found = false; 064 int length = partyTypeNames.length; 065 066 for(int i = 0 ; i < length ; i++) { 067 if(partyTypeName.equals(partyTypeNames[i])) { 068 found = true; 069 break; 070 } 071 } 072 073 return found; 074 } 075 076 public String checkPartyType(final ExecutionErrorAccumulator eea, final Party party, final String ... partyTypeNames) { 077 String partyTypeName = party.getLastDetail().getPartyType().getPartyTypeName(); 078 boolean found = false; 079 int length = partyTypeNames.length; 080 081 for(int i = 0 ; i < length ; i++) { 082 if(partyTypeName.equals(partyTypeNames[i])) { 083 found = true; 084 break; 085 } 086 } 087 088 if(!found) { 089 handleExecutionError(InvalidPartyTypeException.class, eea, ExecutionErrors.InvalidPartyType.name(), partyTypeName); 090 } 091 092 return partyTypeName; 093 } 094 095 public PartyType getPartyTypeByName(final ExecutionErrorAccumulator eea, final String partyTypeName) { 096 var partyControl = Session.getModelController(PartyControl.class); 097 PartyType partyType = partyControl.getPartyTypeByName(partyTypeName); 098 099 if(partyType == null) { 100 handleExecutionError(UnknownPartyTypeNameException.class, eea, ExecutionErrors.UnknownPartyTypeName.name(), partyTypeName); 101 } 102 103 return partyType; 104 } 105 106 public Party getPartyByName(final ExecutionErrorAccumulator eea, final String partyName, 107 final EntityPermission entityPermission) { 108 var partyControl = Session.getModelController(PartyControl.class); 109 Party party = partyControl.getPartyByName(partyName, entityPermission); 110 111 if(party == null) { 112 handleExecutionError(UnknownPartyNameException.class, eea, ExecutionErrors.UnknownPartyName.name(), partyName); 113 } 114 115 return party; 116 } 117 118 public Party getPartyByName(final ExecutionErrorAccumulator eea, final String partyName) { 119 return getPartyByName(eea, partyName, EntityPermission.READ_ONLY); 120 } 121 122 public Party getPartyByNameForUpdate(final ExecutionErrorAccumulator eea, final String partyName) { 123 return getPartyByName(eea, partyName, EntityPermission.READ_WRITE); 124 } 125 126 public Party getPartyByUlid(final ExecutionErrorAccumulator eea, final String ulid, final EntityPermission entityPermission) { 127 Party party = null; 128 129 var entityInstance = EntityInstanceLogic.getInstance().getEntityInstance(eea, (String)null, null, null, ulid, 130 ComponentVendors.ECHO_THREE.name(), EntityTypes.Party.name()); 131 132 if(eea == null || !eea.hasExecutionErrors()) { 133 var partyControl = Session.getModelController(PartyControl.class); 134 135 party = partyControl.getPartyByEntityInstance(entityInstance, entityPermission); 136 } 137 138 return party; 139 } 140 141 public Party getPartyByUlid(final ExecutionErrorAccumulator eea, final String ulid) { 142 return getPartyByUlid(eea, ulid, EntityPermission.READ_ONLY); 143 } 144 145 public Party getPartyByUlidForUpdate(final ExecutionErrorAccumulator eea, final String ulid) { 146 return getPartyByUlid(eea, ulid, EntityPermission.READ_WRITE); 147 } 148 149 public Party getPartyByName(final ExecutionErrorAccumulator eea, final String partyName, final String ... partyTypeNames) { 150 Party party = getPartyByName(eea, partyName); 151 152 if(party != null) { 153 checkPartyType(eea, party, partyTypeNames); 154 } 155 156 return party; 157 } 158 159 public Party getPartyByName(final ExecutionErrorAccumulator eea, final String partyName, 160 final UniversalEntitySpec universalEntitySpec) { 161 var parameterCount = (partyName == null ? 0 : 1) + 162 EntityInstanceLogic.getInstance().countPossibleEntitySpecs(universalEntitySpec); 163 Party party = null; 164 165 if(parameterCount == 1) { 166 var partyControl = Session.getModelController(PartyControl.class); 167 168 if(partyName != null) { 169 party = partyControl.getPartyByName(partyName); 170 171 if(party == null) { 172 handleExecutionError(UnknownPartyNameException.class, eea, ExecutionErrors.UnknownPartyName.name(), partyName); 173 } 174 } else if(universalEntitySpec != null) { 175 var entityInstance = EntityInstanceLogic.getInstance().getEntityInstance(eea, universalEntitySpec, 176 ComponentVendors.ECHO_THREE.name(), EntityTypes.Party.name()); 177 178 if(!eea.hasExecutionErrors()) { 179 party = partyControl.getPartyByEntityInstance(entityInstance); 180 } 181 } 182 } else { 183 handleExecutionError(InvalidParameterCountException.class, eea, ExecutionErrors.InvalidParameterCount.name()); 184 } 185 186 return party; 187 } 188 189}