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.plugins.workflow.component.WorkflowArgument;
029import org.ametys.plugins.workflow.support.WorkflowElementDefinitionHelper;
030import org.ametys.runtime.i18n.I18nizableText;
031import org.ametys.runtime.model.StaticEnumerator;
032
033import com.opensymphony.module.propertyset.PropertySet;
034import com.opensymphony.workflow.WorkflowException;
035
036/**
037 * Workflow function which marks a content archived.
038 */
039public class MarkContentArchivedFunction extends AbstractContentWorkflowComponent implements EnhancedFunction
040{
041    
042    @Override
043    public void execute(Map transientVars, Map args, PropertySet ps) throws WorkflowException
044    {
045        WorkflowAwareContent content = getContent(transientVars);
046        boolean archive = !"true".equals(args.get("unarchive"));
047        
048        if (archive)
049        {
050            // TODO CMS-9336 this metadata should be stored as internal using data holders
051            content.getMetadataHolder().setMetadata(ArchiveConstants.META_ARCHIVE_DATE, new Date());
052            
053            if (content instanceof ModifiableDataAwareVersionableAmetysObject)
054            {
055                ModifiableModelLessDataHolder unversionedMetadataHolder = ((ModifiableDataAwareVersionableAmetysObject) content).getUnversionedDataHolder();
056                
057                // Remove archive related metadata
058                unversionedMetadataHolder.removeValue(ArchiveConstants.META_ARCHIVE_SCHEDULED_DATE);
059                unversionedMetadataHolder.removeValue(AlertsConstants.SCHEDULED_ARCHIVING_REMINDER_LAST_DATE);
060            }
061        }
062        else
063        {
064            // TODO CMS-9336 this metadata should be stored as internal using data holders
065            content.getMetadataHolder().removeMetadata(ArchiveConstants.META_ARCHIVE_DATE);
066        }
067        
068        content.saveChanges();
069    }
070    
071    @Override
072    public FunctionType getFunctionExecType()
073    {
074        return FunctionType.PRE;
075    }
076
077    @SuppressWarnings("unchecked")
078    @Override
079    public List<WorkflowArgument> getArguments()
080    {
081        WorkflowArgument unarchive = WorkflowElementDefinitionHelper.getElementDefinition(
082            "unarchive", 
083            new I18nizableText("plugin.cms", "PLUGINS_CMS_MARK_CONTENT_ARCHIVED_UNARCHIVE_ARGUMENT_UNARCHIVE_LABEL"),
084            new I18nizableText("plugin.cms", "PLUGINS_CMS_MARK_CONTENT_ARCHIVED_UNARCHIVE_ARGUMENT_UNARCHIVE_DESC"),
085            false,
086            false
087        );
088        StaticEnumerator<String> booleanStaticEnumerator = new StaticEnumerator<>();
089        booleanStaticEnumerator.add(new I18nizableText("plugin.cms", "PLUGINS_CMS_MARK_CONTENT_ARCHIVED_UNARCHIVE_ARGUMENT_TRUE_LABEL"), "true");
090        booleanStaticEnumerator.add(new I18nizableText("plugin.cms", "PLUGINS_CMS_MARK_CONTENT_ARCHIVED_UNARCHIVE_ARGUMENT_FALSE_LABEL"), "false");
091        unarchive.setEnumerator(booleanStaticEnumerator);
092        unarchive.setDefaultValue("true");
093        
094        return List.of(unarchive);
095    }
096    
097    @Override
098    public I18nizableText getLabel()
099    {
100        return new I18nizableText("plugin.cms", "PLUGINS_CMS_MARK_CONTENT_ARCHIVED_FUNCTION_LABEL");
101    }
102
103    @Override
104    public I18nizableText getFullLabel(Map<String, String> argumentsValues)
105    {
106        return "true".equals(argumentsValues.get("unarchive"))
107                ? new I18nizableText("plugin.cms", "PLUGINS_CMS_MARK_CONTENT_ARCHIVED_UNARCHIVE_FUNCTION_DESCRIPTION")
108                : new I18nizableText("plugin.cms", "PLUGINS_CMS_MARK_CONTENT_ARCHIVED_FUNCTION_DESCRIPTION");
109    }
110    
111}