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.FilterNameHelper;
025import org.ametys.cms.repository.ModifiableWorkflowAwareContent;
026import org.ametys.cms.workflow.CreateContentFunction;
027import org.ametys.odf.ODFHelper;
028import org.ametys.plugins.repository.ModifiableTraversableAmetysObject;
029import org.ametys.plugins.repository.RepositoryIntegrityViolationException;
030import org.ametys.plugins.repository.collection.AmetysObjectCollection;
031import org.ametys.runtime.config.Config;
032
033import com.opensymphony.module.propertyset.PropertySet;
034import com.opensymphony.workflow.WorkflowException;
035
036/**
037 * Abstract OSWorkflow function for creating a ODF content.
038 */
039public abstract class AbstractCreateODFContentFunction extends CreateContentFunction
040{
041    /** Constant for storing the catalog name to use into the transient variables map. */
042    public static final String CONTENT_CATALOG_KEY = AbstractCreateODFContentFunction.class.getName() + "$catalog";
043    
044    /** ODF helper */
045    protected ODFHelper _odfHelper;
046    
047    @Override
048    public void service(ServiceManager manager) throws ServiceException
049    {
050        super.service(manager);
051        _odfHelper = (ODFHelper) manager.lookup(ODFHelper.ROLE);
052    }
053    
054    @Override
055    protected AmetysObjectCollection< ? , ModifiableWorkflowAwareContent> _getContentRoot(Map transientVars) throws WorkflowException
056    {
057        return _odfHelper.getRootContent(true);
058    }
059    
060    @Override
061    public void execute(Map transientVars, Map args, PropertySet ps) throws WorkflowException
062    {
063        if (StringUtils.isEmpty((String) transientVars.get(CONTENT_LANGUAGE_KEY)))
064        {
065            // Override the content language with the odf main language
066            String odfLang = Config.getInstance().getValue("odf.programs.lang");
067            transientVars.put(CONTENT_LANGUAGE_KEY, odfLang);
068        }
069        
070        super.execute(transientVars, args, ps);
071    }
072    
073    @Override
074    protected ModifiableWorkflowAwareContent _createContent(Map transientVars, String desiredContentName, ModifiableTraversableAmetysObject contentsNode)
075    {
076        ModifiableWorkflowAwareContent content = null;
077        
078        String prefix = _getContentNamePrefix();
079        String prefixedContentName = prefix + desiredContentName;
080        String contentName = FilterNameHelper.filterName(prefixedContentName);
081        
082        if (contentName.substring(prefix.length()).contains(prefix))
083        {
084            String afterPrefix = contentName.substring(prefix.length());
085            afterPrefix = StringUtils.replace(afterPrefix, prefix, prefix.substring(0, prefix.length() - 1) + "1-", -1);
086            contentName = prefix + afterPrefix;
087        }
088        
089        int errorCount = 0;
090        do
091        {
092            if (errorCount != 0)
093            {
094                contentName = FilterNameHelper.filterName(prefixedContentName + " " + (errorCount + 1));
095            }
096            try
097            {
098                content = contentsNode.createChild(contentName, _getNodeType());
099            }
100            catch (RepositoryIntegrityViolationException e)
101            {
102                // Content name is already used
103                errorCount++;
104            }
105        }
106        while (content == null);
107        
108        return content;
109    }
110    
111    /**
112     * Get the prefix for content name
113     * @return the prefix
114     */
115    protected abstract String _getContentNamePrefix ();
116    
117    /**
118     * Get the content node type
119     * @return the content node type
120     */
121    protected abstract String _getNodeType ();
122}