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