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.item.server.graphql; 018 019import com.echothree.model.control.graphql.server.graphql.BaseEntityInstanceObject; 020import com.echothree.model.control.graphql.server.util.BaseGraphQl; 021import com.echothree.model.control.graphql.server.util.count.ObjectLimiter; 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.item.server.control.ItemControl; 027import com.echothree.model.control.sequence.server.graphql.SequenceObject; 028import com.echothree.model.control.sequence.server.graphql.SequenceSecurityUtils; 029import com.echothree.model.control.user.server.control.UserControl; 030import com.echothree.model.data.item.common.ItemConstants; 031import com.echothree.model.data.item.server.entity.ItemCategory; 032import com.echothree.model.data.item.server.entity.ItemCategoryDetail; 033import com.echothree.util.server.persistence.Session; 034import graphql.annotations.annotationTypes.GraphQLDescription; 035import graphql.annotations.annotationTypes.GraphQLField; 036import graphql.annotations.annotationTypes.GraphQLName; 037import graphql.annotations.annotationTypes.GraphQLNonNull; 038import graphql.annotations.connection.GraphQLConnection; 039import graphql.schema.DataFetchingEnvironment; 040import java.util.ArrayList; 041import java.util.stream.Collectors; 042 043@GraphQLDescription("item category object") 044@GraphQLName("ItemCategory") 045public class ItemCategoryObject 046 extends BaseEntityInstanceObject { 047 048 private final ItemCategory itemCategory; // Always Present 049 050 public ItemCategoryObject(ItemCategory itemCategory) { 051 super(itemCategory.getPrimaryKey()); 052 053 this.itemCategory = itemCategory; 054 } 055 056 private ItemCategoryDetail itemCategoryDetail; // Optional, use getItemCategoryDetail() 057 058 private ItemCategoryDetail getItemCategoryDetail() { 059 if(itemCategoryDetail == null) { 060 itemCategoryDetail = itemCategory.getLastDetail(); 061 } 062 063 return itemCategoryDetail; 064 } 065 066 @GraphQLField 067 @GraphQLDescription("item category name") 068 @GraphQLNonNull 069 public String getItemCategoryName() { 070 return getItemCategoryDetail().getItemCategoryName(); 071 } 072 073 @GraphQLField 074 @GraphQLDescription("parent item category") 075 public ItemCategoryObject getParentItemCategory() { 076 ItemCategory parentItemCategory = getItemCategoryDetail().getParentItemCategory(); 077 078 return parentItemCategory == null ? null : new ItemCategoryObject(parentItemCategory); 079 } 080 081 @GraphQLField 082 @GraphQLDescription("item sequence") 083 public SequenceObject getItemSequence(final DataFetchingEnvironment env) { 084 if(SequenceSecurityUtils.getHasSequenceAccess(env)) { 085 var itemSequence = getItemCategoryDetail().getItemSequence(); 086 087 return itemSequence == null ? null : new SequenceObject(itemSequence); 088 } else { 089 return null; 090 } 091 } 092 093 @GraphQLField 094 @GraphQLDescription("is default") 095 @GraphQLNonNull 096 public boolean getIsDefault() { 097 return getItemCategoryDetail().getIsDefault(); 098 } 099 100 @GraphQLField 101 @GraphQLDescription("sort order") 102 @GraphQLNonNull 103 public int getSortOrder() { 104 return getItemCategoryDetail().getSortOrder(); 105 } 106 107 @GraphQLField 108 @GraphQLDescription("description") 109 @GraphQLNonNull 110 public String getDescription(final DataFetchingEnvironment env) { 111 var itemControl = Session.getModelController(ItemControl.class); 112 var userControl = Session.getModelController(UserControl.class); 113 114 return itemControl.getBestItemCategoryDescription(itemCategory, userControl.getPreferredLanguageFromUserVisit(BaseGraphQl.getUserVisit(env))); 115 } 116 117 @GraphQLField 118 @GraphQLDescription("items") 119 @GraphQLNonNull 120 @GraphQLConnection(connectionFetcher = CountingDataConnectionFetcher.class) 121 public CountingPaginatedData<ItemObject> getItems(final DataFetchingEnvironment env) { 122 if(ItemSecurityUtils.getHasItemsAccess(env)) { 123 var itemControl = Session.getModelController(ItemControl.class); 124 var totalCount = itemControl.countItemsByItemCategory(itemCategory); 125 126 try(var objectLimiter = new ObjectLimiter(env, ItemConstants.COMPONENT_VENDOR_NAME, ItemConstants.ENTITY_TYPE_NAME, totalCount)) { 127 var entities = itemControl.getItemsByItemCategory(itemCategory); 128 var items = entities.stream().map(ItemObject::new).collect(Collectors.toCollection(() -> new ArrayList<>(entities.size()))); 129 130 return new CountedObjects<>(objectLimiter, items); 131 } 132 } else { 133 return Connections.emptyConnection(); 134 } 135 } 136 137}