001/*
002 *  Copyright 2019 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.repository;
017
018import org.apache.cocoon.xml.AttributesImpl;
019import org.apache.cocoon.xml.XMLUtils;
020import org.xml.sax.ContentHandler;
021import org.xml.sax.SAXException;
022
023import org.ametys.plugins.repository.data.holder.ModelLessDataHolder;
024import org.ametys.plugins.repository.data.holder.ModifiableModelLessDataHolder;
025
026/**
027 * Helper class which provides methods to manage reports on a object
028 */
029public final class ReportableObjectHelper
030{
031    /** Constants for the reports count attribute */
032    public static final String REPORTS_COUNT_ATTRIBUTE_NAME = "reportsCount";
033    
034    private ReportableObjectHelper()
035    {
036        // Hide the default constructor
037    }
038    
039    /**
040     * Add a report to the object
041     * @param unversionedDataHolder the unversioned data holder
042     */
043    public static void addReport(ModifiableModelLessDataHolder unversionedDataHolder)
044    {
045        long reportsCount = getReportsCount(unversionedDataHolder);
046        long increasedReportsCount = reportsCount + 1;
047        unversionedDataHolder.setValue(REPORTS_COUNT_ATTRIBUTE_NAME, increasedReportsCount);
048    }
049    
050    /**
051     * Sets the number of reports on the object
052     * @param unversionedDataHolder the unversioned data holder
053     * @param reportsCount the number of reports to set
054     */
055    public static void setReportsCount(ModifiableModelLessDataHolder unversionedDataHolder, long reportsCount)
056    {
057        unversionedDataHolder.setValue(REPORTS_COUNT_ATTRIBUTE_NAME, reportsCount);
058    }
059    
060    /**
061     * Remove all reports on the object
062     * @param unversionedMetadataHolder the unversioned data holder
063     */
064    public static void clearReports(ModifiableModelLessDataHolder unversionedMetadataHolder)
065    {
066        unversionedMetadataHolder.removeValue(REPORTS_COUNT_ATTRIBUTE_NAME);
067    }
068    
069    /**
070     * Retrieves the number of reports on the object
071     * @param unversionedDataHolder the unversioned data holder
072     * @return the number of reports
073     */
074    public static long getReportsCount(ModelLessDataHolder unversionedDataHolder)
075    {
076        return unversionedDataHolder.getValue(REPORTS_COUNT_ATTRIBUTE_NAME, 0L);
077    }
078    
079    /**
080     * Generates SAX events for the given object's reports.
081     * @param reportable the {@link Content}.
082     * @param contentHandler the ContentHandler receiving SAX events.
083     * @throws SAXException if an error occurs during the SAX events generation.
084     */
085    public static void saxReports(ReportableObject reportable, ContentHandler contentHandler) throws SAXException
086    {
087        long reportsCount = reportable.getReportsCount();
088        if (reportsCount > 0)
089        {
090            AttributesImpl attrs = new AttributesImpl();
091            attrs.addCDATAAttribute("count", String.valueOf(reportsCount));
092            XMLUtils.createElement(contentHandler, "reports", attrs);
093        }
094    }
095}