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.user.server.control.UserControl; 028import com.echothree.model.data.content.common.ContentPageConstants; 029import com.echothree.model.data.content.common.ContentSectionConstants; 030import com.echothree.model.data.content.server.entity.ContentSection; 031import com.echothree.model.data.content.server.entity.ContentSectionDetail; 032import com.echothree.util.server.persistence.Session; 033import graphql.annotations.annotationTypes.GraphQLDescription; 034import graphql.annotations.annotationTypes.GraphQLField; 035import graphql.annotations.annotationTypes.GraphQLName; 036import graphql.annotations.annotationTypes.GraphQLNonNull; 037import graphql.annotations.connection.GraphQLConnection; 038import graphql.schema.DataFetchingEnvironment; 039import java.util.ArrayList; 040import java.util.stream.Collectors; 041 042@GraphQLDescription("content section object") 043@GraphQLName("ContentSection") 044public class ContentSectionObject 045 extends BaseEntityInstanceObject { 046 047 private final ContentSection contentSection; // Always Present 048 049 public ContentSectionObject(ContentSection contentSection) { 050 super(contentSection.getPrimaryKey()); 051 052 this.contentSection = contentSection; 053 } 054 055 private ContentSectionDetail contentSectionDetail; // Optional, use getContentSectionDetail() 056 057 private ContentSectionDetail getContentSectionDetail() { 058 if(contentSectionDetail == null) { 059 contentSectionDetail = contentSection.getLastDetail(); 060 } 061 062 return contentSectionDetail; 063 } 064 065 @GraphQLField 066 @GraphQLDescription("content collection") 067 public ContentCollectionObject getContentCollection(final DataFetchingEnvironment env) { 068 return ContentSecurityUtils.getHasContentCollectionAccess(env) ? new ContentCollectionObject(getContentSectionDetail().getContentCollection()) : null; 069 } 070 071 @GraphQLField 072 @GraphQLDescription("content section name") 073 @GraphQLNonNull 074 public String getContentSectionName() { 075 return getContentSectionDetail().getContentSectionName(); 076 } 077 078 @GraphQLField 079 @GraphQLDescription("parent content section") 080 public ContentSectionObject getParentContentSection(final DataFetchingEnvironment env) { 081 var parentContentSection = getContentSectionDetail().getParentContentSection(); 082 083 return parentContentSection == null ? null : 084 ContentSecurityUtils.getHasContentSectionAccess(env) ? 085 new ContentSectionObject(parentContentSection) : null; 086 } 087 088 @GraphQLField 089 @GraphQLDescription("is default") 090 @GraphQLNonNull 091 public boolean getIsDefault() { 092 return getContentSectionDetail().getIsDefault(); 093 } 094 095 @GraphQLField 096 @GraphQLDescription("sort order") 097 @GraphQLNonNull 098 public int getSortOrder() { 099 return getContentSectionDetail().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.getBestContentSectionDescription(contentSection, userControl.getPreferredLanguageFromUserVisit(BaseGraphQl.getUserVisit(env))); 110 } 111 112 @GraphQLField 113 @GraphQLDescription("contentPages") 114 @GraphQLNonNull 115 @GraphQLConnection(connectionFetcher = CountingDataConnectionFetcher.class) 116 public CountingPaginatedData<ContentPageObject> getContentPages(final DataFetchingEnvironment env) { 117 if(ContentSecurityUtils.getHasContentPagesAccess(env)) { 118 var contentControl = Session.getModelController(ContentControl.class); 119 var totalCount = contentControl.countContentPagesByContentSection(contentSection); 120 121 try(var objectLimiter = new ObjectLimiter(env, ContentPageConstants.COMPONENT_VENDOR_NAME, ContentPageConstants.ENTITY_TYPE_NAME, totalCount)) { 122 var entities = contentControl.getContentPagesByContentSection(contentSection); 123 var contentPages = entities.stream().map(ContentPageObject::new).collect(Collectors.toCollection(() -> new ArrayList<>(entities.size()))); 124 125 return new CountedObjects<>(objectLimiter, contentPages); 126 } 127 } else { 128 return Connections.emptyConnection(); 129 } 130 } 131 132}