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.payment.server.logic;
018
019import com.echothree.model.control.payment.common.exception.ItemNotAcceptibleForPaymentMethodException;
020import com.echothree.model.control.payment.common.exception.UnknownPaymentMethodNameException;
021import com.echothree.model.control.payment.server.control.PaymentMethodControl;
022import com.echothree.model.control.selector.common.SelectorKinds;
023import com.echothree.model.control.selector.common.SelectorTypes;
024import com.echothree.model.control.selector.server.evaluator.PaymentMethodItemSelectorEvaluator;
025import com.echothree.model.control.selector.server.evaluator.SelectorCache;
026import com.echothree.model.control.selector.server.evaluator.SelectorCacheFactory;
027import com.echothree.model.data.item.server.entity.Item;
028import com.echothree.model.data.payment.server.entity.PaymentMethod;
029import com.echothree.util.common.message.ExecutionErrors;
030import com.echothree.util.common.persistence.BasePK;
031import com.echothree.util.server.control.BaseLogic;
032import com.echothree.util.server.message.ExecutionErrorAccumulator;
033import com.echothree.util.server.persistence.EntityPermission;
034import com.echothree.util.server.persistence.Session;
035import java.util.Set;
036import javax.enterprise.context.ApplicationScoped;
037import javax.inject.Inject;
038
039@ApplicationScoped
040public class PaymentMethodLogic
041    extends BaseLogic {
042
043    @Inject
044    PaymentMethodControl paymentMethodControl;
045
046    protected PaymentMethodLogic() {
047        super();
048    }
049
050    private PaymentMethod getPaymentMethodByName(final ExecutionErrorAccumulator eea, final String paymentMethodName, final EntityPermission entityPermission) {
051        var paymentMethod = paymentMethodControl.getPaymentMethodByName(paymentMethodName, entityPermission);
052
053        if(paymentMethod == null) {
054            handleExecutionError(UnknownPaymentMethodNameException.class, eea, ExecutionErrors.UnknownPaymentMethodName.name(), paymentMethodName);
055        }
056
057        return paymentMethod;
058    }
059
060    public PaymentMethod getPaymentMethodByName(final ExecutionErrorAccumulator eea, final String paymentMethodName) {
061        return getPaymentMethodByName(eea, paymentMethodName, EntityPermission.READ_ONLY);
062    }
063
064    public PaymentMethod getPaymentMethodByNameForUpdate(final ExecutionErrorAccumulator eea, final String paymentMethodName) {
065        return getPaymentMethodByName(eea, paymentMethodName, EntityPermission.READ_WRITE);
066    }
067    
068    public void checkAcceptanceOfItem(final Session session, final ExecutionErrorAccumulator eea, final SelectorCache selectorCache,
069            final PaymentMethod paymentMethod, final Item item, final BasePK evaluatedBy) {
070        var selector = paymentMethod.getLastDetail().getItemSelector();
071        
072        if(selector != null) {
073            var cachedSelector = selectorCache.getSelector(selector);
074            
075            if(!new PaymentMethodItemSelectorEvaluator(session, evaluatedBy).evaluate(cachedSelector, item)) {
076                handleExecutionError(ItemNotAcceptibleForPaymentMethodException.class, eea, ExecutionErrors.ItemNotAcceptibleForPaymentMethod.name(),
077                        paymentMethod.getLastDetail().getPaymentMethodName(), item.getLastDetail().getItemName());
078            }
079        }
080    }
081    
082    public void checkAcceptanceOfItem(final Session session, final ExecutionErrorAccumulator eea, final PaymentMethod paymentMethod, final Item item,
083            final BasePK evaluatedBy) {
084        var selector = paymentMethod.getLastDetail().getItemSelector();
085        
086        if(selector != null) {
087            var selectorCache = SelectorCacheFactory.getInstance().getSelectorCache(session, SelectorKinds.ITEM.name(),
088                    SelectorTypes.PAYMENT_METHOD.name());
089            
090            checkAcceptanceOfItem(session, eea, selectorCache, paymentMethod, item, evaluatedBy);
091        }
092    }
093    
094    public void checkAcceptanceOfItems(final Session session, final ExecutionErrorAccumulator eea, final PaymentMethod paymentMethod, final Set<Item> items,
095            final BasePK evaluatedBy) {
096        var selector = paymentMethod.getLastDetail().getItemSelector();
097        
098        if(selector != null) {
099            var selectorCache = SelectorCacheFactory.getInstance().getSelectorCache(session, SelectorKinds.ITEM.name(),
100                    SelectorTypes.PAYMENT_METHOD.name());
101            
102            items.forEach((item) -> {
103                checkAcceptanceOfItem(session, eea, selectorCache, paymentMethod, item, evaluatedBy);
104            });
105        }
106    }
107    
108}