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