001/* 002 * (C) Copyright 2006-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 * Contributors: 017 * Nuxeo - initial API and implementation 018 */ 019package org.nuxeo.ecm.platform.scanimporter.processor; 020 021import java.io.IOException; 022import java.util.HashMap; 023import java.util.Map; 024 025import org.nuxeo.ecm.core.api.CoreSession; 026import org.nuxeo.ecm.core.api.DocumentModel; 027import org.nuxeo.ecm.core.api.DocumentRef; 028import org.nuxeo.ecm.core.api.PathRef; 029import org.nuxeo.ecm.core.api.blobholder.BlobHolder; 030import org.nuxeo.ecm.platform.importer.factories.DefaultDocumentModelFactory; 031import org.nuxeo.ecm.platform.importer.factories.ImporterDocumentModelFactory; 032import org.nuxeo.ecm.platform.importer.source.SourceNode; 033import org.nuxeo.ecm.platform.scanimporter.service.ImporterConfig; 034import org.nuxeo.ecm.platform.scanimporter.service.ScanFileBlobHolder; 035import org.nuxeo.ecm.platform.scanimporter.service.ScannedFileMapperService; 036import org.nuxeo.runtime.api.Framework; 037 038/** 039 * Custom implementation of the {@link ImporterDocumentModelFactory}. Provides : - container doc type configuration from 040 * service - leaf doc type configuration from service 041 * 042 * @author Thierry Delprat 043 */ 044public class ScanedFileFactory extends DefaultDocumentModelFactory implements ImporterDocumentModelFactory { 045 046 protected static String targetContainerType = null; 047 048 protected ImporterConfig config; 049 050 public ScanedFileFactory(ImporterConfig config) { 051 super(); 052 this.config = config; 053 } 054 055 protected String getTargetContainerType() { 056 if (targetContainerType == null) { 057 ScannedFileMapperService service = Framework.getService(ScannedFileMapperService.class); 058 targetContainerType = service.getTargetContainerType(); 059 } 060 return targetContainerType; 061 } 062 063 @Override 064 public DocumentModel createFolderishNode(CoreSession session, DocumentModel parent, SourceNode node) { 065 066 String docType = getTargetContainerType(); 067 String name = getValidNameFromFileName(node.getName()); 068 boolean isUpdateDocument = false; 069 070 PathRef ref = new PathRef(parent.getPathAsString(), name); 071 if (parent.getPathAsString().equals(config.getTargetPath()) 072 && node.getSourcePath().equals(config.getSourcePath())) { 073 // initial root creation 074 if (!config.isCreateInitialFolder()) { 075 return parent; 076 } 077 if (config.isMergeInitialFolder()) { 078 if (session.exists(ref)) { 079 return session.getDocument(ref); 080 } 081 } else { 082 if (session.exists(ref)) { 083 if (config.isUpdate()) { 084 isUpdateDocument = true; 085 } else { 086 name = name + "-" + System.currentTimeMillis(); 087 } 088 } 089 } 090 } else { 091 if (session.exists(ref) && config.isUpdate()) { 092 isUpdateDocument = true; 093 } 094 } 095 096 Map<String, Object> options = new HashMap<>(); 097 DocumentModel doc; 098 if (isUpdateDocument) { 099 doc = session.getDocument(ref); 100 } else { 101 doc = session.createDocumentModel(docType, options); 102 doc.setPathInfo(parent.getPathAsString(), name); 103 doc.setProperty("dublincore", "title", node.getName()); 104 doc = session.createDocument(doc); 105 } 106 return doc; 107 } 108 109 @Override 110 public DocumentModel createLeafNode(CoreSession session, DocumentModel parent, SourceNode node) throws IOException { 111 112 BlobHolder bh = node.getBlobHolder(); 113 114 if (bh instanceof ScanFileBlobHolder) { 115 ScanFileBlobHolder scanBH = (ScanFileBlobHolder) bh; 116 String docType = scanBH.getTargetType(); 117 setLeafType(docType); 118 } 119 120 DocumentModel doc = defaultCreateLeafNode(session, parent, node); 121 122 // XXX should be a callback on commit !!! 123 if (node instanceof ScanedFileSourceNode) { 124 ScanedFileSourceNode scanNode = (ScanedFileSourceNode) node; 125 ScannedFileImporter.addProcessedDescriptor(scanNode.getDescriptorFileName()); 126 } 127 128 return doc; 129 } 130 131 @Override 132 protected DocumentModel defaultCreateLeafNode(CoreSession session, DocumentModel parent, SourceNode node) 133 throws IOException { 134 135 BlobHolder bh = node.getBlobHolder(); 136 137 String name = getValidNameFromFileName(node.getName()); 138 String fileName = node.getName(); 139 140 DocumentRef docRef = new PathRef(parent.getPathAsString(), name); 141 142 DocumentModel doc; 143 boolean docExists = session.exists(docRef); 144 if (docExists && config.isUpdate()) { 145 doc = session.getDocument(docRef); 146 } else { 147 Map<String, Object> options = new HashMap<>(); 148 doc = session.createDocumentModel(leafType, options); 149 doc.setPathInfo(parent.getPathAsString(), name); 150 doc.setProperty("dublincore", "title", node.getName()); 151 } 152 doc.setProperty("file", "content", bh.getBlob()); 153 154 if (docExists && config.isUpdate()) { 155 doc = session.saveDocument(doc); 156 } else { 157 doc = session.createDocument(doc); 158 } 159 160 doc = setDocumentProperties(session, bh.getProperties(), doc); 161 162 return doc; 163 } 164}