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.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.GlAccountClass;
030import com.echothree.model.data.accounting.server.entity.GlAccountClassDetail;
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 class object")
042@GraphQLName("GlAccountClass")
043public class GlAccountClassObject
044        extends BaseEntityInstanceObject {
045    
046    private final GlAccountClass glAccountClass; // Always Present
047    
048    public GlAccountClassObject(GlAccountClass glAccountClass) {
049        super(glAccountClass.getPrimaryKey());
050        
051        this.glAccountClass = glAccountClass;
052    }
053
054    private GlAccountClassDetail glAccountClassDetail; // Optional, use getGlAccountClassDetail()
055    
056    private GlAccountClassDetail getGlAccountClassDetail() {
057        if(glAccountClassDetail == null) {
058            glAccountClassDetail = glAccountClass.getLastDetail();
059        }
060        
061        return glAccountClassDetail;
062    }
063    
064    @GraphQLField
065    @GraphQLDescription("GL account class name")
066    @GraphQLNonNull
067    public String getGlAccountClassName() {
068        return getGlAccountClassDetail().getGlAccountClassName();
069    }
070
071    @GraphQLField
072    @GraphQLDescription("parent GL account class")
073    public GlAccountClassObject getParentGlAccountClass() {
074        var parentGlAccountClass = getGlAccountClassDetail().getParentGlAccountClass();
075        
076        return parentGlAccountClass == null ? null : new GlAccountClassObject(parentGlAccountClass);
077    }
078
079    @GraphQLField
080    @GraphQLDescription("is default")
081    @GraphQLNonNull
082    public boolean getIsDefault() {
083        return getGlAccountClassDetail().getIsDefault();
084    }
085    
086    @GraphQLField
087    @GraphQLDescription("sort order")
088    @GraphQLNonNull
089    public int getSortOrder() {
090        return getGlAccountClassDetail().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.getBestGlAccountClassDescription(glAccountClass, 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.countGlAccountsByGlAccountClass(glAccountClass);
111
112            try(var objectLimiter = new ObjectLimiter(env, GlAccountConstants.COMPONENT_VENDOR_NAME, GlAccountConstants.ENTITY_TYPE_NAME, totalCount)) {
113                var entities = accountingControl.getGlAccountsByGlAccountClass(glAccountClass);
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}