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.util.Map;
019
020import org.apache.avalon.framework.service.ServiceException;
021import org.apache.avalon.framework.service.ServiceManager;
022
023import org.ametys.cms.content.ContentHelper;
024import org.ametys.cms.repository.Content;
025import org.ametys.cms.repository.WorkflowAwareContent;
026import org.ametys.plugins.workflow.AbstractWorkflowComponent;
027
028import com.opensymphony.workflow.WorkflowException;
029
030/**
031 * Abstract class for easily retrieving environment components
032 * in a condition or a function and especially a content.
033 */
034public abstract class AbstractContentWorkflowComponent extends AbstractWorkflowComponent
035{
036    /** Constant for storing the content into the transient variables map. */
037    public static final String CONTENT_KEY = Content.class.getName();
038    
039    /** Constant for storing the content's state (has changed or not) into the transient varaibles map. */
040    public static final String HAS_CHANGED_KEY = "content.has.changed";
041
042    /** The content helper */
043    protected ContentHelper _contentHelper;
044    
045    @Override
046    public void service(ServiceManager smanager) throws ServiceException
047    {
048        super.service(smanager);
049        _contentHelper = (ContentHelper) smanager.lookup(ContentHelper.ROLE);
050    }
051    /**
052     * Retrieve the content associated with the workflow.
053     * @param transientVars the parameters from the call.
054     * @return the content.
055     * @throws WorkflowException if the content is not found.
056     */
057    protected WorkflowAwareContent getContent(Map transientVars) throws WorkflowException
058    {
059        WorkflowAwareContent content = (WorkflowAwareContent) transientVars.get(CONTENT_KEY);
060        
061        if (content == null)
062        {
063            throw new WorkflowException("Unable to retrieve content");
064        }
065        
066        // Found in transient variables map
067        return content;
068    }
069}