Changeset 1350 for MDService2/trunk/MDService2/src/eu/clarin/cmdi/mdservice/action/WorkspaceAction.java
- Timestamp:
- 05/22/11 21:55:04 (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
MDService2/trunk/MDService2/src/eu/clarin/cmdi/mdservice/action/WorkspaceAction.java
r1310 r1350 88 88 public static String USER = "user"; 89 89 public static String SERVER = "server"; 90 91 // save element type 92 public static String SE_WORKSPACE = "workspace"; 93 public static String SE_QUERY = "query"; 94 90 95 // administration query identification 91 96 public static String ADMIN = "admin"; … … 103 108 //private String actionkey; 104 109 private String data; 105 private String type; 110 private String type; //workspacetype 111 private String elementtype; 106 112 private String qid; 113 private String qsid; 107 114 //private String format; 108 115 //private String userMsg; … … 138 145 } 139 146 147 public String getEelemnttype(){ 148 return elementtype; 149 } 150 151 public void setElementtype(String elementtype) { 152 this.elementtype = elementtype; 153 } 154 140 155 public String getType() { 141 156 return type; … … 149 164 return qid; 150 165 } 151 166 152 167 public void setQid(String qid) { 153 168 this.qid = qid; 154 169 } 155 170 156 public WorkspaceAction () { 171 public void setQsid(String qsid) { 172 this.qsid = qsid; 173 } 174 175 public String getQsid() { 176 return qsid; 177 } 178 179 public WorkspaceAction () { 180 this.elementtype = WorkspaceAction.SE_WORKSPACE; 157 181 workspacefilename = Admin.getConfig().getProperty("workspaceprofile.path") + Admin.getConfig().getProperty("workspace.file"); 158 182 if (workspace_doc == null){ … … 423 447 XPathExpression expr; 424 448 String xpath_expr = ""; 425 xpath_expr = "//WorkspaceProfile/Querysets/item/Queries/item/id[.='" + from_id + "']"; 449 //xpath_expr = "//WorkspaceProfile/Querysets/item/Queries/item/id[.='" + from_id + "']"; 450 xpath_expr = "//item/id[.='" + from_id + "']"; 426 451 expr = xpath.compile(xpath_expr); 427 452 Object result = null; … … 474 499 475 500 } 501 public String getXMLRootName(){ 502 String rootname = "WorkspaceProfile"; 503 if (this.elementtype.equals(WorkspaceAction.SE_QUERY)){ 504 rootname = "Query"; 505 } 506 return rootname; 507 } 508 public String DocumentReplaceNewElement(Document new_doc) throws XPathExpressionException{ 509 String newid = ""; 510 if (this.elementtype.equals(WorkspaceAction.SE_WORKSPACE)){ 511 NodeList wsnodelist = (NodeList) getWorkspace(); 512 // remove 513 //childeNode 514 if (wsnodelist.getLength() > 0){ 515 ((Node)wsnodelist.item(0)).getParentNode().removeChild(wsnodelist.item(0)); 516 } 517 //String qidstring = ""; 518 //if (qid.equals("0")) { 519 // newid = createNewQid(); 520 // changeQueryId(new_doc,qid,newid); 521 //} 522 523 Node node = new_doc.getFirstChild(); 524 Node node2 = workspace_doc.adoptNode(node.cloneNode(true)); 525 ((Element)node2).setAttribute("user", getUserName()); 526 workspace_doc.getElementsByTagName("WorkspaceProfiles").item(0).appendChild(node2); 527 } 528 if (this.elementtype.equals(WorkspaceAction.SE_QUERY)){ 529 // new query 530 Node node = new_doc.getFirstChild(); 531 if (this.qid.equals("0")){ 532 newid = createNewQid(); 533 new_doc.getElementsByTagName("id").item(0).setTextContent(newid); 534 Element root = workspace_doc.getDocumentElement(); 535 Node node2 = workspace_doc.adoptNode(node.cloneNode(true)); 536 node2 = workspace_doc.renameNode(node2, "", "item"); 537 // apend to specific queryset 538 NodeList wsnodelist = (NodeList) getQueryset(); 539 wsnodelist.item(0).appendChild(node2); 540 } 541 542 else { 543 // edit existing query 544 NodeList wsnodelist = (NodeList) getQuery(); 545 // remove 546 //childeNode 547 if (wsnodelist.getLength() > 0){ 548 //Element root = workspace_doc.getDocumentElement(); 549 Node node2 = workspace_doc.adoptNode(node.cloneNode(true)); 550 node2 = workspace_doc.renameNode(node2, "", "item"); 551 Node qnode = (Node)wsnodelist.item(0); 552 //root.replaceChild(node2,(Node)wsnodelist.item(0)); 553 554 Element parentElement = (Element)qnode.getParentNode(); 555 parentElement.insertBefore(node2, qnode); 556 qnode.getParentNode().removeChild(qnode); 557 parentElement.normalize(); 558 } 559 } 560 } 561 return newid; 562 } 563 564 //todo 565 public void parseDataInit(JSONObject json){ 566 // workspaceelement 567 if (json.containsKey("Querysets")) { 568 this.setQid("0"); 569 this.setElementtype(WorkspaceAction.SE_WORKSPACE); 570 } else { 571 // Queryelement 572 this.setElementtype(WorkspaceAction.SE_QUERY); 573 this.setQid(json.getString("id")); 574 //this.// 575 } 576 577 } 476 578 public String save() throws ParserConfigurationException, SAXException, IOException, DOMException, XPathExpressionException, TransformerException, TransformerFactoryConfigurationError{ 477 579 … … 480 582 XMLSerializer serializer = new XMLSerializer(); 481 583 JSON json = JSONSerializer.toJSON( jsonData ); 482 serializer.setRootName("WorkspaceProfile"); 584 // init qid, elementtype 585 parseDataInit((JSONObject)json); 586 serializer.setRootName(getXMLRootName()); //"WorkspaceProfile"); 483 587 serializer.setElementName("item"); 484 588 serializer.setTypeHintsEnabled(false); … … 490 594 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 491 595 DocumentBuilder builder = factory.newDocumentBuilder(); 492 Document d= builder.parse( is_xml );596 Document new_doc = builder.parse( is_xml ); 493 597 // set user 494 598 //((Element)d.getFirstChild()).setAttribute("user", getServletRequest().getRemoteUser()); 495 NodeList wsnodelist = (NodeList) getWorkspace(); 496 // remove 497 //childeNode 498 if (wsnodelist.getLength() > 0){ 499 ((Node)wsnodelist.item(0)).getParentNode().removeChild(wsnodelist.item(0)); 500 } 501 String qidstring = ""; 502 if (qid.equals("0")) { 503 qidstring = createNewQid(); 504 changeQueryId(d,qid,qidstring); 505 } 599 String newid = DocumentReplaceNewElement(new_doc); 506 600 507 Node node = d.getFirstChild();508 Node node2 = workspace_doc.adoptNode(node.cloneNode(true));509 ((Element)node2).setAttribute("user", getUserName());510 workspace_doc.getElementsByTagName("WorkspaceProfiles").item(0).appendChild(node2);511 601 512 602 //TODO … … 516 606 //javax.xml.transform.stream.StreamSource s = new StreamSource(src); 517 607 //InputStream in = s.getInputStream(); 518 this.setResultStream(createStream( qidstring));//(InputStream)(new ByteArrayInputStream(qidstring.getBytes())));608 this.setResultStream(createStream(newid));//(InputStream)(new ByteArrayInputStream(qidstring.getBytes()))); 519 609 520 610 … … 645 735 } 646 736 647 737 public Object getQuery() throws XPathExpressionException{ 738 739 XPathFactory factory = XPathFactory.newInstance(); 740 XPath xpath = factory.newXPath(); 741 XPathExpression expr; 742 expr = xpath.compile("//Profiles/WorkspaceProfiles/WorkspaceProfile/Querysets/item/Queries/item[id='" + this.qid + "']"); 743 //expr = xpath.compile("//item[@id='" + this.qid + "']"); 744 //expression is evaluated with respect to a certain context node which is doc. 745 Object result = null; 746 try{ 747 result = expr.evaluate(workspace_doc, XPathConstants.NODESET); 748 } catch(Exception e){ 749 750 } 751 752 return result; 753 } 754 755 public Object getQueryset() throws XPathExpressionException{ 756 757 XPathFactory factory = XPathFactory.newInstance(); 758 XPath xpath = factory.newXPath(); 759 XPathExpression expr; 760 expr = xpath.compile("//Profiles/WorkspaceProfiles/WorkspaceProfile/Querysets/item[name='" + this.qsid + "']/Queries"); 761 //expression is evaluated with respect to a certain context node which is doc. 762 Object result = null; 763 try{ 764 result = expr.evaluate(workspace_doc, XPathConstants.NODESET); 765 } catch(Exception e){ 766 767 } 768 769 return result; 770 } 771 648 772 @Override 649 773 public void prepare() throws Exception {
Note: See TracChangeset
for help on using the changeset viewer.