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.accounting.server.transfer;
018
019import javax.inject.Inject;
020import com.echothree.model.control.accounting.common.AccountingOptions;
021import com.echothree.model.control.accounting.common.transfer.TransactionGroupTransfer;
022import com.echothree.model.control.accounting.common.workflow.TransactionGroupStatusConstants;
023import com.echothree.model.control.accounting.server.control.AccountingControl;
024import com.echothree.model.control.workflow.server.control.WorkflowControl;
025import com.echothree.model.data.accounting.server.entity.TransactionGroup;
026import com.echothree.model.data.user.server.entity.UserVisit;
027import com.echothree.util.common.transfer.ListWrapper;
028import javax.enterprise.context.RequestScoped;
029
030@RequestScoped
031public class TransactionGroupTransferCache
032        extends BaseAccountingTransferCache<TransactionGroup, TransactionGroupTransfer> {
033
034    @Inject
035    AccountingControl accountingControl;
036
037    @Inject
038    WorkflowControl workflowControl;
039
040    boolean includeTransactions;
041    
042    /** Creates a new instance of TransactionGroupTransferCache */
043    protected TransactionGroupTransferCache() {
044        super();
045        
046        var options = session.getOptions();
047        if(options != null) {
048            includeTransactions = options.contains(AccountingOptions.TransactionGroupIncludeTransactions);
049        }
050        
051        setIncludeEntityInstance(true);
052    }
053    
054    @Override
055    public TransactionGroupTransfer getTransfer(UserVisit userVisit, TransactionGroup transactionGroup) {
056        var transactionGroupTransfer = get(transactionGroup);
057        
058        if(transactionGroupTransfer == null) {
059            var transactionGroupDetail = transactionGroup.getLastDetail();
060            var transactionGroupName = transactionGroupDetail.getTransactionGroupName();
061
062            var entityInstance = entityInstanceControl.getEntityInstanceByBasePK(transactionGroup.getPrimaryKey());
063            var transactionGroupStatusTransfer = workflowControl.getWorkflowEntityStatusTransferByEntityInstanceUsingNames(userVisit,
064                    TransactionGroupStatusConstants.Workflow_TRANSACTION_GROUP_STATUS, entityInstance);
065            
066            transactionGroupTransfer = new TransactionGroupTransfer(transactionGroupName, transactionGroupStatusTransfer);
067            put(userVisit, transactionGroup, transactionGroupTransfer, entityInstance);
068            
069            if(includeTransactions) {
070                transactionGroupTransfer.setTransactions(new ListWrapper<>(accountingControl.getTransactionTransfersByTransactionGroup(userVisit, transactionGroup)));
071            }
072        }
073        
074        return transactionGroupTransfer;
075    }
076    
077}