001/*
002 *  Copyright 2024 Anyware Services
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 */
016package org.ametys.web.properties.section.content;
017
018import java.util.LinkedHashMap;
019import java.util.Map;
020import java.util.stream.Stream;
021
022import org.ametys.cms.properties.section.AbstractDefaultPropertySection;
023import org.ametys.plugins.repository.AmetysObject;
024import org.ametys.runtime.i18n.I18nizableText;
025import org.ametys.web.repository.page.Page;
026
027/**
028 * Abstract section implementation to display referenced pages.
029 */
030public abstract class AbstractWebContentReferencerSection extends AbstractDefaultPropertySection
031{
032    @Override
033    protected Map<String, Object> buildData(AmetysObject ametysObject)
034    {
035        Map<String, Object> resultMap = new LinkedHashMap<>();
036        
037        resultMap.put("title", _getTitle());
038        resultMap.put("pages", _getPages(ametysObject).map(this::_page2JSON).toList());
039        
040        return resultMap;
041    }
042    
043    @Override
044    protected String _getDefaultXSLT()
045    {
046        return "view:web://stylesheets/properties/content/referencer-pages.xsl";
047    }
048    
049    /**
050     * Get the title of the section
051     * @return The title
052     */
053    protected abstract I18nizableText _getTitle();
054    
055    /**
056     * Get the pages to display in the section
057     * @param ametysObject The Ametys object on which we are searching pages
058     * @return The pages
059     */
060    protected abstract Stream<Page> _getPages(AmetysObject ametysObject);
061    
062    private Map<String, Object> _page2JSON(Page page)
063    {
064        return Map.of(
065            "id", page.getId(),
066            "title", page.getTitle()
067        );
068    }
069}