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.ContentPageArea;
025import com.echothree.model.data.content.server.entity.ContentPageDetail;
026import com.echothree.model.data.content.server.entity.ContentPageLayoutArea;
027import com.echothree.model.data.party.server.entity.Language;
028import com.echothree.util.server.persistence.Session;
029import graphql.annotations.annotationTypes.GraphQLDescription;
030import graphql.annotations.annotationTypes.GraphQLField;
031import graphql.annotations.annotationTypes.GraphQLName;
032import graphql.annotations.annotationTypes.GraphQLNonNull;
033import graphql.schema.DataFetchingEnvironment;
034import java.util.ArrayList;
035import java.util.List;
036
037@GraphQLDescription("content page object")
038@GraphQLName("ContentPage")
039public class ContentPageObject
040        extends BaseEntityInstanceObject {
041    
042    private final ContentPage contentPage; // Always Present
043    
044    public ContentPageObject(ContentPage contentPage) {
045        super(contentPage.getPrimaryKey());
046        
047        this.contentPage = contentPage;
048    }
049
050    private ContentPageDetail contentPageDetail; // Optional, use getContentPageDetail()
051    
052    private ContentPageDetail getContentPageDetail() {
053        if(contentPageDetail == null) {
054            contentPageDetail = contentPage.getLastDetail();
055        }
056        
057        return contentPageDetail;
058    }
059
060    @GraphQLField
061    @GraphQLDescription("content section")
062    public ContentSectionObject getContentSection(final DataFetchingEnvironment env) {
063        return ContentSecurityUtils.getHasContentSectionAccess(env) ? new ContentSectionObject(getContentPageDetail().getContentSection()) : null;
064    }
065
066    @GraphQLField
067    @GraphQLDescription("content page name")
068    @GraphQLNonNull
069    public String getContentPageName() {
070        return getContentPageDetail().getContentPageName();
071    }
072
073    @GraphQLField
074    @GraphQLDescription("content page layout")
075    public ContentPageLayoutObject getContentPageLayout(final DataFetchingEnvironment env) {
076        return ContentSecurityUtils.getHasContentPageLayoutAccess(env) ? new ContentPageLayoutObject(getContentPageDetail().getContentPageLayout()) : null;
077    }
078
079    @GraphQLField
080    @GraphQLDescription("is default")
081    @GraphQLNonNull
082    public boolean getIsDefault() {
083        return getContentPageDetail().getIsDefault();
084    }
085    
086    @GraphQLField
087    @GraphQLDescription("sort order")
088    @GraphQLNonNull
089    public int getSortOrder() {
090        return getContentPageDetail().getSortOrder();
091    }
092    
093    @GraphQLField
094    @GraphQLDescription("description")
095    @GraphQLNonNull
096    public String getDescription(final DataFetchingEnvironment env) {
097        var contentControl = Session.getModelController(ContentControl.class);
098        var userControl = Session.getModelController(UserControl.class);
099
100        return contentControl.getBestContentPageDescription(contentPage, userControl.getPreferredLanguageFromUserVisit(BaseGraphQl.getUserVisit(env)));
101    }
102    
103    @GraphQLField
104    @GraphQLDescription("content page areas")
105    @GraphQLNonNull
106    public List<ContentPageAreaObject> getContentPageAreas(final DataFetchingEnvironment env) {
107        var contentControl = Session.getModelController(ContentControl.class);
108        var userControl = Session.getModelController(UserControl.class);
109        Language preferredLanguage = userControl.getPreferredLanguageFromUserVisit(BaseGraphQl.getUserVisit(env));
110        List<ContentPageLayoutArea> entities = contentControl.getContentPageLayoutAreasByContentPageLayout(getContentPageDetail().getContentPageLayout());
111        List<ContentPageAreaObject> contentPageAreas = new ArrayList<>(entities.size());
112        
113        entities.forEach((entity) -> {
114            ContentPageArea contentPageArea = contentControl.getBestContentPageArea(contentPage, entity, preferredLanguage);
115
116            if(contentPageArea != null) {
117                contentPageAreas.add(new ContentPageAreaObject(contentPageArea));
118            }
119        });
120        
121        return contentPageAreas;
122    }
123    
124}