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.metadata.ModifiableCompositeMetadata;
025import org.ametys.plugins.repository.version.ModifiableMetadataAwareVersionableAmetysObject;
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            content.getMetadataHolder().setMetadata(ArchiveConstants.META_ARCHIVE_DATE, new Date());
046            
047            if (content instanceof ModifiableMetadataAwareVersionableAmetysObject)
048            {
049                ModifiableCompositeMetadata unversionedMetadataHolder = ((ModifiableMetadataAwareVersionableAmetysObject) content).getUnversionedMetadataHolder();
050                
051                // Remove archive related metadata
052                if (unversionedMetadataHolder.hasMetadata(ArchiveConstants.META_ARCHIVE_SCHEDULED_DATE))
053                {
054                    unversionedMetadataHolder.removeMetadata(ArchiveConstants.META_ARCHIVE_SCHEDULED_DATE);
055                }
056                
057                if (unversionedMetadataHolder.hasMetadata(AlertsConstants.SCHEDULED_ARCHIVING_REMINDER_LAST_DATE))
058                {
059                    unversionedMetadataHolder.removeMetadata(AlertsConstants.SCHEDULED_ARCHIVING_REMINDER_LAST_DATE);
060                }
061            }
062        }
063        else
064        {
065            content.getMetadataHolder().removeMetadata(ArchiveConstants.META_ARCHIVE_DATE);
066        }
067        
068        content.saveChanges();
069    }
070    
071}