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.selector.server.logic; 018 019import com.echothree.control.user.selector.common.spec.SelectorUniversalSpec; 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.selector.common.exception.DuplicateSelectorNameException; 025import com.echothree.model.control.selector.common.exception.UnknownDefaultSelectorException; 026import com.echothree.model.control.selector.common.exception.UnknownDefaultSelectorKindException; 027import com.echothree.model.control.selector.common.exception.UnknownDefaultSelectorTypeException; 028import com.echothree.model.control.selector.common.exception.UnknownSelectorNameException; 029import com.echothree.model.control.selector.server.control.SelectorControl; 030import com.echothree.model.data.party.server.entity.Language; 031import com.echothree.model.data.selector.server.entity.Selector; 032import com.echothree.model.data.selector.server.entity.SelectorKind; 033import com.echothree.model.data.selector.server.entity.SelectorType; 034import com.echothree.util.common.message.ExecutionErrors; 035import com.echothree.util.common.persistence.BasePK; 036import com.echothree.util.server.control.BaseLogic; 037import com.echothree.util.server.message.ExecutionErrorAccumulator; 038import com.echothree.util.server.persistence.EntityPermission; 039import com.echothree.util.server.persistence.Session; 040import com.echothree.util.server.validation.ParameterUtils; 041import javax.enterprise.context.ApplicationScoped; 042import javax.enterprise.inject.spi.CDI; 043 044@ApplicationScoped 045public class SelectorLogic 046 extends BaseLogic { 047 048 protected SelectorLogic() { 049 super(); 050 } 051 052 public static SelectorLogic getInstance() { 053 return CDI.current().select(SelectorLogic.class).get(); 054 } 055 056 public Selector createSelector(final ExecutionErrorAccumulator eea, final String selectorKindName, final String selectorTypeName, 057 final String selectorName, final Boolean isDefault, final Integer sortOrder, final Language language, final String description, 058 final BasePK createdBy) { 059 var selectorType = SelectorTypeLogic.getInstance().getSelectorTypeByName(eea, selectorKindName, selectorTypeName); 060 Selector selector = null; 061 062 if(eea == null || !eea.hasExecutionErrors()) { 063 selector = createSelector(eea, selectorType, selectorName, isDefault, sortOrder, language, description, createdBy); 064 } 065 066 return selector; 067 } 068 069 public Selector createSelector(final ExecutionErrorAccumulator eea, final SelectorType selectorType, final String selectorName, 070 final Boolean isDefault, final Integer sortOrder, final Language language, final String description, final BasePK createdBy) { 071 var selectorControl = Session.getModelController(SelectorControl.class); 072 var selector = selectorControl.getSelectorByName(selectorType, selectorName); 073 074 if(selector == null) { 075 selector = selectorControl.createSelector(selectorType, selectorName, isDefault, sortOrder, createdBy); 076 077 if(description != null) { 078 selectorControl.createSelectorDescription(selector, language, description, createdBy); 079 } 080 } else { 081 handleExecutionError(DuplicateSelectorNameException.class, eea, ExecutionErrors.DuplicateSelectorName.name(), 082 selectorType.getLastDetail().getSelectorKind().getLastDetail().getSelectorKindName(), 083 selectorType.getLastDetail().getSelectorTypeName(), selectorName); 084 } 085 return selector; 086 } 087 088 public Selector getSelectorByName(final ExecutionErrorAccumulator eea, final SelectorType selectorType, final String selectorName, 089 final EntityPermission entityPermission) { 090 var selectorControl = Session.getModelController(SelectorControl.class); 091 var selector = selectorControl.getSelectorByName(selectorType, selectorName, entityPermission); 092 093 if(selector == null) { 094 var selectorTypeDetail = selectorType.getLastDetail(); 095 096 handleExecutionError(UnknownSelectorNameException.class, eea, ExecutionErrors.UnknownSelectorName.name(), 097 selectorTypeDetail.getSelectorKind().getLastDetail().getSelectorKindName(), 098 selectorTypeDetail.getSelectorTypeName(), selectorName); 099 } 100 101 return selector; 102 } 103 104 public Selector getSelectorByName(final ExecutionErrorAccumulator eea, final SelectorType selectorType, final String selectorName) { 105 return getSelectorByName(eea, selectorType, selectorName, EntityPermission.READ_ONLY); 106 } 107 108 public Selector getSelectorByNameForUpdate(final ExecutionErrorAccumulator eea, final SelectorType selectorType, final String selectorName) { 109 return getSelectorByName(eea, selectorType, selectorName, EntityPermission.READ_WRITE); 110 } 111 112 public Selector getSelectorByName(final ExecutionErrorAccumulator eea, final String selectorKindName, 113 final String selectorTypeName, final String selectorName, final EntityPermission entityPermission) { 114 var selectorType = SelectorTypeLogic.getInstance().getSelectorTypeByName(eea, selectorKindName, selectorTypeName); 115 Selector selector = null; 116 117 if(!eea.hasExecutionErrors()) { 118 selector = getSelectorByName(eea, selectorType, selectorName, entityPermission); 119 } 120 121 return selector; 122 } 123 124 public Selector getSelectorByName(final ExecutionErrorAccumulator eea, final String selectorKindName, 125 final String selectorType, final String selectorName) { 126 return getSelectorByName(eea, selectorKindName, selectorType, selectorName, EntityPermission.READ_ONLY); 127 } 128 129 public Selector getSelectorByNameForUpdate(final ExecutionErrorAccumulator eea, final String selectorKindName, 130 final String selectorType, final String selectorName) { 131 return getSelectorByName(eea, selectorKindName, selectorType, selectorName, EntityPermission.READ_WRITE); 132 } 133 134 public Selector getSelectorByUniversalSpec(final ExecutionErrorAccumulator eea, final SelectorUniversalSpec universalSpec, 135 final boolean allowDefault, final EntityPermission entityPermission) { 136 var selectorControl = Session.getModelController(SelectorControl.class); 137 var selectorKindName = universalSpec.getSelectorKindName(); 138 var selectorTypeName = universalSpec.getSelectorTypeName(); 139 var selectorName = universalSpec.getSelectorName(); 140 var nameParameterCount= ParameterUtils.getInstance().countNonNullParameters(selectorKindName, selectorTypeName, selectorName); 141 var possibleEntitySpecs= EntityInstanceLogic.getInstance().countPossibleEntitySpecs(universalSpec); 142 Selector selector = null; 143 144 if(nameParameterCount < 4 && possibleEntitySpecs == 0) { 145 SelectorKind selectorKind = null; 146 SelectorType selectorType = null; 147 148 if(selectorKindName == null) { 149 if(allowDefault) { 150 selectorKind = selectorControl.getDefaultSelectorKind(); 151 152 if(selectorKind == null) { 153 handleExecutionError(UnknownDefaultSelectorKindException.class, eea, ExecutionErrors.UnknownDefaultSelectorKind.name()); 154 } 155 } else { 156 handleExecutionError(InvalidParameterCountException.class, eea, ExecutionErrors.InvalidParameterCount.name()); 157 } 158 } else { 159 selectorKind = SelectorKindLogic.getInstance().getSelectorKindByName(eea, selectorKindName); 160 } 161 162 if(selectorTypeName == null) { 163 if(allowDefault) { 164 selectorType = selectorControl.getDefaultSelectorType(selectorKind); 165 166 if(selectorType == null) { 167 handleExecutionError(UnknownDefaultSelectorTypeException.class, eea, ExecutionErrors.UnknownDefaultSelectorType.name()); 168 } 169 } else { 170 handleExecutionError(InvalidParameterCountException.class, eea, ExecutionErrors.InvalidParameterCount.name()); 171 } 172 } else { 173 selectorType = SelectorTypeLogic.getInstance().getSelectorTypeByName(eea, selectorKind, selectorTypeName); 174 } 175 176 if(!eea.hasExecutionErrors()) { 177 if(selectorName == null) { 178 if(allowDefault) { 179 selector = selectorControl.getDefaultSelector(selectorType, entityPermission); 180 181 if(selector == null) { 182 handleExecutionError(UnknownDefaultSelectorException.class, eea, ExecutionErrors.UnknownDefaultSelector.name()); 183 } 184 } else { 185 handleExecutionError(InvalidParameterCountException.class, eea, ExecutionErrors.InvalidParameterCount.name()); 186 } 187 } else { 188 selector = getSelectorByName(eea, selectorType, selectorName, entityPermission); 189 } 190 } 191 } else if(nameParameterCount == 0 && possibleEntitySpecs == 1) { 192 var entityInstance = EntityInstanceLogic.getInstance().getEntityInstance(eea, universalSpec, 193 ComponentVendors.ECHO_THREE.name(), EntityTypes.Selector.name()); 194 195 if(!eea.hasExecutionErrors()) { 196 selector = selectorControl.getSelectorByEntityInstance(entityInstance, entityPermission); 197 } 198 } else { 199 handleExecutionError(InvalidParameterCountException.class, eea, ExecutionErrors.InvalidParameterCount.name()); 200 } 201 202 return selector; 203 } 204 205 public Selector getSelectorByUniversalSpec(final ExecutionErrorAccumulator eea, final SelectorUniversalSpec universalSpec, 206 boolean allowDefault) { 207 return getSelectorByUniversalSpec(eea, universalSpec, allowDefault, EntityPermission.READ_ONLY); 208 } 209 210 public Selector getSelectorByUniversalSpecForUpdate(final ExecutionErrorAccumulator eea, final SelectorUniversalSpec universalSpec, 211 boolean allowDefault) { 212 return getSelectorByUniversalSpec(eea, universalSpec, allowDefault, EntityPermission.READ_WRITE); 213 } 214 215}