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