Skip to content

Commit 54f0076

Browse files
committed
#216 New DMSDK tasks
1 parent 693b423 commit 54f0076

File tree

9 files changed

+330
-1
lines changed

9 files changed

+330
-1
lines changed

src/main/groovy/com/marklogic/gradle/MarkLogicPlugin.groovy

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ import com.marklogic.gradle.task.cluster.*
2121
import com.marklogic.gradle.task.cpf.DeployCpfTask
2222
import com.marklogic.gradle.task.cpf.LoadDefaultPipelinesTask
2323
import com.marklogic.gradle.task.databases.*
24+
import com.marklogic.gradle.task.datamovement.AddCollectionsTask
25+
import com.marklogic.gradle.task.datamovement.AddPermissionsTask
26+
import com.marklogic.gradle.task.datamovement.DeleteCollectionsTask
27+
import com.marklogic.gradle.task.datamovement.RemoveCollectionsTask
28+
import com.marklogic.gradle.task.datamovement.RemovePermissionsTask
29+
import com.marklogic.gradle.task.datamovement.SetCollectionsTask
2430
import com.marklogic.gradle.task.es.GenerateModelArtifactsTask
2531
import com.marklogic.gradle.task.export.ExportResourcesTask
2632
import com.marklogic.gradle.task.flexrep.*
@@ -114,14 +120,35 @@ class MarkLogicPlugin implements Plugin<Project> {
114120
project.task("mlClearModulesDatabase", type: ClearModulesDatabaseTask, group: dbGroup, dependsOn: "mlDeleteModuleTimestampsFile", description: "Deletes potentially all of the documents in the modules database; has a property for excluding documents from deletion")
115121
project.task("mlClearSchemasDatabase", type: ClearSchemasDatabaseTask, group: dbGroup, description: "Deletes all documents in the schemas database")
116122
project.task("mlClearTriggersDatabase", type: ClearTriggersDatabaseTask, group: dbGroup, description: "Deletes all documents in the triggers database")
117-
project.task("mlDeleteCollection", type: DeleteCollectionTask, group: dbGroup, description: "Delete the collection of documents in the content database; use -Pcollection=name to specify the collection name on the command line")
118123
project.task("mlDeployDatabases", type: DeployDatabasesTask, group: dbGroup, dependsOn: "mlPrepareRestApiDependencies", description: "Deploy each database, updating it if it exists, in the configuration directory")
119124
project.task("mlMergeContentDatabase", type: MergeContentDatabaseTask, group: dbGroup, description: "Merge the database named by mlAppConfig.contentDatabaseName")
120125
project.task("mlMergeDatabase", type: MergeDatabaseTask, group: dbGroup, description: "Merge the database named by the project property dbName; e.g. gradle mlMergeDatabase -PdbName=my-database")
121126
project.task("mlReindexContentDatabase", type: ReindexContentDatabaseTask, group: dbGroup, description: "Reindex the database named by mlAppConfig.contentDatabaseName")
122127
project.task("mlReindexDatabase", type: ReindexDatabaseTask, group: dbGroup, description: "Reindex the database named by the project property dbName; e.g. gradle mlReindexDatabase -PdbName=my-database")
123128
project.task("mlSetContentUpdatesAllowed", type: SetContentUpdatesAllowedTask, group: dbGroup, description: "Sets updated-allowed on each primary forest for the content database; must set the mode via e.g. -Pmode=flash-backup")
124129

130+
String dmGroup = "ml-Gradle Data Movement"
131+
132+
project.task("mlAddCollections", type: AddCollectionsTask, group: dmGroup, description: "Add all documents, either in a comma-separated list of " +
133+
"collection names specified by the 'sourceCollections' property or matching a URI pattern specified by the 'uriPattern' property, " +
134+
"to a comma-separated list of collection names specified by the 'collections' property")
135+
project.task("mlDeleteCollections", type: DeleteCollectionsTask, group: dmGroup, description: "Delete all documents in a comma-separated list of " +
136+
"collection names specified by the 'collections' property")
137+
project.task("mlRemoveCollections", type: RemoveCollectionsTask, group: dmGroup, description: "Remove all documents, either in a comma-separated list of " +
138+
"collection names specified by the 'sourceCollections' property or matching a URI pattern specified by the 'uriPattern' property, " +
139+
"from a comma-separated list of collection names specified by the 'collections' property; " +
140+
"if the values of 'sourceCollections' and 'collections' are the same, you only need to specify the 'collections' property")
141+
project.task("mlSetCollections", type: SetCollectionsTask, group: dmGroup, description: "Set collections on all documents, either in a comma-separated list of " +
142+
"collection names specified by the 'sourceCollections' property or matching a URI pattern specified by the 'uriPattern' property, " +
143+
"to a comma-separated list of collection names specified by the 'collections' property")
144+
145+
project.task("mlAddPermissions", type: AddPermissionsTask, group: dmGroup, description: "Add permissions, specified as a comma-separated list of roles and capabilities via the 'permissions' property, " +
146+
"to all documents either in the set of collection names specified by the 'collections' property or with URIs matching the pattern specified by the 'uriPattern' property")
147+
project.task("mlRemovePermissions", type: RemovePermissionsTask, group: dmGroup, description: "Remove permissions, specified as a comma-separated list of roles and capabilities via the 'permissions' property, " +
148+
"from all documents either in the set of collection names specified by the 'collections' property or with URIs matching the pattern specified by the 'uriPattern' property")
149+
project.task("mlSetPermissions", type: AddPermissionsTask, group: dmGroup, description: "Set permissions, specified as a comma-separated list of roles and capabilities via the 'permissions' property, " +
150+
"on all documents in the set of collection names specified by the 'collections' property or with URIs matching the pattern specified by the 'uriPattern' property")
151+
125152
String devGroup = "ml-gradle Development"
126153
project.task("mlCreateResource", type: CreateResourceTask, group: devGroup, description: "Create a new resource extension in the modules services directory; use -PresourceName and -PresourceType to set the resource name and type (either xqy or sjs)")
127154
project.task("mlCreateTransform", type: CreateTransformTask, group: devGroup, description: "Create a new transform in the modules transforms directory; use -PtranssformName and -PtransformType to set the transform name and type (xqy, xsl, or sjs)")
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.marklogic.gradle.task.datamovement
2+
3+
import com.marklogic.client.datamovement.QueryBatchListener
4+
import com.marklogic.client.ext.datamovement.CollectionsQueryBatcherBuilder
5+
import com.marklogic.client.ext.datamovement.QueryBatcherBuilder
6+
import com.marklogic.client.ext.datamovement.UriPatternQueryBatcherBuilder
7+
import com.marklogic.client.ext.datamovement.listener.AddCollectionsListener
8+
import org.gradle.api.tasks.TaskAction
9+
10+
class AddCollectionsTask extends DataMovementTask {
11+
12+
@TaskAction
13+
void addCollections() {
14+
if (
15+
(!project.hasProperty("sourceCollections") && !project.hasProperty("pattern")) ||
16+
!project.hasProperty("collections")
17+
) {
18+
println "Invalid inputs; " + getDescription()
19+
return;
20+
}
21+
22+
String[] collections = getProject().property("collections").split(",")
23+
QueryBatchListener listener = new AddCollectionsListener(collections)
24+
QueryBatcherBuilder builder = null
25+
26+
String message = " to collections " + Arrays.asList(collections);
27+
if (project.hasProperty("collections")) {
28+
String[] sourceCollections = getProject().property("sourceCollections").split(",")
29+
message = "documents in collections " + Arrays.asList(sourceCollections) + message
30+
builder = new CollectionsQueryBatcherBuilder(sourceCollections)
31+
} else if (project.hasProperty("uriPattern")) {
32+
String pattern = getProject().property("pattern")
33+
message = "documents matching URI pattern " + pattern + message
34+
builder = new UriPatternQueryBatcherBuilder(pattern)
35+
}
36+
37+
println "Adding " + message
38+
applyWithQueryBatcherBuilder(listener, builder)
39+
println "Finished adding " + message
40+
}
41+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.marklogic.gradle.task.datamovement
2+
3+
import com.marklogic.client.datamovement.QueryBatchListener
4+
import com.marklogic.client.ext.datamovement.CollectionsQueryBatcherBuilder
5+
import com.marklogic.client.ext.datamovement.QueryBatcherBuilder
6+
import com.marklogic.client.ext.datamovement.UriPatternQueryBatcherBuilder
7+
import com.marklogic.client.ext.datamovement.listener.AddPermissionsListener
8+
import org.gradle.api.tasks.TaskAction
9+
10+
class AddPermissionsTask extends DataMovementTask {
11+
12+
@TaskAction
13+
void addPermissions() {
14+
if ((!project.hasProperty("collections") && !project.hasProperty("uriPattern")) || !project.hasProperty("permissions")) {
15+
println "Invalid input; " + getDescription()
16+
return;
17+
}
18+
19+
String[] permissions = getProject().property("permissions").split(",")
20+
QueryBatchListener listener = new AddPermissionsListener(permissions)
21+
QueryBatcherBuilder builder = null
22+
23+
String message = "permissions " + Arrays.asList(permissions) + " to documents "
24+
if (project.hasProperty("collections")) {
25+
String[] collections = getProject().property("collections").split(",")
26+
message += "in collections " + Arrays.asList(collections)
27+
builder = new CollectionsQueryBatcherBuilder(collections)
28+
} else if (project.hasProperty("uriPattern")) {
29+
String pattern = getProject().property("uriPattern")
30+
message += "matching URI pattern " + pattern
31+
builder = new UriPatternQueryBatcherBuilder(pattern)
32+
}
33+
34+
println "Adding " + message
35+
applyWithQueryBatcherBuilder(listener, builder)
36+
println "Finished adding " + message
37+
}
38+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.marklogic.gradle.task.datamovement
2+
3+
import com.marklogic.client.DatabaseClient
4+
import com.marklogic.client.datamovement.QueryBatchListener
5+
import com.marklogic.client.ext.datamovement.QueryBatcherBuilder
6+
import com.marklogic.client.ext.datamovement.QueryBatcherTemplate
7+
import com.marklogic.gradle.task.MarkLogicTask
8+
9+
class DataMovementTask extends MarkLogicTask {
10+
11+
void applyOnCollections(QueryBatchListener listener, String... collections) {
12+
DatabaseClient client = newClient()
13+
try {
14+
new QueryBatcherTemplate(client).applyOnCollections(listener, collections);
15+
} finally {
16+
client.release()
17+
}
18+
}
19+
20+
void applyOnUriPattern(QueryBatchListener listener, String uriPattern) {
21+
DatabaseClient client = newClient()
22+
try {
23+
new QueryBatcherTemplate(client).applyOnUriPattern(listener, uriPattern);
24+
} finally {
25+
client.release()
26+
}
27+
}
28+
29+
void applyWithQueryBatcherBuilder(QueryBatchListener listener, QueryBatcherBuilder builder) {
30+
DatabaseClient client = newClient()
31+
try {
32+
new QueryBatcherTemplate(client).apply(listener, builder)
33+
} finally {
34+
client.release()
35+
}
36+
}
37+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.marklogic.gradle.task.datamovement
2+
3+
import com.marklogic.client.datamovement.DeleteListener
4+
import org.gradle.api.tasks.TaskAction
5+
6+
class DeleteCollectionsTask extends DataMovementTask {
7+
8+
@TaskAction
9+
void deleteCollections() {
10+
if (!project.hasProperty("collections")) {
11+
println "Invalid inputs; " + getDescription()
12+
return
13+
}
14+
15+
String[] collections = getProject().property("collections").split(",")
16+
String message = "collections " + Arrays.asList(collections)
17+
println "Deleting " + message
18+
applyOnCollections(new DeleteListener(), collections)
19+
println "Finished deleting " + message
20+
}
21+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.marklogic.gradle.task.datamovement
2+
3+
import com.marklogic.client.datamovement.QueryBatchListener
4+
import com.marklogic.client.ext.datamovement.CollectionsQueryBatcherBuilder
5+
import com.marklogic.client.ext.datamovement.QueryBatcherBuilder
6+
import com.marklogic.client.ext.datamovement.UriPatternQueryBatcherBuilder
7+
import com.marklogic.client.ext.datamovement.listener.RemoveCollectionsListener
8+
import org.gradle.api.tasks.TaskAction
9+
10+
class RemoveCollectionsTask extends DataMovementTask {
11+
12+
/**
13+
* This task allows for specifying source collections for documents that should be removed from target collections,
14+
* but it's often the case that that list of collections is the same (and often just one collection). Thus, only the
15+
* "collections" property needs to be specified if the lists are the same.
16+
*/
17+
@TaskAction
18+
void removeCollections() {
19+
if (!project.hasProperty("collections")) {
20+
println "Invalid inputs; " + getDescription()
21+
return;
22+
}
23+
24+
String[] collections = getProject().property("collections").split(",")
25+
String message = " from collections " + Arrays.asList(collections)
26+
QueryBatchListener listener = new RemoveCollectionsListener(collections)
27+
QueryBatcherBuilder builder = null
28+
29+
if (project.hasProperty("uriPattern")) {
30+
String pattern = project.property("uriPattern")
31+
message = "documents matching URI pattern " + pattern + message
32+
builder = new UriPatternQueryBatcherBuilder(pattern)
33+
}
34+
else {
35+
String[] sourceCollections = collections
36+
if (project.hasProperty("sourceCollections")) {
37+
sourceCollections = getProject().property("sourceCollections").split(",")
38+
}
39+
message = "documents in collections " + Arrays.asList(sourceCollections) + message
40+
builder = new CollectionsQueryBatcherBuilder(sourceCollections)
41+
}
42+
43+
println "Removing " + message
44+
applyWithQueryBatcherBuilder(listener, builder)
45+
println "Finished removing " + message
46+
}
47+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.marklogic.gradle.task.datamovement
2+
3+
import com.marklogic.client.datamovement.QueryBatchListener
4+
import com.marklogic.client.ext.datamovement.CollectionsQueryBatcherBuilder
5+
import com.marklogic.client.ext.datamovement.QueryBatcherBuilder
6+
import com.marklogic.client.ext.datamovement.UriPatternQueryBatcherBuilder
7+
import com.marklogic.client.ext.datamovement.listener.RemovePermissionsListener
8+
import org.gradle.api.tasks.TaskAction
9+
10+
class RemovePermissionsTask extends DataMovementTask {
11+
12+
@TaskAction
13+
void removePermissions() {
14+
if ((!project.hasProperty("collections") && !project.hasProperty("uriPattern")) || !project.hasProperty("permissions")) {
15+
println "Invalid input; " + getDescription()
16+
return;
17+
}
18+
19+
String[] permissions = getProject().property("permissions").split(",")
20+
QueryBatchListener listener = new RemovePermissionsListener(permissions)
21+
QueryBatcherBuilder builder = null
22+
23+
String message = "permissions " + Arrays.asList(permissions) + " from documents "
24+
if (project.hasProperty("collections")) {
25+
String[] collections = getProject().property("collections").split(",")
26+
message += "in collections " + Arrays.asList(collections)
27+
builder = new CollectionsQueryBatcherBuilder(collections)
28+
} else if (project.hasProperty("uriPattern")) {
29+
String pattern = getProject().property("uriPattern")
30+
message += "matching URI pattern " + pattern
31+
builder = new UriPatternQueryBatcherBuilder(pattern)
32+
}
33+
34+
println "Removing " + message
35+
applyWithQueryBatcherBuilder(listener, builder)
36+
println "Finished removing " + message
37+
}
38+
}
39+
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.marklogic.gradle.task.datamovement
2+
3+
import com.marklogic.client.datamovement.QueryBatchListener
4+
import com.marklogic.client.ext.datamovement.CollectionsQueryBatcherBuilder
5+
import com.marklogic.client.ext.datamovement.QueryBatcherBuilder
6+
import com.marklogic.client.ext.datamovement.UriPatternQueryBatcherBuilder
7+
import com.marklogic.client.ext.datamovement.listener.SetCollectionsListener
8+
import org.gradle.api.tasks.TaskAction
9+
10+
class SetCollectionsTask extends DataMovementTask {
11+
12+
@TaskAction
13+
void setCollections() {
14+
if (
15+
(!project.hasProperty("sourceCollections") && !project.hasProperty("pattern")) ||
16+
!project.hasProperty("collections")
17+
) {
18+
println "Invalid inputs; " + getDescription()
19+
return;
20+
}
21+
22+
String[] collections = getProject().property("collections").split(",")
23+
QueryBatchListener listener = new SetCollectionsListener(collections)
24+
QueryBatcherBuilder builder = null
25+
26+
String message = " collections " + Arrays.asList(collections);
27+
if (project.hasProperty("collections")) {
28+
String[] sourceCollections = getProject().property("sourceCollections").split(",")
29+
message += " on documents in collections " + Arrays.asList(sourceCollections)
30+
builder = new CollectionsQueryBatcherBuilder(sourceCollections)
31+
} else if (project.hasProperty("uriPattern")) {
32+
String pattern = getProject().property("pattern")
33+
message += " on documents matching URI pattern " + pattern
34+
builder = new UriPatternQueryBatcherBuilder(pattern)
35+
}
36+
37+
println "Setting " + message
38+
applyWithQueryBatcherBuilder(listener, builder)
39+
println "Finished setting " + message
40+
}
41+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.marklogic.gradle.task.datamovement
2+
3+
import com.marklogic.client.datamovement.QueryBatchListener
4+
import com.marklogic.client.ext.datamovement.CollectionsQueryBatcherBuilder
5+
import com.marklogic.client.ext.datamovement.QueryBatcherBuilder
6+
import com.marklogic.client.ext.datamovement.UriPatternQueryBatcherBuilder
7+
import com.marklogic.client.ext.datamovement.listener.SetPermissionsListener
8+
import org.gradle.api.tasks.TaskAction
9+
10+
class SetPermissionsTask extends DataMovementTask {
11+
12+
@TaskAction
13+
void setPermissions() {
14+
if ((!project.hasProperty("collections") && !project.hasProperty("uriPattern")) || !project.hasProperty("permissions")) {
15+
println "Invalid input; " + getDescription()
16+
return;
17+
}
18+
19+
String[] permissions = getProject().property("permissions").split(",")
20+
QueryBatchListener listener = new SetPermissionsListener(permissions)
21+
QueryBatcherBuilder builder = null
22+
23+
String message = "permissions " + Arrays.asList(permissions) + " on documents "
24+
if (project.hasProperty("collections")) {
25+
String[] collections = getProject().property("collections").split(",")
26+
message += "in collections " + Arrays.asList(collections)
27+
builder = new CollectionsQueryBatcherBuilder(collections)
28+
} else if (project.hasProperty("uriPattern")) {
29+
String pattern = getProject().property("uriPattern")
30+
message += "matching URI pattern " + pattern
31+
builder = new UriPatternQueryBatcherBuilder(pattern)
32+
}
33+
34+
println "Setting " + message
35+
applyWithQueryBatcherBuilder(listener, builder)
36+
println "Finished setting " + message
37+
}
38+
}

0 commit comments

Comments
 (0)