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