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.geo.server.logic; 018 019import com.echothree.control.user.geo.common.spec.GeoCodeScopeUniversalSpec; 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.geo.common.exception.DuplicateGeoCodeScopeNameException; 025import com.echothree.model.control.geo.common.exception.UnknownDefaultGeoCodeScopeException; 026import com.echothree.model.control.geo.common.exception.UnknownGeoCodeScopeNameException; 027import com.echothree.model.control.geo.server.control.GeoControl; 028import com.echothree.model.data.geo.server.entity.GeoCodeScope; 029import com.echothree.model.data.geo.server.value.GeoCodeScopeDetailValue; 030import com.echothree.model.data.party.server.entity.Language; 031import com.echothree.util.common.message.ExecutionErrors; 032import com.echothree.util.common.persistence.BasePK; 033import com.echothree.util.server.control.BaseLogic; 034import com.echothree.util.server.message.ExecutionErrorAccumulator; 035import com.echothree.util.server.persistence.EntityPermission; 036import com.echothree.util.server.persistence.Session; 037import javax.enterprise.context.ApplicationScoped; 038import javax.enterprise.inject.spi.CDI; 039 040@ApplicationScoped 041public class GeoCodeScopeLogic 042 extends BaseLogic { 043 044 protected GeoCodeScopeLogic() { 045 super(); 046 } 047 048 public static GeoCodeScopeLogic getInstance() { 049 return CDI.current().select(GeoCodeScopeLogic.class).get(); 050 } 051 052 public GeoCodeScope createGeoCodeScope(final ExecutionErrorAccumulator eea, final String geoCodeScopeName, 053 final Boolean isDefault, final Integer sortOrder, final Language language, final String description, 054 final BasePK createdBy) { 055 var geoControl = Session.getModelController(GeoControl.class); 056 var geoCodeScope = geoControl.getGeoCodeScopeByName(geoCodeScopeName); 057 058 if(geoCodeScope == null) { 059 geoCodeScope = geoControl.createGeoCodeScope(geoCodeScopeName, isDefault, sortOrder, createdBy); 060 061 if(description != null) { 062 geoControl.createGeoCodeScopeDescription(geoCodeScope, language, description, createdBy); 063 } 064 } else { 065 handleExecutionError(DuplicateGeoCodeScopeNameException.class, eea, ExecutionErrors.DuplicateGeoCodeScopeName.name(), 066 geoCodeScopeName); 067 } 068 069 return geoCodeScope; 070 } 071 072 public GeoCodeScope getGeoCodeScopeByName(final ExecutionErrorAccumulator eea, final String geoCodeScopeName, 073 final EntityPermission entityPermission) { 074 var geoControl = Session.getModelController(GeoControl.class); 075 var geoCodeScope = geoControl.getGeoCodeScopeByName(geoCodeScopeName, entityPermission); 076 077 if(geoCodeScope == null) { 078 handleExecutionError(UnknownGeoCodeScopeNameException.class, eea, ExecutionErrors.UnknownGeoCodeScopeName.name(), geoCodeScopeName); 079 } 080 081 return geoCodeScope; 082 } 083 084 public GeoCodeScope getGeoCodeScopeByName(final ExecutionErrorAccumulator eea, final String geoCodeScopeName) { 085 return getGeoCodeScopeByName(eea, geoCodeScopeName, EntityPermission.READ_ONLY); 086 } 087 088 public GeoCodeScope getGeoCodeScopeByNameForUpdate(final ExecutionErrorAccumulator eea, final String geoCodeScopeName) { 089 return getGeoCodeScopeByName(eea, geoCodeScopeName, EntityPermission.READ_WRITE); 090 } 091 092 public GeoCodeScope getGeoCodeScopeByUniversalSpec(final ExecutionErrorAccumulator eea, 093 final GeoCodeScopeUniversalSpec universalSpec, boolean allowDefault, final EntityPermission entityPermission) { 094 GeoCodeScope geoCodeScope = null; 095 var geoControl = Session.getModelController(GeoControl.class); 096 var geoCodeScopeName = universalSpec.getGeoCodeScopeName(); 097 var parameterCount = (geoCodeScopeName == null ? 0 : 1) + EntityInstanceLogic.getInstance().countPossibleEntitySpecs(universalSpec); 098 099 switch(parameterCount) { 100 case 0 -> { 101 if(allowDefault) { 102 geoCodeScope = geoControl.getDefaultGeoCodeScope(entityPermission); 103 104 if(geoCodeScope == null) { 105 handleExecutionError(UnknownDefaultGeoCodeScopeException.class, eea, ExecutionErrors.UnknownDefaultGeoCodeScope.name()); 106 } 107 } else { 108 handleExecutionError(InvalidParameterCountException.class, eea, ExecutionErrors.InvalidParameterCount.name()); 109 } 110 } 111 case 1 -> { 112 if(geoCodeScopeName == null) { 113 var entityInstance = EntityInstanceLogic.getInstance().getEntityInstance(eea, universalSpec, 114 ComponentVendors.ECHO_THREE.name(), EntityTypes.GeoCodeScope.name()); 115 116 if(!eea.hasExecutionErrors()) { 117 geoCodeScope = geoControl.getGeoCodeScopeByEntityInstance(entityInstance, entityPermission); 118 } 119 } else { 120 geoCodeScope = getGeoCodeScopeByName(eea, geoCodeScopeName, entityPermission); 121 } 122 } 123 default -> 124 handleExecutionError(InvalidParameterCountException.class, eea, ExecutionErrors.InvalidParameterCount.name()); 125 } 126 127 return geoCodeScope; 128 } 129 130 public GeoCodeScope getGeoCodeScopeByUniversalSpec(final ExecutionErrorAccumulator eea, 131 final GeoCodeScopeUniversalSpec universalSpec, boolean allowDefault) { 132 return getGeoCodeScopeByUniversalSpec(eea, universalSpec, allowDefault, EntityPermission.READ_ONLY); 133 } 134 135 public GeoCodeScope getGeoCodeScopeByUniversalSpecForUpdate(final ExecutionErrorAccumulator eea, 136 final GeoCodeScopeUniversalSpec universalSpec, boolean allowDefault) { 137 return getGeoCodeScopeByUniversalSpec(eea, universalSpec, allowDefault, EntityPermission.READ_WRITE); 138 } 139 140 public void updateGeoCodeScopeFromValue(GeoCodeScopeDetailValue geoCodeScopeDetailValue, BasePK updatedBy) { 141 var geoControl = Session.getModelController(GeoControl.class); 142 143 geoControl.updateGeoCodeScopeFromValue(geoCodeScopeDetailValue, updatedBy); 144 } 145 146 private void checkDeleteGeoCodeScope(final ExecutionErrorAccumulator eea, final GeoCodeScope geoCodeScope) { 147 var geoControl = Session.getModelController(GeoControl.class); 148 149 if(geoControl.countGeoCodesByGeoCodeScope(geoCodeScope) != 0) { 150 eea.addExecutionError(ExecutionErrors.CannotDeleteGeoCodeScopeInUse.name(), 151 geoCodeScope.getLastDetail().getGeoCodeScopeName()); 152 } 153 } 154 155 public void deleteGeoCodeScope(final ExecutionErrorAccumulator eea, final GeoCodeScope geoCodeScope, final BasePK deletedBy) { 156 checkDeleteGeoCodeScope(eea, geoCodeScope); 157 158 if(!eea.hasExecutionErrors()) { 159 var geoControl = Session.getModelController(GeoControl.class); 160 161 geoControl.deleteGeoCodeScope(geoCodeScope, deletedBy); 162 } 163 } 164 165}