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.ContentSectionTransfer;
022import com.echothree.model.control.content.server.control.ContentControl;
023import com.echothree.model.data.content.server.entity.ContentSection;
024import com.echothree.model.data.user.server.entity.UserVisit;
025import com.echothree.util.common.form.TransferProperties;
026import com.echothree.util.server.persistence.Session;
027import com.echothree.util.server.transfer.ListWrapperBuilder;
028import javax.enterprise.context.RequestScoped;
029
030@RequestScoped
031public class ContentSectionTransferCache
032        extends BaseContentTransferCache<ContentSection, ContentSectionTransfer> {
033
034    ContentControl contentControl = Session.getModelController(ContentControl.class);
035
036    boolean includeContentPages;
037    
038    TransferProperties transferProperties;
039    boolean filterContentCollection;
040    boolean filterContentSectionName;
041    boolean filterParentContentSection;
042    boolean filterIsDefault;
043    boolean filterSortOrder;
044    boolean filterDescription;
045    boolean filterEntityInstance;
046    
047    /** Creates a new instance of ContentSectionTransferCache */
048    protected ContentSectionTransferCache() {
049        super();
050
051        var options = session.getOptions();
052        if(options != null) {
053            includeContentPages = options.contains(ContentOptions.ContentSectionIncludeContentPages);
054            setIncludeUuid(options.contains(ContentOptions.ContentSectionIncludeUuid));
055            setIncludeEntityAttributeGroups(options.contains(ContentOptions.ContentSectionIncludeEntityAttributeGroups));
056            setIncludeTagScopes(options.contains(ContentOptions.ContentSectionIncludeTagScopes));
057        }
058        
059        transferProperties = session.getTransferProperties();
060        if(transferProperties != null) {
061            var properties = transferProperties.getProperties(ContentSectionTransfer.class);
062            
063            if(properties != null) {
064                filterContentCollection = !properties.contains(ContentProperties.CONTENT_COLLECTION);
065                filterContentSectionName = !properties.contains(ContentProperties.CONTENT_SECTION_NAME);
066                filterParentContentSection = !properties.contains(ContentProperties.PARENT_CONTENT_SECTION);
067                filterIsDefault = !properties.contains(ContentProperties.IS_DEFAULT);
068                filterSortOrder = !properties.contains(ContentProperties.SORT_ORDER);
069                filterDescription = !properties.contains(ContentProperties.DESCRIPTION);
070                filterEntityInstance = !properties.contains(ContentProperties.ENTITY_INSTANCE);
071            }
072        }
073        
074        setIncludeEntityInstance(!filterEntityInstance);
075    }
076    
077    public ContentSectionTransfer getContentSectionTransfer(UserVisit userVisit, ContentSection contentSection) {
078        var contentSectionTransfer = get(contentSection);
079        
080        if(contentSectionTransfer == null) {
081            var contentSectionDetail = contentSection.getLastDetail();
082            var contentCollectionTransfer = filterContentCollection ? null : contentControl.getContentCollectionTransfer(userVisit, contentSectionDetail.getContentCollection());
083            var contentSectionName = filterContentSectionName ? null : contentSectionDetail.getContentSectionName();
084            var parentContentSection = filterParentContentSection ? null : contentSectionDetail.getParentContentSection();
085            var parentContentSectionTransfer = parentContentSection == null ? null : contentControl.getContentSectionTransfer(userVisit, parentContentSection);
086            var isDefault = filterIsDefault ? null : contentSectionDetail.getIsDefault();
087            var sortOrder = filterSortOrder ? null : contentSectionDetail.getSortOrder();
088            var description = filterDescription ? null : contentControl.getBestContentSectionDescription(contentSection, getLanguage(userVisit));
089            
090            contentSectionTransfer = new ContentSectionTransfer(contentCollectionTransfer, contentSectionName, parentContentSectionTransfer, isDefault,
091                    sortOrder, description);
092            put(userVisit, contentSection, contentSectionTransfer);
093            
094            if(includeContentPages) {
095                contentSectionTransfer.setContentPages(ListWrapperBuilder.getInstance().filter(transferProperties, contentControl.getContentPageTransfers(userVisit, contentSectionDetail.getContentSection())));
096            }
097        }
098        
099        return contentSectionTransfer;
100    }
101    
102}