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.ContentCategory;
026import com.echothree.model.data.content.server.entity.ContentCategoryDetail;
027import com.echothree.model.data.content.server.entity.ContentCategoryItem;
028import com.echothree.util.server.persistence.Session;
029import graphql.annotations.annotationTypes.GraphQLDescription;
030import graphql.annotations.annotationTypes.GraphQLField;
031import graphql.annotations.annotationTypes.GraphQLName;
032import graphql.annotations.annotationTypes.GraphQLNonNull;
033import graphql.schema.DataFetchingEnvironment;
034import java.util.ArrayList;
035import java.util.List;
036
037@GraphQLDescription("content category object")
038@GraphQLName("ContentCategory")
039public class ContentCategoryObject
040        extends BaseEntityInstanceObject {
041    
042    private final ContentCategory contentCategory; // Always Present
043    
044    public ContentCategoryObject(ContentCategory contentCategory) {
045        super(contentCategory.getPrimaryKey());
046        
047        this.contentCategory = contentCategory;
048    }
049
050    private ContentCategoryDetail contentCategoryDetail; // Optional, use getContentCategoryDetail()
051    
052    private ContentCategoryDetail getContentCategoryDetail() {
053        if(contentCategoryDetail == null) {
054            contentCategoryDetail = contentCategory.getLastDetail();
055        }
056        
057        return contentCategoryDetail;
058    }
059
060    @GraphQLField
061    @GraphQLDescription("content catalog")
062    public ContentCatalogObject getContentCatalog(final DataFetchingEnvironment env) {
063        return ContentSecurityUtils.getHasContentCatalogAccess(env) ? new ContentCatalogObject(getContentCategoryDetail().getContentCatalog()) : null;
064    }
065
066    @GraphQLField
067    @GraphQLDescription("content category name")
068    @GraphQLNonNull
069    public String getContentCategoryName() {
070        return getContentCategoryDetail().getContentCategoryName();
071    }
072
073    @GraphQLField
074    @GraphQLDescription("parent content category")
075    public ContentCategoryObject getParentContentCategory(final DataFetchingEnvironment env) {
076        return ContentSecurityUtils.getHasContentCategoryAccess(env) ? new ContentCategoryObject(getContentCategoryDetail().getParentContentCategory()) : null;
077    }
078
079    @GraphQLField
080    @GraphQLDescription("default offer use")
081    public OfferUseObject getDefaultOfferUse(final DataFetchingEnvironment env) {
082        return OfferSecurityUtils.getHasOfferUseAccess(env) ?
083                new OfferUseObject(getContentCategoryDetail().getDefaultOfferUse()) : null;
084    }
085
086//    @GraphQLField
087//    @GraphQLDescription("content category item selector")
088//    public SelectorObject getContentCategoryItemSelector(final DataFetchingEnvironment env) {
089//        return SelectorSecurityUtils.getHasSelectorAccess(env) ? new SelectorObject(getContentCategoryDetail().getContentCategoryItemSelector()) : null;
090//    }
091
092    @GraphQLField
093    @GraphQLDescription("is default")
094    @GraphQLNonNull
095    public boolean getIsDefault() {
096        return getContentCategoryDetail().getIsDefault();
097    }
098    
099    @GraphQLField
100    @GraphQLDescription("sort order")
101    @GraphQLNonNull
102    public int getSortOrder() {
103        return getContentCategoryDetail().getSortOrder();
104    }
105    
106    @GraphQLField
107    @GraphQLDescription("description")
108    @GraphQLNonNull
109    public String getDescription(final DataFetchingEnvironment env) {
110        var contentControl = Session.getModelController(ContentControl.class);
111        var userControl = Session.getModelController(UserControl.class);
112
113        return contentControl.getBestContentCategoryDescription(contentCategory, userControl.getPreferredLanguageFromUserVisit(BaseGraphQl.getUserVisit(env)));
114    }
115    
116    @GraphQLField
117    @GraphQLDescription("content category items count")
118    public Long getContentCategoryItemsCount(final DataFetchingEnvironment env) {
119        var contentControl = Session.getModelController(ContentControl.class);
120        
121        return ContentSecurityUtils.getHasContentCategoryItemsAccess(env) ? contentControl.countContentCategoryItemsByContentCategory(contentCategory) : null;
122    }
123    
124    @GraphQLField
125    @GraphQLDescription("content category items")
126    public List<ContentCategoryItemObject> getContentCategoryItems(final DataFetchingEnvironment env) {
127        var contentControl = Session.getModelController(ContentControl.class);
128        List<ContentCategoryItem> entities = ContentSecurityUtils.getHasContentCategoryItemsAccess(env) ? contentControl.getContentCategoryItemsByContentCategory(contentCategory) : null;
129        List<ContentCategoryItemObject> contentCategoryItems = entities == null ? null : new ArrayList<>(entities.size());
130                
131        if(entities != null) {
132            entities.forEach((entity) -> {
133                contentCategoryItems.add(new ContentCategoryItemObject(entity));
134            });
135        }
136        
137        return contentCategoryItems;
138    }
139    
140}