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) {
long nodeId = EJBLookup.treeEngine.createNodes(FxTreeMode.Edit, FxTreeNode.ROOT_NODE, -1, folders.join("/"))[-1]
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())
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"])