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.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 @GraphQLNonNull 083 public OfferUseObject getDefaultOfferUse(final DataFetchingEnvironment env) { 084 return OfferSecurityUtils.getHasOfferUseAccess(env) ? 085 new OfferUseObject(getContentCatalogDetail().getDefaultOfferUse()) : null; 086 } 087 088 @GraphQLField 089 @GraphQLDescription("is default") 090 @GraphQLNonNull 091 public boolean getIsDefault() { 092 return getContentCatalogDetail().getIsDefault(); 093 } 094 095 @GraphQLField 096 @GraphQLDescription("sort order") 097 @GraphQLNonNull 098 public int getSortOrder() { 099 return getContentCatalogDetail().getSortOrder(); 100 } 101 102 @GraphQLField 103 @GraphQLDescription("description") 104 @GraphQLNonNull 105 public String getDescription(final DataFetchingEnvironment env) { 106 var contentControl = Session.getModelController(ContentControl.class); 107 var userControl = Session.getModelController(UserControl.class); 108 109 return contentControl.getBestContentCatalogDescription(contentCatalog, userControl.getPreferredLanguageFromUserVisit(BaseGraphQl.getUserVisit(env))); 110 } 111 112 @GraphQLField 113 @GraphQLDescription("content categories") 114 @GraphQLNonNull 115 @GraphQLConnection(connectionFetcher = CountingDataConnectionFetcher.class) 116 public CountingPaginatedData<ContentCategoryObject> getContentCategories(final DataFetchingEnvironment env) { 117 if(ContentSecurityUtils.getHasContentCategoriesAccess(env)) { 118 var contentControl = Session.getModelController(ContentControl.class); 119 var totalCount = contentControl.countContentCategoriesByContentCatalog(contentCatalog); 120 121 try(var objectLimiter = new ObjectLimiter(env, ContentCategoryConstants.COMPONENT_VENDOR_NAME, ContentCategoryConstants.ENTITY_TYPE_NAME, totalCount)) { 122 var entities = contentControl.getContentCategories(contentCatalog); 123 var contentCategories = entities.stream().map(ContentCategoryObject::new).collect(Collectors.toCollection(() -> new ArrayList<>(entities.size()))); 124 125 return new CountedObjects<>(objectLimiter, contentCategories); 126 } 127 } else { 128 return Connections.emptyConnection(); 129 } 130 } 131 132 @GraphQLField 133 @GraphQLDescription("content catalog items") 134 @GraphQLNonNull 135 @GraphQLConnection(connectionFetcher = CountingDataConnectionFetcher.class) 136 public CountingPaginatedData<ContentCatalogItemObject> getContentCatalogItems(final DataFetchingEnvironment env) { 137 if(ContentSecurityUtils.getHasContentCatalogItemsAccess(env)) { 138 var contentControl = Session.getModelController(ContentControl.class); 139 var totalCount = contentControl.countContentCatalogItemsByContentCatalog(contentCatalog); 140 141 try(var objectLimiter = new ObjectLimiter(env, ContentCatalogItemConstants.COMPONENT_VENDOR_NAME, ContentCatalogItemConstants.ENTITY_TYPE_NAME, totalCount)) { 142 var entities = contentControl.getContentCatalogItemsByContentCatalog(contentCatalog); 143 var contentCatalogItems = entities.stream().map(ContentCatalogItemObject::new).collect(Collectors.toCollection(() -> new ArrayList<>(entities.size()))); 144 145 return new CountedObjects<>(objectLimiter, contentCatalogItems); 146 } 147 } else { 148 return Connections.emptyConnection(); 149 } 150 } 151 152}