|
16 | 16 |
|
17 | 17 | /** |
18 | 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). |
| 19 | + * will be exported and imported to/from ~/.qconsole/workspaces/(ML username). This can be overridden by |
| 20 | + * calling setBaseDir. |
20 | 21 | */ |
21 | 22 | public class DefaultWorkspaceManager extends LoggingObject implements WorkspaceManager { |
22 | 23 |
|
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 | | - } |
| 24 | + private DatabaseClient client; |
| 25 | + |
| 26 | + private File baseDir; |
| 27 | + |
| 28 | + public DefaultWorkspaceManager(DatabaseClient client) { |
| 29 | + this.client = client; |
| 30 | + } |
| 31 | + |
| 32 | + @Override |
| 33 | + public List<File> exportWorkspaces(String user, String... workspaceNames) { |
| 34 | + if (baseDir == null) { |
| 35 | + baseDir = getDefaultWorkspacesDir(); |
| 36 | + } |
| 37 | + |
| 38 | + File userDir = new File(baseDir, user); |
| 39 | + userDir.mkdirs(); |
| 40 | + |
| 41 | + List<File> files = new ArrayList<>(); |
| 42 | + |
| 43 | + for (String workspaceName : workspaceNames) { |
| 44 | + EvalResultIterator result = client.newServerEval() |
| 45 | + .addVariable("user", user) |
| 46 | + .addVariable("workspace", workspaceName) |
| 47 | + .xquery(QconsoleScripts.EXPORT).eval(); |
| 48 | + |
| 49 | + while (result.hasNext()) { |
| 50 | + DOMHandle dom = result.next().get(new DOMHandle()); |
| 51 | + File f = new File(userDir, workspaceName + ".xml"); |
| 52 | + try { |
| 53 | + FileCopyUtils.copy(dom.toBuffer(), f); |
| 54 | + if (logger.isInfoEnabled()) { |
| 55 | + logger.info(format("Exported workspace %s to %s", workspaceName, f.getAbsolutePath())); |
| 56 | + } |
| 57 | + files.add(f); |
| 58 | + } catch (IOException ie) { |
| 59 | + throw new RuntimeException("Unable to write workspace XML to file, workspace: " + workspaceName + "; cause: " + ie.getMessage()); |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + return files; |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + public List<File> importWorkspaces(String user, String... workspaceNames) { |
| 69 | + if (baseDir == null) { |
| 70 | + baseDir = getDefaultWorkspacesDir(); |
| 71 | + } |
| 72 | + |
| 73 | + List<File> files = new ArrayList<>(); |
| 74 | + |
| 75 | + File userDir = new File(baseDir, user); |
| 76 | + if (!userDir.exists()) { |
| 77 | + return files; |
| 78 | + } |
| 79 | + |
| 80 | + for (String workspace : workspaceNames) { |
| 81 | + File f = new File(userDir, workspace + ".xml"); |
| 82 | + if (f.isFile() && f.exists()) { |
| 83 | + client.newServerEval() |
| 84 | + .addVariable("user", user) |
| 85 | + .addVariable("exported-workspace", new FileHandle(f).withFormat(Format.XML)) |
| 86 | + .xquery(QconsoleScripts.IMPORT).eval(); |
| 87 | + |
| 88 | + if (logger.isInfoEnabled()) { |
| 89 | + logger.info(format("Imported workspace from %s", f.getAbsolutePath())); |
| 90 | + } |
| 91 | + |
| 92 | + files.add(f); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + return files; |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Defaults to ~/.qconsole/workspaces. |
| 101 | + * |
| 102 | + * @return |
| 103 | + */ |
| 104 | + protected File getDefaultWorkspacesDir() { |
| 105 | + File homeDir = new File(System.getProperty("user.home")); |
| 106 | + File qcDir = new File(homeDir, ".qconsole"); |
| 107 | + File dir = new File(qcDir, "workspaces"); |
| 108 | + dir.mkdirs(); |
| 109 | + return dir; |
| 110 | + } |
| 111 | + |
| 112 | + |
| 113 | + public void setBaseDir(File baseDir) { |
| 114 | + this.baseDir = baseDir; |
| 115 | + } |
125 | 116 | } |
0 commit comments