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.transfer;
018
019import javax.inject.Inject;
020import com.echothree.model.control.comment.common.CommentConstants;
021import com.echothree.model.control.payment.common.PaymentOptions;
022import com.echothree.model.control.payment.common.transfer.PaymentProcessorTransfer;
023import com.echothree.model.control.payment.server.control.PaymentProcessorControl;
024import com.echothree.model.control.payment.server.control.PaymentProcessorTransactionControl;
025import com.echothree.model.control.payment.server.control.PaymentProcessorTypeControl;
026import com.echothree.model.data.payment.server.entity.PaymentProcessor;
027import com.echothree.model.data.user.server.entity.UserVisit;
028import com.echothree.util.common.transfer.ListWrapper;
029import javax.enterprise.context.RequestScoped;
030
031@RequestScoped
032public class PaymentProcessorTransferCache
033        extends BasePaymentTransferCache<PaymentProcessor, PaymentProcessorTransfer> {
034
035    @Inject
036    PaymentProcessorControl paymentProcessorControl;
037
038    @Inject
039    PaymentProcessorTransactionControl paymentProcessorTransactionControl;
040
041    @Inject
042    PaymentProcessorTypeControl paymentProcessorTypeControl;
043
044    boolean includeComments;
045    boolean includePaymentProcessorTransactions;
046
047    /** Creates a new instance of PaymentProcessorTransferCache */
048    protected PaymentProcessorTransferCache() {
049        super();
050
051        var options = session.getOptions();
052        if(options != null) {
053            setIncludeUuid(options.contains(PaymentOptions.PaymentProcessorIncludeUuid));
054            includeComments = options.contains(PaymentOptions.PaymentProcessorIncludeComments);
055            includePaymentProcessorTransactions = options.contains(PaymentOptions.PaymentProcessorIncludePaymentProcessorTransactions);
056        }
057        
058        setIncludeEntityInstance(true);
059    }
060
061    @Override
062    public PaymentProcessorTransfer getTransfer(UserVisit userVisit, PaymentProcessor paymentProcessor) {
063        var paymentProcessorTransfer = get(paymentProcessor);
064        
065        if(paymentProcessorTransfer == null) {
066            var paymentProcessorDetail = paymentProcessor.getLastDetail();
067            var paymentProcessorName = paymentProcessorDetail.getPaymentProcessorName();
068            var paymentProcessorType = paymentProcessorTypeControl.getPaymentProcessorTypeTransfer(userVisit, paymentProcessorDetail.getPaymentProcessorType());
069            var isDefault = paymentProcessorDetail.getIsDefault();
070            var sortOrder = paymentProcessorDetail.getSortOrder();
071            var description = paymentProcessorControl.getBestPaymentProcessorDescription(paymentProcessor, getLanguage(userVisit));
072            
073            paymentProcessorTransfer = new PaymentProcessorTransfer(paymentProcessorName, paymentProcessorType, isDefault, sortOrder, description);
074            put(userVisit, paymentProcessor, paymentProcessorTransfer);
075
076            if(includeComments) {
077                setupComments(userVisit, paymentProcessor, null, paymentProcessorTransfer, CommentConstants.CommentType_PAYMENT_PROCESSOR);
078            }
079            
080            if(includePaymentProcessorTransactions) {
081                paymentProcessorTransfer.setPaymentProcessorTransactions(new ListWrapper<>(paymentProcessorTransactionControl.getPaymentProcessorTransactionTransfersByPaymentProcessor(userVisit, paymentProcessor)));
082            }
083        }
084
085        return paymentProcessorTransfer;
086    }
087    
088}