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; 022 023import org.nuxeo.ecm.core.api.Blob; 024import org.nuxeo.ecm.core.api.CoreSession; 025import org.nuxeo.ecm.core.api.DocumentModel; 026import org.nuxeo.ecm.core.api.blobholder.BlobHolder; 027import org.nuxeo.ecm.core.api.model.PropertyNotFoundException; 028import org.nuxeo.ecm.platform.filemanager.api.FileManager; 029import org.nuxeo.ecm.platform.importer.log.ImporterLogger; 030import org.nuxeo.ecm.platform.importer.queue.manager.QueuesManager; 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, QueuesManager queuesManager, int queue) { 044 super(log, root, batchSize, queuesManager, 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", "content", bh.getBlob()); 078 079 if (bh != null) { 080 doc = setDocumentProperties(session, bh.getProperties(), doc); 081 } 082 083 doc = session.createDocument(doc); 084 } 085 } 086 087 protected DocumentModel setDocumentProperties(CoreSession session, Map<String, Serializable> properties, 088 DocumentModel doc) { 089 if (properties != null) { 090 091 for (Map.Entry<String, Serializable> entry : properties.entrySet()) { 092 try { 093 doc.setPropertyValue(entry.getKey(), entry.getValue()); 094 } catch (PropertyNotFoundException e) { 095 String message = String.format("Property '%s' not found on document type: %s. Skipping it.", 096 entry.getKey(), doc.getType()); 097 log.error(message, e); 098 } 099 } 100 doc = session.saveDocument(doc); 101 } 102 return doc; 103 } 104 105 @Override 106 public double getNbDocsCreated() { 107 return getNbProcessed(); 108 } 109 110}