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