001/*
002 *  Copyright 2012 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.clientsideelement;
017
018import java.text.DateFormat;
019import java.util.ArrayList;
020import java.util.Date;
021import java.util.HashSet;
022import java.util.List;
023import java.util.Map;
024import java.util.Set;
025
026import org.ametys.cms.content.archive.ArchiveConstants;
027import org.ametys.cms.repository.Content;
028import org.ametys.core.ui.Callable;
029import org.ametys.core.ui.SimpleMenu;
030import org.ametys.plugins.repository.version.ModifiableMetadataAwareVersionableAmetysObject;
031import org.ametys.runtime.i18n.I18nizableText;
032
033/**
034 * Item for scheduled archiving menu, based on {@link SmartContentClientSideElement}.
035 * Some methods come from {@link SimpleMenu} 
036 */
037public class ArchiveMenuClientSideElement extends SmartContentMenu
038{
039    /** Set of the dates of each content that has a schedule archiving */
040    protected Set<Date> _scheduledDates;
041    /** The number of content with a scheduled archiving. */
042    protected int _scheduledArchivingCount;
043    
044    @Override
045    @Callable
046    public Map<String, Object> getStatus(List<String> contentsId)
047    {
048        _scheduledDates = new HashSet<>();
049        
050        Map<String, Object> results = super.getStatus(contentsId);
051        
052        if (_scheduledDates.size() > 1)
053        {
054            DateFormat df = DateFormat.getDateInstance(DateFormat.LONG);
055            results.put("scheduled-archiving-several-nearest-date", df.format(_getDateNearestToCurrent(_scheduledDates)));
056        }
057        
058        return results;
059    }
060    
061    @SuppressWarnings("unchecked")
062    @Override
063    protected boolean _isAllRight(Content content, boolean hasError, Map<String, Object> results)
064    {
065        boolean error = hasError;
066        
067        if (!results.containsKey("schedule-archive-contents"))
068        {
069            results.put("schedule-archive-contents", new ArrayList<Map<String, Object>>());
070        }
071        
072        if (_hasScheduledArchiving(content))
073        {
074            Map<String, Object> contentParams = getContentDefaultParameters(content);
075            contentParams.put("description", _getScheduledArchivingDescription (content));
076            
077            List<Map<String, Object>> scheduledContents = (List<Map<String, Object>>) results.get("schedule-archive-contents");
078            scheduledContents.add(contentParams);
079        }
080        
081        return super._isAllRight(content, error, results);
082    }
083    
084    /**
085     * Get description when user can not do action because there is already a scheduled archiving
086     * @param content The content
087     * @return The {@link I18nizableText} description
088     */
089    protected I18nizableText _getScheduledArchivingDescription (Content content)
090    {
091        List<String> scheduled18nParameters = new ArrayList<>();
092        scheduled18nParameters.add(content.getTitle());
093        
094        Date scheduledDate = ((ModifiableMetadataAwareVersionableAmetysObject) content).getUnversionedMetadataHolder().getDate(ArchiveConstants.META_ARCHIVE_SCHEDULED_DATE);
095        _scheduledDates.add(scheduledDate);
096        DateFormat df = DateFormat.getDateInstance(DateFormat.LONG);
097        scheduled18nParameters.add(df.format(scheduledDate));
098
099        I18nizableText ed = (I18nizableText) this._script.getParameters().get("scheduled-archiving-content-description");
100        return new I18nizableText(ed.getCatalogue(), ed.getKey(), scheduled18nParameters);
101    }
102    
103    /**
104     * Determines if the content has a scheduled archiving
105     * @param content the content
106     * @return true if the content has a scheduled archiving
107     */
108    protected boolean _hasScheduledArchiving(Content content)
109    {
110        if (content instanceof ModifiableMetadataAwareVersionableAmetysObject)
111        {
112            ModifiableMetadataAwareVersionableAmetysObject uContent = (ModifiableMetadataAwareVersionableAmetysObject) content;
113            return uContent.getUnversionedMetadataHolder().hasMetadata(ArchiveConstants.META_ARCHIVE_SCHEDULED_DATE);
114        }
115
116        return false;
117    }
118    
119    private Date _getDateNearestToCurrent(Set<Date> dates)
120    {
121        Date returnDate = null;
122        Date currentDate = new Date();
123
124        for (Date date : dates)
125        {
126            if (date.compareTo(currentDate) >= 0 && (returnDate == null || date.compareTo(returnDate) < 0))
127            {
128                returnDate = date;
129            }
130        }
131        return returnDate;
132    }
133}