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.CopyContentMetadataComponent; 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 // Override component for copy 048 _copyContentMetadataHelper = (CopyContentMetadataComponent) manager.lookup(org.ametys.odf.content.CopyContentMetadataComponent.ROLE); 049 } 050 051 @Override 052 protected AmetysObjectCollection< ? , ModifiableWorkflowAwareContent> _getContentRoot(Map transientVars) throws WorkflowException 053 { 054 return _odfHelper.getRootContent(true); 055 } 056 057 @Override 058 protected ModifiableWorkflowAwareContent _createContent(Map transientVars, String desiredContentName, ModifiableTraversableAmetysObject contentsNode) 059 { 060 ModifiableWorkflowAwareContent content = null; 061 062 String prefix = _getContentNamePrefix(); 063 String prefixedContentName = prefix + desiredContentName; 064 String contentName = FilterNameHelper.filterName(prefixedContentName); 065 066 if (contentName.substring(prefix.length()).contains(prefix)) 067 { 068 String afterPrefix = contentName.substring(prefix.length()); 069 afterPrefix = StringUtils.replace(afterPrefix, prefix, prefix.substring(0, prefix.length() - 1) + "1-", -1); 070 contentName = prefix + afterPrefix; 071 } 072 073 int errorCount = 0; 074 do 075 { 076 if (errorCount != 0) 077 { 078 contentName = FilterNameHelper.filterName(prefixedContentName + " " + (errorCount + 1)); 079 } 080 try 081 { 082 content = contentsNode.createChild(contentName, _getNodeType()); 083 } 084 catch (RepositoryIntegrityViolationException e) 085 { 086 // Content name is already used 087 errorCount++; 088 } 089 } 090 while (content == null); 091 092 return content; 093 } 094 095 /** 096 * Get the prefix for content name 097 * @return the prefix 098 */ 099 protected abstract String _getContentNamePrefix (); 100 101 /** 102 * Get the content node type 103 * @return the content node type 104 */ 105 protected abstract String _getNodeType (); 106}