001/*
002 *  Copyright 2017 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.copy;
017
018import java.util.Map;
019
020import org.ametys.cms.FilterNameHelper;
021import org.ametys.cms.repository.ModifiableWorkflowAwareContent;
022import org.ametys.cms.workflow.copy.CreateContentByCopyFunction;
023import org.ametys.odf.ODFHelper;
024import org.ametys.plugins.repository.ModifiableTraversableAmetysObject;
025import org.ametys.plugins.repository.RepositoryIntegrityViolationException;
026import org.ametys.plugins.repository.collection.AmetysObjectCollection;
027import org.apache.avalon.framework.service.ServiceException;
028import org.apache.avalon.framework.service.ServiceManager;
029import org.apache.commons.lang.StringUtils;
030
031import com.opensymphony.workflow.WorkflowException;
032
033/**
034 * OSWorkflow function to create a odf content by copy of another
035 */
036public abstract class AbstractCreateODFContentByCopyFunction extends CreateContentByCopyFunction
037{
038    /** ODF helper */
039    protected ODFHelper _odfHelper;
040    
041    @Override
042    public void service(ServiceManager manager) throws ServiceException
043    {
044        super.service(manager);
045        _odfHelper = (ODFHelper) manager.lookup(ODFHelper.ROLE);
046    }
047    
048    @Override
049    protected AmetysObjectCollection< ? , ModifiableWorkflowAwareContent> _getContentRoot(Map transientVars) throws WorkflowException
050    {
051        return _odfHelper.getRootContent(true);
052    }
053    
054    @Override
055    protected ModifiableWorkflowAwareContent _createContent(Map transientVars, String desiredContentName, ModifiableTraversableAmetysObject contentsNode)
056    {
057        ModifiableWorkflowAwareContent content = null;
058        
059        String prefix = _getContentNamePrefix();
060        String prefixedContentName = prefix + desiredContentName;
061        String contentName = FilterNameHelper.filterName(prefixedContentName);
062        
063        if (contentName.substring(prefix.length()).contains(prefix))
064        {
065            String afterPrefix = contentName.substring(prefix.length());
066            afterPrefix = StringUtils.replace(afterPrefix, prefix, prefix.substring(0, prefix.length() - 1) + "1-", -1);
067            contentName = prefix + afterPrefix;
068        }
069        
070        int errorCount = 0;
071        do
072        {
073            if (errorCount != 0)
074            {
075                contentName = FilterNameHelper.filterName(prefixedContentName + " " + (errorCount + 1));
076            }
077            try
078            {
079                content = contentsNode.createChild(contentName, _getNodeType());
080            }
081            catch (RepositoryIntegrityViolationException e)
082            {
083                // Content name is already used
084                errorCount++;
085            }
086        }
087        while (content == null);
088        
089        return content;
090    }
091    
092    /**
093     * Get the prefix for content name
094     * @return the prefix
095     */
096    protected abstract String _getContentNamePrefix ();
097    
098    /**
099     * Get the content node type
100     * @return the content node type
101     */
102    protected abstract String _getNodeType ();
103}