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