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