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.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;
029
030public class PaymentProcessorTransferCache
031        extends BasePaymentTransferCache<PaymentProcessor, PaymentProcessorTransfer> {
032
033    PaymentProcessorControl paymentProcessorControl = Session.getModelController(PaymentProcessorControl.class);
034    PaymentProcessorTypeControl paymentProcessorTypeControl = Session.getModelController(PaymentProcessorTypeControl.class);
035    PaymentProcessorTransactionControl paymentProcessorTransactionControl = Session.getModelController(PaymentProcessorTransactionControl.class);
036
037    boolean includeComments;
038    boolean includePaymentProcessorTransactions;
039
040    /** Creates a new instance of PaymentProcessorTransferCache */
041    public PaymentProcessorTransferCache(UserVisit userVisit) {
042        super(userVisit);
043
044        var options = session.getOptions();
045        if(options != null) {
046            setIncludeKey(options.contains(PaymentOptions.PaymentProcessorIncludeKey));
047            setIncludeGuid(options.contains(PaymentOptions.PaymentProcessorIncludeGuid));
048            includeComments = options.contains(PaymentOptions.PaymentProcessorIncludeComments);
049            includePaymentProcessorTransactions = options.contains(PaymentOptions.PaymentProcessorIncludePaymentProcessorTransactions);
050        }
051        
052        setIncludeEntityInstance(true);
053    }
054
055    @Override
056    public PaymentProcessorTransfer getTransfer(PaymentProcessor paymentProcessor) {
057        PaymentProcessorTransfer paymentProcessorTransfer = get(paymentProcessor);
058        
059        if(paymentProcessorTransfer == null) {
060            var paymentProcessorDetail = paymentProcessor.getLastDetail();
061            var paymentProcessorName = paymentProcessorDetail.getPaymentProcessorName();
062            var paymentProcessorType = paymentProcessorTypeControl.getPaymentProcessorTypeTransfer(userVisit, paymentProcessorDetail.getPaymentProcessorType());
063            var isDefault = paymentProcessorDetail.getIsDefault();
064            var sortOrder = paymentProcessorDetail.getSortOrder();
065            var description = paymentProcessorControl.getBestPaymentProcessorDescription(paymentProcessor, getLanguage());
066            
067            paymentProcessorTransfer = new PaymentProcessorTransfer(paymentProcessorName, paymentProcessorType, isDefault, sortOrder, description);
068            put(paymentProcessor, paymentProcessorTransfer);
069
070            if(includeComments) {
071                setupComments(paymentProcessor, null, paymentProcessorTransfer, CommentConstants.CommentType_PAYMENT_PROCESSOR);
072            }
073            
074            if(includePaymentProcessorTransactions) {
075                paymentProcessorTransfer.setPaymentProcessorTransactions(new ListWrapper<>(paymentProcessorTransactionControl.getPaymentProcessorTransactionTransfersByPaymentProcessor(userVisit, paymentProcessor)));
076            }
077        }
078
079        return paymentProcessorTransfer;
080    }
081    
082}