comparison src/de/mpiwg/itgroup/eSciDoc/Tools/EScidocBasicHandler.java @ 3:58b52df9763c

added update functionality if index.meta has changed
author dwinter
date Wed, 12 Jan 2011 11:00:14 +0100
parents fab8e78184fa
children cb5668b07bfc
comparison
equal deleted inserted replaced
2:fab8e78184fa 3:58b52df9763c
9 import java.io.UnsupportedEncodingException; 9 import java.io.UnsupportedEncodingException;
10 import java.net.URI; 10 import java.net.URI;
11 import java.net.URL; 11 import java.net.URL;
12 import java.net.URLEncoder; 12 import java.net.URLEncoder;
13 import java.util.ArrayList; 13 import java.util.ArrayList;
14 import java.util.HashMap;
14 import java.util.List; 15 import java.util.List;
16 import java.util.Map;
15 import java.util.StringTokenizer; 17 import java.util.StringTokenizer;
16 import java.util.regex.Matcher; 18 import java.util.regex.Matcher;
17 import java.util.regex.Pattern; 19 import java.util.regex.Pattern;
18 20
19 import javax.swing.text.html.HTMLDocument.HTMLReader.IsindexAction; 21 import javax.swing.text.html.HTMLDocument.HTMLReader.IsindexAction;
20 22
21 23
24 import org.apache.commons.codec.EncoderException;
22 import org.apache.http.HttpEntity; 25 import org.apache.http.HttpEntity;
23 import org.apache.http.HttpResponse; 26 import org.apache.http.HttpResponse;
24 import org.apache.http.client.ClientProtocolException; 27 import org.apache.http.client.ClientProtocolException;
25 import org.apache.http.client.HttpClient; 28 import org.apache.http.client.HttpClient;
26 import org.apache.http.client.methods.HttpDelete; 29 import org.apache.http.client.methods.HttpDelete;
42 import org.jdom.Element; 45 import org.jdom.Element;
43 import org.jdom.JDOMException; 46 import org.jdom.JDOMException;
44 import org.jdom.Text; 47 import org.jdom.Text;
45 import org.jdom.input.SAXBuilder; 48 import org.jdom.input.SAXBuilder;
46 import org.jdom.xpath.XPath; 49 import org.jdom.xpath.XPath;
47 50 import org.w3c.dom.Node;
51
52 import de.mpiwg.itgroup.eSciDoc.echoObjects.ECHOObject;
53 import de.mpiwg.itgroup.eSciDoc.echoObjects.ECHORessource;
54 import de.mpiwg.itgroup.eSciDoc.exceptions.ConnectorException;
55 import de.mpiwg.itgroup.eSciDoc.exceptions.ESciDocXmlObjectException;
56 import de.mpiwg.itgroup.eSciDoc.exceptions.ObjectNotUniqueError;
48 import de.mpiwg.itgroup.eSciDoc.utils.eSciDocXmlObject; 57 import de.mpiwg.itgroup.eSciDoc.utils.eSciDocXmlObject;
49 58
50 59
51 60
52 61
475 484
476 } 485 }
477 486
478 487
479 488
480 public String getIDfromPID(String pid, String context) throws ClientProtocolException, IOException, IllegalStateException, JDOMException { 489 public String getIDfromPID(String pid, String context) throws ConnectorException {
481 490
482 491
483 String filter = "<param><filter name=\"http://escidoc.de/core/01/properties/pid\">"; 492 String filter = "<param><filter name=\"http://escidoc.de/core/01/properties/pid\">";
484 493
485 filter += pid; 494 filter += pid;
486 filter += "</filter></param>"; 495 filter += "</filter></param>";
487 496
488 String command = context 497 String command = context
489 + "/resources/members/filter"; 498 + "/resources/members/filter";
490 HttpResponse result =eScidocPost(command, 499 HttpResponse result;
491 new ByteArrayInputStream(filter.getBytes())); 500 try {
492 501 result = eScidocPost(command,
493 Document dom = new SAXBuilder().build(result.getEntity().getContent()); 502 new ByteArrayInputStream(filter.getBytes()));
494 503 } catch (IOException e) {
495 XPath xp = EScidocTools.getESciDocXpath("//escidocItem:item/@xlink:href"); 504 // TODO Auto-generated catch block
496 505 e.printStackTrace();
497 Attribute attr = (Attribute)xp.selectSingleNode(dom); 506 throw new ConnectorException();
498
499 if (attr!=null){
500 return attr.getValue();
501 } 507 }
502 508
503 return null; 509 try {
504 //return convertStreamToString(result.getEntity().getContent()); 510 Document dom = new SAXBuilder().build(result.getEntity().getContent());
511
512 XPath xp = EScidocTools.getESciDocXpath("//escidocItem:item/@xlink:href");
513
514 Attribute attr = (Attribute)xp.selectSingleNode(dom);
515
516 if (attr!=null){
517 return attr.getValue();
518 }
519
520 return null;
521 //return convertStreamToString(result.getEntity().getContent());
522 } catch (IllegalStateException e) {
523 // TODO Auto-generated catch block
524 e.printStackTrace();
525 throw new ConnectorException();
526 } catch (JDOMException e) {
527 // TODO Auto-generated catch block
528 e.printStackTrace();
529 throw new ConnectorException();
530 } catch (IOException e) {
531 // TODO Auto-generated catch block
532 e.printStackTrace();
533 throw new ConnectorException();
534 }
505 535
506 536
507 } 537 }
508 538
509 539
527 557
528 return result; 558 return result;
529 559
530 } 560 }
531 561
532 public List<eSciDocXmlObject> getObjectListFromFilterResult(String command, String objectXpath) throws IOException, IllegalStateException, JDOMException { 562 public List<eSciDocXmlObject> getObjectListFromFilterResult(String command, String objectXpath) throws IOException, IllegalStateException, JDOMException,ESciDocXmlObjectException {
533 //String filter = "<param><filter></filter></param>"; 563 //String filter = "<param><filter></filter></param>";
534 // 564 //
535 //String command = context 565 //String command = context
536 // + "/resources/members/filter"; 566 // + "/resources/members/filter";
537 //HttpResponse result =eScidocPost(command, 567 //HttpResponse result =eScidocPost(command,
644 return true; 674 return true;
645 } 675 }
646 676
647 677
648 678
649 public boolean alreadyExists(String indexField, String testString, String context) throws Exception { 679 public ECHOObject alreadyExists(String indexField, String testString, String context) throws ConnectorException, ObjectNotUniqueError {
650 680
651 String[] ct = context.split("/"); // gebraucht wird hier nur die id, dh ohne /ir/... 681 String[] ct = context.split("/"); // gebraucht wird hier nur die id, dh ohne /ir/...
652 682
653 String contextId=ct[ct.length-1]; 683 String contextId=ct[ct.length-1];
654 684
655 685
656 String searchString = String.format("\"%s\"=\"%s\"",indexField,testString); 686 String searchString = String.format("\"%s\"=\"%s\"",indexField,testString);
657 searchString += " and "+String.format("\"%s\"=\"%s\"","/properties/context/id",contextId); 687 searchString += " and "+String.format("\"%s\"=\"%s\"","/properties/context/id",contextId);
658 688
689 HttpResponse ret;
690 try{
659 searchString = URLEncoder.encode(searchString,"utf-8"); 691 searchString = URLEncoder.encode(searchString,"utf-8");
660 HttpResponse ret = eScidocGet("/ir/items?operation=searchRetrieve&version=1.1&query="+searchString); 692 ret = eScidocGet("/ir/items?operation=searchRetrieve&version=1.1&query="+searchString);
693 } catch (UnsupportedEncodingException e) {
694 throw new ConnectorException();
695 } catch (IOException e) {
696 // TODO Auto-generated catch block
697 e.printStackTrace();
698 throw new ConnectorException();
699 }
661 700
662 if (ret.getStatusLine().getStatusCode()!=200) 701 if (ret.getStatusLine().getStatusCode()!=200)
663 { 702 {
664 logger.debug("alreadyExists: error searchstring:"+searchString); 703 logger.debug("alreadyExists: error searchstring:"+searchString);
665 HttpEntity ent = ret.getEntity(); 704 HttpEntity ent = ret.getEntity();
666 if (ent!=null) 705 if (ent!=null) {
667 ent.consumeContent(); 706 try {
668 throw new Exception(); 707 ent.consumeContent();
669 } 708 } catch (IOException e) {
709 // TODO Auto-generated catch block
710 e.printStackTrace();
711 throw new ConnectorException();
712 }
713 throw new ConnectorException();
714 }
715 }
716
717 try{
670 Document doc = new SAXBuilder().build(ret.getEntity().getContent()); 718 Document doc = new SAXBuilder().build(ret.getEntity().getContent());
671 719
672 XPath xp = EScidocTools.getESciDocXpath("//zs:numberOfRecords/text()"); 720 XPath xp = EScidocTools.getESciDocXpath("//zs:numberOfRecords/text()");
673 String hitsStr = ((Text)xp.selectSingleNode(doc)).getText(); 721 String hitsStr = ((Text)xp.selectSingleNode(doc)).getText();
674 Integer hits = Integer.valueOf(hitsStr); 722 Integer hits = Integer.valueOf(hitsStr);
675 if (hits>0) 723 if (hits>0){
676 return true; 724 if (hits>1)
677 return false; 725 throw new ObjectNotUniqueError();
726 return getOldObjectFromESciDoc(doc);
727 }
728 return null;
729 } catch (IOException e) {
730 e.printStackTrace();
731 throw new ConnectorException();
732 } catch (IllegalStateException e) {
733 // TODO Auto-generated catch block
734 e.printStackTrace();
735 throw new ConnectorException();
736 } catch (JDOMException e) {
737 // TODO Auto-generated catch block
738 e.printStackTrace();
739 throw new ConnectorException();
740 }
741 }
742
743
744
745 private ECHOObject getOldObjectFromESciDoc(Document doc) {
746 Map<String,String>retMap = new HashMap<String,String>();
747 XPath md5Nodes;
748 XPath itemId;
749 XPath lastModificationDate;
750 try {
751 md5Nodes= EScidocTools.getESciDocXpath(".//escidocComponents:component/escidocComponents:properties[prop:content-category[text()='index_meta']]/prop:checksum");
752 itemId= EScidocTools.getESciDocXpath(".//escidocItem:item/@xlink:href");
753 lastModificationDate = EScidocTools.getESciDocXpath(".//escidocItem:item/@last-modification-date");
754 } catch (JDOMException e) {
755 // TODO Auto-generated catch block
756 e.printStackTrace();
757 return null;
758 }
759 Element node;
760 Attribute idNode;
761 Attribute lastModificationDateNode;
762 try {
763 node = (Element)md5Nodes.selectSingleNode(doc);
764 idNode = (Attribute)itemId.selectSingleNode(doc);
765 lastModificationDateNode =(Attribute)lastModificationDate.selectSingleNode(doc);
766
767 } catch (JDOMException e) {
768 // TODO Auto-generated catch block
769 e.printStackTrace();
770 return null;
771 }
772 String md5= node.getTextTrim();
773 String escidocId= idNode.getValue();
774 ECHORessource er;
775 try {
776 er = new ECHORessource();
777 er.eScidocId=escidocId;
778 er.indexMetaMD5stored=md5;
779 er.lastModificationDate= lastModificationDateNode.getValue();
780 } catch (IOException e) {
781 // TODO Auto-generated catch block
782 e.printStackTrace();
783 return null;
784 }
785 return er;
678 } 786 }
679 787
680 788
681 789
682 public ArrayList<String> getAllLinksOfContext(String string, String context) throws IOException, IllegalStateException, JDOMException { 790 public ArrayList<String> getAllLinksOfContext(String string, String context) throws IOException, IllegalStateException, JDOMException {
709 return ret; 817 return ret;
710 } 818 }
711 819
712 820
713 821
822
823
824
714 825
715 } 826 }