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