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.GeoCodeCurrencySpec;
020import com.echothree.model.control.accounting.server.logic.CurrencyLogic;
021import com.echothree.model.control.geo.common.exception.UnknownGeoCodeCurrencyException;
022import com.echothree.model.control.geo.server.control.GeoControl;
023import com.echothree.model.data.geo.server.entity.GeoCodeCurrency;
024import com.echothree.util.common.message.ExecutionErrors;
025import com.echothree.util.server.control.BaseLogic;
026import com.echothree.util.server.message.ExecutionErrorAccumulator;
027import com.echothree.util.server.persistence.EntityPermission;
028import javax.enterprise.context.ApplicationScoped;
029import javax.inject.Inject;
030
031@ApplicationScoped
032public class GeoCodeCurrencyLogic
033        extends BaseLogic {
034
035    @Inject
036    GeoControl geoControl;
037
038    @Inject
039    CurrencyLogic currencyLogic;
040
041    @Inject
042    GeoCodeLogic geoCodeLogic;
043
044    protected GeoCodeCurrencyLogic() {
045        super();
046    }
047
048    public GeoCodeCurrency getGeoCodeCurrencyBySpec(final ExecutionErrorAccumulator eea,
049            final GeoCodeCurrencySpec spec, final EntityPermission entityPermission) {
050        GeoCodeCurrency geoCodeCurrency = null;
051        var geoCodeName = spec.getGeoCodeName();
052        var geoCode = geoCodeLogic.getGeoCodeByName(eea, geoCodeName);
053
054        if(!hasExecutionErrors(eea)) {
055            var currencyIsoName = spec.getCurrencyIsoName();
056            var currency = currencyLogic.getCurrencyByName(eea, currencyIsoName);
057
058            if(!hasExecutionErrors(eea)) {
059                geoCodeCurrency = geoControl.getGeoCodeCurrency(geoCode, currency, entityPermission);
060
061                if(geoCodeCurrency == null) {
062                    handleExecutionError(UnknownGeoCodeCurrencyException.class, eea, ExecutionErrors.UnknownGeoCodeCurrency.name(), geoCodeName, currencyIsoName);
063                }
064            }
065        }
066
067        return geoCodeCurrency;
068    }
069
070}