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