001/*
002 *  Copyright 2011 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.odf.workflow;
017
018import java.util.Map;
019
020import org.apache.avalon.framework.service.ServiceException;
021import org.apache.avalon.framework.service.ServiceManager;
022import org.apache.commons.lang.StringUtils;
023
024import org.ametys.cms.repository.ModifiableWorkflowAwareContent;
025import org.ametys.cms.workflow.CreateContentFunction;
026import org.ametys.odf.ODFHelper;
027import org.ametys.plugins.repository.ModifiableTraversableAmetysObject;
028import org.ametys.plugins.repository.collection.AmetysObjectCollection;
029import org.ametys.runtime.config.Config;
030
031import com.opensymphony.module.propertyset.PropertySet;
032import com.opensymphony.workflow.WorkflowException;
033
034/**
035 * Abstract OSWorkflow function for creating a ODF content.
036 */
037public abstract class AbstractCreateODFContentFunction extends CreateContentFunction
038{
039    /** Constant for storing the catalog name to use into the transient variables map. */
040    public static final String CONTENT_CATALOG_KEY = AbstractCreateODFContentFunction.class.getName() + "$catalog";
041    
042    /** ODF helper */
043    protected ODFHelper _odfHelper;
044    
045    @Override
046    public void service(ServiceManager manager) throws ServiceException
047    {
048        super.service(manager);
049        _odfHelper = (ODFHelper) manager.lookup(ODFHelper.ROLE);
050    }
051    
052    @Override
053    protected AmetysObjectCollection< ? , ModifiableWorkflowAwareContent> _getContentRoot(Map transientVars) throws WorkflowException
054    {
055        return _odfHelper.getRootContent(true);
056    }
057    
058    @Override
059    public void execute(Map transientVars, Map args, PropertySet ps) throws WorkflowException
060    {
061        if (StringUtils.isEmpty((String) transientVars.get(CONTENT_LANGUAGE_KEY)))
062        {
063            // Override the content language with the odf main language
064            String odfLang = Config.getInstance().getValue("odf.programs.lang");
065            transientVars.put(CONTENT_LANGUAGE_KEY, odfLang);
066        }
067        
068        super.execute(transientVars, args, ps);
069    }
070    
071    @Override
072    protected ModifiableWorkflowAwareContent _createContent(Map transientVars, Map args, String desiredContentName, ModifiableTraversableAmetysObject contentsNode)
073    {
074        String baseName = desiredContentName;
075        String prefix = _getContentNamePrefix();
076        if (!baseName.startsWith(prefix))
077        {
078            baseName = prefix + baseName;
079        }
080        
081        return super._createContent(transientVars, args, baseName, contentsNode);
082    }
083    
084    /**
085     * Get the prefix for content name
086     * @return the prefix
087     */
088    protected abstract String _getContentNamePrefix ();
089}