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.common.ContentPageAreaTypes;
020import com.echothree.model.control.content.server.control.ContentControl;
021import com.echothree.model.control.core.server.graphql.CoreSecurityUtils;
022import com.echothree.model.control.core.server.graphql.MimeTypeObject;
023import com.echothree.model.control.graphql.server.graphql.BaseEntityInstanceObject;
024import com.echothree.model.control.party.server.graphql.LanguageObject;
025import com.echothree.model.control.party.server.graphql.PartySecurityUtils;
026import com.echothree.model.data.content.server.entity.ContentPageArea;
027import com.echothree.model.data.content.server.entity.ContentPageAreaClob;
028import com.echothree.model.data.content.server.entity.ContentPageAreaDetail;
029import com.echothree.model.data.content.server.entity.ContentPageAreaString;
030import com.echothree.model.data.content.server.entity.ContentPageAreaUrl;
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.schema.DataFetchingEnvironment;
037
038@GraphQLDescription("content page area object")
039@GraphQLName("ContentPageArea")
040public class ContentPageAreaObject
041        extends BaseEntityInstanceObject {
042    
043    private final ContentPageArea contentPageArea; // Always Present
044    
045    public ContentPageAreaObject(ContentPageArea contentPageArea) {
046        super(contentPageArea.getPrimaryKey());
047        
048        this.contentPageArea = contentPageArea;
049    }
050
051    private ContentPageAreaDetail contentPageAreaDetail; // Optional, use getContentPageAreaDetail()
052    
053    private ContentPageAreaDetail getContentPageAreaDetail() {
054        if(contentPageAreaDetail == null) {
055            contentPageAreaDetail = contentPageArea.getLastDetail();
056        }
057        
058        return contentPageAreaDetail;
059    }
060    
061    String contentPageAreaTypeName;
062
063    private String getContentPageAreaTypeName() {
064        if(contentPageAreaTypeName == null) {
065            contentPageAreaTypeName = getContentPageAreaDetail().getContentPageLayoutArea().getContentPageAreaType().getContentPageAreaTypeName();
066        }
067
068        return contentPageAreaTypeName;
069    }
070    
071    ContentPageAreaClob contentPageAreaClob;
072
073    private ContentPageAreaClob getContentPageAreaClob() {
074        if(contentPageAreaClob == null) {
075            if(getContentPageAreaTypeName().equals(ContentPageAreaTypes.CLOB.name())) {
076                var contentControl = Session.getModelController(ContentControl.class);
077
078                contentPageAreaClob = contentControl.getContentPageAreaClob(getContentPageAreaDetail());
079            }
080        }
081
082        return contentPageAreaClob;
083    }
084
085    ContentPageAreaString contentPageAreaString;
086
087    private ContentPageAreaString getContentPageAreaString() {
088        if(contentPageAreaString == null) {
089            var contentControl = Session.getModelController(ContentControl.class);
090
091            contentPageAreaString = contentControl.getContentPageAreaString(getContentPageAreaDetail());
092        }
093
094        return contentPageAreaString;
095    }
096
097    ContentPageAreaUrl contentPageAreaUrl;
098
099    private ContentPageAreaUrl getContentPageAreaUrl() {
100        if(contentPageAreaUrl == null) {
101            if(getContentPageAreaTypeName().equals(ContentPageAreaTypes.LINK.name())) {
102                var contentControl = Session.getModelController(ContentControl.class);
103
104                contentPageAreaUrl = contentControl.getContentPageAreaUrl(getContentPageAreaDetail());
105            }
106        }
107
108        return contentPageAreaUrl;
109    }
110
111    @GraphQLField
112    @GraphQLDescription("content page")
113    @GraphQLNonNull
114    public ContentPageObject getContentPage(final DataFetchingEnvironment env) {
115        return ContentSecurityUtils.getHasContentPageAccess(env) ? new ContentPageObject(getContentPageAreaDetail().getContentPage()) : null;
116    }
117
118    @GraphQLField
119    @GraphQLDescription("content page layout area")
120    @GraphQLNonNull
121    public ContentPageLayoutAreaObject getContentPageLayoutArea(final DataFetchingEnvironment env) {
122        return new ContentPageLayoutAreaObject(getContentPageAreaDetail().getContentPageLayoutArea());
123    }
124
125    @GraphQLField
126    @GraphQLDescription("language")
127    @GraphQLNonNull
128    public LanguageObject getLanguage(final DataFetchingEnvironment env) {
129        return PartySecurityUtils.getHasLanguageAccess(env) ? new LanguageObject(getContentPageAreaDetail().getLanguage()) : null;
130    }
131
132    @GraphQLField
133    @GraphQLDescription("mime type")
134    @GraphQLNonNull
135    public MimeTypeObject getMimeType(final DataFetchingEnvironment env) {
136        return CoreSecurityUtils.getHasMimeTypeAccess(env) ? new MimeTypeObject(getContentPageAreaDetail().getMimeType()) : null;
137    }
138
139    @GraphQLField
140    @GraphQLDescription("clob")
141    public String getClob(final DataFetchingEnvironment env) {
142        ContentPageAreaClob contentPageAreaClob = getContentPageAreaClob();
143        
144        return contentPageAreaClob == null ? null : contentPageAreaClob.getClob();
145    }
146
147    @GraphQLField
148    @GraphQLDescription("string")
149    public String getString(final DataFetchingEnvironment env) {
150        ContentPageAreaString contentPageAreaString = getContentPageAreaString();
151        
152        return contentPageAreaString == null ? null : contentPageAreaString.getString();
153    }
154
155    @GraphQLField
156    @GraphQLDescription("url")
157    public String getUrl(final DataFetchingEnvironment env) {
158        ContentPageAreaUrl contentPageAreaUrl = getContentPageAreaUrl();
159        
160        return contentPageAreaUrl == null ? null : contentPageAreaUrl.getUrl();
161    }
162
163}