001/*
002 * (C) Copyright 2011-2019 Nuxeo (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 static java.util.Objects.requireNonNullElseGet;
022
023import org.apache.commons.logging.Log;
024import org.apache.commons.logging.LogFactory;
025import org.nuxeo.ecm.platform.importer.factories.DefaultDocumentModelFactory;
026import org.nuxeo.ecm.platform.importer.log.ImporterLogger;
027import org.nuxeo.ecm.platform.importer.service.ImporterConfigurationDescriptor.DocumentModelFactory;
028import org.nuxeo.ecm.platform.importer.source.FileSourceNode;
029import org.nuxeo.ecm.platform.importer.source.SourceNode;
030import org.nuxeo.runtime.model.ComponentContext;
031import org.nuxeo.runtime.model.ComponentInstance;
032import org.nuxeo.runtime.model.DefaultComponent;
033
034public class DefaultImporterComponent extends DefaultComponent {
035
036    private static final Log log = LogFactory.getLog(DefaultImporterComponent.class);
037
038    protected DefaultImporterService importerService;
039
040    public static final String IMPORTER_CONFIGURATION_XP = "importerConfiguration";
041
042    public static final String DEFAULT_FOLDERISH_DOC_TYPE = "Folder";
043
044    public static final String DEFAULT_LEAF_DOC_TYPE = "File";
045
046    @SuppressWarnings("unchecked")
047    @Override
048    public void registerContribution(Object contribution, String extensionPoint, ComponentInstance contributor) {
049        if (IMPORTER_CONFIGURATION_XP.equals(extensionPoint)) {
050
051            ImporterConfigurationDescriptor descriptor = (ImporterConfigurationDescriptor) contribution;
052            Class<? extends SourceNode> sourceNodeClass = (Class<? extends SourceNode>) descriptor.getSourceNodeClass();
053            if (sourceNodeClass == null) {
054                sourceNodeClass = FileSourceNode.class;
055                log.info("No custom implementation defined for the SourceNode, using FileSourceNode");
056            }
057            importerService.setSourceNodeClass(sourceNodeClass);
058
059            DocumentModelFactory docFactory = requireNonNullElseGet(descriptor.getDocumentModelFactory(),
060                    DocumentModelFactory::new);
061            Class<? extends DefaultDocumentModelFactory> docFactoryClass = docFactory.getDocumentModelFactoryClass();
062            if (docFactoryClass == null) {
063                docFactoryClass = DefaultDocumentModelFactory.class;
064                log.info(
065                        "No custom implementation provided for the documentModelFactory, using DefaultDocumentModelFactory");
066            }
067            importerService.setDocModelFactoryClass(docFactoryClass);
068
069            String folderishType = docFactory.getFolderishType();
070            if (folderishType == null) {
071                folderishType = DEFAULT_FOLDERISH_DOC_TYPE;
072                log.info("No folderish type defined, using Folder by default");
073            }
074            importerService.setFolderishDocType(folderishType);
075
076            String leafType = docFactory.getLeafType();
077            if (leafType == null) {
078                leafType = DEFAULT_LEAF_DOC_TYPE;
079                log.info("No leaf type doc defined, using File by deafult");
080            }
081            importerService.setLeafDocType(leafType);
082
083            Class<? extends ImporterLogger> logClass = descriptor.getImporterLog();
084            if (logClass == null) {
085                log.info("No specific ImporterLogger configured for this importer");
086            } else {
087                try {
088                    importerService.setImporterLogger(logClass.getDeclaredConstructor().newInstance());
089                } catch (ReflectiveOperationException e) {
090                    throw new RuntimeException(e);
091                }
092            }
093
094            if (descriptor.getRepository() != null) {
095                importerService.setRepository(descriptor.getRepository());
096            }
097
098            if (descriptor.getBulkMode() != null) {
099                importerService.setBulkMode(descriptor.getBulkMode().booleanValue());
100            }
101
102            if (descriptor.getEnablePerfLogging() != null) {
103                importerService.setEnablePerfLogging(descriptor.getEnablePerfLogging().booleanValue());
104            }
105        }
106    }
107
108    @Override
109    public void activate(ComponentContext context) {
110        importerService = new DefaultImporterServiceImpl();
111    }
112
113    @Override
114    public void deactivate(ComponentContext context) {
115        importerService = null;
116    }
117
118    @Override
119    public <T> T getAdapter(Class<T> adapter) {
120        if (adapter.isAssignableFrom(DefaultImporterService.class)) {
121            return adapter.cast(importerService);
122        }
123        return super.getAdapter(adapter);
124    }
125}