Log In | Sign Up   View a printable version of the current page.
  Dashboard > [fleXive] > ... > How-Tos > Import a folder structure of documents on the server

Added by Daniel Lichtenberger, last edited by Daniel Lichtenberger on Sep 14, 2009  (view change)
Labels: 
(None)

Overview

This script is an extension of the image import script in Import a folder of images on the server. Here we traverse a folder structure recursively and mirror its structure in the flexive content tree. Also, we create documents instead of images here, so you can for example import a folder of eBooks and enjoy indexed fulltext queries on your eBook collection.

Executing the code

Open the backend administration with a user who can execute scripts in the Groovy console, and open Scripts/Script Console. Check the "Execute at Web layer" box to use one transaction per document import (otherwise timeouts may occur). Copy and paste the code below, change the path in the last line (note that the path must be available on the server, not on the client!) and click execute. The import messages are written to stdout.

Code

import com.flexive.shared.*
import com.flexive.shared.value.*
import java.io.FileInputStream
import com.flexive.shared.scripting.groovy.GroovyContentBuilder
import com.flexive.shared.tree.FxTreeNodeEdit
import com.flexive.shared.tree.FxTreeMode
import com.flexive.shared.tree.FxTreeNode

def importDirectory(String path, folders) {
  // create folder
  long nodeId = EJBLookup.treeEngine.createNodes(FxTreeMode.Edit, FxTreeNode.ROOT_NODE, -1, folders.join("/"))[-1]

  // import content
  new File(path).eachFile { file ->
      if (file.directory) {
          importDirectory(file.path, folders + file.name);
      } else {
        def builder = new GroovyContentBuilder("DOCUMENTFILE")
        try {
            builder {
                document(new FxBinary(false, new BinaryDescriptor(file.name, new FileInputStream(file))))
            }
            pk = EJBLookup.contentEngine.save(builder.getContent())

            // create tree entry
            EJBLookup.treeEngine.save(
                    FxTreeNodeEdit.createNew(file.name).setParentNodeId(nodeId).setReference(pk)
            )

            println "Imported " + file.name
        } catch (Exception e) {
             println "Failed to import file: " + e
        }
      }
  }
}

importDirectory("/path/to/import", ["Documents"])
Site running on a free Atlassian Confluence Open Source Project License granted to [fleXive] . Evaluate Confluence today.
Powered by Atlassian Confluence, the Enterprise Wiki. (Version: 2.6.1 Build:#916 Nov 09, 2007) - Bug/feature request - Contact Administrators