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.cms.properties.section.content;
017
018import java.util.LinkedHashMap;
019import java.util.Locale;
020import java.util.Map;
021import java.util.Optional;
022
023import org.apache.commons.lang3.LocaleUtils;
024
025import org.ametys.cms.repository.Content;
026import org.ametys.cms.repository.Content.ReferencingContentsSearch;
027
028/**
029 * Section to display references to the content.
030 */
031public class ContentReferencerSection extends AbstractContentReferencerSection
032{
033    @Override
034    protected boolean _supportReferenceTable()
035    {
036        return false;
037    }
038    
039    @Override
040    protected Map<String, Object> _buildData(Content content, ReferencingContentsSearch referencingContents)
041    {
042        Map<String, Object> resultMap = new LinkedHashMap<>(super._buildData(content, referencingContents));
043
044        Locale locale = Optional.of(content)
045            .map(Content::getLanguage)
046            .map(LocaleUtils::toLocale)
047            .orElse(null);
048        
049        resultMap.put(
050            "referencingContents",
051            referencingContents.referencingContents()
052                .stream()
053                .map(c -> _content2JSON(c, locale))
054                .toList()
055        );
056        
057        if (referencingContents.hasOtherReferences())
058        {
059            resultMap.put("hasOther", true);
060        }
061        
062        return resultMap;
063    }
064    
065    private Map<String, Object> _content2JSON(Content content, Locale locale)
066    {
067        return Map.of(
068            "id", content.getId(),
069            "title", content.getTitle(locale)
070        );
071    }
072}