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