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.server.control.AccountingControl;
020import com.echothree.model.control.graphql.server.graphql.BaseEntityInstanceObject;
021import com.echothree.model.control.graphql.server.util.BaseGraphQl;
022import com.echothree.model.control.user.server.control.UserControl;
023import com.echothree.model.data.accounting.server.entity.TransactionGlAccountCategory;
024import com.echothree.model.data.accounting.server.entity.TransactionGlAccountCategoryDetail;
025import com.echothree.util.server.persistence.Session;
026import graphql.annotations.annotationTypes.GraphQLDescription;
027import graphql.annotations.annotationTypes.GraphQLField;
028import graphql.annotations.annotationTypes.GraphQLName;
029import graphql.annotations.annotationTypes.GraphQLNonNull;
030import graphql.schema.DataFetchingEnvironment;
031
032@GraphQLDescription("transaction GL account category object")
033@GraphQLName("TransactionGlAccountCategory")
034public class TransactionGlAccountCategoryObject
035        extends BaseEntityInstanceObject {
036    
037    private final TransactionGlAccountCategory transactionGlAccountCategory; // Always Present
038    
039    public TransactionGlAccountCategoryObject(TransactionGlAccountCategory transactionGlAccountCategory) {
040        super(transactionGlAccountCategory.getPrimaryKey());
041
042        this.transactionGlAccountCategory = transactionGlAccountCategory;
043    }
044
045    private TransactionGlAccountCategoryDetail transactionGlAccountCategoryDetail; // Optional, use getTransactionGlAccountCategoryDetail()
046    
047    private TransactionGlAccountCategoryDetail getTransactionGlAccountCategoryDetail() {
048        if(transactionGlAccountCategoryDetail == null) {
049            transactionGlAccountCategoryDetail = transactionGlAccountCategory.getLastDetail();
050        }
051        
052        return transactionGlAccountCategoryDetail;
053    }
054    
055    @GraphQLField
056    @GraphQLDescription("transaction type")
057    @GraphQLNonNull
058    public TransactionTypeObject getTransactionType(final DataFetchingEnvironment env) {
059        return AccountingSecurityUtils.getHasTransactionTypeAccess(env) ?
060                new TransactionTypeObject(getTransactionGlAccountCategoryDetail().getTransactionType()) :
061                null;
062    }
063
064    @GraphQLField
065    @GraphQLDescription("transaction GL account category name")
066    @GraphQLNonNull
067    public String getTransactionGlAccountCategoryName() {
068        return getTransactionGlAccountCategoryDetail().getTransactionGlAccountCategoryName();
069    }
070
071    @GraphQLField
072    @GraphQLDescription("sort order")
073    @GraphQLNonNull
074    public int getSortOrder() {
075        return getTransactionGlAccountCategoryDetail().getSortOrder();
076    }
077
078    @GraphQLField
079    @GraphQLDescription("GL account category")
080    public GlAccountCategoryObject getGlAccountCategory(final DataFetchingEnvironment env) {
081        var glaccountCategory = getTransactionGlAccountCategoryDetail().getGlAccountCategory();
082
083        return glaccountCategory == null ? null :
084                AccountingSecurityUtils.getHasGlAccountCategoryAccess(env) ?
085                new GlAccountCategoryObject(glaccountCategory) :
086                null;
087    }
088
089    @GraphQLField
090    @GraphQLDescription("description")
091    @GraphQLNonNull
092    public String getDescription(final DataFetchingEnvironment env) {
093        var accountingControl = Session.getModelController(AccountingControl.class);
094        var userControl = Session.getModelController(UserControl.class);
095
096        return accountingControl.getBestTransactionGlAccountCategoryDescription(transactionGlAccountCategory, userControl.getPreferredLanguageFromUserVisit(BaseGraphQl.getUserVisit(env)));
097    }
098
099}