001/* 002 * (C) Copyright 2011 Nuxeo SA (http://nuxeo.com/) and contributors. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the GNU Lesser General Public License 006 * (LGPL) version 2.1 which accompanies this distribution, and is available at 007 * http://www.gnu.org/licenses/lgpl.html 008 * 009 * This library is distributed in the hope that it will be useful, 010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 012 * Lesser General Public License for more details. 013 * 014 * Contributors: 015 * Mariana Cedica 016 */ 017package org.nuxeo.ecm.platform.importer.service; 018 019import org.apache.commons.logging.Log; 020import org.apache.commons.logging.LogFactory; 021import org.nuxeo.ecm.platform.importer.base.GenericMultiThreadedImporter; 022import org.nuxeo.ecm.platform.importer.base.ImporterRunnerConfiguration; 023import org.nuxeo.ecm.platform.importer.executor.AbstractImporterExecutor; 024import org.nuxeo.ecm.platform.importer.executor.DefaultImporterExecutor; 025import org.nuxeo.ecm.platform.importer.factories.DefaultDocumentModelFactory; 026import org.nuxeo.ecm.platform.importer.factories.ImporterDocumentModelFactory; 027import org.nuxeo.ecm.platform.importer.filter.EventServiceConfiguratorFilter; 028import org.nuxeo.ecm.platform.importer.filter.ImporterFilter; 029import org.nuxeo.ecm.platform.importer.log.ImporterLogger; 030import org.nuxeo.ecm.platform.importer.source.FileSourceNode; 031import org.nuxeo.ecm.platform.importer.source.SourceNode; 032 033public class DefaultImporterServiceImpl implements DefaultImporterService { 034 035 private static Log log = LogFactory.getLog(DefaultImporterServiceImpl.class); 036 037 private Class<? extends ImporterDocumentModelFactory> docModelFactoryClass; 038 039 private Class<? extends SourceNode> sourceNodeClass; 040 041 private ImporterDocumentModelFactory documentModelFactory; 042 043 private String folderishDocType; 044 045 private String leafDocType; 046 047 private ImporterLogger importerLogger; 048 049 private int transactionTimeout = 0; 050 051 private String repositoryName; 052 053 @Override 054 public void importDocuments(String destinationPath, String sourcePath, boolean skipRootContainerCreation, 055 int batchSize, int noImportingThreads) { 056 SourceNode sourceNode = createNewSourceNodeInstanceForSourcePath(sourcePath); 057 if (sourceNode == null) { 058 log.error("Need to set a sourceNode to be used by this importer"); 059 return; 060 } 061 if (getDocumentModelFactory() == null) { 062 log.error("Need to set a documentModelFactory to be used by this importer"); 063 } 064 065 DefaultImporterExecutor executor = new DefaultImporterExecutor(repositoryName); 066 executor.setFactory(getDocumentModelFactory()); 067 executor.setTransactionTimeout(transactionTimeout); 068 executor.run(sourceNode, destinationPath, skipRootContainerCreation, batchSize, noImportingThreads, true); 069 } 070 071 @Override 072 public String importDocuments(AbstractImporterExecutor executor, String destinationPath, String sourcePath, 073 boolean skipRootContainerCreation, int batchSize, int noImportingThreads, boolean interactive) 074 { 075 076 SourceNode sourceNode = createNewSourceNodeInstanceForSourcePath(sourcePath); 077 if (sourceNode == null) { 078 log.error("Need to set a sourceNode to be used by this importer"); 079 return "Can not import"; 080 } 081 if (getDocumentModelFactory() == null) { 082 log.error("Need to set a documentModelFactory to be used by this importer"); 083 } 084 085 ImporterRunnerConfiguration configuration = new ImporterRunnerConfiguration.Builder(sourceNode, 086 destinationPath, executor.getLogger()).skipRootContainerCreation(skipRootContainerCreation).batchSize( 087 batchSize).nbThreads(noImportingThreads).repository(repositoryName).build(); 088 GenericMultiThreadedImporter runner = new GenericMultiThreadedImporter(configuration); 089 runner.setTransactionTimeout(transactionTimeout); 090 ImporterFilter filter = new EventServiceConfiguratorFilter(false, false, false, true); 091 runner.addFilter(filter); 092 runner.setFactory(getDocumentModelFactory()); 093 return executor.run(runner, interactive); 094 } 095 096 @Override 097 public String importDocuments(AbstractImporterExecutor executor, String leafType, String folderishType, 098 String destinationPath, String sourcePath, boolean skipRootContainerCreation, int batchSize, 099 int noImportingThreads, boolean interactive) { 100 ImporterDocumentModelFactory docModelFactory = getDocumentModelFactory(); 101 if (docModelFactory instanceof DefaultDocumentModelFactory) { 102 DefaultDocumentModelFactory defaultDocModelFactory = (DefaultDocumentModelFactory) docModelFactory; 103 defaultDocModelFactory.setLeafType(leafType == null ? getLeafDocType() : leafType); 104 defaultDocModelFactory.setFolderishType(folderishType == null ? getFolderishDocType() : folderishType); 105 } 106 setDocumentModelFactory(docModelFactory); 107 executor.setTransactionTimeout(transactionTimeout); 108 String res = importDocuments(executor, destinationPath, sourcePath, skipRootContainerCreation, batchSize, 109 noImportingThreads, interactive); 110 setDocumentModelFactory(null); 111 return res; 112 113 } 114 115 @Override 116 public void setDocModelFactoryClass(Class<? extends ImporterDocumentModelFactory> docModelFactoryClass) { 117 this.docModelFactoryClass = docModelFactoryClass; 118 } 119 120 @Override 121 public void setSourceNodeClass(Class<? extends SourceNode> sourceNodeClass) { 122 this.sourceNodeClass = sourceNodeClass; 123 } 124 125 protected SourceNode createNewSourceNodeInstanceForSourcePath(String sourcePath) { 126 SourceNode sourceNode = null; 127 if (sourceNodeClass != null && FileSourceNode.class.isAssignableFrom(sourceNodeClass)) { 128 try { 129 sourceNode = sourceNodeClass.getConstructor(String.class).newInstance(sourcePath); 130 } catch (ReflectiveOperationException e) { 131 log.error(e, e); 132 } 133 } 134 return sourceNode; 135 } 136 137 protected ImporterDocumentModelFactory getDocumentModelFactory() { 138 if (documentModelFactory == null) { 139 if (docModelFactoryClass != null 140 && DefaultDocumentModelFactory.class.isAssignableFrom(docModelFactoryClass)) { 141 try { 142 setDocumentModelFactory(docModelFactoryClass.getConstructor(String.class, String.class).newInstance( 143 getFolderishDocType(), getLeafDocType())); 144 } catch (ReflectiveOperationException e) { 145 log.error(e, e); 146 } 147 } 148 } 149 return documentModelFactory; 150 } 151 152 protected void setDocumentModelFactory(ImporterDocumentModelFactory documentModelFactory) { 153 this.documentModelFactory = documentModelFactory; 154 } 155 156 public String getFolderishDocType() { 157 return folderishDocType; 158 } 159 160 @Override 161 public void setFolderishDocType(String folderishDocType) { 162 this.folderishDocType = folderishDocType; 163 } 164 165 public String getLeafDocType() { 166 return leafDocType; 167 } 168 169 @Override 170 public void setLeafDocType(String fileDocType) { 171 leafDocType = fileDocType; 172 } 173 174 public ImporterLogger getImporterLogger() { 175 return importerLogger; 176 } 177 178 @Override 179 public void setImporterLogger(ImporterLogger importerLogger) { 180 this.importerLogger = importerLogger; 181 } 182 183 /* 184 * @since 5.9.4 185 */ 186 @Override 187 public void setTransactionTimeout(int transactionTimeout) { 188 this.transactionTimeout = transactionTimeout; 189 } 190 191 /* 192 * @since 5.7.3 193 */ 194 @Override 195 public Class<? extends SourceNode> getSourceNodeClass() { 196 return sourceNodeClass; 197 } 198 199 /* 200 * @since 5.7.3 201 */ 202 @Override 203 public Class<? extends ImporterDocumentModelFactory> getDocModelFactoryClass() { 204 return docModelFactoryClass; 205 } 206 207 /** 208 *@since 7.1 209 * @param repositoryName 210 */ 211 @Override 212 public void setRepository(String repositoryName) { 213 this.repositoryName=repositoryName; 214 } 215}