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.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; 037 038public class GeoCodeScopeLogic 039 extends BaseLogic { 040 041 private GeoCodeScopeLogic() { 042 super(); 043 } 044 045 private static class GeoCodeScopeLogicTypeHolder { 046 static GeoCodeScopeLogic instance = new GeoCodeScopeLogic(); 047 } 048 049 public static GeoCodeScopeLogic getInstance() { 050 return GeoCodeScopeLogicTypeHolder.instance; 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 geoControl = Session.getModelController(GeoControl.class); 057 var geoCodeScope = geoControl.getGeoCodeScopeByName(geoCodeScopeName); 058 059 if(geoCodeScope == null) { 060 geoCodeScope = geoControl.createGeoCodeScope(geoCodeScopeName, isDefault, sortOrder, createdBy); 061 062 if(description != null) { 063 geoControl.createGeoCodeScopeDescription(geoCodeScope, language, description, createdBy); 064 } 065 } else { 066 handleExecutionError(DuplicateGeoCodeScopeNameException.class, eea, ExecutionErrors.DuplicateGeoCodeScopeName.name(), 067 geoCodeScopeName); 068 } 069 070 return geoCodeScope; 071 } 072 073 public GeoCodeScope getGeoCodeScopeByName(final ExecutionErrorAccumulator eea, final String geoCodeScopeName, 074 final EntityPermission entityPermission) { 075 var geoControl = Session.getModelController(GeoControl.class); 076 var geoCodeScope = geoControl.getGeoCodeScopeByName(geoCodeScopeName, entityPermission); 077 078 if(geoCodeScope == null) { 079 handleExecutionError(UnknownGeoCodeScopeNameException.class, eea, ExecutionErrors.UnknownGeoCodeScopeName.name(), geoCodeScopeName); 080 } 081 082 return geoCodeScope; 083 } 084 085 public GeoCodeScope getGeoCodeScopeByName(final ExecutionErrorAccumulator eea, final String geoCodeScopeName) { 086 return getGeoCodeScopeByName(eea, geoCodeScopeName, EntityPermission.READ_ONLY); 087 } 088 089 public GeoCodeScope getGeoCodeScopeByNameForUpdate(final ExecutionErrorAccumulator eea, final String geoCodeScopeName) { 090 return getGeoCodeScopeByName(eea, geoCodeScopeName, EntityPermission.READ_WRITE); 091 } 092 093 public GeoCodeScope getGeoCodeScopeByUniversalSpec(final ExecutionErrorAccumulator eea, 094 final GeoCodeScopeUniversalSpec universalSpec, boolean allowDefault, final EntityPermission entityPermission) { 095 GeoCodeScope geoCodeScope = null; 096 var geoControl = Session.getModelController(GeoControl.class); 097 var geoCodeScopeName = universalSpec.getGeoCodeScopeName(); 098 var parameterCount = (geoCodeScopeName == null ? 0 : 1) + EntityInstanceLogic.getInstance().countPossibleEntitySpecs(universalSpec); 099 100 switch(parameterCount) { 101 case 0 -> { 102 if(allowDefault) { 103 geoCodeScope = geoControl.getDefaultGeoCodeScope(entityPermission); 104 105 if(geoCodeScope == null) { 106 handleExecutionError(UnknownDefaultGeoCodeScopeException.class, eea, ExecutionErrors.UnknownDefaultGeoCodeScope.name()); 107 } 108 } else { 109 handleExecutionError(InvalidParameterCountException.class, eea, ExecutionErrors.InvalidParameterCount.name()); 110 } 111 } 112 case 1 -> { 113 if(geoCodeScopeName == null) { 114 var entityInstance = EntityInstanceLogic.getInstance().getEntityInstance(eea, universalSpec, 115 ComponentVendors.ECHO_THREE.name(), EntityTypes.GeoCodeScope.name()); 116 117 if(!eea.hasExecutionErrors()) { 118 geoCodeScope = geoControl.getGeoCodeScopeByEntityInstance(entityInstance, entityPermission); 119 } 120 } else { 121 geoCodeScope = getGeoCodeScopeByName(eea, geoCodeScopeName, entityPermission); 122 } 123 } 124 default -> 125 handleExecutionError(InvalidParameterCountException.class, eea, ExecutionErrors.InvalidParameterCount.name()); 126 } 127 128 return geoCodeScope; 129 } 130 131 public GeoCodeScope getGeoCodeScopeByUniversalSpec(final ExecutionErrorAccumulator eea, 132 final GeoCodeScopeUniversalSpec universalSpec, boolean allowDefault) { 133 return getGeoCodeScopeByUniversalSpec(eea, universalSpec, allowDefault, EntityPermission.READ_ONLY); 134 } 135 136 public GeoCodeScope getGeoCodeScopeByUniversalSpecForUpdate(final ExecutionErrorAccumulator eea, 137 final GeoCodeScopeUniversalSpec universalSpec, boolean allowDefault) { 138 return getGeoCodeScopeByUniversalSpec(eea, universalSpec, allowDefault, EntityPermission.READ_WRITE); 139 } 140 141 public void updateGeoCodeScopeFromValue(GeoCodeScopeDetailValue geoCodeScopeDetailValue, BasePK updatedBy) { 142 var geoControl = Session.getModelController(GeoControl.class); 143 144 geoControl.updateGeoCodeScopeFromValue(geoCodeScopeDetailValue, updatedBy); 145 } 146 147 private void checkDeleteGeoCodeScope(final ExecutionErrorAccumulator eea, final GeoCodeScope geoCodeScope) { 148 var geoControl = Session.getModelController(GeoControl.class); 149 150 if(geoControl.countGeoCodesByGeoCodeScope(geoCodeScope) != 0) { 151 eea.addExecutionError(ExecutionErrors.CannotDeleteGeoCodeScopeInUse.name(), 152 geoCodeScope.getLastDetail().getGeoCodeScopeName()); 153 } 154 } 155 156 public void deleteGeoCodeScope(final ExecutionErrorAccumulator eea, final GeoCodeScope geoCodeScope, final BasePK deletedBy) { 157 checkDeleteGeoCodeScope(eea, geoCodeScope); 158 159 if(!eea.hasExecutionErrors()) { 160 var geoControl = Session.getModelController(GeoControl.class); 161 162 geoControl.deleteGeoCodeScope(geoCodeScope, deletedBy); 163 } 164 } 165 166}