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