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.ContentPageAreaConstants;
029import com.echothree.model.data.content.server.entity.ContentPage;
030import com.echothree.model.data.content.server.entity.ContentPageDetail;
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("content page object")
042@GraphQLName("ContentPage")
043public class ContentPageObject
044        extends BaseEntityInstanceObject {
045    
046    private final ContentPage contentPage; // Always Present
047    
048    public ContentPageObject(ContentPage contentPage) {
049        super(contentPage.getPrimaryKey());
050        
051        this.contentPage = contentPage;
052    }
053
054    private ContentPageDetail contentPageDetail; // Optional, use getContentPageDetail()
055    
056    private ContentPageDetail getContentPageDetail() {
057        if(contentPageDetail == null) {
058            contentPageDetail = contentPage.getLastDetail();
059        }
060        
061        return contentPageDetail;
062    }
063
064    @GraphQLField
065    @GraphQLDescription("content section")
066    public ContentSectionObject getContentSection(final DataFetchingEnvironment env) {
067        return ContentSecurityUtils.getHasContentSectionAccess(env) ? new ContentSectionObject(getContentPageDetail().getContentSection()) : null;
068    }
069
070    @GraphQLField
071    @GraphQLDescription("content page name")
072    @GraphQLNonNull
073    public String getContentPageName() {
074        return getContentPageDetail().getContentPageName();
075    }
076
077    @GraphQLField
078    @GraphQLDescription("content page layout")
079    public ContentPageLayoutObject getContentPageLayout(final DataFetchingEnvironment env) {
080        return ContentSecurityUtils.getHasContentPageLayoutAccess(env) ? new ContentPageLayoutObject(getContentPageDetail().getContentPageLayout()) : null;
081    }
082
083    @GraphQLField
084    @GraphQLDescription("is default")
085    @GraphQLNonNull
086    public boolean getIsDefault() {
087        return getContentPageDetail().getIsDefault();
088    }
089    
090    @GraphQLField
091    @GraphQLDescription("sort order")
092    @GraphQLNonNull
093    public int getSortOrder() {
094        return getContentPageDetail().getSortOrder();
095    }
096    
097    @GraphQLField
098    @GraphQLDescription("description")
099    @GraphQLNonNull
100    public String getDescription(final DataFetchingEnvironment env) {
101        var contentControl = Session.getModelController(ContentControl.class);
102        var userControl = Session.getModelController(UserControl.class);
103
104        return contentControl.getBestContentPageDescription(contentPage, userControl.getPreferredLanguageFromUserVisit(BaseGraphQl.getUserVisit(env)));
105    }
106
107    @GraphQLField
108    @GraphQLDescription("contentPageAreas")
109    @GraphQLNonNull
110    @GraphQLConnection(connectionFetcher = CountingDataConnectionFetcher.class)
111    public CountingPaginatedData<ContentPageAreaObject> getContentPageAreas(final DataFetchingEnvironment env) {
112        if(ContentSecurityUtils.getHasContentPageAreasAccess(env)) {
113            var contentControl = Session.getModelController(ContentControl.class);
114            var totalCount = contentControl.countContentPageAreasByContentPage(contentPage);
115
116            try(var objectLimiter = new ObjectLimiter(env, ContentPageAreaConstants.COMPONENT_VENDOR_NAME, ContentPageAreaConstants.ENTITY_TYPE_NAME, totalCount)) {
117                var entities = contentControl.getContentPageAreasByContentPage(contentPage);
118                var contentPageAreas = entities.stream().map(ContentPageAreaObject::new).collect(Collectors.toCollection(() -> new ArrayList<>(entities.size())));
119                
120                return new CountedObjects<>(objectLimiter, contentPageAreas);
121            }
122        } else {
123            return Connections.emptyConnection();
124        }
125    }
126    
127}