001/*
002 *  Copyright 2010 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.repository.WorkflowAwareContent;
023import org.ametys.plugins.repository.AmetysRepositoryException;
024import org.ametys.plugins.workflow.EnhancedFunction;
025import org.ametys.runtime.i18n.I18nizableText;
026
027import com.opensymphony.module.propertyset.PropertySet;
028import com.opensymphony.workflow.WorkflowException;
029
030/**
031 * Function setting or removing the date at which the content was proposed for
032 * validation, i.e. put in a workflow step in which it's awaiting validation.
033 */
034public class SetProposalDateContentFunction extends AbstractContentWorkflowComponent implements EnhancedFunction
035{
036    
037    /** The "remove" parameter. */
038    private static final String __REMOVE_PARAM = "remove";
039    
040    @Override
041    public void execute(Map transientVars, Map args, PropertySet ps) throws WorkflowException
042    {
043        _logger.info("Performing content proposal for validation.");
044        
045        // Retrieve current content
046        WorkflowAwareContent content = getContent(transientVars);
047        boolean remove = "true".equals(args.get(__REMOVE_PARAM));
048        
049        try
050        {
051            content.setProposalDate(remove ? null : ZonedDateTime.now());
052            content.saveChanges();
053        }
054        catch (AmetysRepositoryException e)
055        {
056            throw new WorkflowException("Unable to set the proposal date on the content", e);
057        }
058        
059    }
060    
061    @Override
062    public List<FunctionArgument> getArguments()
063    {
064        return List.of(new FunctionArgument(__REMOVE_PARAM));
065    }
066
067    @Override
068    public I18nizableText getDescription(Map<String, String> args)
069    {
070        String toRemove = args.get(__REMOVE_PARAM);
071        return ("true".equals(toRemove))
072                ? new I18nizableText("plugin.cms", "PLUGINS_CMS_SET_PROPOSAL_DATE_REMOVE_FUNCTION_DESCRIPTION")
073                : new I18nizableText("plugin.cms", "PLUGINS_CMS_SET_PROPOSAL_DATE_FUNCTION_DESCRIPTION");
074    }
075}