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.enterprise.inject.spi.CDI; 038import javax.inject.Inject; 039 040@ApplicationScoped 041public class PaymentMethodLogic 042 extends BaseLogic { 043 044 @Inject 045 PaymentMethodControl paymentMethodControl; 046 047 protected PaymentMethodLogic() { 048 super(); 049 } 050 051 public static PaymentMethodLogic getInstance() { 052 return CDI.current().select(PaymentMethodLogic.class).get(); 053 } 054 055 private PaymentMethod getPaymentMethodByName(final ExecutionErrorAccumulator eea, final String paymentMethodName, final EntityPermission entityPermission) { 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 var selector = paymentMethod.getLastDetail().getItemSelector(); 076 077 if(selector != null) { 078 var 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 var selector = paymentMethod.getLastDetail().getItemSelector(); 090 091 if(selector != null) { 092 var 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 var selector = paymentMethod.getLastDetail().getItemSelector(); 102 103 if(selector != null) { 104 var 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}