1 | package eu.clarin.cmdi.mdservice.action; |
---|
2 | |
---|
3 | import java.io.BufferedInputStream; |
---|
4 | import java.io.ByteArrayInputStream; |
---|
5 | import java.io.File; |
---|
6 | import java.io.FileInputStream; |
---|
7 | import java.io.FileWriter; |
---|
8 | import java.io.IOException; |
---|
9 | import java.io.InputStream; |
---|
10 | import java.io.StringWriter; |
---|
11 | import java.io.UnsupportedEncodingException; |
---|
12 | import java.net.MalformedURLException; |
---|
13 | import java.net.URL; |
---|
14 | import java.text.DateFormat; |
---|
15 | import java.text.SimpleDateFormat; |
---|
16 | import java.util.Date; |
---|
17 | |
---|
18 | import javax.xml.parsers.DocumentBuilder; |
---|
19 | import javax.xml.parsers.DocumentBuilderFactory; |
---|
20 | import javax.xml.parsers.ParserConfigurationException; |
---|
21 | import javax.xml.transform.OutputKeys; |
---|
22 | import javax.xml.transform.Transformer; |
---|
23 | import javax.xml.transform.TransformerConfigurationException; |
---|
24 | import javax.xml.transform.TransformerException; |
---|
25 | import javax.xml.transform.TransformerFactory; |
---|
26 | import javax.xml.transform.TransformerFactoryConfigurationError; |
---|
27 | import javax.xml.transform.dom.DOMSource; |
---|
28 | import javax.xml.transform.stream.StreamResult; |
---|
29 | import javax.xml.xpath.XPath; |
---|
30 | import javax.xml.xpath.XPathConstants; |
---|
31 | import javax.xml.xpath.XPathExpression; |
---|
32 | import javax.xml.xpath.XPathExpressionException; |
---|
33 | import javax.xml.xpath.XPathFactory; |
---|
34 | |
---|
35 | import net.sf.json.JSON; |
---|
36 | import net.sf.json.JSONObject; |
---|
37 | import net.sf.json.JSONSerializer; |
---|
38 | import net.sf.json.xml.XMLSerializer; |
---|
39 | |
---|
40 | import org.apache.log4j.Logger; |
---|
41 | import org.w3c.dom.DOMException; |
---|
42 | import org.w3c.dom.Document; |
---|
43 | import org.w3c.dom.Element; |
---|
44 | import org.w3c.dom.Node; |
---|
45 | import org.w3c.dom.NodeList; |
---|
46 | import org.xml.sax.SAXException; |
---|
47 | |
---|
48 | import eu.clarin.cmdi.mdservice.internal.Utils; |
---|
49 | import eu.clarin.cmdi.mdservice.internal.MDTransformer; |
---|
50 | import eu.clarin.cmdi.mdservice.model.Diagnostic; |
---|
51 | |
---|
52 | |
---|
53 | /** |
---|
54 | * |
---|
55 | * @author |
---|
56 | * |
---|
57 | */ |
---|
58 | public class WorkspaceAction extends GenericAction |
---|
59 | //implements ServletRequestAware |
---|
60 | { |
---|
61 | |
---|
62 | private static final long serialVersionUID = 1L; |
---|
63 | private static Logger log = Logger.getLogger("WorkspaceAction"); |
---|
64 | |
---|
65 | // workspaceprofiles |
---|
66 | public static String USER = "user"; |
---|
67 | public static String SERVER = "server"; |
---|
68 | |
---|
69 | // save element type |
---|
70 | public static String SE_WORKSPACE = "workspace"; |
---|
71 | public static String SE_QUERYSET = "queryset"; |
---|
72 | public static String SE_QUERY = "query"; |
---|
73 | public static String SE_BOOKMARKSET = "bookmarkset"; |
---|
74 | public static String SE_BOOKMARK = "bookmark"; |
---|
75 | |
---|
76 | // administration query identification |
---|
77 | public static String ADMIN = "admin"; |
---|
78 | public static String FILENAME = "filename"; |
---|
79 | |
---|
80 | public static String PROFILENAME_BASE = "WorkspaceProfile_"; |
---|
81 | public static String PROFILENAME_SERVER = "WorkspaceProfileServer.xml"; |
---|
82 | public static String PROFILENAME_DEFAULT = PROFILENAME_BASE + "default.xml"; |
---|
83 | public static String ADMINISTRATION_FILENAME = "Administration.xml"; |
---|
84 | |
---|
85 | public static String WORKSPACE_FILENAME = "WorkspaceProfile.xml"; |
---|
86 | /** |
---|
87 | * Properties to be filled by Struts with according request-parameters |
---|
88 | */ |
---|
89 | //private String actionkey; |
---|
90 | private String data; |
---|
91 | private String type; //workspacetype |
---|
92 | private String elementtype; |
---|
93 | private String qid; |
---|
94 | private String qsid; |
---|
95 | //private String format; |
---|
96 | //private String userMsg; |
---|
97 | private String base_path; |
---|
98 | |
---|
99 | |
---|
100 | private static WorkspaceAction singleton; |
---|
101 | private static String workspacefilename; |
---|
102 | private static Document workspace_doc; |
---|
103 | |
---|
104 | //private String workspaceindex_path; |
---|
105 | //private static Integer querycounter; |
---|
106 | private static final Integer start_query = 1; |
---|
107 | |
---|
108 | |
---|
109 | |
---|
110 | //private HttpServletRequest request; |
---|
111 | |
---|
112 | public String Workspace_filename(){ |
---|
113 | return WORKSPACE_FILENAME; |
---|
114 | } |
---|
115 | |
---|
116 | public void setWorkspace_filename(String workspace_filename) { |
---|
117 | this.WORKSPACE_FILENAME = workspace_filename; |
---|
118 | } |
---|
119 | |
---|
120 | public String getData(){ |
---|
121 | return data; |
---|
122 | } |
---|
123 | |
---|
124 | public void setData(String data) { |
---|
125 | this.data = data; |
---|
126 | } |
---|
127 | |
---|
128 | public String getEelemnttype(){ |
---|
129 | return elementtype; |
---|
130 | } |
---|
131 | |
---|
132 | public void setElementtype(String elementtype) { |
---|
133 | this.elementtype = elementtype; |
---|
134 | } |
---|
135 | |
---|
136 | public String getType() { |
---|
137 | return type; |
---|
138 | } |
---|
139 | |
---|
140 | public void setType(String type) { |
---|
141 | this.type = type; |
---|
142 | } |
---|
143 | |
---|
144 | public String getQid() { |
---|
145 | return qid; |
---|
146 | } |
---|
147 | |
---|
148 | public void setQid(String qid) { |
---|
149 | this.qid = qid; |
---|
150 | } |
---|
151 | |
---|
152 | public void setQsid(String qsid) { |
---|
153 | this.qsid = qsid; |
---|
154 | } |
---|
155 | |
---|
156 | public String getQsid() { |
---|
157 | return qsid; |
---|
158 | } |
---|
159 | |
---|
160 | public WorkspaceAction () { |
---|
161 | this.elementtype = WorkspaceAction.SE_WORKSPACE; |
---|
162 | workspacefilename = Utils.getConfig().getProperty("workspaceprofile.path") + Utils.getConfig().getProperty("workspace.file"); |
---|
163 | if (workspace_doc == null){ |
---|
164 | //workspacefilename = Utils.getConfig().getProperty("workspace.path") + Utils.getConfig().getProperty("workspace.file"); |
---|
165 | initDocument(); |
---|
166 | } |
---|
167 | } |
---|
168 | |
---|
169 | public static WorkspaceAction getWorkspaceAction() { |
---|
170 | if (singleton == null) { |
---|
171 | singleton = new WorkspaceAction(); |
---|
172 | } |
---|
173 | return singleton; |
---|
174 | } |
---|
175 | |
---|
176 | protected void finalize() throws Throwable { |
---|
177 | try { |
---|
178 | //updateQuerycounter(); |
---|
179 | //saveDocument(); |
---|
180 | } catch(Exception e) { |
---|
181 | } |
---|
182 | finally { |
---|
183 | super.finalize(); |
---|
184 | //more code can be written here as per need of application |
---|
185 | } |
---|
186 | } |
---|
187 | /** |
---|
188 | * primitive identification of the target-proxy |
---|
189 | * base for finding the right base_url in the props |
---|
190 | * subclass has to override with its specific proxykey |
---|
191 | * @return |
---|
192 | */ |
---|
193 | public String getProxyKey() { |
---|
194 | return "workspaceprofile"; |
---|
195 | } |
---|
196 | |
---|
197 | @Override |
---|
198 | public String getActionContentType() { |
---|
199 | if (this.getFormat().toLowerCase().startsWith("json")) { |
---|
200 | this.setActionContentType("application/json"); |
---|
201 | //this.actionContentType = "application/xhtml+xml"; |
---|
202 | } else { |
---|
203 | this.setActionContentType("text/xml"); |
---|
204 | } |
---|
205 | |
---|
206 | return getActionContentType(); |
---|
207 | } |
---|
208 | |
---|
209 | //@Override |
---|
210 | public URL getBaseURL() { |
---|
211 | File file=new File(getPath()); |
---|
212 | URL url=null; |
---|
213 | try{ |
---|
214 | url=file.toURI().toURL(); |
---|
215 | log.debug("DEBUG: WorkspaceAction.getBaseURL(): " + url); |
---|
216 | } catch (MalformedURLException e){ |
---|
217 | log.error("getBaseURL: JSON exception has been caught;\n getPath=" + getPath() ); |
---|
218 | getDiagnostics().Add(Diagnostic.SYSTEM_ERROR, "WorkspaceAction.getBaseURL()", "JSON exception has been caught;\n getPath=" + getPath() ); |
---|
219 | } |
---|
220 | |
---|
221 | return url; |
---|
222 | } |
---|
223 | |
---|
224 | public String getBasePath() { |
---|
225 | if (base_path == null) { |
---|
226 | base_path = Utils.getConfig().getProperty(getProxyKey() + ".path"); |
---|
227 | } |
---|
228 | return base_path; |
---|
229 | } |
---|
230 | |
---|
231 | |
---|
232 | public String getWorkspaceProfile() { |
---|
233 | String profilename = null; |
---|
234 | |
---|
235 | if (type.toLowerCase().equals(USER)) { |
---|
236 | if (getServletRequest().getRemoteUser() != null) { |
---|
237 | profilename = PROFILENAME_BASE + getServletRequest().getRemoteUser() + ".xml"; |
---|
238 | } else { |
---|
239 | profilename = PROFILENAME_DEFAULT; |
---|
240 | } |
---|
241 | } else if (type.toLowerCase().equals(SERVER)) { |
---|
242 | profilename = PROFILENAME_SERVER; |
---|
243 | /* |
---|
244 | File f = new File(getBasePath() + profilename); |
---|
245 | if (! f.exists()){ |
---|
246 | //log.debug(" not exists"); |
---|
247 | profilename = "WorkspaceProfile_default.xml"; |
---|
248 | } |
---|
249 | */ |
---|
250 | } else if (type.toLowerCase().equals(ADMIN)){ |
---|
251 | profilename = ADMINISTRATION_FILENAME; |
---|
252 | } else if (type.toLowerCase().equals(FILENAME)){ |
---|
253 | profilename = WORKSPACE_FILENAME; |
---|
254 | } |
---|
255 | |
---|
256 | if (profilename == null){ |
---|
257 | profilename = PROFILENAME_DEFAULT; |
---|
258 | log.debug("WorkspaceProfilePath not specified (USER, SERVER)!"); |
---|
259 | } |
---|
260 | |
---|
261 | log.debug("WorkspaceProfilename : " + profilename); |
---|
262 | return profilename; |
---|
263 | } |
---|
264 | |
---|
265 | @Override |
---|
266 | public String getFullFormat() { |
---|
267 | return "xml2json"; |
---|
268 | } |
---|
269 | |
---|
270 | public URL getURL() { |
---|
271 | File file=new File(getPath()); |
---|
272 | URL url=null; |
---|
273 | try{ |
---|
274 | url=file.toURI().toURL(); |
---|
275 | //log.debug("url " + url); |
---|
276 | } catch (MalformedURLException e){ |
---|
277 | //log.debug("" + e); |
---|
278 | } |
---|
279 | return url; |
---|
280 | } |
---|
281 | |
---|
282 | |
---|
283 | public String getPath() { |
---|
284 | String targetPath = getBasePath() + this.WORKSPACE_FILENAME;//getWorkspaceProfile(); |
---|
285 | log.debug("WorkspaceAction TARGETPATH: " + targetPath); |
---|
286 | return targetPath; |
---|
287 | } |
---|
288 | |
---|
289 | @Override |
---|
290 | public InputStream getSourceStream() throws IOException { |
---|
291 | InputStream stream = new BufferedInputStream( new FileInputStream(getPath())); |
---|
292 | |
---|
293 | |
---|
294 | return stream; |
---|
295 | } |
---|
296 | |
---|
297 | // set the server - session data = repositories, termsets |
---|
298 | public void setSessionData(InputStream source) throws IOException { |
---|
299 | if (source == null) return; |
---|
300 | if (this.getWorkspaceProfile() != PROFILENAME_SERVER) return; |
---|
301 | |
---|
302 | |
---|
303 | |
---|
304 | } |
---|
305 | |
---|
306 | |
---|
307 | public void changeQueryId(Document doc, String from_id, String to_id) throws XPathExpressionException{ |
---|
308 | InputStream is = null; |
---|
309 | XPathFactory factory = XPathFactory.newInstance(); |
---|
310 | XPath xpath = factory.newXPath(); |
---|
311 | XPathExpression expr; |
---|
312 | String xpath_expr = ""; |
---|
313 | //xpath_expr = "//WorkspaceProfile/Querysets/item/Queries/item/id[.='" + from_id + "']"; |
---|
314 | xpath_expr = "//item/id[.='" + from_id + "']"; |
---|
315 | expr = xpath.compile(xpath_expr); |
---|
316 | Object result = null; |
---|
317 | result = expr.evaluate(doc, XPathConstants.NODESET); |
---|
318 | |
---|
319 | NodeList nodelist = (NodeList) result; |
---|
320 | nodelist.item(0).setTextContent(to_id); |
---|
321 | } |
---|
322 | public String createNewId(){//String name){ |
---|
323 | String name = "maxid"; |
---|
324 | String id = workspace_doc.getElementsByTagName(name).item(0).getTextContent(); |
---|
325 | Integer i = Integer.parseInt(id) + 1; |
---|
326 | id = i.toString(); |
---|
327 | workspace_doc.getElementsByTagName(name).item(0).setTextContent(id); |
---|
328 | return id.toString(); |
---|
329 | } |
---|
330 | /* |
---|
331 | public String createNewQsid(){ |
---|
332 | String id = workspace_doc.getElementsByTagName("maxqsid").item(0).getTextContent(); |
---|
333 | Integer i = Integer.parseInt(id) + 1; |
---|
334 | id = i.toString(); |
---|
335 | workspace_doc.getElementsByTagName("maxqsid").item(0).setTextContent(id); |
---|
336 | return id.toString(); |
---|
337 | } |
---|
338 | */ |
---|
339 | public InputStream createStream(String value) throws TransformerException, TransformerFactoryConfigurationError, ParserConfigurationException, UnsupportedEncodingException{ |
---|
340 | DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance(); |
---|
341 | DocumentBuilder docBuilder = dbfac.newDocumentBuilder(); |
---|
342 | Document doc = docBuilder.newDocument(); |
---|
343 | |
---|
344 | //////////////////////// |
---|
345 | //Creating the XML tree |
---|
346 | Element root = doc.createElement("root"); |
---|
347 | root.setTextContent(value); |
---|
348 | doc.appendChild(root); |
---|
349 | //set up a transformer |
---|
350 | TransformerFactory transfac = TransformerFactory.newInstance(); |
---|
351 | Transformer trans = transfac.newTransformer(); |
---|
352 | //trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); |
---|
353 | //trans.setOutputProperty(OutputKeys.INDENT, "yes"); |
---|
354 | |
---|
355 | //create string from xml tree |
---|
356 | //StringWriter sw = new StringWriter(); |
---|
357 | //StreamResult result = new StreamResult(sw); |
---|
358 | //DOMSource source = new DOMSource(doc); |
---|
359 | //trans.transform(source, result); |
---|
360 | |
---|
361 | |
---|
362 | DOMSource source = new DOMSource(doc); |
---|
363 | StringWriter xmlAsWriter = new StringWriter(); |
---|
364 | StreamResult result = new StreamResult(xmlAsWriter); |
---|
365 | TransformerFactory.newInstance().newTransformer().transform(source, result); |
---|
366 | |
---|
367 | // write changes |
---|
368 | ByteArrayInputStream is = new ByteArrayInputStream(xmlAsWriter.toString().getBytes("UTF-8")); |
---|
369 | |
---|
370 | |
---|
371 | return is; |
---|
372 | |
---|
373 | |
---|
374 | } |
---|
375 | public String getXMLRootName(){ |
---|
376 | String rootname = "WorkspaceProfile"; |
---|
377 | if (this.elementtype.equals(WorkspaceAction.SE_QUERY)){ |
---|
378 | rootname = "item"; |
---|
379 | } |
---|
380 | if (this.elementtype.equals(WorkspaceAction.SE_QUERYSET)){ |
---|
381 | rootname = "item"; |
---|
382 | } |
---|
383 | return rootname; |
---|
384 | } |
---|
385 | public String DocumentReplaceNewElement(Document new_doc) throws XPathExpressionException{ |
---|
386 | String newid = ""; |
---|
387 | if (this.elementtype.equals(WorkspaceAction.SE_WORKSPACE)){ |
---|
388 | NodeList wsnodelist = (NodeList) getWorkspaceData(); |
---|
389 | // remove |
---|
390 | //childeNode |
---|
391 | if (wsnodelist.getLength() > 0){ |
---|
392 | ((Node)wsnodelist.item(0)).getParentNode().removeChild(wsnodelist.item(0)); |
---|
393 | } |
---|
394 | //String qidstring = ""; |
---|
395 | //if (qid.equals("0")) { |
---|
396 | // newid = createNewQid(); |
---|
397 | // changeQueryId(new_doc,qid,newid); |
---|
398 | //} |
---|
399 | |
---|
400 | Node node = new_doc.getFirstChild(); |
---|
401 | Node node2 = workspace_doc.adoptNode(node.cloneNode(true)); |
---|
402 | ((Element)node2).setAttribute("user", getUserName()); |
---|
403 | workspace_doc.getElementsByTagName("WorkspaceProfiles").item(0).appendChild(node2); |
---|
404 | } |
---|
405 | if (this.elementtype.equals(WorkspaceAction.SE_QUERY)){ |
---|
406 | // new query |
---|
407 | Node node = new_doc.getFirstChild(); |
---|
408 | if (this.qid.equals("0")){ |
---|
409 | newid = createNewId();//"maxqid"); |
---|
410 | new_doc.getElementsByTagName("id").item(0).setTextContent(newid); |
---|
411 | Element root = workspace_doc.getDocumentElement(); |
---|
412 | Node node2 = workspace_doc.adoptNode(node.cloneNode(true)); |
---|
413 | node2 = workspace_doc.renameNode(node2, "", "item"); |
---|
414 | // apend to specific queryset |
---|
415 | NodeList wsnodelist = (NodeList) getQueryParent(); |
---|
416 | Node anode = wsnodelist.item(0); |
---|
417 | if (anode.getTextContent().trim().toLowerCase().equals("null")){ |
---|
418 | anode.setTextContent(""); |
---|
419 | } |
---|
420 | anode.appendChild(node2); |
---|
421 | } |
---|
422 | |
---|
423 | else { |
---|
424 | // edit existing query |
---|
425 | NodeList wsnodelist = (NodeList) getWorkspaceQuery(); |
---|
426 | // remove |
---|
427 | //childeNode |
---|
428 | if (wsnodelist.getLength() > 0){ |
---|
429 | //Element root = workspace_doc.getDocumentElement(); |
---|
430 | Node node2 = workspace_doc.adoptNode(node.cloneNode(true)); |
---|
431 | node2 = workspace_doc.renameNode(node2, "", "item"); |
---|
432 | Node qnode = (Node)wsnodelist.item(0); |
---|
433 | //root.replaceChild(node2,(Node)wsnodelist.item(0)); |
---|
434 | |
---|
435 | Element parentElement = (Element)qnode.getParentNode(); |
---|
436 | parentElement.insertBefore(node2, qnode); |
---|
437 | qnode.getParentNode().removeChild(qnode); |
---|
438 | parentElement.normalize(); |
---|
439 | } |
---|
440 | } |
---|
441 | } |
---|
442 | if (this.elementtype.equals(WorkspaceAction.SE_BOOKMARK)){ |
---|
443 | // new bookmark |
---|
444 | Node node = new_doc.getFirstChild(); |
---|
445 | if (this.qid.equals("0")){ |
---|
446 | newid = createNewId();//"maxbid"); |
---|
447 | new_doc.getElementsByTagName("id").item(0).setTextContent(newid); |
---|
448 | Element root = workspace_doc.getDocumentElement(); |
---|
449 | Node node2 = workspace_doc.adoptNode(node.cloneNode(true)); |
---|
450 | node2 = workspace_doc.renameNode(node2, "", "item"); |
---|
451 | // apend to specific bookmarkset |
---|
452 | NodeList wsnodelist = (NodeList) getBookmarkParent(); |
---|
453 | Node anode = wsnodelist.item(0); |
---|
454 | if (anode.getTextContent().trim().toLowerCase().equals("null")){ |
---|
455 | anode.setTextContent(""); |
---|
456 | } |
---|
457 | anode.appendChild(node2); |
---|
458 | } |
---|
459 | |
---|
460 | else { |
---|
461 | // edit existing query |
---|
462 | NodeList wsnodelist = (NodeList) getWorkspaceQuery(); |
---|
463 | // remove |
---|
464 | //childeNode |
---|
465 | if (wsnodelist.getLength() > 0){ |
---|
466 | //Element root = workspace_doc.getDocumentElement(); |
---|
467 | Node node2 = workspace_doc.adoptNode(node.cloneNode(true)); |
---|
468 | node2 = workspace_doc.renameNode(node2, "", "item"); |
---|
469 | Node qnode = (Node)wsnodelist.item(0); |
---|
470 | //root.replaceChild(node2,(Node)wsnodelist.item(0)); |
---|
471 | |
---|
472 | Element parentElement = (Element)qnode.getParentNode(); |
---|
473 | parentElement.insertBefore(node2, qnode); |
---|
474 | qnode.getParentNode().removeChild(qnode); |
---|
475 | parentElement.normalize(); |
---|
476 | } |
---|
477 | } |
---|
478 | } |
---|
479 | //new queryset |
---|
480 | if (this.elementtype.equals(WorkspaceAction.SE_QUERYSET)){ |
---|
481 | // new queryset |
---|
482 | Node node = new_doc.getFirstChild(); |
---|
483 | if (this.qsid.equals("0")){ |
---|
484 | newid = createNewId();//"maxqsid"); |
---|
485 | new_doc.getElementsByTagName("id").item(0).setTextContent(newid); |
---|
486 | Element root = workspace_doc.getDocumentElement(); |
---|
487 | Node node2 = workspace_doc.adoptNode(node.cloneNode(true)); |
---|
488 | node2 = workspace_doc.renameNode(node2, "", "item"); |
---|
489 | // apend to specific querysets |
---|
490 | NodeList wsnodelist = (NodeList) this.getWorkspaceData(); |
---|
491 | Element welem = (Element)wsnodelist.item(0); |
---|
492 | Node anode = welem.getElementsByTagName("Querysets").item(0); |
---|
493 | if (anode.getTextContent().trim().toLowerCase().equals("null")){ |
---|
494 | anode.setTextContent(""); |
---|
495 | } |
---|
496 | anode.appendChild(node2); |
---|
497 | } |
---|
498 | |
---|
499 | else { |
---|
500 | // edit existing queryset |
---|
501 | NodeList wsnodelist = (NodeList) getQueryset(); |
---|
502 | // remove |
---|
503 | //childeNode |
---|
504 | if (wsnodelist.getLength() > 0){ |
---|
505 | //Element root = workspace_doc.getDocumentElement(); |
---|
506 | Node node2 = workspace_doc.adoptNode(node.cloneNode(true)); |
---|
507 | node2 = workspace_doc.renameNode(node2, "", "item"); |
---|
508 | Node qset = (Node)wsnodelist.item(0); |
---|
509 | |
---|
510 | Element parentElement = (Element)qset.getParentNode(); |
---|
511 | parentElement.insertBefore(node2, qset); |
---|
512 | qset.getParentNode().removeChild(qset); |
---|
513 | parentElement.normalize(); |
---|
514 | } |
---|
515 | } |
---|
516 | } |
---|
517 | |
---|
518 | //new bookmarkset |
---|
519 | if (this.elementtype.equals(WorkspaceAction.SE_BOOKMARKSET)){ |
---|
520 | // new queryset |
---|
521 | Node node = new_doc.getFirstChild(); |
---|
522 | if (this.qsid.equals("0")){ |
---|
523 | newid = createNewId();//"maxbsid"); |
---|
524 | new_doc.getElementsByTagName("id").item(0).setTextContent(newid); |
---|
525 | Element root = workspace_doc.getDocumentElement(); |
---|
526 | Node node2 = workspace_doc.adoptNode(node.cloneNode(true)); |
---|
527 | node2 = workspace_doc.renameNode(node2, "", "item"); |
---|
528 | // apend to specific querysets |
---|
529 | NodeList wsnodelist = (NodeList) this.getWorkspaceData(); |
---|
530 | Element welem = (Element)wsnodelist.item(0); |
---|
531 | Node anode = welem.getElementsByTagName("Bookmarksets").item(0); |
---|
532 | if (anode.getTextContent().trim().toLowerCase().equals("null")){ |
---|
533 | anode.setTextContent(""); |
---|
534 | } |
---|
535 | anode.appendChild(node2); |
---|
536 | } |
---|
537 | |
---|
538 | else { |
---|
539 | // edit existing bookmarkset |
---|
540 | NodeList wsnodelist = (NodeList) getBookmarkset(); |
---|
541 | // remove |
---|
542 | //childeNode |
---|
543 | if (wsnodelist.getLength() > 0){ |
---|
544 | //Element root = workspace_doc.getDocumentElement(); |
---|
545 | Node node2 = workspace_doc.adoptNode(node.cloneNode(true)); |
---|
546 | node2 = workspace_doc.renameNode(node2, "", "item"); |
---|
547 | Node qset = (Node)wsnodelist.item(0); |
---|
548 | |
---|
549 | Element parentElement = (Element)qset.getParentNode(); |
---|
550 | parentElement.insertBefore(node2, qset); |
---|
551 | qset.getParentNode().removeChild(qset); |
---|
552 | parentElement.normalize(); |
---|
553 | } |
---|
554 | } |
---|
555 | } |
---|
556 | return newid; |
---|
557 | } |
---|
558 | |
---|
559 | //todo .has("Queries") |
---|
560 | public void parseDataInit(JSONObject json){ |
---|
561 | // workspaceelement |
---|
562 | if (json.has("Querysets")) { |
---|
563 | this.setQid("0"); |
---|
564 | this.setElementtype(WorkspaceAction.SE_WORKSPACE); |
---|
565 | } else { |
---|
566 | // Querysetelement |
---|
567 | if (json.has("Queries")) { |
---|
568 | this.setElementtype(WorkspaceAction.SE_QUERYSET); |
---|
569 | this.setQsid(json.getString("id")); |
---|
570 | } else { |
---|
571 | if (json.has("Bookmarks")) { |
---|
572 | this.setElementtype(WorkspaceAction.SE_BOOKMARKSET); |
---|
573 | this.setQsid(json.getString("id")); |
---|
574 | } else { |
---|
575 | // Query-Bookmark-element |
---|
576 | if (json.getString("bookmark").equals("1")) { |
---|
577 | this.setElementtype(WorkspaceAction.SE_BOOKMARK); |
---|
578 | } else { |
---|
579 | // Queryelement |
---|
580 | this.setElementtype(WorkspaceAction.SE_QUERY); |
---|
581 | } |
---|
582 | this.setQid(json.getString("id")); |
---|
583 | } |
---|
584 | } |
---|
585 | } |
---|
586 | |
---|
587 | } |
---|
588 | public String save() throws ParserConfigurationException, SAXException, IOException, DOMException, XPathExpressionException, TransformerException, TransformerFactoryConfigurationError{ |
---|
589 | |
---|
590 | // user data |
---|
591 | String jsonData = getData(); |
---|
592 | XMLSerializer serializer = new XMLSerializer(); |
---|
593 | JSON json = JSONSerializer.toJSON( jsonData ); |
---|
594 | // init qid, elementtype |
---|
595 | parseDataInit((JSONObject)json); |
---|
596 | serializer.setRootName(getXMLRootName()); //"WorkspaceProfile"); |
---|
597 | serializer.setElementName("item"); |
---|
598 | serializer.setTypeHintsEnabled(false); |
---|
599 | String xml = serializer.write( json ); |
---|
600 | //log.debug("XML:" + xml); |
---|
601 | InputStream is_xml = new ByteArrayInputStream( xml.getBytes( ) ); |
---|
602 | // add to document |
---|
603 | //Utils.writeToFile(getPath(), is_xml); |
---|
604 | DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); |
---|
605 | DocumentBuilder builder = factory.newDocumentBuilder(); |
---|
606 | Document new_doc = builder.parse( is_xml ); |
---|
607 | // set user |
---|
608 | //((Element)d.getFirstChild()).setAttribute("user", getServletRequest().getRemoteUser()); |
---|
609 | String newid = DocumentReplaceNewElement(new_doc); |
---|
610 | |
---|
611 | |
---|
612 | //TODO |
---|
613 | saveDocument(); |
---|
614 | // return new qid |
---|
615 | //String src = "<body>" + qidstring + "</body>"; |
---|
616 | //javax.xml.transform.stream.StreamSource s = new StreamSource(src); |
---|
617 | //InputStream in = s.getInputStream(); |
---|
618 | this.setResultStream(createStream(newid));//(InputStream)(new ByteArrayInputStream(qidstring.getBytes()))); |
---|
619 | |
---|
620 | |
---|
621 | return SUCCESS; |
---|
622 | |
---|
623 | } |
---|
624 | |
---|
625 | public String DocumentToXMLString(){ |
---|
626 | Transformer transformer; |
---|
627 | String xmlString = ""; |
---|
628 | try { |
---|
629 | transformer = TransformerFactory.newInstance().newTransformer(); |
---|
630 | transformer.setOutputProperty(OutputKeys.INDENT, "yes"); |
---|
631 | |
---|
632 | //initialize StreamResult with File object to save to file |
---|
633 | StreamResult result = new StreamResult(new StringWriter()); |
---|
634 | DOMSource source = new DOMSource(workspace_doc); |
---|
635 | try { |
---|
636 | transformer.transform(source, result); |
---|
637 | } catch (TransformerException e1) { |
---|
638 | // TODO Auto-generated catch block |
---|
639 | e1.printStackTrace(); |
---|
640 | } |
---|
641 | |
---|
642 | xmlString = result.getWriter().toString(); |
---|
643 | } catch (TransformerConfigurationException e) { |
---|
644 | // TODO Auto-generated catch block |
---|
645 | e.printStackTrace(); |
---|
646 | } catch (TransformerFactoryConfigurationError e) { |
---|
647 | // TODO Auto-generated catch block |
---|
648 | e.printStackTrace(); |
---|
649 | } |
---|
650 | |
---|
651 | return xmlString; |
---|
652 | } |
---|
653 | |
---|
654 | public void addWorkspaceToDocument(){ |
---|
655 | //{'WorkspaceProfile':{'CustomTermsets':'null', |
---|
656 | // 'Termsets':'null', |
---|
657 | // 'Repositories':'null', |
---|
658 | // 'Querysets':'null','created':'null','lastchanged':'null','profilename':'null'}}; |
---|
659 | Element we = workspace_doc.createElement("WorkspaceProfile"); |
---|
660 | we.setAttribute("user", getType().toLowerCase()); |
---|
661 | workspace_doc.getFirstChild().appendChild(we); |
---|
662 | |
---|
663 | Element e = workspace_doc.createElement("CustomTermsets"); |
---|
664 | e.setNodeValue("null"); |
---|
665 | we.appendChild(e); |
---|
666 | |
---|
667 | e = workspace_doc.createElement("Termsets"); |
---|
668 | e.setNodeValue("null"); |
---|
669 | we.appendChild(e); |
---|
670 | |
---|
671 | e = workspace_doc.createElement("Repositories"); |
---|
672 | e.setNodeValue("null"); |
---|
673 | we.appendChild(e); |
---|
674 | |
---|
675 | e = workspace_doc.createElement("Querysets"); |
---|
676 | e.setNodeValue("null"); |
---|
677 | we.appendChild(e); |
---|
678 | |
---|
679 | DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); |
---|
680 | String strdate = dateFormat.format(new Date()).toString(); |
---|
681 | e = workspace_doc.createElement("created"); |
---|
682 | e.setNodeValue(strdate); |
---|
683 | we.appendChild(e); |
---|
684 | |
---|
685 | e = workspace_doc.createElement("lastchanged"); |
---|
686 | e.setNodeValue(strdate); |
---|
687 | we.appendChild(e); |
---|
688 | |
---|
689 | e = workspace_doc.createElement("profilename"); |
---|
690 | e.setNodeValue(getType().toLowerCase()); |
---|
691 | we.appendChild(e); |
---|
692 | } |
---|
693 | |
---|
694 | public String getUserName(){ |
---|
695 | String userstring; |
---|
696 | if (getType().toLowerCase().equals(SERVER)){ |
---|
697 | userstring = SERVER; |
---|
698 | }else { |
---|
699 | userstring = getServletRequest().getRemoteUser(); |
---|
700 | if (userstring == null){ |
---|
701 | userstring = "default"; |
---|
702 | } |
---|
703 | } |
---|
704 | return userstring; |
---|
705 | } |
---|
706 | public String getWorkspaceDataXPathExpression(){ |
---|
707 | String xpath_expr = ""; |
---|
708 | |
---|
709 | xpath_expr = "//Profiles/WorkspaceProfiles/WorkspaceProfile[@user='" + getUserName() + "']"; |
---|
710 | |
---|
711 | return xpath_expr; |
---|
712 | } |
---|
713 | |
---|
714 | public Object getWorkspaceData() throws XPathExpressionException{ |
---|
715 | InputStream is = null; |
---|
716 | XPathFactory factory = XPathFactory.newInstance(); |
---|
717 | XPath xpath = factory.newXPath(); |
---|
718 | XPathExpression expr; |
---|
719 | expr = xpath.compile(getWorkspaceDataXPathExpression()); |
---|
720 | //expression is evaluated with respect to a certain context node which is doc. |
---|
721 | Object result = null; |
---|
722 | try{ |
---|
723 | result = expr.evaluate(workspace_doc, XPathConstants.NODESET); |
---|
724 | } catch(Exception e){ |
---|
725 | |
---|
726 | } |
---|
727 | |
---|
728 | return result; |
---|
729 | } |
---|
730 | |
---|
731 | public Object getWorkspaceQuery() throws XPathExpressionException{ |
---|
732 | |
---|
733 | XPathFactory factory = XPathFactory.newInstance(); |
---|
734 | XPath xpath = factory.newXPath(); |
---|
735 | XPathExpression expr; |
---|
736 | expr = xpath.compile("//Profiles/WorkspaceProfiles/WorkspaceProfile/Querysets/item/Queries/item[id='" + this.qid + "']"); |
---|
737 | //expr = xpath.compile("//Profiles/WorkspaceProfiles/WorkspaceProfile/Querysets/item/Queries/item[id='" +this.qid.toString()+"']"); |
---|
738 | //expr = xpath.compile("//item[@id='" + this.qid + "']"); |
---|
739 | //expression is evaluated with respect to a certain context node which is doc. |
---|
740 | Object result = null; |
---|
741 | try{ |
---|
742 | result = expr.evaluate(workspace_doc, XPathConstants.NODESET); |
---|
743 | } catch(Exception e){ |
---|
744 | |
---|
745 | } |
---|
746 | |
---|
747 | return result; |
---|
748 | } |
---|
749 | |
---|
750 | public Object getQueryParent() throws XPathExpressionException{ |
---|
751 | |
---|
752 | XPathFactory factory = XPathFactory.newInstance(); |
---|
753 | XPath xpath = factory.newXPath(); |
---|
754 | XPathExpression expr; |
---|
755 | expr = xpath.compile("//Profiles/WorkspaceProfiles/WorkspaceProfile/Querysets/item[id='" + this.qsid + "']/Queries"); |
---|
756 | //expression is evaluated with respect to a certain context node which is doc. |
---|
757 | Object result = null; |
---|
758 | try{ |
---|
759 | result = expr.evaluate(workspace_doc, XPathConstants.NODESET); |
---|
760 | } catch(Exception e){ |
---|
761 | |
---|
762 | } |
---|
763 | |
---|
764 | return result; |
---|
765 | } |
---|
766 | |
---|
767 | public Object getQueryVcrid() throws XPathExpressionException{ |
---|
768 | |
---|
769 | XPathFactory factory = XPathFactory.newInstance(); |
---|
770 | XPath xpath = factory.newXPath(); |
---|
771 | XPathExpression expr; |
---|
772 | // expr = xpath.compile("//Profiles/WorkspaceProfiles/WorkspaceProfile/Querysets/item[id='" + this.qsid + "']/vcrid"); |
---|
773 | expr = xpath.compile("//Profiles/WorkspaceProfiles/WorkspaceProfile/Querysets/item/Queries/item[id='" + this.qid + "']/vcrid"); |
---|
774 | |
---|
775 | //expression is evaluated with respect to a certain context node which is doc. |
---|
776 | Object result = null; |
---|
777 | try{ |
---|
778 | result = expr.evaluate(workspace_doc, XPathConstants.STRING); |
---|
779 | } catch(Exception e){ |
---|
780 | |
---|
781 | } |
---|
782 | |
---|
783 | return result; |
---|
784 | } |
---|
785 | |
---|
786 | public Object getVcrid() throws XPathExpressionException{ |
---|
787 | |
---|
788 | XPathFactory factory = XPathFactory.newInstance(); |
---|
789 | XPath xpath = factory.newXPath(); |
---|
790 | XPathExpression expr1, expr2; |
---|
791 | //expr = xpath.compile("item[id='" + this.qid + "']/vcrid"); |
---|
792 | expr1 = xpath.compile("//Profiles/WorkspaceProfiles/WorkspaceProfile/Querysets/item/Queries/item[id='" + this.qid + "']/vcrid"); |
---|
793 | expr2 = xpath.compile("//Profiles/WorkspaceProfiles/WorkspaceProfile/Bookmarksets/item[id='" + this.qid + "']/vcrid"); |
---|
794 | |
---|
795 | //expression is evaluated with respect to a certain context node which is doc. |
---|
796 | Object result = null; |
---|
797 | try{ |
---|
798 | result = expr1.evaluate(workspace_doc, XPathConstants.STRING); |
---|
799 | } catch(Exception e){ |
---|
800 | |
---|
801 | } |
---|
802 | if (result.equals("")){ |
---|
803 | try{ |
---|
804 | result = expr2.evaluate(workspace_doc, XPathConstants.STRING); |
---|
805 | } catch(Exception e){ |
---|
806 | |
---|
807 | } |
---|
808 | } |
---|
809 | return result; |
---|
810 | } |
---|
811 | public Object getQueryset() throws XPathExpressionException{ |
---|
812 | |
---|
813 | XPathFactory factory = XPathFactory.newInstance(); |
---|
814 | XPath xpath = factory.newXPath(); |
---|
815 | XPathExpression expr; |
---|
816 | expr = xpath.compile("//Profiles/WorkspaceProfiles/WorkspaceProfile/Querysets/item[id='" + this.qsid + "']"); |
---|
817 | //expression is evaluated with respect to a certain context node which is doc. |
---|
818 | Object result = null; |
---|
819 | try{ |
---|
820 | result = expr.evaluate(workspace_doc, XPathConstants.NODESET); |
---|
821 | } catch(Exception e){ |
---|
822 | |
---|
823 | } |
---|
824 | |
---|
825 | return result; |
---|
826 | } |
---|
827 | |
---|
828 | public Object getBookmarkParent() throws XPathExpressionException{ |
---|
829 | |
---|
830 | XPathFactory factory = XPathFactory.newInstance(); |
---|
831 | XPath xpath = factory.newXPath(); |
---|
832 | XPathExpression expr; |
---|
833 | expr = xpath.compile("//Profiles/WorkspaceProfiles/WorkspaceProfile/Bookmarksets/item[id='" + this.qsid + "']/Bookmarks"); |
---|
834 | //expression is evaluated with respect to a certain context node which is doc. |
---|
835 | Object result = null; |
---|
836 | try{ |
---|
837 | result = expr.evaluate(workspace_doc, XPathConstants.NODESET); |
---|
838 | } catch(Exception e){ |
---|
839 | |
---|
840 | } |
---|
841 | |
---|
842 | return result; |
---|
843 | } |
---|
844 | |
---|
845 | public Object getBookmarkset() throws XPathExpressionException{ |
---|
846 | |
---|
847 | XPathFactory factory = XPathFactory.newInstance(); |
---|
848 | XPath xpath = factory.newXPath(); |
---|
849 | XPathExpression expr; |
---|
850 | expr = xpath.compile("//Profiles/WorkspaceProfiles/WorkspaceProfile/Bookmarksets/item[id='" + this.qsid + "']"); |
---|
851 | //expression is evaluated with respect to a certain context node which is doc. |
---|
852 | Object result = null; |
---|
853 | try{ |
---|
854 | result = expr.evaluate(workspace_doc, XPathConstants.NODESET); |
---|
855 | } catch(Exception e){ |
---|
856 | |
---|
857 | } |
---|
858 | |
---|
859 | return result; |
---|
860 | } |
---|
861 | |
---|
862 | |
---|
863 | @Override |
---|
864 | public void prepare() { |
---|
865 | |
---|
866 | try{ |
---|
867 | loadParams(); |
---|
868 | |
---|
869 | NodeList list = (NodeList) getWorkspaceData();//result; |
---|
870 | if (list.getLength() < 1) { |
---|
871 | addWorkspaceToDocument(); |
---|
872 | list = (NodeList) getWorkspaceData(); |
---|
873 | } |
---|
874 | if (list.getLength() > 1){ |
---|
875 | // error |
---|
876 | } |
---|
877 | // nodelistto stream |
---|
878 | InputStream is = Utils.document2Stream(list.item(0)); |
---|
879 | this.setSourceStream(is); |
---|
880 | |
---|
881 | if (getFormat().equals("xml")) { |
---|
882 | setResultStream(is); |
---|
883 | }else { //JSON |
---|
884 | // set srcFile (for MDTransformer to pass to xsl-scripts) |
---|
885 | MDTransformer transformer = new MDTransformer(); |
---|
886 | transformer.setSrcFile(getURL()); |
---|
887 | transformer.setParams(getParams()); |
---|
888 | //transformer.setParams(MDTransformer.createParamsMap(getFullFormat())); |
---|
889 | setResultStream(transformer.transformXML(is));//, getFullFormat())); |
---|
890 | //setSessionData(getResultStream()); |
---|
891 | } |
---|
892 | |
---|
893 | log.debug(getProxyKey() + " success:" + (getResultStream()!=null)); |
---|
894 | |
---|
895 | } catch (Exception e){ |
---|
896 | |
---|
897 | } |
---|
898 | } |
---|
899 | |
---|
900 | |
---|
901 | |
---|
902 | public void initDocument() { |
---|
903 | |
---|
904 | boolean init=false; |
---|
905 | Integer counter = start_query; |
---|
906 | //String fname = getPath();//workspacefile; |
---|
907 | File f = new File (workspacefilename); |
---|
908 | |
---|
909 | if (!f.exists()) { |
---|
910 | // create new counter document |
---|
911 | DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); |
---|
912 | DocumentBuilder docBuilder; |
---|
913 | try { |
---|
914 | docBuilder = docFactory.newDocumentBuilder(); |
---|
915 | workspace_doc = docBuilder.newDocument(); |
---|
916 | // append root tag <index > |
---|
917 | Element root = (Element) workspace_doc.createElement("Profiles"); |
---|
918 | //root.setAttribute("idcounter", "1"); |
---|
919 | workspace_doc.appendChild(root); |
---|
920 | Element e = workspace_doc.createElement("Querycounter"); |
---|
921 | root.appendChild(e); |
---|
922 | e = workspace_doc.createElement("maxqid"); |
---|
923 | e.setNodeValue(counter.toString()); |
---|
924 | e = workspace_doc.createElement("WorkspaceProfiles"); |
---|
925 | root.appendChild(e); |
---|
926 | |
---|
927 | //updateQuerycounter(); |
---|
928 | this.saveDocument(); |
---|
929 | |
---|
930 | //log.debug("new document"); |
---|
931 | } catch (ParserConfigurationException e) { |
---|
932 | // TODO Auto-generated catch block |
---|
933 | e.printStackTrace(); |
---|
934 | } |
---|
935 | } |
---|
936 | else { |
---|
937 | try { |
---|
938 | |
---|
939 | DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); |
---|
940 | DocumentBuilder docBuilder; |
---|
941 | try { |
---|
942 | docBuilder = docFactory.newDocumentBuilder(); |
---|
943 | try { |
---|
944 | workspace_doc = docBuilder.parse(workspacefilename); |
---|
945 | } catch (SAXException e) { |
---|
946 | // TODO Auto-generated catch block |
---|
947 | |
---|
948 | e.printStackTrace(); |
---|
949 | } |
---|
950 | } catch (ParserConfigurationException e) { |
---|
951 | // TODO Auto-generated catch block |
---|
952 | e.printStackTrace(); |
---|
953 | } |
---|
954 | |
---|
955 | //read counter |
---|
956 | //counter = new Integer(workspace_doc.getFirstChild().getFirstChild().getFirstChild().getNodeValue()); |
---|
957 | init = true; |
---|
958 | } catch (IOException ex){ |
---|
959 | //log.debug("initCacheCounter:" + ex.toString()); |
---|
960 | ex.printStackTrace(); |
---|
961 | } |
---|
962 | } |
---|
963 | |
---|
964 | //return counter; |
---|
965 | } |
---|
966 | /* |
---|
967 | public void updateQuerycounter () { |
---|
968 | workspace_doc.getFirstChild().getAttributes().getNamedItem("idcounter").setNodeValue(querycounter.toString()); |
---|
969 | saveDocument(); |
---|
970 | } |
---|
971 | */ |
---|
972 | public void saveDocument() { |
---|
973 | //public void writeQuerycounter (Integer i) { |
---|
974 | |
---|
975 | // first update <index idcounter> |
---|
976 | //workspace_doc.getFirstChild().getAttributes().getNamedItem("idcounter").setNodeValue(i.toString()); |
---|
977 | |
---|
978 | //log.debug("writeCacheCounter:" + i.toString()); |
---|
979 | // write xml |
---|
980 | Transformer transformer; |
---|
981 | try { |
---|
982 | transformer = TransformerFactory.newInstance().newTransformer(); |
---|
983 | transformer.setOutputProperty(OutputKeys.INDENT, "yes"); |
---|
984 | |
---|
985 | //initialize StreamResult with File object to save to file |
---|
986 | StreamResult result = new StreamResult(new StringWriter()); |
---|
987 | DOMSource source = new DOMSource(workspace_doc); |
---|
988 | try { |
---|
989 | transformer.transform(source, result); |
---|
990 | } catch (TransformerException e1) { |
---|
991 | // TODO Auto-generated catch block |
---|
992 | e1.printStackTrace(); |
---|
993 | } |
---|
994 | |
---|
995 | String xmlString = result.getWriter().toString(); |
---|
996 | |
---|
997 | File f = new File (workspacefilename); |
---|
998 | FileWriter fw; |
---|
999 | try { |
---|
1000 | fw = new FileWriter(f); |
---|
1001 | try { |
---|
1002 | fw.write(xmlString); |
---|
1003 | fw.close(); |
---|
1004 | } catch (IOException e) { |
---|
1005 | // TODO Auto-generated catch block |
---|
1006 | e.printStackTrace(); |
---|
1007 | } |
---|
1008 | } catch (IOException e) { |
---|
1009 | // TODO Auto-generated catch block |
---|
1010 | e.printStackTrace(); |
---|
1011 | } |
---|
1012 | } catch (TransformerConfigurationException e2) { |
---|
1013 | // TODO Auto-generated catch block |
---|
1014 | e2.printStackTrace(); |
---|
1015 | } catch (TransformerFactoryConfigurationError e2) { |
---|
1016 | // TODO Auto-generated catch block |
---|
1017 | e2.printStackTrace(); |
---|
1018 | } |
---|
1019 | } |
---|
1020 | |
---|
1021 | |
---|
1022 | } |
---|