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.plugins.workflow.component.WorkflowArgument;
026import org.ametys.plugins.workflow.support.WorkflowElementDefinitionHelper;
027import org.ametys.runtime.i18n.I18nizableText;
028import org.ametys.runtime.model.StaticEnumerator;
029
030import com.opensymphony.module.propertyset.PropertySet;
031import com.opensymphony.workflow.WorkflowException;
032
033/**
034 * Function setting or removing the date at which the content was proposed for
035 * validation, i.e. put in a workflow step in which it's awaiting validation.
036 */
037public class SetProposalDateContentFunction extends AbstractContentWorkflowComponent implements EnhancedFunction
038{
039    
040    /** The "remove" parameter. */
041    private static final String __REMOVE_PARAM = "remove";
042    
043    @Override
044    public void execute(Map transientVars, Map args, PropertySet ps) throws WorkflowException
045    {
046        _logger.info("Performing content proposal for validation.");
047        
048        // Retrieve current content
049        WorkflowAwareContent content = getContent(transientVars);
050        boolean remove = "true".equals(args.get(__REMOVE_PARAM));
051        
052        try
053        {
054            content.setProposalDate(remove ? null : ZonedDateTime.now());
055            content.saveChanges();
056        }
057        catch (AmetysRepositoryException e)
058        {
059            throw new WorkflowException("Unable to set the proposal date on the content", e);
060        }
061        
062    }
063    
064    @Override
065    public FunctionType getFunctionExecType()
066    {
067        return FunctionType.PRE;
068    }
069    
070    @SuppressWarnings("unchecked")
071    @Override
072    public List<WorkflowArgument> getArguments()
073    {
074        WorkflowArgument remove = WorkflowElementDefinitionHelper.getElementDefinition(
075            __REMOVE_PARAM,
076            new I18nizableText("plugin.cms", "PLUGINS_CMS_SET_PROPOSAL_DATE_REMOVE_ARGUMENT_REMOVE_LABEL"),
077            new I18nizableText("plugin.cms", "PLUGINS_CMS_SET_PROPOSAL_DATE_REMOVE_ARGUMENT_REMOVE_DESCRIPTION"),
078            false,
079            false
080        );
081        StaticEnumerator<String> booleanStaticEnumerator = new StaticEnumerator<>();
082        booleanStaticEnumerator.add(new I18nizableText("plugin.cms", "PLUGINS_CMS_SET_PROPOSAL_DATE_REMOVE_ARGUMENT_REMOVE_TRUE_LABEL"), "true");
083        booleanStaticEnumerator.add(new I18nizableText("plugin.cms", "PLUGINS_CMS_SET_PROPOSAL_DATE_REMOVE_ARGUMENT_REMOVE_FALSE_LABEL"), "false");
084        remove.setEnumerator(booleanStaticEnumerator);
085        remove.setDefaultValue("true");
086        return List.of(remove);
087    }
088
089    public I18nizableText getLabel()
090    {
091        return new I18nizableText("plugin.cms", "PLUGINS_CMS_SET_PROPOSAL_DATE_FUNCTION_LABEL");
092    }
093    
094    @Override
095    public I18nizableText getFullLabel(Map<String, String> args)
096    {
097        String toRemove = args.get(__REMOVE_PARAM);
098        return ("true".equals(toRemove))
099                ? new I18nizableText("plugin.cms", "PLUGINS_CMS_SET_PROPOSAL_DATE_REMOVE_FUNCTION_DESCRIPTION")
100                : new I18nizableText("plugin.cms", "PLUGINS_CMS_SET_PROPOSAL_DATE_FUNCTION_DESCRIPTION");
101    }
102}