|
| 1 | +package com.marklogic.client.qconsole.impl; |
| 2 | + |
| 3 | +import com.marklogic.client.DatabaseClient; |
| 4 | +import com.marklogic.client.eval.EvalResultIterator; |
| 5 | +import com.marklogic.client.helper.LoggingObject; |
| 6 | +import com.marklogic.client.io.DOMHandle; |
| 7 | +import com.marklogic.client.io.FileHandle; |
| 8 | +import com.marklogic.client.io.Format; |
| 9 | +import com.marklogic.client.qconsole.WorkspaceManager; |
| 10 | +import org.springframework.util.FileCopyUtils; |
| 11 | + |
| 12 | +import java.io.File; |
| 13 | +import java.io.IOException; |
| 14 | +import java.util.ArrayList; |
| 15 | +import java.util.List; |
| 16 | + |
| 17 | +/** |
| 18 | + * Default implementation that uses the ML Java Client API to manage workspaces. By default, workspaces |
| 19 | + * will be exported and imported to/from ~/.qconsole/workspaces/(ML username). |
| 20 | + */ |
| 21 | +public class DefaultWorkspaceManager extends LoggingObject implements WorkspaceManager { |
| 22 | + |
| 23 | + private DatabaseClient client; |
| 24 | + |
| 25 | + private File baseDir; |
| 26 | + |
| 27 | + public DefaultWorkspaceManager(DatabaseClient client) { |
| 28 | + this.client = client; |
| 29 | + } |
| 30 | + |
| 31 | + @Override |
| 32 | + public List<File> exportWorkspaces(String user, String... workspaceNames) { |
| 33 | + String xquery = "declare namespace qconsole='http://marklogic.com/appservices/qconsole'; " + |
| 34 | + "declare variable $user external; " + |
| 35 | + "cts:search(/qconsole:workspace, cts:element-value-query(xs:QName('qconsole:userid'), string(xdmp:user($user))))"; |
| 36 | + |
| 37 | + EvalResultIterator result = client.newServerEval() |
| 38 | + .addVariable("user", user) |
| 39 | + .xquery(xquery).eval(); |
| 40 | + |
| 41 | + if (baseDir == null) { |
| 42 | + baseDir = getDefaultWorkspacesDir(); |
| 43 | + } |
| 44 | + |
| 45 | + File userDir = new File(baseDir, user); |
| 46 | + userDir.mkdirs(); |
| 47 | + |
| 48 | + List<File> files = new ArrayList<>(); |
| 49 | + |
| 50 | + while (result.hasNext()) { |
| 51 | + DOMHandle dom = result.next().get(new DOMHandle()); |
| 52 | + String workspaceId = getWorkspaceId(dom); |
| 53 | + File f = new File(userDir, workspaceId + ".xml"); |
| 54 | + try { |
| 55 | + FileCopyUtils.copy(dom.toBuffer(), f); |
| 56 | + if (logger.isInfoEnabled()) { |
| 57 | + logger.info(format("Exported workspace %s to %s", workspaceId, f.getAbsolutePath())); |
| 58 | + } |
| 59 | + files.add(f); |
| 60 | + } catch (IOException ie) { |
| 61 | + throw new RuntimeException("Unable to write workspace XML to file, workspace ID: " + workspaceId + "; cause: " + ie.getMessage()); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + return files; |
| 66 | + } |
| 67 | + |
| 68 | + protected String getWorkspaceId(DOMHandle dom) { |
| 69 | + return dom.get().getDocumentElement().getElementsByTagNameNS("http://marklogic.com/appservices/qconsole", "id").item(0).getTextContent(); |
| 70 | + } |
| 71 | + |
| 72 | + @Override |
| 73 | + public List<File> importWorkspaces(String user, String... workspaceNames) { |
| 74 | + if (baseDir == null) { |
| 75 | + baseDir = getDefaultWorkspacesDir(); |
| 76 | + } |
| 77 | + |
| 78 | + List<File> files = new ArrayList<>(); |
| 79 | + |
| 80 | + File userDir = new File(baseDir, user); |
| 81 | + if (!userDir.exists()) { |
| 82 | + return files; |
| 83 | + } |
| 84 | + |
| 85 | + String xquery = "import module namespace amped-qconsole = 'http://marklogic.com/appservices/qconsole/util-amped' at '/MarkLogic/appservices/qconsole/qconsole-amped.xqy'; " + |
| 86 | + "declare variable $workspace external; " + |
| 87 | + "declare variable $uri external; " + |
| 88 | + "amped-qconsole:qconsole-document-insert($uri, $workspace)"; |
| 89 | + |
| 90 | + for (File f : userDir.listFiles()) { |
| 91 | + if (f.isFile() && f.getName().endsWith(".xml")) { |
| 92 | + client.newServerEval() |
| 93 | + .addVariable("uri", "/workspaces/" + f.getName()) |
| 94 | + .addVariable("workspace", new FileHandle(f).withFormat(Format.XML)) |
| 95 | + .xquery(xquery).eval(); |
| 96 | + |
| 97 | + if (logger.isInfoEnabled()) { |
| 98 | + logger.info(format("Imported workspace from %s", f.getAbsolutePath())); |
| 99 | + } |
| 100 | + |
| 101 | + files.add(f); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + return files; |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * Defaults to ~/.qconsole/workspaces. |
| 110 | + * |
| 111 | + * @return |
| 112 | + */ |
| 113 | + protected File getDefaultWorkspacesDir() { |
| 114 | + File homeDir = new File(System.getProperty("user.home")); |
| 115 | + File qcDir = new File(homeDir, ".qconsole"); |
| 116 | + File dir = new File(qcDir, "workspaces"); |
| 117 | + dir.mkdirs(); |
| 118 | + return dir; |
| 119 | + } |
| 120 | + |
| 121 | + |
| 122 | + public void setBaseDir(File baseDir) { |
| 123 | + this.baseDir = baseDir; |
| 124 | + } |
| 125 | +} |
0 commit comments