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.ContentPageLayoutAreaConstants;
029import com.echothree.model.data.content.server.entity.ContentPageLayout;
030import com.echothree.model.data.content.server.entity.ContentPageLayoutDetail;
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 layout object")
042@GraphQLName("ContentPageLayout")
043public class ContentPageLayoutObject
044        extends BaseEntityInstanceObject {
045    
046    private final ContentPageLayout contentPageLayout; // Always Present
047    
048    public ContentPageLayoutObject(ContentPageLayout contentPageLayout) {
049        super(contentPageLayout.getPrimaryKey());
050        
051        this.contentPageLayout = contentPageLayout;
052    }
053
054    private ContentPageLayoutDetail contentPageLayoutDetail; // Optional, use getContentPageLayoutDetail()
055    
056    private ContentPageLayoutDetail getContentPageLayoutDetail() {
057        if(contentPageLayoutDetail == null) {
058            contentPageLayoutDetail = contentPageLayout.getLastDetail();
059        }
060        
061        return contentPageLayoutDetail;
062    }
063    
064    @GraphQLField
065    @GraphQLDescription("content page layout name")
066    @GraphQLNonNull
067    public String getContentPageLayoutName() {
068        return getContentPageLayoutDetail().getContentPageLayoutName();
069    }
070
071    @GraphQLField
072    @GraphQLDescription("is default")
073    @GraphQLNonNull
074    public boolean getIsDefault() {
075        return getContentPageLayoutDetail().getIsDefault();
076    }
077    
078    @GraphQLField
079    @GraphQLDescription("sort order")
080    @GraphQLNonNull
081    public int getSortOrder() {
082        return getContentPageLayoutDetail().getSortOrder();
083    }
084    
085    @GraphQLField
086    @GraphQLDescription("description")
087    @GraphQLNonNull
088    public String getDescription(final DataFetchingEnvironment env) {
089        var contentControl = Session.getModelController(ContentControl.class);
090        var userControl = Session.getModelController(UserControl.class);
091
092        return contentControl.getBestContentPageLayoutDescription(contentPageLayout, userControl.getPreferredLanguageFromUserVisit(BaseGraphQl.getUserVisit(env)));
093    }
094
095    @GraphQLField
096    @GraphQLDescription("contentPageLayoutAreas")
097    @GraphQLNonNull
098    @GraphQLConnection(connectionFetcher = CountingDataConnectionFetcher.class)
099    public CountingPaginatedData<ContentPageLayoutAreaObject> getContentPageLayoutAreas(final DataFetchingEnvironment env) {
100        if(ContentSecurityUtils.getHasContentPageLayoutAreasAccess(env)) {
101            var contentControl = Session.getModelController(ContentControl.class);
102            var totalCount = contentControl.countContentPageLayoutAreasByContentPageLayout(contentPageLayout);
103
104            try(var objectLimiter = new ObjectLimiter(env, ContentPageLayoutAreaConstants.COMPONENT_VENDOR_NAME, ContentPageLayoutAreaConstants.ENTITY_TYPE_NAME, totalCount)) {
105                var entities = contentControl.getContentPageLayoutAreasByContentPageLayout(contentPageLayout);
106                var contentPageLayoutAreas = entities.stream().map(ContentPageLayoutAreaObject::new).collect(Collectors.toCollection(() -> new ArrayList<>(entities.size())));
107
108                return new CountedObjects<>(objectLimiter, contentPageLayoutAreas);
109            }
110        } else {
111            return Connections.emptyConnection();
112        }
113    }
114
115}