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.graphql;
018
019import com.echothree.model.control.accounting.common.workflow.TransactionGroupStatusConstants;
020import com.echothree.model.control.accounting.server.control.AccountingControl;
021import com.echothree.model.control.graphql.server.graphql.BaseEntityInstanceObject;
022import com.echothree.model.control.graphql.server.graphql.count.Connections;
023import com.echothree.model.control.graphql.server.graphql.count.CountedObjects;
024import com.echothree.model.control.graphql.server.graphql.count.CountingDataConnectionFetcher;
025import com.echothree.model.control.graphql.server.graphql.count.CountingPaginatedData;
026import com.echothree.model.control.graphql.server.util.count.ObjectLimiter;
027import com.echothree.model.control.workflow.server.graphql.WorkflowEntityStatusObject;
028import com.echothree.model.data.accounting.common.TransactionConstants;
029import com.echothree.model.data.accounting.server.entity.TransactionGroup;
030import com.echothree.model.data.accounting.server.entity.TransactionGroupDetail;
031import com.echothree.util.server.persistence.Session;
032import graphql.annotations.annotationTypes.GraphQLDescription;
033import graphql.annotations.annotationTypes.GraphQLField;
034import graphql.annotations.annotationTypes.GraphQLName;
035import graphql.annotations.annotationTypes.GraphQLNonNull;
036import graphql.annotations.connection.GraphQLConnection;
037import graphql.schema.DataFetchingEnvironment;
038import java.util.ArrayList;
039import java.util.stream.Collectors;
040
041@GraphQLDescription("transaction group object")
042@GraphQLName("TransactionGroup")
043public class TransactionGroupObject
044        extends BaseEntityInstanceObject {
045    
046    private final TransactionGroup transactionGroup; // Always Present
047    
048    public TransactionGroupObject(TransactionGroup transactionGroup) {
049        super(transactionGroup.getPrimaryKey());
050        
051        this.transactionGroup = transactionGroup;
052    }
053
054    private TransactionGroupDetail transactionGroupDetail; // Optional, use getTransactionGroupDetail()
055    
056    private TransactionGroupDetail getTransactionGroupDetail() {
057        if(transactionGroupDetail == null) {
058            transactionGroupDetail = transactionGroup.getLastDetail();
059        }
060        
061        return transactionGroupDetail;
062    }
063    
064    @GraphQLField
065    @GraphQLDescription("transaction group name")
066    @GraphQLNonNull
067    public String getTransactionGroupName() {
068        return getTransactionGroupDetail().getTransactionGroupName();
069    }
070
071    @GraphQLField
072    @GraphQLDescription("transaction group status")
073    public WorkflowEntityStatusObject getTransactionGroupStatus(final DataFetchingEnvironment env) {
074        return getWorkflowEntityStatusObject(env, TransactionGroupStatusConstants.Workflow_TRANSACTION_GROUP_STATUS);
075    }
076
077    @GraphQLField
078    @GraphQLDescription("transactions")
079    @GraphQLNonNull
080    @GraphQLConnection(connectionFetcher = CountingDataConnectionFetcher.class)
081    public CountingPaginatedData<TransactionObject> getTransactions(final DataFetchingEnvironment env) {
082        if(AccountingSecurityUtils.getHasTransactionsAccess(env)) {
083            var accountingControl = Session.getModelController(AccountingControl.class);
084            var totalCount = accountingControl.countTransactionsByTransactionGroup(transactionGroup);
085    
086            try(var objectLimiter = new ObjectLimiter(env, TransactionConstants.COMPONENT_VENDOR_NAME, TransactionConstants.ENTITY_TYPE_NAME, totalCount)) {
087                var entities = accountingControl.getTransactionsByTransactionGroup(transactionGroup);
088                var transactions = entities.stream().map(TransactionObject::new).collect(Collectors.toCollection(() -> new ArrayList<>(entities.size())));
089    
090                return new CountedObjects<>(objectLimiter, transactions);
091            }
092        } else {
093            return Connections.emptyConnection();
094        }
095    }
096
097}