Search code examples
javarestjcr

Create File/Folder hierarchy in Apache JCR


We have a functionality to create a number of folders when user saves the data in CM. The format would is attached in image:

  • ParentFolder

    • ChildFolder1
    • ChildFolder2
    • ChildFolder3
    • File1
    • File2
    • File3
  • ParentFolderConfig

    • ChildFolderConfig1
    • ChildFolderConfig2
    • ChildFolderConfig3
    • FileConfig1
    • FileConfig2
    • FileConfig3

These all are created all the times when user creates it. I have found a way to add nodes one by one using addNode(). But to save the time and increase performance I wanted to find out a way in which I create this files and folder temporary in JAVA and save them to JCR in one call and afterwards dispose these temporary files.


Solution

  • Calling addNode() multiple times and saving a the end with Session.save() is a common pattern in JCR, it's perfectly fine to create your structure like that.

    To make your code simpler you could use a utility class that takes the path of a node that's deep in your hierarchy, and creates intermediate nodes as needed. The JcrUtils.getOrCreateByPath method provided by the Jackrabbit commons module does that.