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.item.server.control.ItemControl; 028import com.echothree.model.control.item.server.graphql.ItemObject; 029import com.echothree.model.control.item.server.graphql.ItemSecurityUtils; 030import com.echothree.model.control.user.server.control.UserControl; 031import com.echothree.model.data.accounting.common.GlAccountConstants; 032import com.echothree.model.data.accounting.server.entity.GlAccountClass; 033import com.echothree.model.data.accounting.server.entity.GlAccountClassDetail; 034import com.echothree.model.data.item.common.ItemConstants; 035import com.echothree.util.server.persistence.Session; 036import graphql.annotations.annotationTypes.GraphQLDescription; 037import graphql.annotations.annotationTypes.GraphQLField; 038import graphql.annotations.annotationTypes.GraphQLName; 039import graphql.annotations.annotationTypes.GraphQLNonNull; 040import graphql.annotations.connection.GraphQLConnection; 041import graphql.schema.DataFetchingEnvironment; 042import java.util.ArrayList; 043import java.util.stream.Collectors; 044 045@GraphQLDescription("GL account class object") 046@GraphQLName("GlAccountClass") 047public class GlAccountClassObject 048 extends BaseEntityInstanceObject { 049 050 private final GlAccountClass glAccountClass; // Always Present 051 052 public GlAccountClassObject(GlAccountClass glAccountClass) { 053 super(glAccountClass.getPrimaryKey()); 054 055 this.glAccountClass = glAccountClass; 056 } 057 058 private GlAccountClassDetail glAccountClassDetail; // Optional, use getGlAccountClassDetail() 059 060 private GlAccountClassDetail getGlAccountClassDetail() { 061 if(glAccountClassDetail == null) { 062 glAccountClassDetail = glAccountClass.getLastDetail(); 063 } 064 065 return glAccountClassDetail; 066 } 067 068 @GraphQLField 069 @GraphQLDescription("GL account class name") 070 @GraphQLNonNull 071 public String getGlAccountClassName() { 072 return getGlAccountClassDetail().getGlAccountClassName(); 073 } 074 075 @GraphQLField 076 @GraphQLDescription("parent GL account class") 077 public GlAccountClassObject getParentGlAccountClass() { 078 var parentGlAccountClass = getGlAccountClassDetail().getParentGlAccountClass(); 079 080 return parentGlAccountClass == null ? null : new GlAccountClassObject(parentGlAccountClass); 081 } 082 083 @GraphQLField 084 @GraphQLDescription("is default") 085 @GraphQLNonNull 086 public boolean getIsDefault() { 087 return getGlAccountClassDetail().getIsDefault(); 088 } 089 090 @GraphQLField 091 @GraphQLDescription("sort order") 092 @GraphQLNonNull 093 public int getSortOrder() { 094 return getGlAccountClassDetail().getSortOrder(); 095 } 096 097 @GraphQLField 098 @GraphQLDescription("description") 099 @GraphQLNonNull 100 public String getDescription(final DataFetchingEnvironment env) { 101 var accountingControl = Session.getModelController(AccountingControl.class); 102 var userControl = Session.getModelController(UserControl.class); 103 104 return accountingControl.getBestGlAccountClassDescription(glAccountClass, userControl.getPreferredLanguageFromUserVisit(BaseGraphQl.getUserVisit(env))); 105 } 106 107 @GraphQLField 108 @GraphQLDescription("GL accounts") 109 @GraphQLNonNull 110 @GraphQLConnection(connectionFetcher = CountingDataConnectionFetcher.class) 111 public CountingPaginatedData<GlAccountObject> getGlAccounts(final DataFetchingEnvironment env) { 112 if(AccountingSecurityUtils.getHasGlAccountsAccess(env)) { 113 var accountingControl = Session.getModelController(AccountingControl.class); 114 var totalCount = accountingControl.countGlAccountsByGlAccountClass(glAccountClass); 115 116 try(var objectLimiter = new ObjectLimiter(env, GlAccountConstants.COMPONENT_VENDOR_NAME, GlAccountConstants.ENTITY_TYPE_NAME, totalCount)) { 117 var entities = accountingControl.getGlAccountsByGlAccountClass(glAccountClass); 118 var glAccounts = entities.stream().map(GlAccountObject::new).collect(Collectors.toCollection(() -> new ArrayList<>(entities.size()))); 119 120 return new CountedObjects<>(objectLimiter, glAccounts); 121 } 122 } else { 123 return Connections.emptyConnection(); 124 } 125 } 126 127}