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