001/*
002 * (C) Copyright 2009 Nuxeo SA (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 *     Thomas Roger
016 */
017
018package org.nuxeo.ecm.platform.importer.factories;
019
020import java.io.IOException;
021
022import org.nuxeo.ecm.core.api.CoreSession;
023import org.nuxeo.ecm.core.api.DocumentModel;
024import org.nuxeo.ecm.core.api.blobholder.BlobHolder;
025import org.nuxeo.ecm.platform.filemanager.api.FileManager;
026import org.nuxeo.ecm.platform.importer.source.SourceNode;
027import org.nuxeo.runtime.api.Framework;
028
029/**
030 * DocumentModel factory based on the {@code FileManager}. Use the {@code FileManager} to create Folderish and Leaf
031 * Nodes.
032 *
033 * @author <a href="mailto:troger@nuxeo.com">Thomas Roger</a>
034 */
035public class FileManagerDocumentModelFactory extends AbstractDocumentModelFactory {
036
037    protected FileManager fileManager;
038
039    @Override
040    public DocumentModel createFolderishNode(CoreSession session, DocumentModel parent, SourceNode node) throws IOException {
041        FileManager fileManager = getFileManager();
042        return fileManager.createFolder(session, node.getName(), parent.getPathAsString());
043    }
044
045    @Override
046    public DocumentModel createLeafNode(CoreSession session, DocumentModel parent, SourceNode node) throws IOException {
047        FileManager fileManager = getFileManager();
048        BlobHolder bh = node.getBlobHolder();
049        DocumentModel doc = fileManager.createDocumentFromBlob(session, bh.getBlob(), parent.getPathAsString(), true,
050                node.getName());
051        doc = setDocumentProperties(session, bh.getProperties(), doc);
052        return doc;
053    }
054
055    protected FileManager getFileManager() {
056        if (fileManager == null) {
057            fileManager = Framework.getService(FileManager.class);
058        }
059        return fileManager;
060    }
061
062}