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.ContentCatalogDetail;
027import com.echothree.model.data.content.server.entity.ContentCatalogItem;
028import com.echothree.model.data.content.server.entity.ContentCategory;
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 catalog object")
039@GraphQLName("ContentCatalog")
040public class ContentCatalogObject
041        extends BaseEntityInstanceObject {
042    
043    private final ContentCatalog contentCatalog; // Always Present
044    
045    public ContentCatalogObject(ContentCatalog contentCatalog) {
046        super(contentCatalog.getPrimaryKey());
047        
048        this.contentCatalog = contentCatalog;
049    }
050
051    private ContentCatalogDetail contentCatalogDetail; // Optional, use getContentCatalogDetail()
052    
053    private ContentCatalogDetail getContentCatalogDetail() {
054        if(contentCatalogDetail == null) {
055            contentCatalogDetail = contentCatalog.getLastDetail();
056        }
057        
058        return contentCatalogDetail;
059    }
060
061    @GraphQLField
062    @GraphQLDescription("content collection")
063    public ContentCollectionObject getContentCollection(final DataFetchingEnvironment env) {
064        return ContentSecurityUtils.getHasContentCollectionAccess(env) ? new ContentCollectionObject(getContentCatalogDetail().getContentCollection()) : null;
065    }
066
067    @GraphQLField
068    @GraphQLDescription("content catalog name")
069    @GraphQLNonNull
070    public String getContentCatalogName() {
071        return getContentCatalogDetail().getContentCatalogName();
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(getContentCatalogDetail().getDefaultOfferUse()) : null;
080    }
081
082    @GraphQLField
083    @GraphQLDescription("is default")
084    @GraphQLNonNull
085    public boolean getIsDefault() {
086        return getContentCatalogDetail().getIsDefault();
087    }
088    
089    @GraphQLField
090    @GraphQLDescription("sort order")
091    @GraphQLNonNull
092    public int getSortOrder() {
093        return getContentCatalogDetail().getSortOrder();
094    }
095    
096    @GraphQLField
097    @GraphQLDescription("description")
098    @GraphQLNonNull
099    public String getDescription(final DataFetchingEnvironment env) {
100        var contentControl = Session.getModelController(ContentControl.class);
101        var userControl = Session.getModelController(UserControl.class);
102
103        return contentControl.getBestContentCatalogDescription(contentCatalog, userControl.getPreferredLanguageFromUserVisit(BaseGraphQl.getUserVisit(env)));
104    }
105    
106    @GraphQLField
107    @GraphQLDescription("content categories count")
108    public Long getContentCategoriesCount(final DataFetchingEnvironment env) {
109        var contentControl = Session.getModelController(ContentControl.class);
110        
111        return ContentSecurityUtils.getHasContentCategoriesAccess(env) ? contentControl.countContentCategoriesByContentCatalog(contentCatalog) : null;
112    }
113    
114    @GraphQLField
115    @GraphQLDescription("content categories")
116    public List<ContentCategoryObject> getContentCategories(final DataFetchingEnvironment env) {
117        var contentControl = Session.getModelController(ContentControl.class);
118        List<ContentCategory> entities = ContentSecurityUtils.getHasContentCategoriesAccess(env) ? contentControl.getContentCategories(contentCatalog) : null;
119        List<ContentCategoryObject> contentCategories = entities == null ? null : new ArrayList<>(entities.size());
120        
121        if(entities != null) {
122            entities.forEach((entity) -> {
123                contentCategories.add(new ContentCategoryObject(entity));
124            });
125        }
126        
127        return contentCategories;
128    }
129    
130    @GraphQLField
131    @GraphQLDescription("content catalog items count")
132    public Long getContentCatalogItemsCount(final DataFetchingEnvironment env) {
133        var contentControl = Session.getModelController(ContentControl.class);
134        
135        return ContentSecurityUtils.getHasContentCatalogItemsAccess(env) ? contentControl.countContentCatalogItemsByContentCatalog(contentCatalog) : null;
136    }
137    
138    @GraphQLField
139    @GraphQLDescription("content catalog items")
140    public List<ContentCatalogItemObject> getContentCatalogItems(final DataFetchingEnvironment env) {
141        var contentControl = Session.getModelController(ContentControl.class);
142        List<ContentCatalogItem> entities = ContentSecurityUtils.getHasContentCatalogItemsAccess(env) ? contentControl.getContentCatalogItemsByContentCatalog(contentCatalog) : null;
143        List<ContentCatalogItemObject> contentCatalogItems = entities == null ? null : new ArrayList<>(entities.size());
144        
145        if(entities != null) {
146            entities.forEach((entity) -> {
147                contentCatalogItems.add(new ContentCatalogItemObject(entity));
148            });
149        }
150        
151        return contentCatalogItems;
152    }
153    
154}