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.time.ZonedDateTime;
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.ModifiableDataHolder;
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            content.getInternalDataHolder().setValue(ArchiveConstants.META_ARCHIVE_DATE, ZonedDateTime.now());
051            
052            if (content instanceof ModifiableDataAwareVersionableAmetysObject)
053            {
054                ModifiableDataHolder unversionedMetadataHolder = ((ModifiableDataAwareVersionableAmetysObject) content).getUnversionedDataHolder();
055                
056                // Remove archive related metadata
057                unversionedMetadataHolder.removeValue(ArchiveConstants.META_ARCHIVE_SCHEDULED_DATE);
058                unversionedMetadataHolder.removeValue(AlertsConstants.SCHEDULED_ARCHIVING_REMINDER_LAST_DATE);
059            }
060        }
061        else
062        {
063            content.getInternalDataHolder().removeValue(ArchiveConstants.META_ARCHIVE_DATE);
064        }
065        
066        content.saveChanges();
067    }
068    
069    @Override
070    public FunctionType getFunctionExecType()
071    {
072        return FunctionType.PRE;
073    }
074
075    @SuppressWarnings("unchecked")
076    @Override
077    public List<WorkflowArgument> getArguments()
078    {
079        WorkflowArgument unarchive = WorkflowElementDefinitionHelper.getElementDefinition(
080            "unarchive", 
081            new I18nizableText("plugin.cms", "PLUGINS_CMS_MARK_CONTENT_ARCHIVED_UNARCHIVE_ARGUMENT_UNARCHIVE_LABEL"),
082            new I18nizableText("plugin.cms", "PLUGINS_CMS_MARK_CONTENT_ARCHIVED_UNARCHIVE_ARGUMENT_UNARCHIVE_DESC"),
083            false,
084            false
085        );
086        StaticEnumerator<String> booleanStaticEnumerator = new StaticEnumerator<>();
087        booleanStaticEnumerator.add(new I18nizableText("plugin.cms", "PLUGINS_CMS_MARK_CONTENT_ARCHIVED_UNARCHIVE_ARGUMENT_TRUE_LABEL"), "true");
088        booleanStaticEnumerator.add(new I18nizableText("plugin.cms", "PLUGINS_CMS_MARK_CONTENT_ARCHIVED_UNARCHIVE_ARGUMENT_FALSE_LABEL"), "false");
089        unarchive.setEnumerator(booleanStaticEnumerator);
090        unarchive.setDefaultValue("true");
091        
092        return List.of(unarchive);
093    }
094    
095    @Override
096    public I18nizableText getLabel()
097    {
098        return new I18nizableText("plugin.cms", "PLUGINS_CMS_MARK_CONTENT_ARCHIVED_FUNCTION_LABEL");
099    }
100
101    @Override
102    public I18nizableText getFullLabel(Map<String, String> argumentsValues)
103    {
104        return "true".equals(argumentsValues.get("unarchive"))
105                ? new I18nizableText("plugin.cms", "PLUGINS_CMS_MARK_CONTENT_ARCHIVED_UNARCHIVE_FUNCTION_DESCRIPTION")
106                : new I18nizableText("plugin.cms", "PLUGINS_CMS_MARK_CONTENT_ARCHIVED_FUNCTION_DESCRIPTION");
107    }
108    
109}