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.ametys.plugins.repository.data.holder.ModelLessDataHolder;
019import org.ametys.plugins.repository.data.holder.ModifiableModelLessDataHolder;
020
021/**
022 * Helper class which provides methods to manage reports on a object
023 */
024public final class ReportableObjectHelper
025{
026    /** Constants for the reports count attribute */
027    public static final String REPORTS_COUNT_ATTRIBUTE_NAME = "reportsCount";
028    
029    private ReportableObjectHelper()
030    {
031        // Hide the default constructor
032    }
033    
034    /**
035     * Add a report to the object
036     * @param unversionedDataHolder the unversioned data holder
037     */
038    public static void addReport(ModifiableModelLessDataHolder unversionedDataHolder)
039    {
040        long reportsCount = getReportsCount(unversionedDataHolder);
041        long increasedReportsCount = reportsCount + 1;
042        unversionedDataHolder.setValue(REPORTS_COUNT_ATTRIBUTE_NAME, increasedReportsCount);
043    }
044    
045    /**
046     * Sets the number of reports on the object
047     * @param unversionedDataHolder the unversioned data holder
048     * @param reportsCount the number of reports to set
049     */
050    public static void setReportsCount(ModifiableModelLessDataHolder unversionedDataHolder, long reportsCount)
051    {
052        unversionedDataHolder.setValue(REPORTS_COUNT_ATTRIBUTE_NAME, reportsCount);
053    }
054    
055    /**
056     * Remove all reports on the object
057     * @param unversionedMetadataHolder the unversioned data holder
058     */
059    public static void clearReports(ModifiableModelLessDataHolder unversionedMetadataHolder)
060    {
061        unversionedMetadataHolder.removeValue(REPORTS_COUNT_ATTRIBUTE_NAME);
062    }
063    
064    /**
065     * Retrieves the number of reports on the object
066     * @param unversionedDataHolder the unversioned data holder
067     * @return the number of reports
068     */
069    public static long getReportsCount(ModelLessDataHolder unversionedDataHolder)
070    {
071        return unversionedDataHolder.getValue(REPORTS_COUNT_ATTRIBUTE_NAME, 0L);
072    }
073}