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.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.server.control.OfferUseControl;
025import com.echothree.model.data.content.server.entity.ContentCatalog;
026import com.echothree.model.data.user.server.entity.UserVisit;
027import com.echothree.util.common.form.TransferProperties;
028import com.echothree.util.common.transfer.ListWrapper;
029import com.echothree.util.server.persistence.Session;
030import javax.enterprise.context.RequestScoped;
031
032@RequestScoped
033public class ContentCatalogTransferCache
034        extends BaseContentTransferCache<ContentCatalog, ContentCatalogTransfer> {
035
036    ContentControl contentControl = Session.getModelController(ContentControl.class);
037    OfferUseControl offerUseControl = Session.getModelController(OfferUseControl.class);
038    
039    boolean includeContentCatalogItems;
040    boolean includeContentCatalogCategories;
041
042    TransferProperties transferProperties;
043    boolean filterContentCollection;
044    boolean filterContentCatalogName;
045    boolean filterDefaultOfferUse;
046    boolean filterIsDefault;
047    boolean filterSortOrder;
048    boolean filterDescription;
049    boolean filterEntityInstance;
050    
051    /** Creates a new instance of ContentCatalogTransferCache */
052    protected ContentCatalogTransferCache() {
053        super();
054    
055        var options = session.getOptions();
056        if(options != null) {
057            includeContentCatalogItems = options.contains(ContentOptions.ContentCatalogIncludeContentCatalogItems);
058            includeContentCatalogCategories = options.contains(ContentOptions.ContentCatalogIncludeContentCatalogCategories);
059            setIncludeUuid(options.contains(ContentOptions.ContentCatalogIncludeUuid));
060            setIncludeEntityAttributeGroups(options.contains(ContentOptions.ContentCatalogIncludeEntityAttributeGroups));
061            setIncludeTagScopes(options.contains(ContentOptions.ContentCatalogIncludeTagScopes));
062        }
063        
064        
065        transferProperties = session.getTransferProperties();
066        if(transferProperties != null) {
067            var properties = transferProperties.getProperties(ContentCategoryTransfer.class);
068            
069            if(properties != null) {
070                filterContentCollection = !properties.contains(ContentProperties.CONTENT_COLLECTION);
071                filterContentCatalogName = !properties.contains(ContentProperties.CONTENT_CATALOG_NAME);
072                filterDefaultOfferUse = !properties.contains(ContentProperties.DEFAULT_OFFER_USE);
073                filterIsDefault = !properties.contains(ContentProperties.IS_DEFAULT);
074                filterSortOrder = !properties.contains(ContentProperties.SORT_ORDER);
075                filterDescription = !properties.contains(ContentProperties.DESCRIPTION);
076                filterEntityInstance = !properties.contains(ContentProperties.ENTITY_INSTANCE);
077            }
078        }
079        
080        setIncludeEntityInstance(!filterEntityInstance);
081    }
082
083    public ContentCatalogTransfer getContentCatalogTransfer(UserVisit userVisit, ContentCatalog contentCatalog) {
084        var contentCatalogTransfer = get(contentCatalog);
085        
086        if(contentCatalogTransfer == null) {
087            var contentCatalogDetail = contentCatalog.getLastDetail();
088            var contentCollectionTransfer = filterContentCollection ? null : contentControl.getContentCollectionTransfer(userVisit, contentCatalogDetail.getContentCollection());
089            var contentCatalogName = filterContentCatalogName ? null : contentCatalogDetail.getContentCatalogName();
090            var defaultOfferUse = filterDefaultOfferUse ? null : contentCatalogDetail.getDefaultOfferUse();
091            var defaultOfferUseTransfer = defaultOfferUse == null ? null : offerUseControl.getOfferUseTransfer(userVisit, defaultOfferUse);
092            var isDefault = filterIsDefault ? null : contentCatalogDetail.getIsDefault();
093            var sortOrder = filterSortOrder ? null : contentCatalogDetail.getSortOrder();
094            var description = filterDescription ? null : contentControl.getBestContentCatalogDescription(contentCatalog, getLanguage(userVisit));
095            
096            contentCatalogTransfer = new ContentCatalogTransfer(contentCollectionTransfer, contentCatalogName, defaultOfferUseTransfer, isDefault, sortOrder,
097                    description);
098            put(userVisit, contentCatalog, contentCatalogTransfer);
099            
100            if(includeContentCatalogItems) {
101                contentCatalogTransfer.setContentCatalogItems(new ListWrapper<>(contentControl.getContentCatalogItemTransfers(userVisit, contentCatalog)));
102            }
103
104            if(includeContentCatalogCategories) {
105                contentCatalogTransfer.setContentCategories(new ListWrapper<>(contentControl.getContentCategoryTransfers(userVisit, contentCatalog)));
106            }
107        }
108        
109        return contentCatalogTransfer;
110    }
111    
112}