Fork me on GitHub

Tutorials - Importing Documents

The code snippets are excerpts (or slightly simplififed ones) from Example_Import_Documents_As_Unpublished.

The example below imports data from JSON files by default. You may see the source linked above if you want to import data from XML files instead. The script in the link uses fileInJson parameter to choose either JSON or XML format.

Introduction

Importing task requires JSON files which should look like the exported JSON files shown in Tutorial: Exporting Document Content.

Initializing Import Task

    // 1. Initialize (workflow) document manager and document importing task using the default implementation.
    def documentManager = new WorkflowDocumentManagerImpl(session)
    def importTask = new WorkflowDocumentVariantImportTask(documentManager)
    // 1.1. Sets the logger to the import task, which is useful when running in groovy updater.
    importTask.setLogger(log)

    // 2. Starting import task, meaning the task starts the execution record bookkeeping.
    importTask.start()
          

Collect Source JSON Content Files in Groovy Updater

    // 1. Find all the files which name end with ".json" by the regular expression in the source base VFS folder
    //    at the minimum depth level, 1, and the maximum depth level, 10.
    def sourceBaseFolder = VFS.getManager().resolveFile("file:///var/data/docdata")
    def files = importTask.findFilesByNamePattern(sourceBaseFolder, "^.+\\.json\$" , 1, 10)
          
          

Import Content from ContentNode bean and Create Document

    // 0. Iterate each JSON file from the collection.
    files.eachWithIndex { file, i ->

      // 1. Read ContentNode from a json file and find its primary type and node location from the meta property (jcr:path).
      def contentNode = importTask.readContentNodeFromJsonFile(file)
      def primaryTypeName = contentNode.getPrimaryType()
      // determine the target document handle node path to create or update content from the jcr:path meta property in ContentNode object.
      def documentLocation = contentNode.getProperty("jcr:path").getValue()

      // 2. Record this import task unit and set the import soure file path as an attribute.
      //    The record instances will be collected and summarized when #logSummary() invoked later.
      def record = importTask.beginRecord("", documentLocation)
      record.setAttribute("file", file.name.path)

      // 3. Mark this unit of import task is being processed before real processing below.
      record.setProcessed(true)

      // 4. Find the localized (translated) document name and the locale for the localized name.
      def locale = contentNode.getProperty("hippotranslation:locale").getValue()
      // find localized document name if jcr:localizedName meta property exists in the ContentNode object.
      def localizedName = contentNode.getProperty("jcr:localizedName").getValue()

      // 5. Create or update document at documentLocation from contentNode with locale and localized name.
      updatedDocumentLocation =
          importTask.createOrUpdateDocumentFromVariantContentNode(contentNode, primaryTypeName, documentLocation, locale, localizedName)

      // 6. (Optional) You can publish the document right away. Otherwise, it will remain as unpublished by default.
      //    By default, the created or updated document is left as preview status.
      //    Optionally, if you want, you can publish the document again right away here by uncommenting the following lines.
      //documentManager.depublishDocument(updatedDocumentLocation)
      //documentManager.publishDocument(updatedDocumentLocation)

      // 8. Report that a unit of import process was done, so groovy updater engine can maintain its batch execution status properly.
      visitorContext.reportUpdated(documentLocation)

      // 9. Mark this unit of import task successful.
      record.setSucceeded(true)

      // 10. End the current execution unit record.
      importTask.endRecord()
    }
          
          

Log execution summary

    // 1. Stop the import task after processing, which means you stop execution recording.
    importTask.stop()

    // 2. Log the execution summary finally for administrator.
    importTask.logSummary()
          
          

The summary logs above will look like the following:

INFO 2016-02-19 14:00:35 

===============================================================================================================
Execution Summary:
---------------------------------------------------------------------------------------------------------------
Total: 18, Processed: 18, Suceeded: 18, Failed: 0, Duration: 4407ms
---------------------------------------------------------------------------------------------------------------
Details (in CSV format):
---------------------------------------------------------------------------------------------------------------
SEQ,PROCESSED,SUCCEEDED,ID,PATH,TYPE,ATTRIBUTES,ERROR
1,true,true,0cc93c4a-8c1a-497d-84b4-a8a2a23d206e,/content/documents/administration/labels/global,resourcebundle:resourcebundle,{file=/home/test/content-exim-demo-TRUNK/target/tomcat8x/temp/content-exim-demo/docdata/content/documents/administration/labels/global.json},
2,true,true,c6978687-3bdd-446c-bf4c-6520f0be0dea,/content/documents/administration/labels/homepage,resourcebundle:resourcebundle,{file=/home/test/content-exim-demo-TRUNK/target/tomcat8x/temp/content-exim-demo/docdata/content/documents/administration/labels/homepage.json},
3,true,true,5b514c77-c719-48c7-90d4-592d2e001910,/content/documents/administration/labels/pagenotfound,resourcebundle:resourcebundle,{file=/home/test/content-exim-demo-TRUNK/target/tomcat8x/temp/content-exim-demo/docdata/content/documents/administration/labels/pagenotfound.json},
4,true,true,ae6cf42e-e19d-433a-a07c-47c8625b1063,/content/documents/administration/labels/pagination,resourcebundle:resourcebundle,{file=/home/test/content-exim-demo-TRUNK/target/tomcat8x/temp/content-exim-demo/docdata/content/documents/administration/labels/pagination.json},
5,true,true,895fb1b6-410d-4972-9894-6b6a06d2b361,/content/documents/contenteximdemo/banners/banner1,contenteximdemo:bannerdocument,{file=/home/test/content-exim-demo-TRUNK/target/tomcat8x/temp/content-exim-demo/docdata/content/documents/contenteximdemo/banners/banner1.json},
6,true,true,9a3f1f5c-5302-43c4-9bec-584e810ffa2f,/content/documents/contenteximdemo/banners/banner2,contenteximdemo:bannerdocument,{file=/home/test/content-exim-demo-TRUNK/target/tomcat8x/temp/content-exim-demo/docdata/content/documents/contenteximdemo/banners/banner2.json},
7,true,true,9caea793-42b1-4f3e-a8e5-d58f810ce2ef,/content/documents/contenteximdemo/content/another-sample-document,contenteximdemo:contentdocument,{file=/home/test/content-exim-demo-TRUNK/target/tomcat8x/temp/content-exim-demo/docdata/content/documents/contenteximdemo/content/another-sample-document.json},
8,true,true,64ab4648-0c20-40d2-9f18-d7a394f0334b,/content/documents/contenteximdemo/content/sample-document,contenteximdemo:contentdocument,{file=/home/test/content-exim-demo-TRUNK/target/tomcat8x/temp/content-exim-demo/docdata/content/documents/contenteximdemo/content/sample-document.json},
9,true,true,b8f5eb45-7200-452a-b26e-3118a0dc60b8,/content/documents/contenteximdemo/events/2016/02/breakfast,contenteximdemo:eventsdocument,{file=/home/test/content-exim-demo-TRUNK/target/tomcat8x/temp/content-exim-demo/docdata/content/documents/contenteximdemo/events/2016/02/breakfast.json},
10,true,true,18e36c35-429d-4fee-b76e-eeabcbfc08bb,/content/documents/contenteximdemo/events/2016/02/introduction-speech,contenteximdemo:eventsdocument,{file=/home/test/content-exim-demo-TRUNK/target/tomcat8x/temp/content-exim-demo/docdata/content/documents/contenteximdemo/events/2016/02/introduction-speech.json},
11,true,true,7a29ec60-2689-48b2-aca2-49696c5c23eb,/content/documents/contenteximdemo/events/2016/02/workshop,contenteximdemo:eventsdocument,{file=/home/test/content-exim-demo-TRUNK/target/tomcat8x/temp/content-exim-demo/docdata/content/documents/contenteximdemo/events/2016/02/workshop.json},
12,true,true,9b993b22-ae8c-4bdc-833e-5a8ce41d68f5,/content/documents/contenteximdemo/faq/about-faq-items/faq-item-1,contenteximdemo:faqitem,{file=/home/test/content-exim-demo-TRUNK/target/tomcat8x/temp/content-exim-demo/docdata/content/documents/contenteximdemo/faq/about-faq-items/faq-item-1.json},
13,true,true,12863d11-40cc-4324-aeac-ffda4b787732,/content/documents/contenteximdemo/faq/about-faq-items/faq-item-2,contenteximdemo:faqitem,{file=/home/test/content-exim-demo-TRUNK/target/tomcat8x/temp/content-exim-demo/docdata/content/documents/contenteximdemo/faq/about-faq-items/faq-item-2.json},
14,true,true,6d1130dd-ba9f-481a-8a40-2d4d449da8a5,/content/documents/contenteximdemo/faq/about-faq,contenteximdemo:faqlist,{file=/home/test/content-exim-demo-TRUNK/target/tomcat8x/temp/content-exim-demo/docdata/content/documents/contenteximdemo/faq/about-faq.json},
15,true,true,30092f4e-2ef7-4c72-86a5-8ce895908937,/content/documents/contenteximdemo/news/2016/02/2013-harvest,contenteximdemo:newsdocument,{file=/home/test/content-exim-demo-TRUNK/target/tomcat8x/temp/content-exim-demo/docdata/content/documents/contenteximdemo/news/2016/02/2013-harvest.json},
16,true,true,aeda2bcd-b21d-4ead-a2e6-c64a2ca051c8,/content/documents/contenteximdemo/news/2016/02/the-gastropoda-news,contenteximdemo:newsdocument,{file=/home/test/content-exim-demo-TRUNK/target/tomcat8x/temp/content-exim-demo/docdata/content/documents/contenteximdemo/news/2016/02/the-gastropoda-news.json},
17,true,true,c580ac64-3874-4717-a6d9-e5ad72080abe,/content/documents/contenteximdemo/news/2016/02/the-medusa-news,contenteximdemo:newsdocument,{file=/home/test/content-exim-demo-TRUNK/target/tomcat8x/temp/content-exim-demo/docdata/content/documents/contenteximdemo/news/2016/02/the-medusa-news.json},
18,true,true,d931c679-fd25-42fc-8659-5a7f183f275e,/content/documents/contenteximdemo/videos/video-example,contenteximdemo:video,{file=/home/test/content-exim-demo-TRUNK/target/tomcat8x/temp/content-exim-demo/docdata/content/documents/contenteximdemo/videos/video-example.json},
===============================================================================================================