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