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