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