001/*
002 *  Copyright 2022 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.Map;
019
020import javax.jcr.RepositoryException;
021
022import org.ametys.cms.repository.WorkflowAwareContent;
023import org.ametys.cms.repository.WorkflowAwareContentHelper;
024import org.ametys.runtime.i18n.I18nizableText;
025
026import com.opensymphony.module.propertyset.PropertySet;
027import com.opensymphony.workflow.WorkflowException;
028import com.opensymphony.workflow.spi.WorkflowEntry;
029
030/**
031 * OS workflow function to set the current step id
032 */
033public class ResetWorkflowIdFunction extends AbstractContentFunction
034{
035    @Override
036    public void execute(Map transientVars, Map args, PropertySet ps) throws WorkflowException
037    {
038        WorkflowAwareContent content = getContent(transientVars);
039        
040        try
041        {
042            // Add the lock token to the current session if the content is locked.
043            _addLockToken(content);
044            
045            // Save the current workflow ID.
046            if (content != null)
047            {
048                long workflowId = ((WorkflowEntry) transientVars.get("entry")).getId();
049                // Remove the old workflow (used on re-initialization of the workflow
050                WorkflowAwareContentHelper.removeWorkflowId(content);
051                content.setWorkflowId(workflowId);
052                content.saveChanges();
053            }
054        }
055        catch (RepositoryException e)
056        {
057            throw new WorkflowException("Unable to add a lock token to the current session.", e);
058        }
059    }
060    
061    @Override
062    public FunctionType getFunctionExecType()
063    {
064        return FunctionType.PRE;
065    }
066    
067    @Override
068    public I18nizableText getLabel()
069    {
070        return new I18nizableText("plugin.cms", "PLUGINS_CMS_RESET_WORKFLOW_ID_FUNCTION_LABEL");
071    }
072}