001/*
002 * (C) Copyright 2016 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 */
017package org.nuxeo.ecm.platform.importer.queue.consumer;
018
019import java.io.IOException;
020import java.io.Serializable;
021import java.util.Map;
022import java.util.concurrent.BlockingQueue;
023
024import org.nuxeo.ecm.core.api.Blob;
025import org.nuxeo.ecm.core.api.ClientException;
026import org.nuxeo.ecm.core.api.CoreSession;
027import org.nuxeo.ecm.core.api.DocumentModel;
028import org.nuxeo.ecm.core.api.blobholder.BlobHolder;
029import org.nuxeo.ecm.core.api.model.PropertyNotFoundException;
030import org.nuxeo.ecm.platform.filemanager.api.FileManager;
031import org.nuxeo.ecm.platform.importer.log.ImporterLogger;
032import org.nuxeo.ecm.platform.importer.source.SourceNode;
033import org.nuxeo.runtime.api.Framework;
034
035/**
036 * @since 8.3
037 */
038public class DefaultConsumer extends AbstractConsumer {
039
040    protected FileManager fileManager;
041
042    protected final String rootPath;
043
044    public DefaultConsumer(ImporterLogger log, DocumentModel root, int batchSize, BlockingQueue<SourceNode> queue) {
045        super(log, root, batchSize, queue);
046        fileManager = Framework.getService(FileManager.class);
047        rootPath = root.getPathAsString();
048    }
049
050    protected String getType() {
051        return "File";
052    }
053
054    @Override
055    protected void process(CoreSession session, SourceNode src) throws IOException {
056
057        String fileName = null;
058        String name = null;
059        BlobHolder bh = src.getBlobHolder();
060        if (bh != null) {
061            Blob blob = bh.getBlob();
062            if (blob != null) {
063                fileName = blob.getFilename();
064            }
065            Map<String, Serializable> props = bh.getProperties();
066            if (props != null) {
067                name = (String) props.get("name");
068            }
069            if (name == null) {
070                name = fileName;
071            } else if (fileName == null) {
072                fileName = name;
073            }
074
075            DocumentModel doc = session.createDocumentModel(rootPath, name, getType());
076
077            doc.setProperty("dublincore", "title", name);
078            doc.setProperty("file", "filename", fileName);
079            doc.setProperty("file", "content", bh.getBlob());
080
081            if (bh != null) {
082                doc = setDocumentProperties(session, bh.getProperties(), doc);
083            }
084
085            doc = session.createDocument(doc);
086            importStat.increaseStat(doc.getType(), 1);
087        }
088    }
089
090    protected DocumentModel setDocumentProperties(CoreSession session, Map<String, Serializable> properties,
091            DocumentModel doc) throws ClientException {
092        if (properties != null) {
093
094            for (Map.Entry<String, Serializable> entry : properties.entrySet()) {
095                try {
096                    doc.setPropertyValue(entry.getKey(), entry.getValue());
097                } catch (PropertyNotFoundException e) {
098                    String message = String.format("Property '%s' not found on document type: %s. Skipping it.",
099                            entry.getKey(), doc.getType());
100                    log.error(message, e);
101                }
102            }
103            doc = session.saveDocument(doc);
104        }
105        return doc;
106    }
107
108    @Override
109    public double getNbDocsCreated() {
110        return getNbProcessed();
111    }
112
113}