001/*
002 * (C) Copyright 2006-2012 Nuxeo SAS (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Nuxeo - initial API and implementation
016 *
017 * $Id$
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.getLocalService(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<String, Object>();
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        String docType = "File";
113        BlobHolder bh = node.getBlobHolder();
114
115        if (bh instanceof ScanFileBlobHolder) {
116            ScanFileBlobHolder scanBH = (ScanFileBlobHolder) bh;
117            docType = scanBH.getTargetType();
118            setLeafType(docType);
119        }
120
121        DocumentModel doc = defaultCreateLeafNode(session, parent, node);
122
123        // XXX should be a callback on commit !!!
124        if (node instanceof ScanedFileSourceNode) {
125            ScanedFileSourceNode scanNode = (ScanedFileSourceNode) node;
126            ScannedFileImporter.addProcessedDescriptor(scanNode.getDescriptorFileName());
127        }
128
129        return doc;
130    }
131
132    @Override
133    protected DocumentModel defaultCreateLeafNode(CoreSession session, DocumentModel parent, SourceNode node)
134            throws IOException {
135
136        BlobHolder bh = node.getBlobHolder();
137
138        String name = getValidNameFromFileName(node.getName());
139        String fileName = node.getName();
140
141        DocumentRef docRef = new PathRef(parent.getPathAsString(), name);
142
143        DocumentModel doc;
144        boolean docExists = session.exists(docRef);
145        if (docExists && config.isUpdate()) {
146            doc = session.getDocument(docRef);
147        } else {
148            Map<String, Object> options = new HashMap<String, Object>();
149            doc = session.createDocumentModel(leafType, options);
150            doc.setPathInfo(parent.getPathAsString(), name);
151            doc.setProperty("dublincore", "title", node.getName());
152        }
153        doc.setProperty("file", "filename", fileName);
154        doc.setProperty("file", "content", bh.getBlob());
155
156        if (docExists && config.isUpdate()) {
157            doc = session.saveDocument(doc);
158        } else {
159            doc = session.createDocument(doc);
160        }
161
162        doc = setDocumentProperties(session, bh.getProperties(), doc);
163
164        return doc;
165    }
166}