Mercurial > hg > eSciDocImport
comparison src/de/mpiwg/itgroup/eSciDoc/Tools/AddMPIWGIdentifiers.java @ 8:a844f6948dd8
?nderungen im Walker
tools f?r pubman
author | dwinter |
---|---|
date | Mon, 14 May 2012 09:58:45 +0200 |
parents | |
children | 9164f3f4b232 |
comparison
equal
deleted
inserted
replaced
7:df8c62d84f8f | 8:a844f6948dd8 |
---|---|
1 package de.mpiwg.itgroup.eSciDoc.Tools; | |
2 | |
3 import java.io.IOException; | |
4 import java.util.HashMap; | |
5 import java.util.List; | |
6 | |
7 import org.apache.http.HttpEntity; | |
8 import org.apache.http.HttpResponse; | |
9 import org.apache.http.client.ClientProtocolException; | |
10 import org.apache.log4j.BasicConfigurator; | |
11 import org.apache.log4j.Level; | |
12 import org.apache.log4j.Logger; | |
13 import org.jdom.Attribute; | |
14 import org.jdom.Document; | |
15 import org.jdom.Element; | |
16 import org.jdom.JDOMException; | |
17 import org.jdom.input.SAXBuilder; | |
18 import org.jdom.xpath.XPath; | |
19 | |
20 import sun.util.logging.resources.logging; | |
21 | |
22 import com.sun.org.apache.xerces.internal.parsers.SAXParser; | |
23 | |
24 import de.mpiwg.itgroup.eSciDoc.exceptions.ESciDocXmlObjectException; | |
25 import de.mpiwg.itgroup.eSciDoc.utils.eSciDocXmlObject; | |
26 | |
27 public class AddMPIWGIdentifiers { | |
28 | |
29 /** | |
30 * Erzeuge MPIWG identifier in den administrativen MPIWG Metadaten. | |
31 * Solange der Identifier nicht in den Metadaten (index.meta) steht, wird dazu der Filename genommen. | |
32 * @param args | |
33 * @throws ESciDocXmlObjectException | |
34 * @throws JDOMException | |
35 * @throws IOException | |
36 * @throws ClientProtocolException | |
37 * @throws IllegalStateException | |
38 */ | |
39 public static void main(String[] args) throws IllegalStateException, ClientProtocolException, IOException, JDOMException, ESciDocXmlObjectException { | |
40 // TODO Auto-generated method stub | |
41 BasicConfigurator.configure(); | |
42 Logger logger = Logger.getRootLogger(); | |
43 logger.setLevel(Level.INFO); | |
44 EScidocBasicHandler connector = new EScidocBasicHandler( | |
45 "escidoc-test.mpiwg-berlin.mpg.de", 8080, "dwinter", "fl0rian"); | |
46 | |
47 if(args.length<2){ | |
48 System.out.println("Usage: startrecord maximumrecords"); | |
49 System.exit(-1); | |
50 } | |
51 String MAX_REC = args[1]; | |
52 String start = args[0]; | |
53 String objectXPath = "//escidocItem:item"; | |
54 | |
55 String query = "?maximumRecords=" + String.valueOf(MAX_REC) | |
56 + "&startRecord=" + String.valueOf(start); | |
57 String command = "/ir/context/escidoc:12001/resources/members"; | |
58 for (eSciDocXmlObject obj : connector.getObjectsFromFilterResult( | |
59 command + query, objectXPath)) { | |
60 | |
61 Document doc = obj.getDocument(); | |
62 Boolean changed=false; | |
63 XPath archivePathXP = EScidocTools.getESciDocXpath("//mpiwg:admin/mpiwg:archivePath"); | |
64 XPath mpiwgMDXP= EScidocTools.getESciDocXpath("//mpiwg:admin"); | |
65 XPath mpiwgIdentifierXP= EScidocTools.getESciDocXpath("//mpiwg:admin/mpiwg:identifier"); | |
66 | |
67 | |
68 Element archivePathNode = (Element) archivePathXP.selectSingleNode(doc); | |
69 if(archivePathNode==null) // kein Treffer | |
70 continue; | |
71 | |
72 Element mpiwgIDNode = (Element) mpiwgIdentifierXP.selectSingleNode(doc); | |
73 if(mpiwgIDNode!=null){ // kein Treffer | |
74 System.out.println("ID already exists:"+mpiwgIDNode.getTextTrim()); | |
75 continue; | |
76 } | |
77 | |
78 | |
79 | |
80 | |
81 String archivePath=archivePathNode.getTextTrim(); | |
82 String[] pathElements=archivePath.split("/"); | |
83 int length=pathElements.length; | |
84 String mpiwgID=pathElements[length-1]; | |
85 Element mpiwgMDNode= (Element) mpiwgMDXP.selectSingleNode(doc); | |
86 | |
87 if(mpiwgID.equals("")) | |
88 mpiwgID=handleEmptyID(doc,mpiwgMDNode); | |
89 | |
90 | |
91 | |
92 | |
93 mpiwgIDNode= new Element("identifier", EScidocTools.MPIWG); | |
94 mpiwgIDNode.setText(mpiwgID); | |
95 mpiwgMDNode.addContent(mpiwgIDNode); | |
96 | |
97 //correct release number, some objecte don't have an release number, why?? | |
98 | |
99 | |
100 Element latestRelease = (Element)EScidocBasicHandler.getXPath(doc.getRootElement(),"//escidocItem:properties/prop:latest-release/release:number",true); | |
101 if(latestRelease==null){ | |
102 logger.info("not released yet:"+obj.getESciDocId()); | |
103 } else { | |
104 String rn= latestRelease.getTextTrim(); | |
105 if (rn.equals("")){ | |
106 String latestVersionNumber = ((Element)EScidocBasicHandler.getXPath(doc.getRootElement(),"//escidocItem:properties/prop:version/version:number",true)).getTextTrim(); | |
107 logger.info("have to add relase number!"); | |
108 logger.info("Will add:"+latestVersionNumber); | |
109 | |
110 latestRelease.setText(latestVersionNumber); | |
111 } | |
112 } | |
113 System.out.println("Adding:"+mpiwgID); | |
114 changed=true; | |
115 | |
116 | |
117 //System.out.println(obj.printXML()); | |
118 if (changed){ | |
119 | |
120 Boolean retVal = connector.updateItem(obj); | |
121 System.out.println("Replaced:"+obj.getESciDocId()); | |
122 HttpResponse retValu = connector.submitAnObject(obj, "adding identifier"); | |
123 | |
124 System.out.println(EScidocBasicHandler.convertStreamToString(retValu.getEntity().getContent())); | |
125 HttpResponse resObj = connector.eScidocGet(obj.getESciDocId()); | |
126 HttpEntity ent = resObj.getEntity(); | |
127 if (ent!=null){ | |
128 obj= new eSciDocXmlObject(ent.getContent()); | |
129 } else { | |
130 System.out.println("Can not retrieve:" + obj.getESciDocId()); | |
131 continue; | |
132 } | |
133 | |
134 HttpResponse reValue2 = connector.releaseAnObject(obj, "repairing publishing info"); | |
135 System.out.println(EScidocBasicHandler.convertStreamToString(reValue2.getEntity().getContent())); | |
136 | |
137 } | |
138 } | |
139 | |
140 } | |
141 | |
142 private static String handleEmptyID(Document doc, Element mpiwgMDNode) throws JDOMException, IOException { | |
143 XPath xlinkXP = EScidocTools.getESciDocXpath("//escidocComponents:component[escidocComponents:properties/prop:content-category/text()='index_meta']/@xlink:href"); | |
144 | |
145 SAXBuilder sb = new SAXBuilder(); | |
146 Object node = xlinkXP.selectSingleNode(doc); | |
147 Attribute attr =(Attribute) node; | |
148 String md = attr.getValue(); | |
149 | |
150 Document doc2 = sb.build("http://escidoc-test.mpiwg-berlin.mpg.de:8080"+md+"/content"); | |
151 XPath apXP= EScidocTools.getESciDocXpath("//resource/archive-path"); | |
152 XPath imXP= EScidocTools.getESciDocXpath("//texttool/image"); | |
153 | |
154 Element apNode = (Element)apXP.selectSingleNode(doc2); | |
155 String ap=""; | |
156 if(apNode!=null) | |
157 { | |
158 ap = apNode.getTextTrim(); | |
159 XPath apNewXP= EScidocTools.getESciDocXpath(".//mpiwg:archivePath"); | |
160 Element apNewNode = (Element) apNewXP.selectSingleNode(mpiwgMDNode); | |
161 apNewNode.setText(ap); | |
162 } | |
163 Element imNode = (Element)imXP.selectSingleNode(doc2); | |
164 | |
165 if(imNode!=null) | |
166 { | |
167 String im = imNode.getTextTrim(); | |
168 XPath imNewXP= EScidocTools.getESciDocXpath(".//mpiwg:imageFolder"); | |
169 Element imNewNode = (Element) imNewXP.selectSingleNode(mpiwgMDNode); | |
170 imNewNode.setText(ap+"/"+im); | |
171 } | |
172 | |
173 String[] pathElements=ap.split("/"); | |
174 int length=pathElements.length; | |
175 return pathElements[length-1]; | |
176 | |
177 | |
178 } | |
179 } |