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