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.transfer.ContentCollectionTransfer;
021import com.echothree.model.control.content.server.control.ContentControl;
022import com.echothree.model.control.offer.server.control.OfferUseControl;
023import com.echothree.model.data.content.server.entity.ContentCollection;
024import com.echothree.model.data.user.server.entity.UserVisit;
025import com.echothree.util.common.transfer.ListWrapper;
026import com.echothree.util.server.persistence.Session;
027import javax.enterprise.context.RequestScoped;
028
029@RequestScoped
030public class ContentCollectionTransferCache
031        extends BaseContentTransferCache<ContentCollection, ContentCollectionTransfer> {
032
033    ContentControl contentControl = Session.getModelController(ContentControl.class);
034    OfferUseControl offerUseControl = Session.getModelController(OfferUseControl.class);
035
036    boolean includeContentCatalogs;
037    boolean includeContentForums;
038    boolean includeContentSections;
039
040    /** Creates a new instance of ContentCollectionTransferCache */
041    protected ContentCollectionTransferCache() {
042        super();
043
044        var options = session.getOptions();
045        if(options != null) {
046            includeContentCatalogs = options.contains(ContentOptions.ContentCollectionIncludeContentCatalogs);
047            includeContentForums = options.contains(ContentOptions.ContentCollectionIncludeContentForums);
048            includeContentSections = options.contains(ContentOptions.ContentCollectionIncludeContentSections);
049            setIncludeUuid(options.contains(ContentOptions.ContentCollectionIncludeUuid));
050            setIncludeEntityAttributeGroups(options.contains(ContentOptions.ContentCollectionIncludeEntityAttributeGroups));
051            setIncludeTagScopes(options.contains(ContentOptions.ContentCollectionIncludeTagScopes));
052        }
053        
054        setIncludeEntityInstance(true);
055    }
056
057    public ContentCollectionTransfer getContentCollectionTransfer(UserVisit userVisit, ContentCollection contentCollection) {
058        var contentCollectionTransfer = get(contentCollection);
059        
060        if(contentCollectionTransfer == null) {
061            var contentCollectionDetail = contentCollection.getLastDetail();
062            var contentCollectionName = contentCollectionDetail.getContentCollectionName();
063            var defaultOfferUse = offerUseControl.getOfferUseTransfer(userVisit, contentCollectionDetail.getDefaultOfferUse());
064            var description = contentControl.getBestContentCollectionDescription(contentCollection, getLanguage(userVisit));
065            
066            contentCollectionTransfer = new ContentCollectionTransfer(contentCollectionName, defaultOfferUse, description);
067            put(userVisit, contentCollection, contentCollectionTransfer);
068            
069            if(includeContentCatalogs) {
070                contentCollectionTransfer.setContentCatalogs(new ListWrapper<>(contentControl.getContentCatalogTransfers(userVisit, contentCollection)));
071            }
072
073            if(includeContentForums) {
074                contentCollectionTransfer.setContentForums(new ListWrapper<>(contentControl.getContentForumTransfers(userVisit, contentCollection)));
075            }
076
077            if(includeContentSections) {
078                contentCollectionTransfer.setContentSections(new ListWrapper<>(contentControl.getContentSectionTransfers(userVisit, contentCollection)));
079            }
080        }
081        
082        return contentCollectionTransfer;
083    }
084    
085}