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.ContentPageAreaTransfer;
022import com.echothree.model.control.content.common.transfer.ContentPageTransfer;
023import com.echothree.model.control.content.server.control.ContentControl;
024import com.echothree.model.data.content.server.entity.ContentPage;
025import com.echothree.model.data.user.server.entity.UserVisit;
026import com.echothree.util.common.form.TransferProperties;
027import com.echothree.util.server.persistence.Session;
028import com.echothree.util.server.transfer.MapWrapperBuilder;
029import java.util.LinkedHashMap;
030import java.util.Map;
031import javax.enterprise.context.RequestScoped;
032
033@RequestScoped
034public class ContentPageTransferCache
035        extends BaseContentTransferCache<ContentPage, ContentPageTransfer> {
036
037    ContentControl contentControl = Session.getModelController(ContentControl.class);
038
039    boolean includeContentPageAreas;
040
041    TransferProperties transferProperties;
042    boolean filterContentSection;
043    boolean filterContentPageName;
044    boolean filterContentPageLayout;
045    boolean filterIsDefault;
046    boolean filterSortOrder;
047    boolean filterDescription;
048    boolean filterEntityInstance;
049    
050    /** Creates a new instance of ContentPageTransferCache */
051    protected ContentPageTransferCache() {
052        super();
053
054        var options = session.getOptions();
055        if(options != null) {
056            includeContentPageAreas = options.contains(ContentOptions.ContentPageIncludeContentPageAreas);
057            setIncludeUuid(options.contains(ContentOptions.ContentPageIncludeUuid));
058            setIncludeEntityAttributeGroups(options.contains(ContentOptions.ContentPageIncludeEntityAttributeGroups));
059            setIncludeTagScopes(options.contains(ContentOptions.ContentPageIncludeTagScopes));
060        }
061        
062        transferProperties = session.getTransferProperties();
063        if(transferProperties != null) {
064            var properties = transferProperties.getProperties(ContentPageTransfer.class);
065            
066            if(properties != null) {
067                filterContentSection = !properties.contains(ContentProperties.CONTENT_SECTION);
068                filterContentPageName = !properties.contains(ContentProperties.CONTENT_PAGE_NAME);
069                filterContentPageLayout = !properties.contains(ContentProperties.CONTENT_PAGE_LAYOUT);
070                filterIsDefault = !properties.contains(ContentProperties.IS_DEFAULT);
071                filterSortOrder = !properties.contains(ContentProperties.SORT_ORDER);
072                filterDescription = !properties.contains(ContentProperties.DESCRIPTION);
073                filterEntityInstance = !properties.contains(ContentProperties.ENTITY_INSTANCE);
074            }
075        }
076        
077        setIncludeEntityInstance(!filterEntityInstance);
078    }
079
080    public ContentPageTransfer getContentPageTransfer(UserVisit userVisit, ContentPage contentPage) {
081        var contentPageTransfer = get(contentPage);
082        
083        if(contentPageTransfer == null) {
084            var contentPageDetail = contentPage.getLastDetail();
085            var contentSection = filterContentSection ? null : contentControl.getContentSectionTransfer(userVisit, contentPageDetail.getContentSection());
086            var contentPageName = filterContentPageName ? null : contentPageDetail.getContentPageName();
087            var contentPageLayout = filterContentPageLayout ? null : contentControl.getContentPageLayoutTransfer(userVisit, contentPageDetail.getContentPageLayout());
088            var isDefault = filterIsDefault ? null : contentPageDetail.getIsDefault();
089            var sortOrder = filterSortOrder ? null : contentPageDetail.getSortOrder();
090            var description = filterDescription ? null : contentControl.getBestContentPageDescription(contentPage, getLanguage(userVisit));
091            
092            contentPageTransfer = new ContentPageTransfer(contentSection, contentPageName, contentPageLayout, isDefault, sortOrder, description);
093            put(userVisit, contentPage, contentPageTransfer);
094
095            if(includeContentPageAreas) {
096                var contentPageAreaTransfers = contentControl.getContentPageAreaTransfersByContentPage(userVisit, contentPage, getLanguage(userVisit));
097                Map<String, ContentPageAreaTransfer> contentPageAreas = new LinkedHashMap<>(contentPageAreaTransfers.size());
098
099                contentPageAreaTransfers.forEach((contentPageAreaTransfer) -> {
100                    contentPageAreas.put(contentPageAreaTransfer.getContentPageLayoutArea().getSortOrder().toString(), contentPageAreaTransfer);
101                });
102
103                contentPageTransfer.setContentPageAreas(MapWrapperBuilder.getInstance().filter(transferProperties, contentPageAreas));
104            }
105        }
106        
107        return contentPageTransfer;
108    }
109    
110}