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.accounting.server.logic;
018
019import com.echothree.model.control.accounting.common.exception.MissingDefaultCurrencyException;
020import com.echothree.model.control.accounting.common.exception.UnknownCurrencyIsoNameException;
021import com.echothree.model.control.accounting.server.control.AccountingControl;
022import com.echothree.model.data.accounting.server.entity.Currency;
023import com.echothree.util.common.message.ExecutionErrors;
024import com.echothree.util.server.control.BaseLogic;
025import com.echothree.util.server.message.ExecutionErrorAccumulator;
026import com.echothree.util.server.persistence.Session;
027
028public class CurrencyLogic
029        extends BaseLogic {
030
031    private CurrencyLogic() {
032        super();
033    }
034
035    private static class CurrencyLogicHolder {
036        static CurrencyLogic instance = new CurrencyLogic();
037    }
038
039    public static CurrencyLogic getInstance() {
040        return CurrencyLogicHolder.instance;
041    }
042    
043    public Currency getCurrencyByName(final ExecutionErrorAccumulator eea, final String currencyIsoName) {
044        var accountingControl = Session.getModelController(AccountingControl.class);
045        Currency currency = accountingControl.getCurrencyByIsoName(currencyIsoName);
046
047        if(currency == null) {
048            handleExecutionError(UnknownCurrencyIsoNameException.class, eea, ExecutionErrors.UnknownCurrencyIsoName.name(), currencyIsoName);
049        }
050
051        return currency;
052    }
053    
054    public Currency getDefaultCurrency(final ExecutionErrorAccumulator eea) {
055        var accountingControl = Session.getModelController(AccountingControl.class);
056        Currency currency = accountingControl.getDefaultCurrency();
057
058        if(currency == null) {
059            handleExecutionError(MissingDefaultCurrencyException.class, eea, ExecutionErrors.MissingDefaultCurrency.name());
060        }
061
062        return currency;
063    }
064    
065}