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