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.accounting.server.logic;
018
019import com.echothree.control.user.accounting.common.spec.SymbolPositionDescriptionSpec;
020import com.echothree.model.control.accounting.common.exception.UnknownSymbolPositionDescriptionException;
021import com.echothree.model.control.accounting.common.exception.UnknownSymbolPositionNameException;
022import com.echothree.model.control.accounting.server.control.AccountingControl;
023import com.echothree.model.control.party.server.logic.LanguageLogic;
024import com.echothree.model.data.accounting.server.entity.SymbolPosition;
025import com.echothree.model.data.accounting.server.entity.SymbolPositionDescription;
026import com.echothree.util.common.message.ExecutionErrors;
027import com.echothree.util.server.control.BaseLogic;
028import com.echothree.util.server.message.ExecutionErrorAccumulator;
029import com.echothree.util.server.persistence.EntityPermission;
030import javax.enterprise.context.ApplicationScoped;
031import javax.inject.Inject;
032
033@ApplicationScoped
034public class SymbolPositionLogic
035    extends BaseLogic {
036
037    @Inject
038    AccountingControl accountingControl;
039
040    @Inject
041    LanguageLogic languageLogic;
042
043    protected SymbolPositionLogic() {
044        super();
045    }
046
047    public SymbolPosition getSymbolPositionByName(final ExecutionErrorAccumulator eea, final String symbolPositionName,
048            final EntityPermission entityPermission) {
049        var symbolPosition = accountingControl.getSymbolPositionByName(symbolPositionName, entityPermission);
050
051        if(symbolPosition == null) {
052            handleExecutionError(UnknownSymbolPositionNameException.class, eea, ExecutionErrors.UnknownSymbolPositionName.name(), symbolPositionName);
053        }
054
055        return symbolPosition;
056    }
057
058    public SymbolPosition getSymbolPositionByName(final ExecutionErrorAccumulator eea, final String symbolPositionName) {
059        return getSymbolPositionByName(eea, symbolPositionName, EntityPermission.READ_ONLY);
060    }
061
062    public SymbolPositionDescription getSymbolPositionDescriptionBySpec(final ExecutionErrorAccumulator eea,
063            final SymbolPositionDescriptionSpec spec, final EntityPermission entityPermission) {
064        SymbolPositionDescription symbolPositionDescription = null;
065        var symbolPositionName = spec.getSymbolPositionName();
066        var symbolPosition = getSymbolPositionByName(eea, symbolPositionName);
067
068        if(!hasExecutionErrors(eea)) {
069            var languageIsoName = spec.getLanguageIsoName();
070            var language = languageLogic.getLanguageByName(eea, languageIsoName);
071
072            if(!hasExecutionErrors(eea)) {
073                symbolPositionDescription = accountingControl.getSymbolPositionDescription(symbolPosition, language, entityPermission);
074
075                if(symbolPositionDescription == null) {
076                    handleExecutionError(UnknownSymbolPositionDescriptionException.class, eea, ExecutionErrors.UnknownSymbolPositionDescription.name(), symbolPositionName, languageIsoName);
077                }
078            }
079        }
080
081        return symbolPositionDescription;
082    }
083
084}