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.GeoCodeUniversalSpec;
020import com.echothree.model.control.contact.server.control.ContactControl;
021import com.echothree.model.control.core.common.ComponentVendors;
022import com.echothree.model.control.core.common.EntityTypes;
023import com.echothree.model.control.core.common.exception.InvalidParameterCountException;
024import com.echothree.model.control.core.server.logic.EntityInstanceLogic;
025import com.echothree.model.control.geo.common.exception.UnknownGeoCodeAliasTypeNameException;
026import com.echothree.model.control.geo.common.exception.UnknownGeoCodeNameException;
027import com.echothree.model.control.geo.server.control.GeoControl;
028import com.echothree.model.control.item.server.control.ItemControl;
029import com.echothree.model.control.selector.server.control.SelectorControl;
030import com.echothree.model.control.tax.server.control.TaxControl;
031import com.echothree.model.data.geo.server.entity.GeoCode;
032import com.echothree.model.data.geo.server.entity.GeoCodeAlias;
033import com.echothree.model.data.geo.server.entity.GeoCodeScope;
034import com.echothree.model.data.geo.server.entity.GeoCodeType;
035import com.echothree.util.common.message.ExecutionErrors;
036import com.echothree.util.common.persistence.BasePK;
037import com.echothree.util.server.control.BaseLogic;
038import com.echothree.util.server.message.ExecutionErrorAccumulator;
039import com.echothree.util.server.persistence.EntityPermission;
040import javax.enterprise.context.ApplicationScoped;
041import javax.enterprise.inject.spi.CDI;
042import javax.inject.Inject;
043
044@ApplicationScoped
045public class GeoCodeLogic
046        extends BaseLogic {
047
048    @Inject
049    ContactControl contactControl;
050
051    @Inject
052    GeoControl geoControl;
053
054    @Inject
055    ItemControl itemControl;
056
057    @Inject
058    SelectorControl selectorControl;
059
060    @Inject
061    TaxControl taxControl;
062
063    @Inject
064    EntityInstanceLogic entityInstanceLogic;
065
066    protected GeoCodeLogic() {
067        super();
068    }
069
070    public static GeoCodeLogic getInstance() {
071        return CDI.current().select(GeoCodeLogic.class).get();
072    }
073
074    public GeoCode getGeoCodeByName(final ExecutionErrorAccumulator eea, final String geoCodeName,
075            final EntityPermission entityPermission) {
076        var geoCode = geoControl.getGeoCodeByName(geoCodeName, entityPermission);
077
078        if(geoCode == null) {
079            handleExecutionError(UnknownGeoCodeNameException.class, eea, ExecutionErrors.UnknownGeoCodeName.name(), geoCodeName);
080        }
081
082        return geoCode;
083    }
084
085    public GeoCode getGeoCodeByName(final ExecutionErrorAccumulator eea, final String geoCodeName) {
086        return getGeoCodeByName(eea, geoCodeName, EntityPermission.READ_ONLY);
087    }
088
089    public GeoCode getGeoCodeByNameForUpdate(final ExecutionErrorAccumulator eea, final String geoCodeName) {
090        return getGeoCodeByName(eea, geoCodeName, EntityPermission.READ_WRITE);
091    }
092
093    public GeoCode getGeoCodeByUniversalSpec(final ExecutionErrorAccumulator eea,
094            final GeoCodeUniversalSpec universalSpec, final EntityPermission entityPermission) {
095        GeoCode geoCode = null;
096        var geoCodeName = universalSpec.getGeoCodeName();
097        var parameterCount = (geoCodeName == null ? 0 : 1) + entityInstanceLogic.countPossibleEntitySpecs(universalSpec);
098
099        switch(parameterCount) {
100            case 1 -> {
101                if(geoCodeName == null) {
102                    var entityInstance = entityInstanceLogic.getEntityInstance(eea, universalSpec,
103                            ComponentVendors.ECHO_THREE.name(), EntityTypes.GeoCode.name());
104
105                    if(eea == null || !eea.hasExecutionErrors()) {
106                        geoCode = geoControl.getGeoCodeByEntityInstance(entityInstance, entityPermission);
107                    }
108                } else {
109                    geoCode = getGeoCodeByName(eea, geoCodeName, entityPermission);
110                }
111            }
112            default ->
113                    handleExecutionError(InvalidParameterCountException.class, eea, ExecutionErrors.InvalidParameterCount.name());
114        }
115
116        return geoCode;
117    }
118
119    public GeoCode getGeoCodeByUniversalSpec(final ExecutionErrorAccumulator eea,
120            final GeoCodeUniversalSpec universalSpec) {
121        return getGeoCodeByUniversalSpec(eea, universalSpec, EntityPermission.READ_ONLY);
122    }
123
124    public GeoCode getGeoCodeByUniversalSpecForUpdate(final ExecutionErrorAccumulator eea,
125            final GeoCodeUniversalSpec universalSpec) {
126        return getGeoCodeByUniversalSpec(eea, universalSpec, EntityPermission.READ_WRITE);
127    }
128
129    public GeoCode getGeoCodeByAlias(final ExecutionErrorAccumulator eea, final GeoCodeType geoCodeType, final GeoCodeScope geoCodeScope,
130            final String geoCodeAliasTypeName, final String alias) {
131        var geoCodeAliasType = geoControl.getGeoCodeAliasTypeByName(geoCodeType, geoCodeAliasTypeName);
132        GeoCodeAlias geoCodeAlias = null;
133
134        if(geoCodeAliasType != null) {
135            geoCodeAlias = geoControl.getGeoCodeAliasByAliasWithinScope(geoCodeScope, geoCodeAliasType, alias);
136        } else {
137            handleExecutionError(UnknownGeoCodeAliasTypeNameException.class, eea, ExecutionErrors.UnknownGeoCodeAliasTypeName.name(),
138                    geoCodeType.getLastDetail().getGeoCodeTypeName(), geoCodeAliasTypeName);
139        }
140
141        return geoCodeAlias == null ? null : geoCodeAlias.getGeoCode();
142    }
143
144    public void deleteGeoCode(final ExecutionErrorAccumulator eea, final GeoCode geoCode, final BasePK deletedBy) {
145        if(contactControl.countContactPostalAddressCorrectionsByCityGeoCode(geoCode) == 0
146                && contactControl.countContactPostalAddressCorrectionsByCountyGeoCode(geoCode) == 0
147                && contactControl.countContactPostalAddressCorrectionsByStateGeoCode(geoCode) == 0
148                && contactControl.countContactPostalAddressCorrectionsByPostalCodeGeoCode(geoCode) == 0
149                && contactControl.countContactPostalAddressCorrectionsByCountryGeoCode(geoCode) == 0
150                && contactControl.countContactTelephonesByCountryGeoCode(geoCode) == 0
151                && geoControl.countGeoCodeRelationshipsByFromGeoCode(geoCode) == 0
152                && itemControl.countItemCountryOfOriginsByCountryGeoCode(geoCode) == 0
153                && selectorControl.countSelectorNodeGeoCodesByGeoCode(geoCode) == 0
154                && taxControl.countGeoCodeTaxesByGeoCode(geoCode) == 0) {
155
156            geoControl.deleteGeoCode(geoCode, deletedBy);
157        } else {
158            handleExecutionError(null, eea, ExecutionErrors.CannotDeleteGeoCodeInUse.name(),
159                    geoCode.getLastDetail().getGeoCodeName());
160        }
161    }
162
163}