001// -------------------------------------------------------------------------------- 002// Copyright 2002-2026 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.content.server.graphql; 018 019import com.echothree.model.control.content.server.control.ContentControl; 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.offer.server.graphql.OfferSecurityUtils; 028import com.echothree.model.control.offer.server.graphql.OfferUseObject; 029import com.echothree.model.control.user.server.control.UserControl; 030import com.echothree.model.data.content.common.ContentCatalogItemConstants; 031import com.echothree.model.data.content.common.ContentCategoryConstants; 032import com.echothree.model.data.content.server.entity.ContentCatalog; 033import com.echothree.model.data.content.server.entity.ContentCatalogDetail; 034import com.echothree.util.server.persistence.Session; 035import graphql.annotations.annotationTypes.GraphQLDescription; 036import graphql.annotations.annotationTypes.GraphQLField; 037import graphql.annotations.annotationTypes.GraphQLName; 038import graphql.annotations.annotationTypes.GraphQLNonNull; 039import graphql.annotations.connection.GraphQLConnection; 040import graphql.schema.DataFetchingEnvironment; 041import java.util.ArrayList; 042import java.util.stream.Collectors; 043 044@GraphQLDescription("content catalog object") 045@GraphQLName("ContentCatalog") 046public class ContentCatalogObject 047 extends BaseEntityInstanceObject { 048 049 private final ContentCatalog contentCatalog; // Always Present 050 051 public ContentCatalogObject(ContentCatalog contentCatalog) { 052 super(contentCatalog.getPrimaryKey()); 053 054 this.contentCatalog = contentCatalog; 055 } 056 057 private ContentCatalogDetail contentCatalogDetail; // Optional, use getContentCatalogDetail() 058 059 private ContentCatalogDetail getContentCatalogDetail() { 060 if(contentCatalogDetail == null) { 061 contentCatalogDetail = contentCatalog.getLastDetail(); 062 } 063 064 return contentCatalogDetail; 065 } 066 067 @GraphQLField 068 @GraphQLDescription("content collection") 069 public ContentCollectionObject getContentCollection(final DataFetchingEnvironment env) { 070 return ContentSecurityUtils.getHasContentCollectionAccess(env) ? new ContentCollectionObject(getContentCatalogDetail().getContentCollection()) : null; 071 } 072 073 @GraphQLField 074 @GraphQLDescription("content catalog name") 075 @GraphQLNonNull 076 public String getContentCatalogName() { 077 return getContentCatalogDetail().getContentCatalogName(); 078 } 079 080 @GraphQLField 081 @GraphQLDescription("default offer use") 082 public OfferUseObject getDefaultOfferUse(final DataFetchingEnvironment env) { 083 return OfferSecurityUtils.getHasOfferUseAccess(env) ? 084 new OfferUseObject(getContentCatalogDetail().getDefaultOfferUse()) : null; 085 } 086 087 @GraphQLField 088 @GraphQLDescription("is default") 089 @GraphQLNonNull 090 public boolean getIsDefault() { 091 return getContentCatalogDetail().getIsDefault(); 092 } 093 094 @GraphQLField 095 @GraphQLDescription("sort order") 096 @GraphQLNonNull 097 public int getSortOrder() { 098 return getContentCatalogDetail().getSortOrder(); 099 } 100 101 @GraphQLField 102 @GraphQLDescription("description") 103 @GraphQLNonNull 104 public String getDescription(final DataFetchingEnvironment env) { 105 var contentControl = Session.getModelController(ContentControl.class); 106 var userControl = Session.getModelController(UserControl.class); 107 108 return contentControl.getBestContentCatalogDescription(contentCatalog, userControl.getPreferredLanguageFromUserVisit(BaseGraphQl.getUserVisit(env))); 109 } 110 111 @GraphQLField 112 @GraphQLDescription("content categories") 113 @GraphQLNonNull 114 @GraphQLConnection(connectionFetcher = CountingDataConnectionFetcher.class) 115 public CountingPaginatedData<ContentCategoryObject> getContentCategories(final DataFetchingEnvironment env) { 116 if(ContentSecurityUtils.getHasContentCategoriesAccess(env)) { 117 var contentControl = Session.getModelController(ContentControl.class); 118 var totalCount = contentControl.countContentCategoriesByContentCatalog(contentCatalog); 119 120 try(var objectLimiter = new ObjectLimiter(env, ContentCategoryConstants.COMPONENT_VENDOR_NAME, ContentCategoryConstants.ENTITY_TYPE_NAME, totalCount)) { 121 var entities = contentControl.getContentCategories(contentCatalog); 122 var contentCategories = entities.stream().map(ContentCategoryObject::new).collect(Collectors.toCollection(() -> new ArrayList<>(entities.size()))); 123 124 return new CountedObjects<>(objectLimiter, contentCategories); 125 } 126 } else { 127 return Connections.emptyConnection(); 128 } 129 } 130 131 @GraphQLField 132 @GraphQLDescription("content catalog items") 133 @GraphQLNonNull 134 @GraphQLConnection(connectionFetcher = CountingDataConnectionFetcher.class) 135 public CountingPaginatedData<ContentCatalogItemObject> getContentCatalogItems(final DataFetchingEnvironment env) { 136 if(ContentSecurityUtils.getHasContentCatalogItemsAccess(env)) { 137 var contentControl = Session.getModelController(ContentControl.class); 138 var totalCount = contentControl.countContentCatalogItemsByContentCatalog(contentCatalog); 139 140 try(var objectLimiter = new ObjectLimiter(env, ContentCatalogItemConstants.COMPONENT_VENDOR_NAME, ContentCatalogItemConstants.ENTITY_TYPE_NAME, totalCount)) { 141 var entities = contentControl.getContentCatalogItemsByContentCatalog(contentCatalog); 142 var contentCatalogItems = entities.stream().map(ContentCatalogItemObject::new).collect(Collectors.toCollection(() -> new ArrayList<>(entities.size()))); 143 144 return new CountedObjects<>(objectLimiter, contentCatalogItems); 145 } 146 } else { 147 return Connections.emptyConnection(); 148 } 149 } 150 151}