org.nuxeo.liveconnect.importer.filesystem.FSImporter.java Source code

Java tutorial

Introduction

Here is the source code for org.nuxeo.liveconnect.importer.filesystem.FSImporter.java

Source

/*
 * (C) Copyright 2016 Nuxeo SA (http://nuxeo.com/) and others.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Contributors:
 *     Michael Vachette
 */

package org.nuxeo.liveconnect.importer.filesystem;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.nuxeo.ecm.core.api.CoreSession;
import org.nuxeo.ecm.core.api.DocumentModel;
import org.nuxeo.ecm.core.api.DocumentModelList;
import org.nuxeo.ecm.core.api.NuxeoException;
import org.nuxeo.ecm.core.blob.BlobManager;
import org.nuxeo.ecm.core.blob.FilesystemBlobProvider;
import org.nuxeo.runtime.api.Framework;

import java.io.IOException;
import java.io.Serializable;
import java.nio.file.*;
import java.util.Map;

public class FSImporter {

    private static final Log log = LogFactory.getLog(FSImporter.class);

    String providerName;

    FilesystemBlobProvider blobProvider;

    public FSImporter(String provider) {
        this.providerName = provider;
    }

    public void importRoot(DocumentModel rootFolder) {
        blobProvider = (FilesystemBlobProvider) Framework.getService(BlobManager.class)
                .getBlobProvider(providerName);
        Map<String, String> properties = blobProvider.properties;
        String path = properties.get("root");
        Path rootPath = Paths.get(path);
        importFolder(rootFolder, rootPath);
        rootFolder.getCoreSession().save();
    }

    protected void importFolder(DocumentModel folder, Path inputPath) {
        try (DirectoryStream<Path> stream = Files.newDirectoryStream(inputPath)) {
            for (Path file : stream) {
                if (!Files.isDirectory(file)) {
                    importFile(folder, file);
                } else {
                    importFolder(folder, file);
                }
            }
        } catch (IOException | DirectoryIteratorException e) {
            throw new NuxeoException(e);
        }
    }

    protected void importFile(DocumentModel parent, Path inputPath) throws IOException {
        String filename = inputPath.getFileName().toString();
        DocumentModel fileDoc = getOrCreateFileDocument(parent, inputPath.toString(), filename);
        BlobManager.BlobInfo blobInfo = new BlobManager.BlobInfo();
        blobInfo.key = inputPath.toString();
        fileDoc.setPropertyValue("file:content", (Serializable) blobProvider.createBlob(blobInfo));
        fileDoc.getCoreSession().saveDocument(fileDoc);

    }

    protected DocumentModel getOrCreateFileDocument(DocumentModel folder, String key, String filename) {
        CoreSession session = folder.getCoreSession();
        String query = String.format("Select * From File Where dc:source = '%s'", key);
        DocumentModelList list = session.query(query);
        if (list.size() > 0)
            return list.get(0);

        DocumentModel file = session.createDocumentModel(folder.getPathAsString(), filename, "File");
        file.setPropertyValue("dc:title", filename);
        file.setPropertyValue("dc:source", key);
        return session.createDocument(file);
    }

}