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.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.util.BaseGraphQl;
022import com.echothree.model.control.user.server.control.UserControl;
023import com.echothree.model.data.content.server.entity.ContentPage;
024import com.echothree.model.data.content.server.entity.ContentSection;
025import com.echothree.model.data.content.server.entity.ContentSectionDetail;
026import com.echothree.util.server.persistence.Session;
027import graphql.annotations.annotationTypes.GraphQLDescription;
028import graphql.annotations.annotationTypes.GraphQLField;
029import graphql.annotations.annotationTypes.GraphQLName;
030import graphql.annotations.annotationTypes.GraphQLNonNull;
031import graphql.schema.DataFetchingEnvironment;
032import java.util.ArrayList;
033import java.util.List;
034
035@GraphQLDescription("content section object")
036@GraphQLName("ContentSection")
037public class ContentSectionObject
038        extends BaseEntityInstanceObject {
039    
040    private final ContentSection contentSection; // Always Present
041    
042    public ContentSectionObject(ContentSection contentSection) {
043        super(contentSection.getPrimaryKey());
044        
045        this.contentSection = contentSection;
046    }
047
048    private ContentSectionDetail contentSectionDetail; // Optional, use getContentSectionDetail()
049    
050    private ContentSectionDetail getContentSectionDetail() {
051        if(contentSectionDetail == null) {
052            contentSectionDetail = contentSection.getLastDetail();
053        }
054        
055        return contentSectionDetail;
056    }
057
058    @GraphQLField
059    @GraphQLDescription("content collection")
060    public ContentCollectionObject getContentCollection(final DataFetchingEnvironment env) {
061        return ContentSecurityUtils.getHasContentCollectionAccess(env) ? new ContentCollectionObject(getContentSectionDetail().getContentCollection()) : null;
062    }
063
064    @GraphQLField
065    @GraphQLDescription("content section name")
066    @GraphQLNonNull
067    public String getContentSectionName() {
068        return getContentSectionDetail().getContentSectionName();
069    }
070
071    @GraphQLField
072    @GraphQLDescription("parent content section")
073    public ContentSectionObject getParentContentSection(final DataFetchingEnvironment env) {
074        return ContentSecurityUtils.getHasContentSectionAccess(env) ? new ContentSectionObject(getContentSectionDetail().getParentContentSection()) : null;
075    }
076
077    @GraphQLField
078    @GraphQLDescription("is default")
079    @GraphQLNonNull
080    public boolean getIsDefault() {
081        return getContentSectionDetail().getIsDefault();
082    }
083    
084    @GraphQLField
085    @GraphQLDescription("sort order")
086    @GraphQLNonNull
087    public int getSortOrder() {
088        return getContentSectionDetail().getSortOrder();
089    }
090    
091    @GraphQLField
092    @GraphQLDescription("description")
093    @GraphQLNonNull
094    public String getDescription(final DataFetchingEnvironment env) {
095        var contentControl = Session.getModelController(ContentControl.class);
096        var userControl = Session.getModelController(UserControl.class);
097
098        return contentControl.getBestContentSectionDescription(contentSection, userControl.getPreferredLanguageFromUserVisit(BaseGraphQl.getUserVisit(env)));
099    }
100    
101    @GraphQLField
102    @GraphQLDescription("content pages count")
103    public Long getContentPagesCount(final DataFetchingEnvironment env) {
104        var contentControl = Session.getModelController(ContentControl.class);
105        
106        return ContentSecurityUtils.getHasContentPagesAccess(env) ? contentControl.countContentPagesByContentSection(contentSection) : null;
107    }
108    
109    @GraphQLField
110    @GraphQLDescription("content pages")
111    public List<ContentPageObject> getContentPages(final DataFetchingEnvironment env) {
112        var contentControl = Session.getModelController(ContentControl.class);
113        List<ContentPage> entities = ContentSecurityUtils.getHasContentPagesAccess(env) ? contentControl.getContentPagesByContentSection(contentSection) : null;
114        List<ContentPageObject> contentPages = entities == null ? null : new ArrayList<>(entities.size());
115        
116        if(entities != null) {
117            entities.forEach((entity) -> {
118                contentPages.add(new ContentPageObject(entity));
119            });
120        }
121        
122        return contentPages;
123    }
124    
125}