Mercurial > hg > eSciDocImport
diff src/de/mpiwg/itgroup/eSciDoc/Tools/EScidocBasicHandler.java @ 8:a844f6948dd8
?nderungen im Walker
tools f?r pubman
author | dwinter |
---|---|
date | Mon, 14 May 2012 09:58:45 +0200 |
parents | 4b1ae52418c1 |
children | e0efd3a9d2f0 |
line wrap: on
line diff
--- a/src/de/mpiwg/itgroup/eSciDoc/Tools/EScidocBasicHandler.java Sat Jan 15 17:05:29 2011 +0100 +++ b/src/de/mpiwg/itgroup/eSciDoc/Tools/EScidocBasicHandler.java Mon May 14 09:58:45 2012 +0200 @@ -24,6 +24,7 @@ import org.apache.commons.codec.EncoderException; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; +import org.apache.http.StatusLine; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpDelete; @@ -58,8 +59,6 @@ - - /** * Handler to create a connection with an eScidoc server * @author dwinter @@ -204,6 +203,8 @@ } + + /** Send a delete command to the escidoc client. performs a login if necessara * @param command * @return @@ -325,7 +326,7 @@ } /** - * Find the content-modell (item) + * Find the content-model (item) * @param ret * @return * @throws IOException @@ -411,8 +412,8 @@ return node.getText(); } - public static Object getXPath(Element node, String string,boolean single) throws JDOMException { - XPath xpath= XPath.newInstance(string); + public static Object getXPath(Element node, String xpathString,boolean single) throws JDOMException { + XPath xpath= XPath.newInstance(xpathString); xpath.addNamespace("dc",EScidocTools.DC); xpath.addNamespace("escidocComponents",EScidocTools.escidocComponents); xpath.addNamespace("prop",EScidocTools.prop); @@ -435,6 +436,33 @@ + /** Updates an item at the eScidocserver + * @param escidocItem + * @return + * @throws JDOMException + */ + public Boolean updateItem(eSciDocXmlObject escidocItem) throws JDOMException { + String cmd=escidocItem.getESciDocId(); + + try { + String retStr = escidocItem.printXML(); + logger.debug(retStr); + String newObj = createObject(cmd, retStr); + return escidocItem.upDateFromXML(newObj); + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + return false; + } + + + } + + /** + * Creates an item + * @param escidocItem + * @return + */ public Boolean createItem(eSciDocXmlObject escidocItem) { String cmd="/ir/item"; @@ -451,6 +479,13 @@ } + /** + * Creates a new object, i.e. does a httPut on command + * @param command should accept an httPut for creating new object, e.g. /ir/irtem + * @param xml eScidocXML describing the new object + * @return + * @throws Exception + */ public String createObject (String command,String xml) throws Exception { @@ -486,6 +521,13 @@ + /** + * Searches in a given context for an object's pid and return its escidoc:id + * @param pid + * @param context + * @return + * @throws ConnectorException + */ public String getIDfromPID(String pid, String context) throws ConnectorException { @@ -538,6 +580,15 @@ + /** + * submits the object + * @param obj + * @param comment + * @return + * @throws ClientProtocolException + * @throws IOException + * @throws JDOMException + */ public HttpResponse submitAnObject(eSciDocXmlObject obj, String comment) throws ClientProtocolException, IOException, JDOMException { try { //addVersionPid(obj); @@ -559,6 +610,17 @@ } + /** + * Generates a list of eScidocObject from a search or filter + * @param command search or filter command (e.g. /resources/members/filter) + * @param objectXpath xpath to the element which separates the objects in the resultXML of the filering or search, e.g. + * "/zs:searchRetrieveResponse/zs:records/zs:record/zs:recordData/escidocItem:item" in a search + * @return + * @throws IOException + * @throws IllegalStateException + * @throws JDOMException + * @throws ESciDocXmlObjectException + */ public List<eSciDocXmlObject> getObjectsFromFilterResult(String command, String objectXpath) throws IOException, IllegalStateException, JDOMException,ESciDocXmlObjectException { //String filter = "<param><filter></filter></param>"; // @@ -585,6 +647,16 @@ return ret; } + /** + * @param command search or filter command (e.g. /resources/members/filter) + * @param objectXpath xpath to the element which separates the objects in the resultXML of the filering or search, e.g. + * "/zs:searchRetrieveResponse/zs:records/zs:record/zs:recordData/escidocItem:item" in a search + * @param mode mode=0 or 2 for pending, mode=1 for submitted objects + * @return + * @throws IOException + * @throws IllegalStateException + * @throws JDOMException + */ public Integer getNumberOfHitsFromFilterResult(String command, String objectXPath, int mode) throws IOException, IllegalStateException, JDOMException { String query=""; @@ -607,6 +679,13 @@ return tmpInt; } + /** adds a PID generated by the MPIWG PID generator to the object. + * @param obj + * @return + * @throws ClientProtocolException + * @throws IOException + * @throws JDOMException + */ public boolean addVersionPid(eSciDocXmlObject obj) throws ClientProtocolException, IOException, JDOMException{ //HttpResponse ret = eScidocGet(href); //String retTxt = EScidocBasicHandler.convertStreamToString(ret.getEntity().getContent()); @@ -639,6 +718,13 @@ + /** + * @param obj + * @param comment + * @return + * @throws IOException + * @throws JDOMException + */ public HttpResponse releaseAnObject(eSciDocXmlObject obj, String comment) throws IOException, JDOMException { //HttpResponse ret = getEsciDocHandler().eScidocGet(href); //String retTxt = EScidocBasicHandler.convertStreamToString(ret.getEntity().getContent()); @@ -676,20 +762,28 @@ - public ECHOObject alreadyExists(String indexField, String testString, String context) throws ConnectorException, ObjectNotUniqueError { + /**check if object already exists + * @param indexField field in the /ir/items object to identify the object, for a list see /ir/items?operation=explain + * @param testString test if this exist in field. + * @param context + * @return + * @throws ConnectorException + * @throws ObjectNotUniqueError + */ + public ECHOObject alreadyExists(String indexField, String testString, String context, String comparator) throws ConnectorException, ObjectNotUniqueError { String[] ct = context.split("/"); // gebraucht wird hier nur die id, dh ohne /ir/... String contextId=ct[ct.length-1]; - String searchString = String.format("\"%s\"=\"%s\"",indexField,testString); - searchString += " and "+String.format("\"%s\"=\"%s\"","/properties/context/id",contextId); + String searchString = String.format("\"%s\"%s\"%s\"",indexField,comparator,testString); + searchString += " and "+String.format("\"%s\"=\"%s\"","context.objid",contextId); HttpResponse ret; try{ searchString = URLEncoder.encode(searchString,"utf-8"); - ret = eScidocGet("/ir/items?operation=searchRetrieve&version=1.1&query="+searchString); + ret = eScidocGet("/srw/search/escidoc_all?operation=searchRetrieve&version=1.1&query="+searchString); } catch (UnsupportedEncodingException e) { throw new ConnectorException(); } catch (IOException e) { @@ -724,6 +818,12 @@ if (hits>1) throw new ObjectNotUniqueError(); return getOldObjectFromESciDoc(doc); + //XPath xpItem = EScidocTools.getESciDocXpath("//escidocItem:item"); + + + //Element attr = (Element) xp.selectSingleNode(doc); + + //return getOldObjectFromESciDoc(attr); } return null; } catch (IOException e) { @@ -742,14 +842,20 @@ + /**Nimmt ein u.U. modifiziertes eScidocObject (als jDom.Document) und sucht das dazugehrige abgespeicherte Objekt. + * @param doc + * @return + */ private ECHOObject getOldObjectFromESciDoc(Document doc) { Map<String,String>retMap = new HashMap<String,String>(); XPath md5Nodes; XPath itemId; XPath lastModificationDate; + XPath objId; try { md5Nodes= EScidocTools.getESciDocXpath(".//escidocComponents:component/escidocComponents:properties[prop:content-category[text()='index_meta']]/prop:checksum"); itemId= EScidocTools.getESciDocXpath(".//escidocItem:item/@xlink:href"); + objId= EScidocTools.getESciDocXpath(".//escidocItem:item/@objid"); lastModificationDate = EScidocTools.getESciDocXpath(".//escidocItem:item/@last-modification-date"); } catch (JDOMException e) { // TODO Auto-generated catch block @@ -762,6 +868,9 @@ try { node = (Element)md5Nodes.selectSingleNode(doc); idNode = (Attribute)itemId.selectSingleNode(doc); + if (idNode==null){ + idNode = (Attribute)objId.selectSingleNode(doc); + } lastModificationDateNode =(Attribute)lastModificationDate.selectSingleNode(doc); } catch (JDOMException e) { @@ -787,14 +896,26 @@ - public ArrayList<String> getAllLinksOfContext(String string, String context) throws IOException, IllegalStateException, JDOMException { + /** + * Suche alle Links aus den Komponenten des Types type, d.h. es wird auf dem Context in allen Elementen nach: + * ".//escidocComponents:component[escidocComponents:properties/prop:content-category[text()='"+type+"']]/escidocComponents:content/@xlink:href" + * gesucht. + * + * @param type + * @param context + * @return + * @throws IOException + * @throws IllegalStateException + * @throws JDOMException + */ + public ArrayList<String> getAllLinksOfContext(String type, String context) throws IOException, IllegalStateException, JDOMException { HttpResponse result = eScidocGet(context+"/resources/members"); Document doc = new SAXBuilder().build(result.getEntity().getContent()); XPath xp = EScidocTools.getESciDocXpath("//escidocItem:item"); XPath id = EScidocTools.getESciDocXpath("./@xlink:href"); - XPath url= EScidocTools.getESciDocXpath(".//escidocComponents:component[escidocComponents:properties/prop:content-category[text()='"+string+"']]/escidocComponents:content/@xlink:href"); + XPath url= EScidocTools.getESciDocXpath(".//escidocComponents:component[escidocComponents:properties/prop:content-category[text()='"+type+"']]/escidocComponents:content/@xlink:href"); ArrayList<String> ret = new ArrayList<String>(); List<Element> items = xp.selectNodes(doc); @@ -819,6 +940,13 @@ + /** Suche alle Objekte mit field = value, die Felder ergeben sich aus /srw/search/escidoc_all?operation= explain + * + * @param field + * @param value + * @return + * @throws ESciDocXmlObjectException + */ public List<eSciDocXmlObject> getObjectsFromSearch(String field, String value) throws ESciDocXmlObjectException { String query = "/srw/search/escidoc_all?operation=searchRetrieve&version=1.1&query="; query+=field+"%3d"; @@ -848,6 +976,15 @@ + /** + * Suche alle Objekte mit field = value, die Felder ergeben sich aus /srw/search/escidoc_all?operation= explain + * + * @param field + * @param value + * @param context + * @return + * @throws ESciDocXmlObjectException + */ public List<eSciDocXmlObject> getObjectsFromSearch(String field, String value, String context) throws ESciDocXmlObjectException { String query = "/srw/search/escidoc_all?operation=searchRetrieve&version=1.1&query="; @@ -883,6 +1020,75 @@ + /** + * Teste ob das Item (itemString) mit Datestamp dateString versehen ist. + * @param itemString + * @param dateString + * @return + * @throws IOException + */ + public boolean isCurrent(String itemString, String dateString) throws IOException { + HttpResponse ret = eScidocGet(itemString); + if (ret.getStatusLine().getStatusCode()!=200){ + return false; + } + String dateStamp = getDateStamp(EScidocBasicHandler.convertStreamToString(ret.getEntity().getContent())); + if(dateStamp.equals(dateString)) + return true; + return false; + } + + + + /** holt alle ids von Objekten, die mit item ber das Predicate ber Relationen verbunden sind (item hat das Format /ir/item/XXX) + * @param itemString + * @param predicate + * @return + */ + public List<String> getIdsOfRelationFromObject(String item, + String predicate) { + String cmd=item+"/relations"; + ArrayList<String> ret = new ArrayList<String>(); + + HttpResponse result; + try { + result = eScidocGet(cmd); + } catch (IOException e) { + logger.error("Cannot deal with:"+cmd); + return ret; + } + + InputStream resultString; + try { + resultString = result.getEntity().getContent();; + } catch (IllegalStateException e) { + logger.error("Cannot deal with:"+cmd); + return ret; + } catch (IOException e) { + logger.error("Cannot deal with:"+cmd); + return ret; + } + + try { + Document doc = (new SAXBuilder()).build(resultString); + List<Attribute> xp = (List<Attribute>) getXPath(doc.getRootElement(), "//relations:relation[@predicate='"+predicate+"']/@xlink:href", false); + + for (Attribute attr: xp){ + ret.add(attr.getValue()); + } + return ret; + } catch (JDOMException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + return ret; + } + + +