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.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.graphql.count.Connections; 022import com.echothree.model.control.graphql.server.graphql.count.CountedObjects; 023import com.echothree.model.control.graphql.server.graphql.count.CountingDataConnectionFetcher; 024import com.echothree.model.control.graphql.server.graphql.count.CountingPaginatedData; 025import com.echothree.model.control.graphql.server.util.BaseGraphQl; 026import com.echothree.model.control.graphql.server.util.count.ObjectLimiter; 027import com.echothree.model.control.user.server.control.UserControl; 028import com.echothree.model.data.accounting.common.GlAccountConstants; 029import com.echothree.model.data.accounting.server.entity.GlAccountCategory; 030import com.echothree.model.data.accounting.server.entity.GlAccountCategoryDetail; 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("GL account category object") 042@GraphQLName("GlAccountCategory") 043public class GlAccountCategoryObject 044 extends BaseEntityInstanceObject { 045 046 private final GlAccountCategory glAccountCategory; // Always Present 047 048 public GlAccountCategoryObject(GlAccountCategory glAccountCategory) { 049 super(glAccountCategory.getPrimaryKey()); 050 051 this.glAccountCategory = glAccountCategory; 052 } 053 054 private GlAccountCategoryDetail glAccountCategoryDetail; // Optional, use getGlAccountCategoryDetail() 055 056 private GlAccountCategoryDetail getGlAccountCategoryDetail() { 057 if(glAccountCategoryDetail == null) { 058 glAccountCategoryDetail = glAccountCategory.getLastDetail(); 059 } 060 061 return glAccountCategoryDetail; 062 } 063 064 @GraphQLField 065 @GraphQLDescription("GL account category name") 066 @GraphQLNonNull 067 public String getGlAccountCategoryName() { 068 return getGlAccountCategoryDetail().getGlAccountCategoryName(); 069 } 070 071 @GraphQLField 072 @GraphQLDescription("parent GL account category") 073 public GlAccountCategoryObject getParentGlAccountCategory() { 074 var parentGlAccountCategory = getGlAccountCategoryDetail().getParentGlAccountCategory(); 075 076 return parentGlAccountCategory == null ? null : new GlAccountCategoryObject(parentGlAccountCategory); 077 } 078 079 @GraphQLField 080 @GraphQLDescription("is default") 081 @GraphQLNonNull 082 public boolean getIsDefault() { 083 return getGlAccountCategoryDetail().getIsDefault(); 084 } 085 086 @GraphQLField 087 @GraphQLDescription("sort order") 088 @GraphQLNonNull 089 public int getSortOrder() { 090 return getGlAccountCategoryDetail().getSortOrder(); 091 } 092 093 @GraphQLField 094 @GraphQLDescription("description") 095 @GraphQLNonNull 096 public String getDescription(final DataFetchingEnvironment env) { 097 var accountingControl = Session.getModelController(AccountingControl.class); 098 var userControl = Session.getModelController(UserControl.class); 099 100 return accountingControl.getBestGlAccountCategoryDescription(glAccountCategory, userControl.getPreferredLanguageFromUserVisit(BaseGraphQl.getUserVisit(env))); 101 } 102 103 @GraphQLField 104 @GraphQLDescription("GL accounts") 105 @GraphQLNonNull 106 @GraphQLConnection(connectionFetcher = CountingDataConnectionFetcher.class) 107 public CountingPaginatedData<GlAccountObject> getGlAccounts(final DataFetchingEnvironment env) { 108 if(AccountingSecurityUtils.getHasGlAccountsAccess(env)) { 109 var accountingControl = Session.getModelController(AccountingControl.class); 110 var totalCount = accountingControl.countGlAccountsByGlAccountCategory(glAccountCategory); 111 112 try(var objectLimiter = new ObjectLimiter(env, GlAccountConstants.COMPONENT_VENDOR_NAME, GlAccountConstants.ENTITY_TYPE_NAME, totalCount)) { 113 var entities = accountingControl.getGlAccountsByGlAccountCategory(glAccountCategory); 114 var glAccounts = entities.stream().map(GlAccountObject::new).collect(Collectors.toCollection(() -> new ArrayList<>(entities.size()))); 115 116 return new CountedObjects<>(objectLimiter, glAccounts); 117 } 118 } else { 119 return Connections.emptyConnection(); 120 } 121 } 122 123}