001/*
002 *  Copyright 2011 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.workflow;
017
018import java.util.Date;
019import java.util.List;
020import java.util.Map;
021
022import org.ametys.cms.alerts.AlertsConstants;
023import org.ametys.cms.content.archive.ArchiveConstants;
024import org.ametys.cms.repository.WorkflowAwareContent;
025import org.ametys.plugins.repository.data.holder.ModifiableModelLessDataHolder;
026import org.ametys.plugins.repository.version.ModifiableDataAwareVersionableAmetysObject;
027import org.ametys.plugins.workflow.EnhancedFunction;
028import org.ametys.runtime.i18n.I18nizableText;
029
030import com.opensymphony.module.propertyset.PropertySet;
031import com.opensymphony.workflow.WorkflowException;
032
033/**
034 * Workflow function which marks a content archived.
035 */
036public class MarkContentArchivedFunction extends AbstractContentWorkflowComponent implements EnhancedFunction
037{
038    
039    @Override
040    public void execute(Map transientVars, Map args, PropertySet ps) throws WorkflowException
041    {
042        WorkflowAwareContent content = getContent(transientVars);
043        boolean archive = !"true".equals(args.get("unarchive"));
044        
045        if (archive)
046        {
047            // TODO CMS-9336 this metadata should be stored as internal using data holders
048            content.getMetadataHolder().setMetadata(ArchiveConstants.META_ARCHIVE_DATE, new Date());
049            
050            if (content instanceof ModifiableDataAwareVersionableAmetysObject)
051            {
052                ModifiableModelLessDataHolder unversionedMetadataHolder = ((ModifiableDataAwareVersionableAmetysObject) content).getUnversionedDataHolder();
053                
054                // Remove archive related metadata
055                unversionedMetadataHolder.removeValue(ArchiveConstants.META_ARCHIVE_SCHEDULED_DATE);
056                unversionedMetadataHolder.removeValue(AlertsConstants.SCHEDULED_ARCHIVING_REMINDER_LAST_DATE);
057            }
058        }
059        else
060        {
061            // TODO CMS-9336 this metadata should be stored as internal using data holders
062            content.getMetadataHolder().removeMetadata(ArchiveConstants.META_ARCHIVE_DATE);
063        }
064        
065        content.saveChanges();
066    }
067
068    @Override
069    public List<FunctionArgument> getArguments()
070    {
071        return List.of(new FunctionArgument("unarchive"));
072    }
073
074    @Override
075    public I18nizableText getDescription(Map<String, String> argumentsValues)
076    {
077        return "true".equals(argumentsValues.get("unarchive"))
078                ? new I18nizableText("plugin.cms", "PLUGINS_CMS_MARK_CONTENT_ARCHIVED_UNARCHIVE_FUNCTION_DESCRIPTION")
079                : new I18nizableText("plugin.cms", "PLUGINS_CMS_MARK_CONTENT_ARCHIVED_FUNCTION_DESCRIPTION");
080    }
081    
082}