001// -------------------------------------------------------------------------------- 002// Copyright 2002-2025 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; 038 039@ApplicationScoped 040public class PaymentMethodLogic 041 extends BaseLogic { 042 043 protected PaymentMethodLogic() { 044 super(); 045 } 046 047 public static PaymentMethodLogic getInstance() { 048 return CDI.current().select(PaymentMethodLogic.class).get(); 049 } 050 051 private PaymentMethod getPaymentMethodByName(final ExecutionErrorAccumulator eea, final String paymentMethodName, final EntityPermission entityPermission) { 052 var paymentMethodControl = Session.getModelController(PaymentMethodControl.class); 053 var paymentMethod = paymentMethodControl.getPaymentMethodByName(paymentMethodName, entityPermission); 054 055 if(paymentMethod == null) { 056 handleExecutionError(UnknownPaymentMethodNameException.class, eea, ExecutionErrors.UnknownPaymentMethodName.name(), paymentMethodName); 057 } 058 059 return paymentMethod; 060 } 061 062 public PaymentMethod getPaymentMethodByName(final ExecutionErrorAccumulator eea, final String paymentMethodName) { 063 return getPaymentMethodByName(eea, paymentMethodName, EntityPermission.READ_ONLY); 064 } 065 066 public PaymentMethod getPaymentMethodByNameForUpdate(final ExecutionErrorAccumulator eea, final String paymentMethodName) { 067 return getPaymentMethodByName(eea, paymentMethodName, EntityPermission.READ_WRITE); 068 } 069 070 public void checkAcceptanceOfItem(final Session session, final ExecutionErrorAccumulator eea, final SelectorCache selectorCache, 071 final PaymentMethod paymentMethod, final Item item, final BasePK evaluatedBy) { 072 var selector = paymentMethod.getLastDetail().getItemSelector(); 073 074 if(selector != null) { 075 var cachedSelector = selectorCache.getSelector(selector); 076 077 if(!new PaymentMethodItemSelectorEvaluator(session, evaluatedBy).evaluate(cachedSelector, item)) { 078 handleExecutionError(ItemNotAcceptibleForPaymentMethodException.class, eea, ExecutionErrors.ItemNotAcceptibleForPaymentMethod.name(), 079 paymentMethod.getLastDetail().getPaymentMethodName(), item.getLastDetail().getItemName()); 080 } 081 } 082 } 083 084 public void checkAcceptanceOfItem(final Session session, final ExecutionErrorAccumulator eea, final PaymentMethod paymentMethod, final Item item, 085 final BasePK evaluatedBy) { 086 var selector = paymentMethod.getLastDetail().getItemSelector(); 087 088 if(selector != null) { 089 var selectorCache = SelectorCacheFactory.getInstance().getSelectorCache(session, SelectorKinds.ITEM.name(), 090 SelectorTypes.PAYMENT_METHOD.name()); 091 092 checkAcceptanceOfItem(session, eea, selectorCache, paymentMethod, item, evaluatedBy); 093 } 094 } 095 096 public void checkAcceptanceOfItems(final Session session, final ExecutionErrorAccumulator eea, final PaymentMethod paymentMethod, final Set<Item> items, 097 final BasePK evaluatedBy) { 098 var selector = paymentMethod.getLastDetail().getItemSelector(); 099 100 if(selector != null) { 101 var selectorCache = SelectorCacheFactory.getInstance().getSelectorCache(session, SelectorKinds.ITEM.name(), 102 SelectorTypes.PAYMENT_METHOD.name()); 103 104 items.forEach((item) -> { 105 checkAcceptanceOfItem(session, eea, selectorCache, paymentMethod, item, evaluatedBy); 106 }); 107 } 108 } 109 110}