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.Map; 020 021import org.apache.avalon.framework.service.ServiceException; 022import org.apache.avalon.framework.service.ServiceManager; 023import org.apache.avalon.framework.service.Serviceable; 024 025import org.ametys.cms.content.ContentHelper; 026import org.ametys.cms.properties.section.AbstractDefaultPropertySection; 027import org.ametys.cms.repository.Content; 028import org.ametys.cms.repository.Content.ReferencingContentsSearch; 029import org.ametys.plugins.repository.AmetysObject; 030 031/** 032 * Section to display references to the content. 033 */ 034public abstract class AbstractContentReferencerSection extends AbstractDefaultPropertySection implements Serviceable 035{ 036 private ContentHelper _contentHelper; 037 038 public void service(ServiceManager smanager) throws ServiceException 039 { 040 _contentHelper = (ContentHelper) smanager.lookup(ContentHelper.ROLE); 041 } 042 043 public boolean supports(AmetysObject ametysObject) 044 { 045 return ametysObject instanceof Content content && _contentHelper.isReferenceTable(content) == _supportReferenceTable(); 046 } 047 048 /** 049 * Determines if the current section support only reference tables or other contents except reference tables. 050 * @return <code>true</code> if it a section for reference table, <code>false</code> otherwise. 051 */ 052 protected abstract boolean _supportReferenceTable(); 053 054 @Override 055 protected Map<String, Object> buildData(AmetysObject ametysObject) 056 { 057 Content content = (Content) ametysObject; 058 ReferencingContentsSearch referencingContents = content.searchReferencingContents(100); 059 060 if (_supportReferenceTable() || referencingContents.total() > 0) 061 { 062 return _buildData(content, referencingContents); 063 } 064 065 return Map.of(); 066 } 067 068 /** 069 * Build the data from referencing contents search. 070 * @param content The content 071 * @param referencingContents The referencing contents 072 * @return the data 073 */ 074 protected Map<String, Object> _buildData(Content content, ReferencingContentsSearch referencingContents) 075 { 076 Map<String, Object> resultMap = new LinkedHashMap<>(); 077 resultMap.put("count", referencingContents.total()); 078 return resultMap; 079 } 080 081 @Override 082 protected String _getDefaultXSLT() 083 { 084 return "view:cms://stylesheets/properties/content/referencer.xsl"; 085 } 086}