001// -------------------------------------------------------------------------------- 002// Copyright 2002-2026 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 javax.enterprise.context.ApplicationScoped; 037import javax.inject.Inject; 038 039@ApplicationScoped 040public class GeoCodeScopeLogic 041 extends BaseLogic { 042 043 @Inject 044 GeoControl geoControl; 045 046 @Inject 047 EntityInstanceLogic entityInstanceLogic; 048 049 protected GeoCodeScopeLogic() { 050 super(); 051 } 052 053 public GeoCodeScope createGeoCodeScope(final ExecutionErrorAccumulator eea, final String geoCodeScopeName, 054 final Boolean isDefault, final Integer sortOrder, final Language language, final String description, 055 final BasePK createdBy) { 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 geoCodeScope = geoControl.getGeoCodeScopeByName(geoCodeScopeName, entityPermission); 075 076 if(geoCodeScope == null) { 077 handleExecutionError(UnknownGeoCodeScopeNameException.class, eea, ExecutionErrors.UnknownGeoCodeScopeName.name(), geoCodeScopeName); 078 } 079 080 return geoCodeScope; 081 } 082 083 public GeoCodeScope getGeoCodeScopeByName(final ExecutionErrorAccumulator eea, final String geoCodeScopeName) { 084 return getGeoCodeScopeByName(eea, geoCodeScopeName, EntityPermission.READ_ONLY); 085 } 086 087 public GeoCodeScope getGeoCodeScopeByNameForUpdate(final ExecutionErrorAccumulator eea, final String geoCodeScopeName) { 088 return getGeoCodeScopeByName(eea, geoCodeScopeName, EntityPermission.READ_WRITE); 089 } 090 091 public GeoCodeScope getGeoCodeScopeByUniversalSpec(final ExecutionErrorAccumulator eea, 092 final GeoCodeScopeUniversalSpec universalSpec, boolean allowDefault, final EntityPermission entityPermission) { 093 GeoCodeScope geoCodeScope = null; 094 var geoCodeScopeName = universalSpec.getGeoCodeScopeName(); 095 var parameterCount = (geoCodeScopeName == null ? 0 : 1) + entityInstanceLogic.countPossibleEntitySpecs(universalSpec); 096 097 switch(parameterCount) { 098 case 0 -> { 099 if(allowDefault) { 100 geoCodeScope = geoControl.getDefaultGeoCodeScope(entityPermission); 101 102 if(geoCodeScope == null) { 103 handleExecutionError(UnknownDefaultGeoCodeScopeException.class, eea, ExecutionErrors.UnknownDefaultGeoCodeScope.name()); 104 } 105 } else { 106 handleExecutionError(InvalidParameterCountException.class, eea, ExecutionErrors.InvalidParameterCount.name()); 107 } 108 } 109 case 1 -> { 110 if(geoCodeScopeName == null) { 111 var entityInstance = entityInstanceLogic.getEntityInstance(eea, universalSpec, 112 ComponentVendors.ECHO_THREE.name(), EntityTypes.GeoCodeScope.name()); 113 114 if(eea == null || !eea.hasExecutionErrors()) { 115 geoCodeScope = geoControl.getGeoCodeScopeByEntityInstance(entityInstance, entityPermission); 116 } 117 } else { 118 geoCodeScope = getGeoCodeScopeByName(eea, geoCodeScopeName, entityPermission); 119 } 120 } 121 default -> 122 handleExecutionError(InvalidParameterCountException.class, eea, ExecutionErrors.InvalidParameterCount.name()); 123 } 124 125 return geoCodeScope; 126 } 127 128 public GeoCodeScope getGeoCodeScopeByUniversalSpec(final ExecutionErrorAccumulator eea, 129 final GeoCodeScopeUniversalSpec universalSpec, boolean allowDefault) { 130 return getGeoCodeScopeByUniversalSpec(eea, universalSpec, allowDefault, EntityPermission.READ_ONLY); 131 } 132 133 public GeoCodeScope getGeoCodeScopeByUniversalSpecForUpdate(final ExecutionErrorAccumulator eea, 134 final GeoCodeScopeUniversalSpec universalSpec, boolean allowDefault) { 135 return getGeoCodeScopeByUniversalSpec(eea, universalSpec, allowDefault, EntityPermission.READ_WRITE); 136 } 137 138 public void updateGeoCodeScopeFromValue(GeoCodeScopeDetailValue geoCodeScopeDetailValue, BasePK updatedBy) { 139 geoControl.updateGeoCodeScopeFromValue(geoCodeScopeDetailValue, updatedBy); 140 } 141 142 private void checkDeleteGeoCodeScope(final ExecutionErrorAccumulator eea, final GeoCodeScope geoCodeScope) { 143 if(geoControl.countGeoCodesByGeoCodeScope(geoCodeScope) != 0) { 144 eea.addExecutionError(ExecutionErrors.CannotDeleteGeoCodeScopeInUse.name(), 145 geoCodeScope.getLastDetail().getGeoCodeScopeName()); 146 } 147 } 148 149 public void deleteGeoCodeScope(final ExecutionErrorAccumulator eea, final GeoCodeScope geoCodeScope, final BasePK deletedBy) { 150 checkDeleteGeoCodeScope(eea, geoCodeScope); 151 152 if(eea == null || !eea.hasExecutionErrors()) { 153 geoControl.deleteGeoCodeScope(geoCodeScope, deletedBy); 154 } 155 } 156 157}