001// --------------------------------------------------------------------------------
002// Copyright 2002-2024 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.ContentPageLayoutTransfer;
023import com.echothree.model.control.content.common.transfer.ContentPageTransfer;
024import com.echothree.model.control.content.common.transfer.ContentSectionTransfer;
025import com.echothree.model.control.content.server.control.ContentControl;
026import com.echothree.model.data.content.server.entity.ContentPage;
027import com.echothree.model.data.content.server.entity.ContentPageDetail;
028import com.echothree.model.data.user.server.entity.UserVisit;
029import com.echothree.util.common.form.TransferProperties;
030import com.echothree.util.server.transfer.MapWrapperBuilder;
031import java.util.LinkedHashMap;
032import java.util.List;
033import java.util.Map;
034import java.util.Set;
035
036public class ContentPageTransferCache
037        extends BaseContentTransferCache<ContentPage, ContentPageTransfer> {
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    public ContentPageTransferCache(UserVisit userVisit, ContentControl contentControl) {
052        super(userVisit, contentControl);
053
054        var options = session.getOptions();
055        if(options != null) {
056            includeContentPageAreas = options.contains(ContentOptions.ContentPageIncludeContentPageAreas);
057            setIncludeKey(options.contains(ContentOptions.ContentPageIncludeKey));
058            setIncludeGuid(options.contains(ContentOptions.ContentPageIncludeGuid));
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(ContentPage contentPage) {
082        ContentPageTransfer contentPageTransfer = get(contentPage);
083        
084        if(contentPageTransfer == null) {
085            ContentPageDetail contentPageDetail = contentPage.getLastDetail();
086            ContentSectionTransfer contentSection = filterContentSection ? null : contentControl.getContentSectionTransfer(userVisit, contentPageDetail.getContentSection());
087            String contentPageName = filterContentPageName ? null : contentPageDetail.getContentPageName();
088            ContentPageLayoutTransfer contentPageLayout = filterContentPageLayout ? null : contentControl.getContentPageLayoutTransfer(userVisit, contentPageDetail.getContentPageLayout());
089            Boolean isDefault = filterIsDefault ? null : contentPageDetail.getIsDefault();
090            Integer sortOrder = filterSortOrder ? null : contentPageDetail.getSortOrder();
091            String description = filterDescription ? null : contentControl.getBestContentPageDescription(contentPage, getLanguage());
092            
093            contentPageTransfer = new ContentPageTransfer(contentSection, contentPageName, contentPageLayout, isDefault, sortOrder, description);
094            put(contentPage, contentPageTransfer);
095
096            if(includeContentPageAreas) {
097                List<ContentPageAreaTransfer> contentPageAreaTransfers = contentControl.getContentPageAreaTransfersByContentPage(userVisit, contentPage, getLanguage());
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}