|
| 1 | +package com.marklogic.gradle.task.datamovement |
| 2 | + |
| 3 | +import com.marklogic.client.DatabaseClient |
| 4 | +import com.marklogic.client.datamovement.ExportListener |
| 5 | +import com.marklogic.client.ext.datamovement.QueryBatcherTemplate |
| 6 | +import com.marklogic.client.ext.datamovement.consumer.WriteToFileConsumer |
| 7 | +import org.gradle.api.tasks.TaskAction |
| 8 | + |
| 9 | +class ExportModulesTask extends DataMovementTask { |
| 10 | + |
| 11 | + @TaskAction |
| 12 | + void exportModules() { |
| 13 | + String exportPath = "" |
| 14 | + String uriPattern = "**" |
| 15 | + String databaseName = getAppConfig().getModulesDatabaseName() |
| 16 | + boolean logErrors = true |
| 17 | + |
| 18 | + if (project.hasProperty("exportPath")) { |
| 19 | + exportPath = project.property("exportPath") |
| 20 | + } else { |
| 21 | + List<String> modulePaths = getAppConfig().getModulePaths() |
| 22 | + if (modulePaths == null || modulePaths.isEmpty()) { |
| 23 | + println "Cannot export modules, no module paths are defined; can use -PexportPath= to define a path to export to" |
| 24 | + return |
| 25 | + } |
| 26 | + |
| 27 | + // Use the last path; if there are multiple, the ones at the beginning may be from dependencies |
| 28 | + exportPath = modulePaths.get(modulePaths.size() - 1) + "/root" |
| 29 | + } |
| 30 | + |
| 31 | + if (project.hasProperty("uriPattern")) { |
| 32 | + uriPattern = project.property("uriPattern") |
| 33 | + } |
| 34 | + |
| 35 | + if (project.hasProperty("databaseName")) { |
| 36 | + databaseName = project.property("databaseName") |
| 37 | + } |
| 38 | + |
| 39 | + if (project.hasProperty("logErrors")) { |
| 40 | + logErrors = Boolean.parseBoolean(project.property("logErrors")) |
| 41 | + } |
| 42 | + |
| 43 | + File out = new File(exportPath) |
| 44 | + println "Will export modules from database '" + databaseName + "' matching pattern '" + uriPattern + "' to path: " + out.getAbsolutePath() |
| 45 | + |
| 46 | + DatabaseClient client = getAppConfig().newAppServicesDatabaseClient(databaseName) |
| 47 | + try { |
| 48 | + WriteToFileConsumer consumer = new WriteToFileConsumer(out) |
| 49 | + consumer.setLogErrors(logErrors) |
| 50 | + |
| 51 | + QueryBatcherTemplate template = newQueryBatcherTemplate(client) |
| 52 | + ExportListener listener = new ExportListener() |
| 53 | + listener.onDocumentReady(consumer) |
| 54 | + template.applyOnUriPattern(listener, "**") |
| 55 | + } finally { |
| 56 | + client.release() |
| 57 | + } |
| 58 | + } |
| 59 | +} |
0 commit comments