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.transfer;
018
019import com.echothree.model.control.content.common.ContentOptions;
020import com.echothree.model.control.content.common.ContentProperties;
021import com.echothree.model.control.content.common.transfer.ContentCatalogTransfer;
022import com.echothree.model.control.content.common.transfer.ContentCategoryTransfer;
023import com.echothree.model.control.content.server.control.ContentControl;
024import com.echothree.model.control.offer.common.transfer.OfferUseTransfer;
025import com.echothree.model.control.offer.server.control.OfferUseControl;
026import com.echothree.model.control.selector.common.transfer.SelectorTransfer;
027import com.echothree.model.control.selector.server.control.SelectorControl;
028import com.echothree.model.data.content.server.entity.ContentCategory;
029import com.echothree.model.data.content.server.entity.ContentCategoryDetail;
030import com.echothree.model.data.offer.server.entity.OfferUse;
031import com.echothree.model.data.selector.server.entity.Selector;
032import com.echothree.model.data.user.server.entity.UserVisit;
033import com.echothree.util.common.form.TransferProperties;
034import com.echothree.util.server.persistence.Session;
035import com.echothree.util.server.transfer.ListWrapperBuilder;
036import java.util.Set;
037
038public class ContentCategoryTransferCache
039        extends BaseContentTransferCache<ContentCategory, ContentCategoryTransfer> {
040
041    OfferUseControl offerUseControl = Session.getModelController(OfferUseControl.class);
042    SelectorControl selectorControl = Session.getModelController(SelectorControl.class);
043    
044    boolean includeContentCategoryItems;
045    
046    TransferProperties transferProperties;
047    boolean filterContentCatalog;
048    boolean filterContentCategoryName;
049    boolean filterParentContentCategory;
050    boolean filterDefaultOfferUse;
051    boolean filterContentCategoryItemSelector;
052    boolean filterIsDefault;
053    boolean filterSortOrder;
054    boolean filterDescription;
055    boolean filterEntityInstance;
056    
057    /** Creates a new instance of ContentCategoryTransferCache */
058    public ContentCategoryTransferCache(UserVisit userVisit, ContentControl contentControl) {
059        super(userVisit, contentControl);
060        
061        var options = session.getOptions();
062        if(options != null) {
063            includeContentCategoryItems = options.contains(ContentOptions.ContentCategoryIncludeContentCategoryItems);
064            setIncludeKey(options.contains(ContentOptions.ContentCategoryIncludeKey));
065            setIncludeGuid(options.contains(ContentOptions.ContentCategoryIncludeGuid));
066            setIncludeEntityAttributeGroups(options.contains(ContentOptions.ContentCategoryIncludeEntityAttributeGroups));
067            setIncludeTagScopes(options.contains(ContentOptions.ContentCategoryIncludeTagScopes));
068        }
069        
070        transferProperties = session.getTransferProperties();
071        if(transferProperties != null) {
072            var properties = transferProperties.getProperties(ContentCategoryTransfer.class);
073            
074            if(properties != null) {
075                filterContentCatalog = !properties.contains(ContentProperties.CONTENT_CATALOG);
076                filterContentCategoryName = !properties.contains(ContentProperties.CONTENT_CATEGORY_NAME);
077                filterParentContentCategory = !properties.contains(ContentProperties.PARENT_CONTENT_CATEGORY);
078                filterDefaultOfferUse = !properties.contains(ContentProperties.DEFAULT_OFFER_USE);
079                filterContentCategoryItemSelector = !properties.contains(ContentProperties.CONTENT_CATEGORY_ITEM_SELECTOR);
080                filterIsDefault = !properties.contains(ContentProperties.IS_DEFAULT);
081                filterSortOrder = !properties.contains(ContentProperties.SORT_ORDER);
082                filterDescription = !properties.contains(ContentProperties.DESCRIPTION);
083                filterEntityInstance = !properties.contains(ContentProperties.ENTITY_INSTANCE);
084            }
085        }
086        
087        setIncludeEntityInstance(!filterEntityInstance);
088    }
089    
090    public ContentCategoryTransfer getContentCategoryTransfer(ContentCategory contentCategory) {
091        ContentCategoryTransfer contentCategoryTransfer = get(contentCategory);
092        
093        if(contentCategoryTransfer == null) {
094            ContentCategoryDetail contentCategoryDetail = contentCategory.getLastDetail();
095            ContentCatalogTransfer contentCatalogTransfer = filterContentCatalog ? null : contentControl.getContentCatalogTransfer(userVisit, contentCategoryDetail.getContentCatalog());
096            String contentCategoryName = filterContentCategoryName ? null : contentCategoryDetail.getContentCategoryName();
097            ContentCategory parentContentCategory = filterParentContentCategory ? null : contentCategoryDetail.getParentContentCategory();
098            ContentCategoryTransfer parentContentCategoryTransfer = parentContentCategory == null ? null : contentControl.getContentCategoryTransfer(userVisit, parentContentCategory);
099            OfferUse defaultOfferUse = filterDefaultOfferUse ? null : contentCategoryDetail.getDefaultOfferUse();
100            OfferUseTransfer defaultOfferUseTransfer = defaultOfferUse == null ? null : offerUseControl.getOfferUseTransfer(userVisit, defaultOfferUse);
101            Selector contentCategoryItemSelector = filterContentCategoryItemSelector ? null : contentCategoryDetail.getContentCategoryItemSelector();
102            SelectorTransfer contentCategoryItemSelectorTransfer = contentCategoryItemSelector == null ? null : selectorControl.getSelectorTransfer(userVisit, contentCategoryItemSelector);
103            Boolean isDefault = filterIsDefault ? null : contentCategoryDetail.getIsDefault();
104            Integer sortOrder = filterSortOrder ? null : contentCategoryDetail.getSortOrder();
105            String description = filterDescription ? null : contentControl.getBestContentCategoryDescription(contentCategory, getLanguage());
106            
107            contentCategoryTransfer = new ContentCategoryTransfer(contentCatalogTransfer, contentCategoryName, parentContentCategoryTransfer,
108                    defaultOfferUseTransfer, contentCategoryItemSelectorTransfer, isDefault, sortOrder, description);
109            put(contentCategory, contentCategoryTransfer);
110
111            if(includeContentCategoryItems) {
112                contentCategoryTransfer.setContentCategoryItems(ListWrapperBuilder.getInstance().filter(transferProperties, contentControl.getContentCategoryItemTransfersByContentCategory(userVisit, contentCategoryDetail.getContentCategory())));
113            }
114        }
115        
116        return contentCategoryTransfer;
117    }
118    
119}