0
|
1 package de.mpiwg.itgroup.eSciDoc.importer;
|
|
2
|
|
3 import java.io.File;
|
|
4 import java.io.FileWriter;
|
|
5 import java.io.IOException;
|
|
6 import java.io.InputStream;
|
|
7 import java.io.InputStreamReader;
|
|
8 import java.io.StringReader;
|
|
9 import java.lang.reflect.Array;
|
|
10 import java.net.URI;
|
|
11 import java.net.URISyntaxException;
|
|
12 import java.net.URL;
|
|
13 import java.util.ArrayList;
|
|
14 import java.util.HashMap;
|
|
15 import java.util.List;
|
|
16
|
|
17 import org.apache.http.HttpEntity;
|
|
18 import org.apache.http.HttpResponse;
|
|
19 import org.apache.http.client.ClientProtocolException;
|
|
20 import org.apache.http.client.HttpClient;
|
|
21 import org.apache.http.client.methods.HttpGet;
|
|
22 import org.apache.http.impl.client.DefaultHttpClient;
|
|
23 import org.apache.log4j.Level;
|
|
24 import org.apache.log4j.Logger;
|
|
25 import org.jdom.Attribute;
|
|
26 import org.jdom.Document;
|
|
27 import org.jdom.Element;
|
|
28 import org.jdom.JDOMException;
|
|
29 import org.jdom.Namespace;
|
|
30 import org.jdom.input.SAXBuilder;
|
|
31 import org.jdom.output.XMLOutputter;
|
|
32 import org.jdom.xpath.XPath;
|
|
33 import org.w3c.dom.Entity;
|
|
34
|
|
35 import de.mpiwg.itgroup.eSciDoc.Tools.EScidocBasicHandler;
|
|
36 import de.mpiwg.itgroup.eSciDoc.Tools.EScidocTools;
|
|
37 import de.mpiwg.itgroup.eSciDoc.Tools.Html2Text;
|
|
38 import de.mpiwg.itgroup.eSciDoc.echoObjects.ECHOCollection;
|
|
39 import de.mpiwg.itgroup.eSciDoc.echoObjects.ECHOObject;
|
|
40 import de.mpiwg.itgroup.eSciDoc.echoObjects.ECHORessource;
|
|
41 import de.mpiwg.itgroup.eSciDoc.utils.eSciDocXmlObject;
|
|
42
|
|
43 public class ECHOImporter implements Importer {
|
|
44
|
|
45 private Logger logger = Logger.getRootLogger();
|
|
46 private static long MAX_RES = 1000000L; // for debugging
|
|
47 private URL instanceUrl;
|
|
48 private String collectionCMM = "/cmm/content-model/escidoc:11004";
|
|
49
|
|
50 public ECHOImporter(URL url) {
|
|
51 this.instanceUrl = url;
|
|
52 }
|
|
53
|
|
54 @Override
|
|
55 public Iterable<ECHOObject> getObjectList(String type) {
|
|
56
|
|
57 try {
|
|
58 return getObjectListfromRDF(type);
|
|
59 } catch (JDOMException e) {
|
|
60 // TODO Auto-generated catch block
|
|
61 e.printStackTrace();
|
|
62 } catch (IOException e) {
|
|
63 // TODO Auto-generated catch block
|
|
64 e.printStackTrace();
|
|
65 }
|
|
66 return new ArrayList<ECHOObject>();
|
|
67 }
|
|
68
|
|
69 private ArrayList<ECHOObject> getObjectListfromRDF(String type)
|
|
70 throws JDOMException, IOException {
|
|
71
|
|
72 ArrayList<ECHOObject> ret = new ArrayList<ECHOObject>();
|
|
73
|
|
74 SAXBuilder builder = new SAXBuilder();
|
|
75
|
|
76 Document doc = builder.build(instanceUrl);
|
|
77
|
|
78 Element el = doc.getRootElement();
|
|
79
|
|
80 // get resources
|
|
81 XPath xpathResources = XPath
|
|
82 .newInstance("//rdf:Description[echonavigation:type='" + type
|
|
83 + "']");
|
|
84 xpathResources.addNamespace("MPIWG",
|
|
85 "http://www.mpiwg-berlin.mpg.de/ns/mpiwg");
|
|
86 xpathResources.addNamespace("rdf",
|
|
87 "http://www.w3.org/1999/02/22-rdf-syntax-ns#");
|
|
88 xpathResources
|
|
89 .addNamespace("echonavigation", "http://www.echo.eu/rdf#");
|
|
90
|
|
91 List<Element> paths = xpathResources.selectNodes(el);
|
|
92
|
|
93 int counter = 0;
|
|
94 for (Element path : paths) {
|
|
95 counter++;
|
|
96 logger.debug("resource counter:" + String.valueOf(counter));
|
|
97 if (logger.getLevel().equals(Level.DEBUG) && (counter > MAX_RES))
|
|
98 break;
|
|
99
|
|
100 ECHOObject obj = getECHORessourceFromRDF(el, path, type);
|
|
101 // fueger dem object seine PID hinzu.
|
|
102 String pid;
|
|
103 try {
|
|
104 pid = obj.getOrCreatePID();
|
|
105 } catch (Exception e) {
|
|
106 // TODO Auto-generated catch block
|
|
107 e.printStackTrace();
|
|
108 pid = null;
|
|
109 }
|
|
110 if (pid == null) {
|
|
111 logger.error("Cannot createOrGetAn a PID for:" + obj.toString());
|
|
112 logger.error("Object will not be added");
|
|
113 } else {
|
|
114 ret.add(obj);
|
|
115 }
|
|
116 }
|
|
117
|
|
118 return ret;
|
|
119 }
|
|
120
|
|
121 private ECHOObject getECHORessourceFromRDF(Element el, Element path,
|
|
122 String echotype) throws JDOMException, IOException {
|
|
123
|
|
124 XPath xpath = EScidocTools.getESciDocXpath("./@rdf:about");
|
|
125 Attribute aboutAttr = (Attribute) xpath.selectSingleNode(path);
|
|
126 String aboutString = aboutAttr.getValue();
|
|
127 // hole das object
|
|
128
|
|
129 xpath = EScidocTools.getESciDocXpath(".//echonavigation:name");
|
|
130 String name = ((Element) xpath.selectSingleNode(path)).getTextTrim();
|
|
131
|
|
132 Html2Text htmlParser = new Html2Text(); // filter html codes
|
|
133 htmlParser.parse(new StringReader(name));
|
|
134 name = htmlParser.getText();
|
|
135
|
|
136 xpath = EScidocTools.getESciDocXpath(".//mpiwg:archive-path");
|
|
137 Element archiveElement = (Element) xpath.selectSingleNode(path);
|
|
138 String archivePath = "";
|
|
139 if (archiveElement != null)
|
|
140 archivePath = archiveElement.getTextTrim();
|
|
141
|
|
142 xpath = EScidocTools.getESciDocXpath("@rdf:about");
|
|
143 String about = ((Attribute) xpath.selectSingleNode(path)).getValue();
|
|
144
|
|
145 // hole seq des objectes
|
|
146 String sequenceString = ("//rdf:Seq[@rdf:about='" + about + "']/rdf:li/@rdf:resource");
|
|
147 xpath = EScidocTools.getESciDocXpath(sequenceString);
|
|
148
|
|
149 List<Attribute> seqs = xpath.selectNodes(el);
|
|
150
|
|
151 ECHOObject er = null;
|
|
152 if (echotype.equals("ECHO_resource")) {
|
|
153 er = new ECHORessource(name, archivePath, aboutString);
|
|
154 } else if (echotype.equals("ECHO_collection")) {
|
|
155 er = new ECHOCollection(name, aboutString);
|
|
156 }
|
|
157
|
|
158 // set description
|
|
159 DefaultHttpClient hc = new DefaultHttpClient();
|
|
160 URI echoUri;
|
|
161 try {
|
|
162 echoUri = new URI(er.echoUrl + "/getDescription");
|
|
163
|
|
164 HttpGet hg = new HttpGet(echoUri);
|
|
165
|
|
166 HttpResponse resp = hc.execute(hg);
|
|
167 HttpEntity respEnt = resp.getEntity();
|
|
168 if (respEnt != null) {
|
|
169 // er.description=EScidocBasicHandler.convertStreamToString(respEnt.getContent());
|
|
170 // filter html codes
|
|
171 htmlParser.parse(new InputStreamReader(respEnt.getContent()));
|
|
172 er.description = htmlParser.getText();
|
|
173
|
|
174 }
|
|
175 } catch (Exception e1) {
|
|
176 logger.debug("echoImporter no URI:" + er.echoUrl);
|
|
177 // e1.printStackTrace();
|
|
178 }
|
|
179
|
|
180 for (Attribute seq : seqs) {
|
|
181 String typeString = ("//rdf:Description[@rdf:about='"
|
|
182 + seq.getValue() + "']/echonavigation:type");
|
|
183 xpath = EScidocTools.getESciDocXpath(typeString);
|
|
184
|
|
185 Element typeNode = (Element) xpath.selectSingleNode(el);
|
|
186 if (typeNode==null){
|
|
187 logger.debug("getRessourceFromRDF, no type in:"+typeString);
|
|
188 continue;
|
|
189 }
|
|
190 String type = (typeNode).getTextTrim();
|
|
191 if (ECHORessource.class.isInstance(er)
|
|
192 && type.equals("ECHO_metaData")) {
|
|
193
|
|
194 HttpClient client = new DefaultHttpClient();
|
|
195 HttpGet get = new HttpGet(seq.getValue().replace(
|
|
196 "showMetaDataXML", "getMetaDataLink"));
|
|
197 HttpResponse ret = null;
|
|
198 try {
|
|
199 ret = client.execute(get);
|
|
200
|
|
201 } catch (IOException e) {
|
|
202 // TODO Auto-generated catch block
|
|
203 e.printStackTrace();
|
|
204 }
|
|
205 int code = ret.getStatusLine().getStatusCode();
|
|
206 try {
|
|
207 if ((code == 204) || (code >= 300))
|
|
208 ((ECHORessource) er).metaData = "";
|
|
209 else {
|
|
210 String str = EScidocBasicHandler
|
|
211 .convertStreamToString(ret.getEntity()
|
|
212 .getContent());
|
|
213 ((ECHORessource) er).metaData = ((ECHORessource) er)
|
|
214 .correctML(str);
|
|
215 }
|
|
216
|
|
217 } catch (IOException e) {
|
|
218 // TODO Auto-generated catch block
|
|
219 e.printStackTrace();
|
|
220 }
|
|
221
|
|
222 } else if (ECHORessource.class.isInstance(er)
|
|
223 && type.equals("ECHO_fulltext")) {
|
|
224
|
|
225 HttpClient client = new DefaultHttpClient();
|
|
226 HttpGet get = new HttpGet(seq.getValue() + "?noredirect=yes");
|
|
227 HttpResponse ret = null;
|
|
228 try {
|
|
229 ret = client.execute(get);
|
|
230
|
|
231 } catch (IOException e) {
|
|
232 // TODO Auto-generated catch block
|
|
233 e.printStackTrace();
|
|
234 }
|
|
235
|
|
236 try {
|
|
237 String str = EScidocBasicHandler.convertStreamToString(ret
|
|
238 .getEntity().getContent());
|
|
239 ((ECHORessource) er).fullText = new String(str);
|
|
240 } catch (IOException e) {
|
|
241 // TODO Auto-generated catch block
|
|
242 e.printStackTrace();
|
|
243 }
|
|
244
|
|
245 }
|
|
246
|
|
247 }
|
|
248
|
|
249 logger.debug(er.toString());
|
|
250 return er;
|
|
251 }
|
|
252
|
|
253 /**
|
|
254 * Erzeugt Collections auf der Basis der in den Metadaten gespeicherten ECHO
|
|
255 * urls. Dabei wir der Pfad schrittweise analysiert und dann ein Baum
|
|
256 * aufgebaut.
|
|
257 *
|
|
258 * @param handler
|
|
259 * eScidoc Serververbindung
|
|
260 * @param context
|
|
261 * Context der Kollektion (sollte eine Kollektio sein die aus
|
|
262 * einer ECHO webseite aufgebaut wurde.
|
|
263 * @throws Exception
|
|
264 */
|
|
265 public void organizeRessourcesInCollections(EScidocBasicHandler handler,
|
|
266 String context) throws Exception {
|
|
267 HashMap<String, ArrayList<String>> tree = new HashMap<String, ArrayList<String>>(); // nimmt
|
|
268 // den
|
|
269 // tree
|
|
270 // der
|
|
271 // items
|
|
272 // auf
|
|
273 HashMap<String, String> url2escidocId = new HashMap<String, String>();
|
|
274
|
|
275 HashMap<String, String> containerUrl2escidocId = new HashMap<String, String>();
|
|
276 HashMap<String, ArrayList<String>> containerTree = new HashMap<String, ArrayList<String>>(); // nimmt
|
|
277 // den
|
|
278 // tree
|
|
279 // der
|
|
280 // container
|
|
281 // auf
|
|
282
|
|
283 ArrayList<String> urls = handler.getAllLinksOfContext("web_page",
|
|
284 context);
|
|
285 generateTreeAndConversion(urls, tree, url2escidocId);
|
|
286
|
|
287 File tt = new File("/tmp/list.out");
|
|
288 FileWriter fw= new FileWriter(tt);
|
|
289 for (String containerUrl : tree.keySet()) {
|
|
290 fw.write(containerUrl);
|
|
291 }
|
|
292 fw.close();
|
|
293
|
|
294 // erzeuge jetzt die container
|
|
295 for (String containerUrl : tree.keySet()) {
|
|
296 XPath xp;
|
|
297
|
|
298 // erzeuge Document des Container mit dem entsprechenden Kontext und den Metadaten aus dem Context.
|
|
299 Document doc = createContainer(handler, context, url2escidocId,
|
|
300 containerUrl);
|
|
301
|
|
302 if (doc==null){
|
|
303 doc= createContainerFromECHO(handler, containerUrl, context);
|
|
304 }
|
|
305
|
|
306 // now fill the container
|
|
307
|
|
308 xp = EScidocTools.getESciDocXpath("//struct-map:struct-map");
|
|
309 Element structmap = (Element) xp.selectSingleNode(doc);
|
|
310
|
|
311 // fuege die Collection selbst in den container
|
|
312 putContentInStructMap(structmap, url2escidocId.get(containerUrl));
|
|
313
|
|
314
|
|
315 //fuege nun nur die ressourcen hinzu
|
|
316 for (String content : tree.get(containerUrl)) {
|
|
317 if (!contentIsCollection(handler,content))
|
|
318 putContentInStructMap(structmap, content);
|
|
319 }
|
|
320 logger.debug(printXML(doc));
|
|
321
|
|
322 try {
|
|
323
|
|
324 // rrzeuge das object jetzt in escidoc
|
|
325 String result = handler.createObject("/ir/container",
|
|
326 printXML(doc));
|
|
327 xp = EScidocTools
|
|
328 .getESciDocXpath("//container:container/@xlink:href");
|
|
329 Document containerDoc = new SAXBuilder().build(EScidocBasicHandler
|
|
330 .convertStringToStream(result));
|
|
331 Attribute containerHref = (Attribute) xp.selectSingleNode(containerDoc);
|
|
332 logger.debug("added container:" + containerHref);
|
|
333 Logger.getLogger("addedFilesLogger").debug(
|
|
334 "added container:" + containerHref);
|
|
335
|
|
336 // sichere jetzt den neuen container im container tree
|
|
337 String[] splitted = containerUrl.split("/"); // teile dazu die
|
|
338 // container url
|
|
339 // wieder auf.
|
|
340 StringBuffer buffer = new StringBuffer();
|
|
341 for (int i = 0; i < splitted.length - 2; i++) {
|
|
342 buffer.append(splitted[i]);
|
|
343 buffer.append("/");
|
|
344
|
|
345 }
|
|
346 buffer.append(splitted[splitted.length - 2]);
|
|
347
|
|
348 String parentContainer = buffer.toString();
|
|
349
|
|
350 if (!containerTree.containsKey(parentContainer)) {
|
|
351 containerTree.put(parentContainer, new ArrayList<String>());
|
|
352 }
|
|
353 containerTree.get(parentContainer).add(containerHref.getValue());
|
|
354
|
|
355 containerUrl2escidocId.put(containerUrl, containerHref.getValue());
|
|
356
|
|
357 } catch (Exception e) {
|
|
358 Logger.getLogger("notAddedFilesLogger").debug(
|
|
359 "notadded container:" + containerUrl);
|
|
360 logger.debug("notadded container:" + containerUrl);
|
|
361 }
|
|
362
|
|
363 }
|
|
364 addContainer(handler, containerTree, containerUrl2escidocId, context); // add
|
|
365 // the
|
|
366 // container
|
|
367 // to
|
|
368 // the
|
|
369 // struct
|
|
370 // maps
|
|
371 // of
|
|
372 // the
|
|
373 // parents
|
|
374 }
|
|
375
|
|
376 /** Teste ob sich hinter content eine ressource oder eine collection versteckt
|
|
377 * @param content, (escidocid,echourl) des content
|
|
378 * @return
|
|
379 * @throws IOException
|
|
380 * @throws JDOMException
|
|
381 */
|
|
382 private boolean contentIsCollection(EScidocBasicHandler handler, String content) throws IOException, JDOMException {
|
|
383 String url = content.split(",")[0];
|
|
384 HttpResponse result = handler.eScidocGet(url);
|
|
385 InputStream xml = result.getEntity().getContent();
|
|
386 String cmm = EScidocBasicHandler.getContentModel(xml);
|
|
387
|
|
388 return cmm.equals(collectionCMM);
|
|
389 }
|
|
390
|
|
391 /** FŸge einen content in die struct-map
|
|
392 * @param structmap
|
|
393 * @param content, (escidocID,url) der Ressource
|
|
394 */
|
|
395 public void putContentInStructMap(Element structmap, String content) {
|
|
396 if (content==null) // existiert nicht
|
|
397 return;
|
|
398
|
|
399 String[] urlSplit = content.split(","); // urls von get all
|
|
400 // links haben immer die
|
|
401 // form escidoc:1,url
|
|
402 String newItemUrl = urlSplit[0];
|
|
403
|
|
404 Element newItem = new Element("item", EScidocTools.srel);
|
|
405
|
|
406 Namespace ns = Namespace.getNamespace("xlink",
|
|
407 EScidocTools.xlink);
|
|
408 newItem.setAttribute("href", newItemUrl, ns);
|
|
409 structmap.addContent(newItem);
|
|
410 }
|
|
411
|
|
412 /** Erzeuge eine Container
|
|
413 * @param handler Context des Containers
|
|
414 * @param url2escidocId Liste mit url -> escidocId Zurordnungen
|
|
415 * @param collectionURL, echo url der collection zu der der Container erzeugt werden soll
|
|
416 * @return
|
|
417 * @throws JDOMException
|
|
418 * @throws IOException
|
|
419 * @throws ClientProtocolException
|
|
420 */
|
|
421 public Document createContainer(EScidocBasicHandler handler,
|
|
422 String context, HashMap<String, String> url2escidocId,
|
|
423 String collectionURL) throws JDOMException, IOException,
|
|
424 ClientProtocolException {
|
|
425
|
|
426 InputStream is = getClass()
|
|
427 .getResourceAsStream(
|
|
428 "/de/mpiwg/itgroup/eSciDoc/xmlTemplates/ECHOCollection_container.xml");
|
|
429 Document doc = new SAXBuilder().build(is);
|
|
430
|
|
431 XPath xp = EScidocTools
|
|
432 .getESciDocXpath("//srel:context/@xlink:href");
|
|
433 Attribute href = (Attribute) xp.selectSingleNode(doc);
|
|
434 href.setValue(context);
|
|
435
|
|
436 xp = EScidocTools
|
|
437 .getESciDocXpath("//srel:content-model/@xlink:href");
|
|
438 href = (Attribute) xp.selectSingleNode(doc);
|
|
439 href.setValue(collectionCMM); // TODO mache das
|
|
440 // konfigurierbar,
|
|
441 // nimm z.z.
|
|
442 // echocollection
|
|
443 // modell
|
|
444 String cmd = url2escidocId.get(collectionURL); // ensprechende collection existiert nicht.
|
|
445 if (cmd==null){
|
|
446 return null;
|
|
447 }
|
|
448 InputStream in = handler
|
|
449 .eScidocGet(cmd).getEntity()
|
|
450 .getContent();
|
|
451 Document ecDoc = new SAXBuilder().build(in);
|
|
452
|
|
453
|
|
454 // copy description from collection to container
|
|
455 xp = EScidocTools.getESciDocXpath("/escidocItem:item//metadata-records:md-record[@name='escidoc']//dc:title");
|
|
456
|
|
457
|
|
458 Element item = (Element) xp.selectSingleNode(ecDoc);
|
|
459 String title = "anon";
|
|
460 if (item != null)
|
|
461 title = item.getTextTrim();
|
|
462
|
|
463 xp = EScidocTools.getESciDocXpath("/container:container//metadata-records:md-record[@name='escidoc']//dc:title");
|
|
464 item = (Element) xp.selectSingleNode(doc);
|
|
465
|
|
466 item.setText(title);
|
|
467
|
|
468 xp = EScidocTools.getESciDocXpath("/escidocItem:item//metadata-records:md-record[@name='escidoc']//dc:description");
|
|
469 item = (Element) xp.selectSingleNode(ecDoc);
|
|
470 String description;
|
|
471 if (item != null) {
|
|
472 description = item.getTextTrim();
|
|
473 xp = EScidocTools.getESciDocXpath("/container:container//metadata-records:md-record[@name='escidoc']//dc:description");
|
|
474 item = (Element) xp.selectSingleNode(doc);
|
|
475 item.setText(description);
|
|
476 } else {
|
|
477 // get description from ECHO
|
|
478 XPath url = EScidocTools
|
|
479 .getESciDocXpath(".//escidocComponents:component[escidocComponents:properties/prop:content-category[text()='web_page']]/escidocComponents:content/@xlink:href");
|
|
480 Attribute hrefECHO = (Attribute) url.selectSingleNode(ecDoc);
|
|
481 if (hrefECHO != null) {
|
|
482 DefaultHttpClient hc = new DefaultHttpClient();
|
|
483 HttpGet hg = new HttpGet(hrefECHO.getValue()
|
|
484 + "/getDescription");
|
|
485 HttpResponse resp = hc.execute(hg);
|
|
486 HttpEntity respEnt = resp.getEntity();
|
|
487 if (respEnt != null) {
|
|
488 item = (Element) xp.selectSingleNode(doc);
|
|
489 item.setText(EScidocBasicHandler
|
|
490 .convertStreamToString(respEnt.getContent()));
|
|
491 }
|
|
492
|
|
493 }
|
|
494 }
|
|
495 return doc;
|
|
496 }
|
|
497
|
|
498 /**
|
|
499 * Erzeugt aus einer Liste von urls der Form escidocID,url einen hierarchischen Tree, sowie jeweils eine Zuordnung der url zur escidocid
|
|
500 * @param urls, liste der urls der Form "escidocID,url"
|
|
501 * @param tree, hier wird der Tree rein geschrieben, sollte ein leerer HashMap sein
|
|
502 * @param url2escidocId, hier wird die Zuordnung, url -> escidocID abgespeichert
|
|
503 */
|
|
504 public void generateTreeAndConversion(ArrayList<String> urls,
|
|
505 HashMap<String, ArrayList<String>> tree,
|
|
506 HashMap<String, String> url2escidocId) {
|
|
507 for (String url : urls) {
|
|
508 // teile die url auf die url ohne den letzten teil ist die url der
|
|
509 // collection
|
|
510 String[] splitted = url.split("/");
|
|
511 url2escidocId.put(url.split(",")[1],
|
|
512 url.split(",")[0]);
|
|
513
|
|
514
|
|
515 if (splitted.length > 1) // pfad is lang genug
|
|
516 {
|
|
517 StringBuffer buffer = new StringBuffer();
|
|
518 for (int i = 0; i < splitted.length - 2; i++) {
|
|
519 buffer.append(splitted[i]);
|
|
520 buffer.append("/");
|
|
521
|
|
522 }
|
|
523 buffer.append(splitted[splitted.length - 2]);
|
|
524
|
|
525 String collection = buffer.toString();
|
|
526 String collectionUrl = collection.split(",")[1]; // nur die url
|
|
527 // nicht den
|
|
528 // escidoc-anteil.
|
|
529
|
|
530 if (!tree.containsKey(collectionUrl)) {
|
|
531 tree.put(collectionUrl, new ArrayList<String>());
|
|
532 }
|
|
533 tree.get(collectionUrl).add(url);
|
|
534
|
|
535 }
|
|
536 }
|
|
537 }
|
|
538
|
|
539 private void addContainer(EScidocBasicHandler handler,
|
|
540 HashMap<String, ArrayList<String>> containerTree,
|
|
541 HashMap<String, String> containerUrl2escidocId, String context)
|
|
542 throws Exception {
|
|
543 for (String containerUrl : containerTree.keySet()) {
|
|
544 String escidocId = containerUrl2escidocId.get(containerUrl);
|
|
545 // if (escidocId == null) {
|
|
546 //
|
|
547 // // TODO: some containers have no ECHOcollection or
|
|
548 // // ECHO_ressourceif this is the case create it here
|
|
549 // escidocId = createContainerFromECHO(handler, containerUrl,
|
|
550 // context);
|
|
551 // logger.debug("container not in containerUrl2escidoc:"
|
|
552 // + containerUrl);
|
|
553 // }
|
|
554 if (escidocId==null){
|
|
555 logger.debug("addContainer problem not in containerUrl2escidocId:"+containerUrl);
|
|
556 Document doc = createContainerFromECHO(handler, containerUrl, context);
|
|
557 String res = handler.createObject("/ir/container",printXML(doc));
|
|
558 escidocId = "/ir/container/"+EScidocBasicHandler.getId(res);
|
|
559 //return "/ir/container/"+EScidocBasicHandler.getId(res);
|
|
560
|
|
561 }
|
|
562 HttpResponse result = handler.eScidocGet(escidocId);
|
|
563 String obj = EScidocBasicHandler.convertStreamToString(result
|
|
564 .getEntity().getContent());
|
|
565 String datestamp = EScidocBasicHandler.getDateStamp(obj);
|
|
566 String body = String.format(
|
|
567 "<param last-modification-date=\"%s\">", datestamp);
|
|
568
|
|
569 // fuege jetzt die id aller sub container ein
|
|
570 for (String content : containerTree.get(containerUrl)) {
|
|
571 String[] tmp = content.split("/");
|
|
572 String addID = tmp[tmp.length - 1];
|
|
573 body += String.format("<id>%s</id>", addID);
|
|
574 }
|
|
575 body += "</param>";
|
|
576 result = handler.eScidocPost(escidocId + "/members/add",
|
|
577 EScidocBasicHandler.convertStringToStream(body));
|
|
578 String retText = EScidocBasicHandler.convertStreamToString(result
|
|
579 .getEntity().getContent());
|
|
580 logger.debug("adding result:" + retText);
|
|
581 }
|
|
582
|
|
583 }
|
|
584
|
|
585 /**
|
|
586 * Erzeuge einen container aus echo daten
|
|
587 * @param handler
|
|
588 * @param url
|
|
589 * @param context
|
|
590 * @return
|
|
591 * @throws Exception
|
|
592 */
|
|
593 private Document createContainerFromECHO(EScidocBasicHandler handler,
|
|
594 String url, String context) throws Exception {
|
|
595 InputStream is = getClass()
|
|
596 .getResourceAsStream(
|
|
597 "/de/mpiwg/itgroup/eSciDoc/xmlTemplates/ECHOCollection_container.xml");
|
|
598 Document doc = new SAXBuilder().build(is);
|
|
599
|
|
600 XPath xp = EScidocTools.getESciDocXpath("//srel:context/@xlink:href");
|
|
601 Attribute href = (Attribute) xp.selectSingleNode(doc);
|
|
602 href.setValue(context);
|
|
603
|
|
604 xp = EScidocTools.getESciDocXpath("//srel:content-model/@xlink:href");
|
|
605 href = (Attribute) xp.selectSingleNode(doc);
|
|
606 href.setValue(collectionCMM); // TODO mache das
|
|
607 // konfigurierbar,
|
|
608 // nimm z.z.
|
|
609 // echocollection
|
|
610 // modell
|
|
611
|
|
612
|
|
613 xp = EScidocTools.getESciDocXpath("//dc:title");
|
|
614
|
|
615
|
|
616 String title = url;
|
|
617
|
|
618
|
|
619 Element item = (Element) xp.selectSingleNode(doc);
|
|
620
|
|
621 item.setText(title);
|
|
622
|
|
623 //String res = handler.createObject("/ir/container",
|
|
624 // printXML(doc));
|
|
625
|
|
626 //return "/ir/container/"+EScidocBasicHandler.getId(res);
|
|
627
|
|
628 return doc;
|
|
629 }
|
|
630
|
|
631 private String printXML(Document doc) {
|
|
632 XMLOutputter out = new XMLOutputter();
|
|
633
|
|
634 String string = out.outputString(doc);
|
|
635 return string;
|
|
636
|
|
637 }
|
|
638 } |