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.offer.server.graphql.OfferSecurityUtils;
023import com.echothree.model.control.offer.server.graphql.OfferUseObject;
024import com.echothree.model.control.user.server.control.UserControl;
025import com.echothree.model.data.content.server.entity.ContentCatalog;
026import com.echothree.model.data.content.server.entity.ContentCollection;
027import com.echothree.model.data.content.server.entity.ContentCollectionDetail;
028import com.echothree.model.data.content.server.entity.ContentSection;
029import com.echothree.util.server.persistence.Session;
030import graphql.annotations.annotationTypes.GraphQLDescription;
031import graphql.annotations.annotationTypes.GraphQLField;
032import graphql.annotations.annotationTypes.GraphQLName;
033import graphql.annotations.annotationTypes.GraphQLNonNull;
034import graphql.schema.DataFetchingEnvironment;
035import java.util.ArrayList;
036import java.util.List;
037
038@GraphQLDescription("content collection object")
039@GraphQLName("ContentCollection")
040public class ContentCollectionObject
041        extends BaseEntityInstanceObject {
042    
043    private final ContentCollection contentCollection; // Always Present
044    
045    public ContentCollectionObject(ContentCollection contentCollection) {
046        super(contentCollection.getPrimaryKey());
047        
048        this.contentCollection = contentCollection;
049    }
050
051    private ContentCollectionDetail contentCollectionDetail; // Optional, use getContentCollectionDetail()
052    
053    private ContentCollectionDetail getContentCollectionDetail() {
054        if(contentCollectionDetail == null) {
055            contentCollectionDetail = contentCollection.getLastDetail();
056        }
057        
058        return contentCollectionDetail;
059    }
060    
061    @GraphQLField
062    @GraphQLDescription("content collection name")
063    @GraphQLNonNull
064    public String getContentCollectionName() {
065        return getContentCollectionDetail().getContentCollectionName();
066    }
067
068    @GraphQLField
069    @GraphQLDescription("default offer use")
070    @GraphQLNonNull
071    public OfferUseObject getDefaultOfferUse(final DataFetchingEnvironment env) {
072        return OfferSecurityUtils.getHasOfferUseAccess(env) ?
073                new OfferUseObject(getContentCollectionDetail().getDefaultOfferUse()) : null;
074    }
075
076    @GraphQLField
077    @GraphQLDescription("description")
078    @GraphQLNonNull
079    public String getDescription(final DataFetchingEnvironment env) {
080        var contentControl = Session.getModelController(ContentControl.class);
081        var userControl = Session.getModelController(UserControl.class);
082
083        return contentControl.getBestContentCollectionDescription(contentCollection, userControl.getPreferredLanguageFromUserVisit(BaseGraphQl.getUserVisit(env)));
084    }
085    
086    @GraphQLField
087    @GraphQLDescription("content catalogs count")
088    public Long getContentCatalogsCount(final DataFetchingEnvironment env) {
089        var contentControl = Session.getModelController(ContentControl.class);
090        
091        return ContentSecurityUtils.getHasContentCatalogsAccess(env) ? contentControl.countContentCatalogsByContentCollection(contentCollection) : null;
092    }
093    
094    @GraphQLField
095    @GraphQLDescription("content catalogs")
096    public List<ContentCatalogObject> getContentCatalogs(final DataFetchingEnvironment env) {
097        var contentControl = Session.getModelController(ContentControl.class);
098        List<ContentCatalog> entities = ContentSecurityUtils.getHasContentCatalogsAccess(env) ? contentControl.getContentCatalogs(contentCollection) : null;
099        List<ContentCatalogObject> contentCatalogs = entities == null ? null : new ArrayList<>(entities.size());
100        
101        if(entities != null) {
102            entities.forEach((entity) -> {
103                contentCatalogs.add(new ContentCatalogObject(entity));
104            });
105        }
106        
107        return contentCatalogs;
108    }
109    
110    @GraphQLField
111    @GraphQLDescription("content sections count")
112    public Long getContentSectionsCount(final DataFetchingEnvironment env) {
113        var contentControl = Session.getModelController(ContentControl.class);
114        
115        return ContentSecurityUtils.getHasContentSectionsAccess(env) ? contentControl.countContentSectionsByContentCollection(contentCollection) : null;
116    }
117    
118    @GraphQLField
119    @GraphQLDescription("content sections")
120    public List<ContentSectionObject> getContentSections(final DataFetchingEnvironment env) {
121        var contentControl = Session.getModelController(ContentControl.class);
122        List<ContentSection> entities = ContentSecurityUtils.getHasContentSectionsAccess(env) ? contentControl.getContentSections(contentCollection) : null;
123        List<ContentSectionObject> contentSections = entities == null ? null : new ArrayList<>(entities.size());
124        
125        if(entities != null) {
126            entities.forEach((entity) -> {
127                contentSections.add(new ContentSectionObject(entity));
128            });
129        }
130        
131        return contentSections;
132    }
133    
134}