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;
017
018import java.util.HashMap;
019import java.util.Map;
020
021import org.apache.avalon.framework.configuration.Configuration;
022import org.apache.avalon.framework.configuration.ConfigurationException;
023
024import org.ametys.core.cocoon.ActionResultGenerator;
025import org.ametys.plugins.repository.AmetysObject;
026
027/**
028 * Abstract default class to define a properties section.
029 * It use a generic pipeline, the implementation has to define an XSL and the data sent to it.
030 */
031public abstract class AbstractDefaultPropertySection extends AbstractPropertySection
032{
033    private String _xslt;
034    
035    @Override
036    public void configure(Configuration configuration) throws ConfigurationException
037    {
038        super.configure(configuration);
039        _xslt = configuration.getChild("xslt").getValue(_getDefaultXSLT());
040        if (_xslt == null)
041        {
042            throw new ConfigurationException("No default XSLT defined");
043        }
044    }
045    
046    public Map<String, Object> getParameters(AmetysObject ametysObject)
047    {
048        Map<String, Object> resultMap = new HashMap<>();
049        resultMap.put(ActionResultGenerator.MAP_REQUEST_ATTR, buildData(ametysObject));
050        resultMap.put("xslt", getXSLT());
051        return resultMap;
052    }
053    
054    public String getDataURL(AmetysObject ametysObject)
055    {
056        return "cocoon://_plugins/cms/property-section";
057    }
058    
059    /**
060     * Build the data sent into the request attribute {@link ActionResultGenerator#MAP_REQUEST_ATTR}, it will be convert from JSON to XML.
061     * @param ametysObject The resolved object
062     * @return the data
063     */
064    protected abstract Map<String, Object> buildData(AmetysObject ametysObject);
065    
066    /**
067     * Path to the XSL to render the section.
068     * It is recommended to use the 'view' protocol, with a plugin, like view:myplugin://stylesheets/properties/filename.xsl
069     * @return The XSL path
070     */
071    protected String getXSLT()
072    {
073        return _xslt;
074    }
075    
076    /**
077     * The default XSL if not defined in the configuration.
078     * Not mandatory to implement this method but if there is not XSLT configured for the section, the loading will failed.
079     * @return The default XSL path
080     */
081    protected String _getDefaultXSLT()
082    {
083        return null;
084    }
085}