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.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    @GraphQLNonNull
077    public OfferUseObject getDefaultOfferUse(final DataFetchingEnvironment env) {
078        return OfferSecurityUtils.getHasOfferUseAccess(env) ?
079                new OfferUseObject(getContentCollectionDetail().getDefaultOfferUse()) : null;
080    }
081
082    @GraphQLField
083    @GraphQLDescription("description")
084    @GraphQLNonNull
085    public String getDescription(final DataFetchingEnvironment env) {
086        var contentControl = Session.getModelController(ContentControl.class);
087        var userControl = Session.getModelController(UserControl.class);
088
089        return contentControl.getBestContentCollectionDescription(contentCollection, userControl.getPreferredLanguageFromUserVisit(BaseGraphQl.getUserVisit(env)));
090    }
091
092    @GraphQLField
093    @GraphQLDescription("contentCatalogs")
094    @GraphQLNonNull
095    @GraphQLConnection(connectionFetcher = CountingDataConnectionFetcher.class)
096    public CountingPaginatedData<ContentCatalogObject> getContentCatalogs(final DataFetchingEnvironment env) {
097        if(ContentSecurityUtils.getHasContentCatalogsAccess(env)) {
098            var contentControl = Session.getModelController(ContentControl.class);
099            var totalCount = contentControl.countContentCatalogsByContentCollection(contentCollection);
100
101            try(var objectLimiter = new ObjectLimiter(env, ContentCatalogConstants.COMPONENT_VENDOR_NAME, ContentCatalogConstants.ENTITY_TYPE_NAME, totalCount)) {
102                var entities = contentControl.getContentCatalogs(contentCollection);
103                var contentCatalogs = entities.stream().map(ContentCatalogObject::new).collect(Collectors.toCollection(() -> new ArrayList<>(entities.size())));
104
105                return new CountedObjects<>(objectLimiter, contentCatalogs);
106            }
107        } else {
108            return Connections.emptyConnection();
109        }
110    }
111
112    @GraphQLField
113    @GraphQLDescription("contentSections")
114    @GraphQLNonNull
115    @GraphQLConnection(connectionFetcher = CountingDataConnectionFetcher.class)
116    public CountingPaginatedData<ContentSectionObject> getContentSections(final DataFetchingEnvironment env) {
117        if(ContentSecurityUtils.getHasContentSectionsAccess(env)) {
118            var contentControl = Session.getModelController(ContentControl.class);
119            var totalCount = contentControl.countContentSectionsByContentCollection(contentCollection);
120
121            try(var objectLimiter = new ObjectLimiter(env, ContentSectionConstants.COMPONENT_VENDOR_NAME, ContentSectionConstants.ENTITY_TYPE_NAME, totalCount)) {
122                var entities = contentControl.getContentSections(contentCollection);
123                var contentSections = entities.stream().map(ContentSectionObject::new).collect(Collectors.toCollection(() -> new ArrayList<>(entities.size())));
124
125                return new CountedObjects<>(objectLimiter, contentSections);
126            }
127        } else {
128            return Connections.emptyConnection();
129        }
130    }
131
132}