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