|
6 | 6 | import com.marklogic.hub.HubConfig; |
7 | 7 | import com.marklogic.hub.deploy.commands.LoadUserModulesCommand; |
8 | 8 |
|
| 9 | +import java.util.List; |
| 10 | +import java.util.stream.Collectors; |
| 11 | + |
9 | 12 | public class CopyQueryOptionsCommand extends AbstractCommand { |
10 | 13 |
|
11 | 14 | private HubConfig hubConfig; |
| 15 | + private List<String> groupNames; |
| 16 | + private List<String> serverNames; |
| 17 | + private String jobDatabaseName; |
| 18 | + |
| 19 | + private String scriptResponse; |
12 | 20 |
|
13 | | - public CopyQueryOptionsCommand(HubConfig hubConfig) { |
| 21 | + public CopyQueryOptionsCommand(HubConfig hubConfig, List<String> groupNames, List<String> serverNames, String jobDatabaseName) { |
14 | 22 | this.hubConfig = hubConfig; |
| 23 | + this.groupNames = groupNames; |
| 24 | + this.serverNames = serverNames; |
| 25 | + this.jobDatabaseName = jobDatabaseName; |
15 | 26 |
|
16 | 27 | // Need to run this after the contents of ml-modules-final is loaded |
17 | 28 | setExecuteSortOrder(new LoadUserModulesCommand().getExecuteSortOrder() + 1); |
18 | 29 | } |
19 | 30 |
|
20 | 31 | @Override |
21 | 32 | public void execute(CommandContext context) { |
22 | | - String script = "declareUpdate();\n" + |
23 | | - "[\n" + |
24 | | - " '/Evaluator/data-hub-JOBS/rest-api/options/jobs.xml',\n" + |
25 | | - " '/Evaluator/data-hub-JOBS/rest-api/options/traces.xml',\n" + |
26 | | - " '/Evaluator/data-hub-STAGING/rest-api/options/default.xml',\n" + |
27 | | - " '/Evaluator/data-hub-STAGING/rest-api/options/exp-default.xml'\n" + |
28 | | - "].forEach(uri => {\n" + |
29 | | - " if (fn.docAvailable(uri)) { console.log('Reproducing: ' + uri); xdmp.documentInsert(uri.replace('Evaluator', 'Curator'), cts.doc(uri), \n" + |
30 | | - " xdmp.documentGetPermissions(uri), xdmp.documentGetCollections(uri));}\n" + |
31 | | - "});\n" + |
32 | | - |
33 | | - "\n" + |
34 | | - "const stagingUri = '/Evaluator/data-hub-STAGING/rest-api/options/default.xml';\n" + |
35 | | - "if (fn.docAvailable(stagingUri)) { " + |
36 | | - " xdmp.documentInsert('/Evaluator/data-hub-FINAL/rest-api/options/default.xml', cts.doc(stagingUri), xdmp.documentGetPermissions(stagingUri), xdmp.documentGetCollections(stagingUri));\n" + |
37 | | - " xdmp.documentInsert('/Curator/data-hub-FINAL/rest-api/options/default.xml', cts.doc(stagingUri), xdmp.documentGetPermissions(stagingUri), xdmp.documentGetCollections(stagingUri));" + |
38 | | - "}\n\n" + |
39 | | - |
40 | | - "const explorerUri = '/Evaluator/data-hub-STAGING/rest-api/options/exp-default.xml';\n" + |
41 | | - "if (fn.docAvailable(explorerUri)) { " + |
42 | | - " xdmp.documentInsert('/Evaluator/data-hub-FINAL/rest-api/options/exp-default.xml', cts.doc(explorerUri), xdmp.documentGetPermissions(explorerUri), xdmp.documentGetCollections(explorerUri));\n" + |
43 | | - " xdmp.documentInsert('/Curator/data-hub-FINAL/rest-api/options/exp-default.xml', cts.doc(explorerUri), xdmp.documentGetPermissions(explorerUri), xdmp.documentGetCollections(explorerUri));" + |
44 | | - "}\n\n" + |
45 | | - |
46 | | - "[\n" + |
47 | | - " '/Analyzer/data-hub-ANALYTICS/rest-api/options/default.xml',\n" + |
48 | | - " '/Analyzer/data-hub-ANALYTICS-REST/rest-api/options/default.xml',\n" + |
49 | | - " '/Operator/data-hub-OPERATION/rest-api/options/default.xml',\n" + |
50 | | - " '/Operator/data-hub-OPERATION-REST/rest-api/options/default.xml',\n" + |
51 | | - " '/Evaluator/data-hub-ANALYTICS/rest-api/options/default.xml',\n" + |
52 | | - " '/Evaluator/data-hub-OPERATION/rest-api/options/default.xml'\n" + |
53 | | - "].forEach(uri => {\n" + |
54 | | - " if (fn.docAvailable(stagingUri)) {console.log('Inserting: ' + uri); xdmp.documentInsert(uri, cts.doc(stagingUri), xdmp.documentGetPermissions(stagingUri), \n" + |
55 | | - " xdmp.documentGetCollections(stagingUri));}\n" + |
56 | | - "})"; |
| 33 | + final String script = buildScript(); |
57 | 34 |
|
58 | 35 | DatabaseClient client = hubConfig.newModulesDbClient(); |
59 | 36 | try { |
60 | | - client.newServerEval().javascript(script).eval().close(); |
| 37 | + scriptResponse = client.newServerEval().javascript(script) |
| 38 | + .addVariable("stagingServer", serverNames.get(0)) |
| 39 | + .addVariable("jobServer", jobDatabaseName) |
| 40 | + .addVariable("groups", groupNames.stream().collect(Collectors.joining(","))) |
| 41 | + .addVariable("servers", serverNames.subList(1, serverNames.size()).stream().collect(Collectors.joining(","))) |
| 42 | + .evalAs(String.class); |
61 | 43 | } finally { |
62 | 44 | client.release(); |
63 | 45 | } |
64 | 46 |
|
| 47 | + logger.info("Copied search options to the following URIs in the modules database:\n" + scriptResponse); |
| 48 | + } |
| 49 | + |
| 50 | + protected String buildScript() { |
| 51 | + return "declareUpdate();\n" + |
| 52 | + "var stagingServer;\n" + |
| 53 | + "var jobServer;\n" + |
| 54 | + "var groups;\n" + |
| 55 | + "var servers;\n" + |
| 56 | + "\n" + |
| 57 | + "groups = groups.split(',');\n" + |
| 58 | + "servers = servers.split(',');\n" + |
| 59 | + "const firstGroup = groups[0];\n" + |
| 60 | + "\n" + |
| 61 | + "// This array is returned for testing purposes\n" + |
| 62 | + "const createdUris = [];\n" + |
| 63 | + "\n" + |
| 64 | + "let stagingUris = cts.uriMatch('/' + firstGroup + '/' + stagingServer + '/rest-api/options/*').toArray().forEach(uri => {\n" + |
| 65 | + " const doc = cts.doc(uri);\n" + |
| 66 | + " const perms = xdmp.documentGetPermissions(uri);\n" + |
| 67 | + " const collections = xdmp.documentGetCollections(uri);\n" + |
| 68 | + " // Copy options from first group to other groups for stagingServer\n" + |
| 69 | + " groups.slice(1).map(group => {\n" + |
| 70 | + " let newUri = uri.toString().replace('/' + firstGroup + '/', '/' + group + '/');\n" + |
| 71 | + " xdmp.documentInsert(newUri, doc, perms, collections);\n" + |
| 72 | + " createdUris.push(newUri);\n" + |
| 73 | + " });\n" + |
| 74 | + " \n" + |
| 75 | + " // Copy options to every group for all other servers\n" + |
| 76 | + " groups.forEach(group => {\n" + |
| 77 | + " servers.forEach(server => {\n" + |
| 78 | + " let newUri = uri.toString().replace('/' + firstGroup + '/', '/' + group + '/');\n" + |
| 79 | + " newUri = newUri.replace('/' + stagingServer + '/', '/' + server + '/');\n" + |
| 80 | + " xdmp.documentInsert(newUri, doc, perms, collections);\n" + |
| 81 | + " createdUris.push(newUri);\n" + |
| 82 | + " });\n" + |
| 83 | + " });\n" + |
| 84 | + "});\n" + |
| 85 | + "\n" + |
| 86 | + "// Copy jobs options to job servers in other groups\n" + |
| 87 | + "cts.uriMatch('/' + firstGroup + '/' + jobServer + '/rest-api/options/*').toArray().map(uri => {\n" + |
| 88 | + " const doc = cts.doc(uri);\n" + |
| 89 | + " const perms = xdmp.documentGetPermissions(uri);\n" + |
| 90 | + " const collections = xdmp.documentGetCollections(uri);\n" + |
| 91 | + " groups.slice(1).forEach(group => {\n" + |
| 92 | + " let newUri = uri.toString().replace('/' + firstGroup + '/', '/' + group + '/');\n" + |
| 93 | + " xdmp.documentInsert(newUri, doc, perms, collections);\n" + |
| 94 | + " createdUris.push(newUri);\n" + |
| 95 | + " });\n" + |
| 96 | + "});\n" + |
| 97 | + "\n" + |
| 98 | + "createdUris;"; |
| 99 | + } |
| 100 | + |
| 101 | + public String getScriptResponse() { |
| 102 | + return scriptResponse; |
65 | 103 | } |
66 | 104 | } |
0 commit comments