diff 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
line wrap: on
line diff
--- a/src/de/mpiwg/itgroup/eSciDoc/Tools/EScidocBasicHandler.java	Mon Jan 10 12:42:27 2011 +0100
+++ b/src/de/mpiwg/itgroup/eSciDoc/Tools/EScidocBasicHandler.java	Wed Jan 12 11:00:14 2011 +0100
@@ -11,7 +11,9 @@
 import java.net.URL;
 import java.net.URLEncoder;
 import java.util.ArrayList;
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
 import java.util.StringTokenizer;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
@@ -19,6 +21,7 @@
 import javax.swing.text.html.HTMLDocument.HTMLReader.IsindexAction;
 
 
+import org.apache.commons.codec.EncoderException;
 import org.apache.http.HttpEntity;
 import org.apache.http.HttpResponse;
 import org.apache.http.client.ClientProtocolException;
@@ -44,7 +47,13 @@
 import org.jdom.Text;
 import org.jdom.input.SAXBuilder;
 import org.jdom.xpath.XPath;
+import org.w3c.dom.Node;
 
+import de.mpiwg.itgroup.eSciDoc.echoObjects.ECHOObject;
+import de.mpiwg.itgroup.eSciDoc.echoObjects.ECHORessource;
+import de.mpiwg.itgroup.eSciDoc.exceptions.ConnectorException;
+import de.mpiwg.itgroup.eSciDoc.exceptions.ESciDocXmlObjectException;
+import de.mpiwg.itgroup.eSciDoc.exceptions.ObjectNotUniqueError;
 import de.mpiwg.itgroup.eSciDoc.utils.eSciDocXmlObject;
 
 
@@ -477,7 +486,7 @@
 
 
 
-	public String getIDfromPID(String pid, String context) throws ClientProtocolException, IOException, IllegalStateException, JDOMException {
+	public String getIDfromPID(String pid, String context) throws ConnectorException {
 			
 			
 			String filter = "<param><filter name=\"http://escidoc.de/core/01/properties/pid\">";
@@ -487,21 +496,42 @@
 
 			String command = context
 				+ "/resources/members/filter";
-			HttpResponse result =eScidocPost(command,
-			new ByteArrayInputStream(filter.getBytes()));
+			HttpResponse result;
+			try {
+				result = eScidocPost(command,
+				new ByteArrayInputStream(filter.getBytes()));
+			} catch (IOException e) {
+				// TODO Auto-generated catch block
+				e.printStackTrace();
+				throw new ConnectorException();
+			}
 
-			Document dom = new SAXBuilder().build(result.getEntity().getContent());
-			
-			XPath xp = EScidocTools.getESciDocXpath("//escidocItem:item/@xlink:href");
-			
-			Attribute attr = (Attribute)xp.selectSingleNode(dom);
-			
-			if (attr!=null){
-				return attr.getValue();
+			try {
+				Document dom = new SAXBuilder().build(result.getEntity().getContent());
+				
+				XPath xp = EScidocTools.getESciDocXpath("//escidocItem:item/@xlink:href");
+				
+				Attribute attr = (Attribute)xp.selectSingleNode(dom);
+				
+				if (attr!=null){
+					return attr.getValue();
+				}
+				
+				return null;
+				//return convertStreamToString(result.getEntity().getContent());
+			} catch (IllegalStateException e) {
+				// TODO Auto-generated catch block
+				e.printStackTrace();
+				throw new ConnectorException();
+			} catch (JDOMException e) {
+				// TODO Auto-generated catch block
+				e.printStackTrace();
+				throw new ConnectorException();
+			} catch (IOException e) {
+				// TODO Auto-generated catch block
+				e.printStackTrace();
+				throw new ConnectorException();
 			}
-			
-			return null;
-			//return convertStreamToString(result.getEntity().getContent());
 
 	
 	}
@@ -529,7 +559,7 @@
 	
 	}
 	
-	public List<eSciDocXmlObject> getObjectListFromFilterResult(String command, String objectXpath) throws IOException, IllegalStateException, JDOMException {
+	public List<eSciDocXmlObject> getObjectListFromFilterResult(String command, String objectXpath) throws IOException, IllegalStateException, JDOMException,ESciDocXmlObjectException {
 		//String filter = "<param><filter></filter></param>";
 		//
 		//String command = context
@@ -646,7 +676,7 @@
 
 
 
-	public boolean alreadyExists(String indexField, String testString, String context) throws Exception {
+	public ECHOObject alreadyExists(String indexField, String testString, String context) throws ConnectorException, ObjectNotUniqueError {
 		
 		String[] ct = context.split("/"); // gebraucht wird hier nur die id, dh ohne /ir/...
 		
@@ -656,25 +686,103 @@
 		String searchString = String.format("\"%s\"=\"%s\"",indexField,testString);
 		searchString += " and "+String.format("\"%s\"=\"%s\"","/properties/context/id",contextId);
 		
+		HttpResponse ret;
+		try{
 		searchString = URLEncoder.encode(searchString,"utf-8");
-		HttpResponse ret = eScidocGet("/ir/items?operation=searchRetrieve&version=1.1&query="+searchString);
+		ret = eScidocGet("/ir/items?operation=searchRetrieve&version=1.1&query="+searchString);
+		} catch (UnsupportedEncodingException e) {
+			throw new ConnectorException();
+		} catch (IOException e) {
+			// TODO Auto-generated catch block
+			e.printStackTrace();
+			throw new ConnectorException();
+		}
 		
 		if (ret.getStatusLine().getStatusCode()!=200)
 		{
 			logger.debug("alreadyExists: error searchstring:"+searchString);
 			HttpEntity ent = ret.getEntity();
-			if (ent!=null)
-				ent.consumeContent();
-			throw new Exception();
+			if (ent!=null) {
+				try {
+					ent.consumeContent();
+				} catch (IOException e) {
+					// TODO Auto-generated catch block
+					e.printStackTrace();
+					throw new ConnectorException();
+				}
+			throw new ConnectorException();
+			}
 		}
+		
+		try{
 		Document doc = new SAXBuilder().build(ret.getEntity().getContent());
 		
 		XPath xp = EScidocTools.getESciDocXpath("//zs:numberOfRecords/text()");
 		String hitsStr = ((Text)xp.selectSingleNode(doc)).getText();
 		Integer hits = Integer.valueOf(hitsStr);
-		if (hits>0)
-			return true;
-		return false;
+		if (hits>0){
+			if (hits>1)
+				throw new ObjectNotUniqueError();
+			return getOldObjectFromESciDoc(doc);
+		}
+		return null;
+		} catch (IOException e) {
+			e.printStackTrace();
+			throw new ConnectorException();
+		} catch (IllegalStateException e) {
+			// TODO Auto-generated catch block
+			e.printStackTrace();
+			throw new ConnectorException();
+		} catch (JDOMException e) {
+			// TODO Auto-generated catch block
+			e.printStackTrace();
+			throw new ConnectorException();
+		}
+	}
+
+
+
+	private ECHOObject getOldObjectFromESciDoc(Document doc) {
+		Map<String,String>retMap = new HashMap<String,String>();
+		XPath md5Nodes;
+		XPath itemId;
+		XPath lastModificationDate;
+		try {
+			md5Nodes= EScidocTools.getESciDocXpath(".//escidocComponents:component/escidocComponents:properties[prop:content-category[text()='index_meta']]/prop:checksum");
+			itemId= EScidocTools.getESciDocXpath(".//escidocItem:item/@xlink:href");
+			lastModificationDate = EScidocTools.getESciDocXpath(".//escidocItem:item/@last-modification-date");
+		} catch (JDOMException e) {
+			// TODO Auto-generated catch block
+			e.printStackTrace();
+			return null;
+		}
+		Element node;
+		Attribute idNode;
+		Attribute lastModificationDateNode;
+		try {
+			node = (Element)md5Nodes.selectSingleNode(doc);
+			idNode = (Attribute)itemId.selectSingleNode(doc);
+			lastModificationDateNode =(Attribute)lastModificationDate.selectSingleNode(doc);
+			
+		} catch (JDOMException e) {
+			// TODO Auto-generated catch block
+			e.printStackTrace();
+			return null;
+		}
+		String md5= node.getTextTrim();
+		String escidocId= idNode.getValue();
+		ECHORessource er;
+		try {
+			er = new ECHORessource();
+			er.eScidocId=escidocId;
+			er.indexMetaMD5stored=md5;
+			er.lastModificationDate= lastModificationDateNode.getValue();
+		} catch (IOException e) {
+			// TODO Auto-generated catch block
+			e.printStackTrace();
+			return null;
+		}
+		return er;
 	}
 
 
@@ -711,5 +819,8 @@
 
 
 
+	
+
+
 
 }