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.ContentPageLayout;
024import com.echothree.model.data.content.server.entity.ContentPageLayoutArea;
025import com.echothree.model.data.content.server.entity.ContentPageLayoutDetail;
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 page layout object")
036@GraphQLName("ContentPageLayout")
037public class ContentPageLayoutObject
038        extends BaseEntityInstanceObject {
039    
040    private final ContentPageLayout contentPageLayout; // Always Present
041    
042    public ContentPageLayoutObject(ContentPageLayout contentPageLayout) {
043        super(contentPageLayout.getPrimaryKey());
044        
045        this.contentPageLayout = contentPageLayout;
046    }
047
048    private ContentPageLayoutDetail contentPageLayoutDetail; // Optional, use getContentPageLayoutDetail()
049    
050    private ContentPageLayoutDetail getContentPageLayoutDetail() {
051        if(contentPageLayoutDetail == null) {
052            contentPageLayoutDetail = contentPageLayout.getLastDetail();
053        }
054        
055        return contentPageLayoutDetail;
056    }
057    
058    @GraphQLField
059    @GraphQLDescription("content page layout name")
060    @GraphQLNonNull
061    public String getContentPageLayoutName() {
062        return getContentPageLayoutDetail().getContentPageLayoutName();
063    }
064
065    @GraphQLField
066    @GraphQLDescription("is default")
067    @GraphQLNonNull
068    public boolean getIsDefault() {
069        return getContentPageLayoutDetail().getIsDefault();
070    }
071    
072    @GraphQLField
073    @GraphQLDescription("sort order")
074    @GraphQLNonNull
075    public int getSortOrder() {
076        return getContentPageLayoutDetail().getSortOrder();
077    }
078    
079    @GraphQLField
080    @GraphQLDescription("description")
081    @GraphQLNonNull
082    public String getDescription(final DataFetchingEnvironment env) {
083        var contentControl = Session.getModelController(ContentControl.class);
084        var userControl = Session.getModelController(UserControl.class);
085
086        return contentControl.getBestContentPageLayoutDescription(contentPageLayout, userControl.getPreferredLanguageFromUserVisit(BaseGraphQl.getUserVisit(env)));
087    }
088    
089    @GraphQLField
090    @GraphQLDescription("content page layout areas count")
091    @GraphQLNonNull
092    public Long getContentPageLayoutAreasCount(final DataFetchingEnvironment env) {
093        var contentControl = Session.getModelController(ContentControl.class);
094        
095        return contentControl.countContentPageLayoutAreasByContentPageLayout(contentPageLayout);
096    }
097    
098    @GraphQLField
099    @GraphQLDescription("content page layout areas")
100    @GraphQLNonNull
101    public List<ContentPageLayoutAreaObject> getContentPageLayoutAreas(final DataFetchingEnvironment env) {
102        var contentControl = Session.getModelController(ContentControl.class);
103        List<ContentPageLayoutArea> entities = contentControl.getContentPageLayoutAreasByContentPageLayout(contentPageLayout);
104        List<ContentPageLayoutAreaObject> contentPageLayoutAreas = new ArrayList<>(entities.size());
105        
106        entities.forEach((entity) -> {
107            contentPageLayoutAreas.add(new ContentPageLayoutAreaObject(entity));
108        });
109        
110        return contentPageLayoutAreas;
111    }
112    
113}