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.web.workflow;
017
018import java.util.List;
019import java.util.Map;
020
021import javax.jcr.RepositoryException;
022
023import org.apache.commons.collections.ListUtils;
024
025import org.ametys.cms.repository.ModifiableContent;
026import org.ametys.cms.repository.WorkflowAwareContent;
027import org.ametys.cms.workflow.AbstractContentWorkflowComponent;
028import org.ametys.plugins.repository.AmetysRepositoryException;
029import org.ametys.plugins.workflow.EnhancedFunction;
030import org.ametys.runtime.i18n.I18nizableText;
031
032import com.opensymphony.module.propertyset.PropertySet;
033import com.opensymphony.workflow.WorkflowException;
034
035/**
036 * OSWorkflow function for deleting a content.
037 */
038public class DeleteContentFunction extends AbstractContentWorkflowComponent implements EnhancedFunction
039{
040    @Override
041    public void execute(Map transientVars, Map args, PropertySet ps) throws WorkflowException
042    {
043        _logger.info("Performing content deletion");
044        
045        try
046        {
047            WorkflowAwareContent content = getContent(transientVars);
048            if (!(content instanceof ModifiableContent))
049            {
050                throw new IllegalArgumentException("The provided content " + content.getId() + " is not a ModifiableWorkflowAwareContent.");
051            }
052            
053            _removeContent((ModifiableContent) content);
054        }
055        catch (RepositoryException e)
056        {
057            throw new WorkflowException("Unable to link the workflow to the content", e);
058        }
059        catch (AmetysRepositoryException e)
060        {
061            throw new WorkflowException("Unable to delete the content", e);
062        }
063    }
064
065    /**
066     * Deletes the content.
067     * @param content the content.
068     * @throws WorkflowException if an error occurs.
069     * @throws RepositoryException if an error occurs.
070     */
071    protected void _removeContent(ModifiableContent content) throws WorkflowException, RepositoryException
072    {
073        content.remove();
074        content.saveChanges();
075    }
076    
077    @Override
078    public List<FunctionArgument> getArguments()
079    {
080        return ListUtils.EMPTY_LIST;
081    }
082
083    @Override
084    public I18nizableText getDescription(Map<String, String> args)
085    {
086        return new I18nizableText("plugin.web", "PLUGINS_WEB_DELETE_CONTENT_FUNCTION_DESCRIPTION");
087    }
088}