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.Map;
020
021import org.ametys.cms.alerts.AlertsConstants;
022import org.ametys.cms.content.archive.ArchiveConstants;
023import org.ametys.cms.repository.WorkflowAwareContent;
024import org.ametys.plugins.repository.data.holder.ModifiableModelLessDataHolder;
025import org.ametys.plugins.repository.version.ModifiableDataAwareVersionableAmetysObject;
026
027import com.opensymphony.module.propertyset.PropertySet;
028import com.opensymphony.workflow.FunctionProvider;
029import com.opensymphony.workflow.WorkflowException;
030
031/**
032 * Workflow function which marks a content archived.
033 */
034public class MarkContentArchivedFunction extends AbstractContentWorkflowComponent implements FunctionProvider
035{
036    
037    @Override
038    public void execute(Map transientVars, Map args, PropertySet ps) throws WorkflowException
039    {
040        WorkflowAwareContent content = getContent(transientVars);
041        boolean archive = !"true".equals(args.get("unarchive"));
042        
043        if (archive)
044        {
045            // TODO CMS-9336 this metadata should be stored as internal using data holders
046            content.getMetadataHolder().setMetadata(ArchiveConstants.META_ARCHIVE_DATE, new Date());
047            
048            if (content instanceof ModifiableDataAwareVersionableAmetysObject)
049            {
050                ModifiableModelLessDataHolder unversionedMetadataHolder = ((ModifiableDataAwareVersionableAmetysObject) content).getUnversionedDataHolder();
051                
052                // Remove archive related metadata
053                unversionedMetadataHolder.removeValue(ArchiveConstants.META_ARCHIVE_SCHEDULED_DATE);
054                unversionedMetadataHolder.removeValue(AlertsConstants.SCHEDULED_ARCHIVING_REMINDER_LAST_DATE);
055            }
056        }
057        else
058        {
059            // TODO CMS-9336 this metadata should be stored as internal using data holders
060            content.getMetadataHolder().removeMetadata(ArchiveConstants.META_ARCHIVE_DATE);
061        }
062        
063        content.saveChanges();
064    }
065    
066}