001/* 002 * (C) Copyright 2011 Nuxeo SA (http://nuxeo.com/) and others. 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 * 016 * Contributors: 017 * Mariana Cedica 018 */ 019package org.nuxeo.ecm.platform.importer.service; 020 021import org.apache.commons.logging.Log; 022import org.apache.commons.logging.LogFactory; 023import org.nuxeo.ecm.platform.importer.base.GenericMultiThreadedImporter; 024import org.nuxeo.ecm.platform.importer.base.ImporterRunnerConfiguration; 025import org.nuxeo.ecm.platform.importer.executor.AbstractImporterExecutor; 026import org.nuxeo.ecm.platform.importer.executor.DefaultImporterExecutor; 027import org.nuxeo.ecm.platform.importer.factories.DefaultDocumentModelFactory; 028import org.nuxeo.ecm.platform.importer.factories.ImporterDocumentModelFactory; 029import org.nuxeo.ecm.platform.importer.filter.EventServiceConfiguratorFilter; 030import org.nuxeo.ecm.platform.importer.filter.ImporterFilter; 031import org.nuxeo.ecm.platform.importer.log.ImporterLogger; 032import org.nuxeo.ecm.platform.importer.source.FileSourceNode; 033import org.nuxeo.ecm.platform.importer.source.SourceNode; 034 035public class DefaultImporterServiceImpl implements DefaultImporterService { 036 037 private static Log log = LogFactory.getLog(DefaultImporterServiceImpl.class); 038 039 private Class<? extends ImporterDocumentModelFactory> docModelFactoryClass; 040 041 private Class<? extends SourceNode> sourceNodeClass; 042 043 private ImporterDocumentModelFactory documentModelFactory; 044 045 private String folderishDocType; 046 047 private String leafDocType; 048 049 private ImporterLogger importerLogger; 050 051 private int transactionTimeout = 0; 052 053 private String repositoryName; 054 055 private boolean bulkMode = true; 056 057 @Override 058 public void importDocuments(String destinationPath, String sourcePath, boolean skipRootContainerCreation, 059 int batchSize, int noImportingThreads) { 060 SourceNode sourceNode = createNewSourceNodeInstanceForSourcePath(sourcePath); 061 if (sourceNode == null) { 062 log.error("Need to set a sourceNode to be used by this importer"); 063 return; 064 } 065 if (getDocumentModelFactory() == null) { 066 log.error("Need to set a documentModelFactory to be used by this importer"); 067 } 068 069 DefaultImporterExecutor executor = new DefaultImporterExecutor(repositoryName); 070 executor.setFactory(getDocumentModelFactory()); 071 executor.setTransactionTimeout(transactionTimeout); 072 executor.run(sourceNode, destinationPath, skipRootContainerCreation, batchSize, noImportingThreads, true); 073 } 074 075 @Override 076 public String importDocuments(AbstractImporterExecutor executor, String destinationPath, String sourcePath, 077 boolean skipRootContainerCreation, int batchSize, int noImportingThreads, boolean interactive) 078 { 079 080 SourceNode sourceNode = createNewSourceNodeInstanceForSourcePath(sourcePath); 081 if (sourceNode == null) { 082 log.error("Need to set a sourceNode to be used by this importer"); 083 return "Can not import"; 084 } 085 if (getDocumentModelFactory() == null) { 086 log.error("Need to set a documentModelFactory to be used by this importer"); 087 } 088 089 ImporterRunnerConfiguration configuration = new ImporterRunnerConfiguration.Builder(sourceNode, 090 destinationPath, executor.getLogger()).skipRootContainerCreation(skipRootContainerCreation).batchSize( 091 batchSize).nbThreads(noImportingThreads).repository(repositoryName).build(); 092 GenericMultiThreadedImporter runner = new GenericMultiThreadedImporter(configuration); 093 runner.setTransactionTimeout(transactionTimeout); 094 ImporterFilter filter = new EventServiceConfiguratorFilter(false, false, false, false, bulkMode); 095 runner.addFilter(filter); 096 runner.setFactory(getDocumentModelFactory()); 097 return executor.run(runner, interactive); 098 } 099 100 @Override 101 public String importDocuments(AbstractImporterExecutor executor, String leafType, String folderishType, 102 String destinationPath, String sourcePath, boolean skipRootContainerCreation, int batchSize, 103 int noImportingThreads, boolean interactive) { 104 ImporterDocumentModelFactory docModelFactory = getDocumentModelFactory(); 105 if (docModelFactory instanceof DefaultDocumentModelFactory) { 106 DefaultDocumentModelFactory defaultDocModelFactory = (DefaultDocumentModelFactory) docModelFactory; 107 defaultDocModelFactory.setLeafType(leafType == null ? getLeafDocType() : leafType); 108 defaultDocModelFactory.setFolderishType(folderishType == null ? getFolderishDocType() : folderishType); 109 } 110 setDocumentModelFactory(docModelFactory); 111 executor.setTransactionTimeout(transactionTimeout); 112 String res = importDocuments(executor, destinationPath, sourcePath, skipRootContainerCreation, batchSize, 113 noImportingThreads, interactive); 114 setDocumentModelFactory(null); 115 return res; 116 117 } 118 119 @Override 120 public void setDocModelFactoryClass(Class<? extends ImporterDocumentModelFactory> docModelFactoryClass) { 121 this.docModelFactoryClass = docModelFactoryClass; 122 } 123 124 @Override 125 public void setSourceNodeClass(Class<? extends SourceNode> sourceNodeClass) { 126 this.sourceNodeClass = sourceNodeClass; 127 } 128 129 protected SourceNode createNewSourceNodeInstanceForSourcePath(String sourcePath) { 130 SourceNode sourceNode = null; 131 if (sourceNodeClass != null && FileSourceNode.class.isAssignableFrom(sourceNodeClass)) { 132 try { 133 sourceNode = sourceNodeClass.getConstructor(String.class).newInstance(sourcePath); 134 } catch (ReflectiveOperationException e) { 135 log.error(e, e); 136 } 137 } 138 return sourceNode; 139 } 140 141 protected ImporterDocumentModelFactory getDocumentModelFactory() { 142 if (documentModelFactory == null) { 143 if (docModelFactoryClass != null 144 && DefaultDocumentModelFactory.class.isAssignableFrom(docModelFactoryClass)) { 145 try { 146 setDocumentModelFactory(docModelFactoryClass.getConstructor(String.class, String.class).newInstance( 147 getFolderishDocType(), getLeafDocType())); 148 } catch (ReflectiveOperationException e) { 149 log.error(e, e); 150 } 151 } 152 } 153 return documentModelFactory; 154 } 155 156 protected void setDocumentModelFactory(ImporterDocumentModelFactory documentModelFactory) { 157 this.documentModelFactory = documentModelFactory; 158 } 159 160 public String getFolderishDocType() { 161 return folderishDocType; 162 } 163 164 @Override 165 public void setFolderishDocType(String folderishDocType) { 166 this.folderishDocType = folderishDocType; 167 } 168 169 public String getLeafDocType() { 170 return leafDocType; 171 } 172 173 @Override 174 public void setLeafDocType(String fileDocType) { 175 leafDocType = fileDocType; 176 } 177 178 public ImporterLogger getImporterLogger() { 179 return importerLogger; 180 } 181 182 @Override 183 public void setImporterLogger(ImporterLogger importerLogger) { 184 this.importerLogger = importerLogger; 185 } 186 187 /* 188 * @since 5.9.4 189 */ 190 @Override 191 public void setTransactionTimeout(int transactionTimeout) { 192 this.transactionTimeout = transactionTimeout; 193 } 194 195 /* 196 * @since 5.7.3 197 */ 198 @Override 199 public Class<? extends SourceNode> getSourceNodeClass() { 200 return sourceNodeClass; 201 } 202 203 /* 204 * @since 5.7.3 205 */ 206 @Override 207 public Class<? extends ImporterDocumentModelFactory> getDocModelFactoryClass() { 208 return docModelFactoryClass; 209 } 210 211 /** 212 *@since 7.1 213 * @param repositoryName 214 */ 215 @Override 216 public void setRepository(String repositoryName) { 217 this.repositoryName=repositoryName; 218 } 219 220 @Override 221 public void setBulkMode(boolean bulkMode) { 222 this.bulkMode = bulkMode; 223 } 224 225}