0
|
1 package de.mpiwg.itgroup.eSciDoc.harvesting;
|
|
2
|
|
3 import java.io.IOException;
|
2
|
4 import java.io.InputStream;
|
3
|
5 import java.io.UnsupportedEncodingException;
|
0
|
6 import java.net.URL;
|
|
7 import java.util.ArrayList;
|
4
|
8 import java.util.List;
|
3
|
9 import java.util.Map;
|
0
|
10
|
2
|
11 import org.apache.http.HttpEntity;
|
0
|
12 import org.apache.http.HttpResponse;
|
|
13 import org.apache.log4j.Level;
|
|
14 import org.apache.log4j.Logger;
|
|
15 import org.apache.log4j.xml.DOMConfigurator;
|
|
16 import org.jdom.JDOMException;
|
|
17
|
|
18 import de.mpiwg.itgroup.eSciDoc.Tools.EScidocBasicHandler;
|
|
19 import de.mpiwg.itgroup.eSciDoc.Tools.EScidocTools;
|
|
20 import de.mpiwg.itgroup.eSciDoc.echoObjects.ECHOObject;
|
|
21 import de.mpiwg.itgroup.eSciDoc.echoObjects.ECHORessource;
|
3
|
22 import de.mpiwg.itgroup.eSciDoc.exceptions.ConnectorException;
|
|
23 import de.mpiwg.itgroup.eSciDoc.exceptions.ESciDocXmlObjectException;
|
|
24 import de.mpiwg.itgroup.eSciDoc.exceptions.ObjectNotUniqueError;
|
|
25 import de.mpiwg.itgroup.eSciDoc.exceptions.TransformerException;
|
0
|
26 import de.mpiwg.itgroup.eSciDoc.importer.ECHOImporter;
|
|
27 import de.mpiwg.itgroup.eSciDoc.importer.Importer;
|
|
28 import de.mpiwg.itgroup.eSciDoc.transformer.ECHOTransformer;
|
|
29 import de.mpiwg.itgroup.eSciDoc.transformer.Transformer;
|
|
30 import de.mpiwg.itgroup.eSciDoc.utils.eSciDocXmlObject;
|
|
31
|
3
|
32 /**
|
|
33 * @author dwinter
|
|
34 * Main class for data harveting from index.meta files into eScidoc
|
|
35 */
|
0
|
36 public class ESciDocDataHarvester {
|
3
|
37 static int MAX_REC=1000; // maximale Anzahl von Records die in einem Stueck eingelesen bzw. bearbeitet werden.
|
2
|
38 //static int MAX_REC=5;
|
0
|
39 protected Logger logger = Logger.getRootLogger();
|
|
40 protected Importer importer;
|
|
41 protected EScidocBasicHandler connector;
|
|
42 protected Transformer transformer;
|
8
|
43
|
6
|
44 private String context;
|
0
|
45 private Logger addedFile = Logger.getLogger("addedFilesLogger");
|
|
46 private Logger notAddedFile = Logger.getLogger("notAddedFilesLogger");
|
2
|
47
|
3
|
48 /**
|
|
49 * @param importer Importer for dataObjects, describes how to access the objects
|
|
50 * @param transformer Transformer, generates the eScidocMetaDatasets
|
|
51 * @param connector connects to the eScidocRepository
|
|
52 * @param context Escidoc context path z.b. /ir/context/escidoc:12001
|
|
53 */
|
2
|
54 public ESciDocDataHarvester(Importer importer, Transformer transformer,
|
3
|
55 EScidocBasicHandler connector, String context) {
|
2
|
56 this.importer = importer;
|
|
57 this.transformer = transformer;
|
|
58 this.connector = connector;
|
3
|
59 //this.tools = new EScidocTools(connector);
|
6
|
60 this.context = context;
|
2
|
61
|
0
|
62 }
|
2
|
63
|
3
|
64 /** Read objects into eScidoc or updates the objects if indexMeta has changed.
|
|
65 * @param type restrict the imported objects to a specific type, possible types should be defined in
|
|
66 * the given importer @see {@link #importer}
|
|
67 * @return
|
|
68 * @throws ConnectorException
|
|
69 * @throws TransformerException
|
|
70 * @throws ESciDocXmlObjectException
|
|
71 */
|
4
|
72 @Deprecated
|
|
73 public Boolean readObjectsFromInstanceOLD(String type) throws ConnectorException, TransformerException, ESciDocXmlObjectException {
|
0
|
74 ArrayList<String> addedObjects = new ArrayList<String>();
|
|
75 ArrayList<String> notAddedObjects = new ArrayList<String>();
|
2
|
76 for (ECHOObject obj : importer.getObjectList(type)) {
|
|
77
|
|
78 if (ECHORessource.class.isInstance(obj)) {
|
|
79 try {
|
3
|
80 ECHOObject old;
|
|
81 try {
|
8
|
82 old = connector.alreadyExists(
|
|
83 "admin.archivePath",
|
|
84 ((ECHORessource) obj).archivePath, context,"=");
|
|
85 if (old==null){ //FIXME Problem bei der Erfassungder Metadaten sollte eigentlich nicht vorkommen!
|
|
86 old = connector.alreadyExists(
|
|
87 "admin.archivePath",
|
|
88 ((ECHORessource) obj).archivePath.replace("/mpiwg/online/permanent", "/Volumes/online/permanent"), context,"=");
|
3
|
89 }
|
8
|
90 if (old==null){
|
|
91 old = connector.alreadyExists(
|
|
92 "admin.archivePath",
|
|
93 ((ECHORessource) obj).archivePath.replace("/mpiwg/online/experimental", "/Volumes/online/experimental"), context,"=");
|
|
94 }
|
4
|
95 } catch (ObjectNotUniqueError e) {
|
|
96 // TODO Auto-generated catch block
|
|
97 e.printStackTrace();
|
|
98 continue;
|
|
99 }
|
|
100 if (old!=null) {
|
|
101 logger.debug("already exist:"
|
|
102 + ((ECHORessource) obj).archivePath);
|
|
103 handleExistingObject(obj,old);
|
|
104 continue;
|
|
105 }
|
|
106 } catch (ConnectorException e) {
|
|
107 logger.debug("already exist error:");
|
|
108 e.printStackTrace();
|
|
109 continue;
|
|
110 }
|
|
111 }
|
|
112
|
6
|
113 obj.context = context;
|
4
|
114
|
6
|
115 String contid = connector.getIDfromPID(obj.pid, context);
|
4
|
116 if (contid != null) {
|
|
117 System.out.println("------- belongsTo:" + contid);
|
|
118 } else {
|
|
119
|
|
120 eSciDocXmlObject escidocItem = transformer.transform(obj);
|
8
|
121
|
|
122
|
|
123 try {
|
|
124 logger.info(escidocItem.printXML());
|
|
125 // TODO write PID to back to echo-obj
|
|
126 Boolean result = connector.createItem(escidocItem);
|
|
127 if (result) {
|
|
128 addedObjects.add(escidocItem.getESciDocId());
|
|
129 addedFile.debug(escidocItem.getESciDocId() + "\n");
|
4
|
130
|
8
|
131 } else {
|
|
132 notAddedObjects.add(obj.echoUrl);
|
|
133 notAddedFile.debug(obj.echoUrl);
|
|
134
|
4
|
135 }
|
8
|
136
|
|
137 } catch (IOException e) {
|
|
138 // TODO Auto-generated catch block
|
|
139 e.printStackTrace();
|
|
140 throw new ESciDocXmlObjectException();
|
|
141 } catch (JDOMException e) {
|
|
142 // TODO Auto-generated catch block
|
|
143 e.printStackTrace();
|
|
144 throw new ESciDocXmlObjectException();
|
|
145 }
|
|
146
|
4
|
147 }
|
|
148 }
|
|
149 if (logger.getLevel() == Level.DEBUG) {
|
|
150 for (String addedObject : addedObjects) {
|
|
151 logger.debug(addedObject);
|
|
152 }
|
|
153 }
|
|
154
|
|
155 return true;
|
|
156 }
|
|
157
|
8
|
158
|
|
159 /** Read objects into eScidoc or updates the objects if indexMeta has changed.
|
|
160 * @param type restrict the imported objects to a specific type, possible types should be defined in
|
|
161 * the given importer @see {@link #importer}
|
|
162 * @return
|
|
163 * @throws ConnectorException
|
|
164 * @throws TransformerException
|
|
165 * @throws ESciDocXmlObjectException
|
|
166 */
|
|
167 public Boolean readObjectsFromInstance(String type) throws ConnectorException, TransformerException, ESciDocXmlObjectException {
|
|
168 ArrayList<String> addedObjects = new ArrayList<String>();
|
|
169 ArrayList<String> notAddedObjects = new ArrayList<String>();
|
|
170 for (ECHOObject obj : importer.getObjectList(type)) {
|
|
171 if (obj==null)
|
|
172 continue;
|
|
173 if (ECHORessource.class.isInstance(obj)) {
|
|
174 try {
|
|
175
|
|
176 // checke zuerst, ob die MD5 schon im publiziert Teil der Metadaten ist, dann tue nichts
|
|
177 String md5 = ((ECHORessource) obj).getIndexMetaMD5onServer();
|
|
178 //List<eSciDocXmlObject> results = connector.getObjectsFromSearch("escidoc.component.checksum",md5);
|
|
179 List<eSciDocXmlObject> results = connector.getObjectsFromSearch("escidoc.component.checksum",md5,context);
|
|
180 if (results.size()>0){ //index.meta schon abgespeichert
|
|
181 notAddedFile.debug("alredy exist:"+obj.echoUrl);
|
|
182
|
|
183 ((ECHORessource) obj).writeEsciDocIDToIndexMeta(results.get(0));
|
|
184
|
|
185 continue;
|
|
186 }
|
|
187
|
|
188
|
|
189
|
|
190
|
|
191 ECHOObject old;
|
|
192 try {
|
|
193
|
|
194 old = connector.alreadyExists(
|
|
195 "admin.archivePath",
|
|
196 ((ECHORessource) obj).archivePath, context,"=");
|
|
197 if (old==null){ //FIXME Problem bei der Erfassungder Metadaten sollte eigentlich nicht vorkommen!
|
|
198 old = connector.alreadyExists(
|
|
199 "admin.archivePath",
|
|
200 ((ECHORessource) obj).archivePath.replace("/mpiwg/online/permanent", "/Volumes/online_permanent"), context,"=");
|
|
201 }
|
|
202 if (old==null){
|
|
203 old = connector.alreadyExists(
|
|
204 "admin.archivePath",
|
|
205 ((ECHORessource) obj).archivePath.replace("/mpiwg/online/experimental", "/Volumes/online_experimental"), context,"=");
|
|
206 }
|
|
207 } catch (ObjectNotUniqueError e) {
|
|
208 // TODO Auto-generated catch block
|
|
209 e.printStackTrace();
|
|
210 continue;
|
|
211 }
|
|
212 if (old!=null) {
|
|
213 logger.debug("already exist:"
|
|
214 + ((ECHORessource) obj).archivePath);
|
|
215 handleExistingObject(obj,old);
|
|
216 continue;
|
|
217 }
|
|
218 } catch (ConnectorException e) {
|
|
219 logger.debug("already exist error:");
|
|
220 e.printStackTrace();
|
|
221 continue;
|
|
222 }
|
|
223 }
|
|
224
|
|
225 obj.context = context;
|
|
226
|
|
227 String contid=null;
|
|
228
|
|
229 if (obj.pid!=null)
|
|
230 contid = connector.getIDfromPID(obj.pid, context);
|
|
231 if (contid != null) {
|
|
232 System.out.println("------- belongsTo:" + contid);
|
|
233 } else {
|
|
234
|
|
235 eSciDocXmlObject escidocItem = transformer.transform(obj);
|
|
236
|
|
237
|
|
238 try {
|
|
239 logger.info(escidocItem.printXML());
|
|
240 // TODO write PID to back to echo-obj
|
|
241 Boolean result = connector.createItem(escidocItem);
|
|
242 if (result) {
|
10
|
243 String objID=escidocItem.getESciDocId();
|
|
244 addedObjects.add(objID);
|
|
245 addedFile.debug(objID + "\n");
|
|
246 ((ECHORessource)obj).writeEsciDocIDToIndexMeta(objID.replace("/ir/item/",""));
|
8
|
247 } else {
|
|
248 notAddedObjects.add(obj.echoUrl);
|
|
249 notAddedFile.debug(obj.echoUrl);
|
|
250
|
|
251 }
|
|
252
|
|
253 } catch (IOException e) {
|
|
254 // TODO Auto-generated catch block
|
|
255 e.printStackTrace();
|
|
256 throw new ESciDocXmlObjectException();
|
|
257 } catch (JDOMException e) {
|
|
258 // TODO Auto-generated catch block
|
|
259 e.printStackTrace();
|
|
260 throw new ESciDocXmlObjectException();
|
|
261 }
|
|
262
|
|
263 }
|
|
264 }
|
|
265 if (logger.getLevel() == Level.DEBUG) {
|
|
266 for (String addedObject : addedObjects) {
|
|
267 logger.debug(addedObject);
|
|
268 }
|
|
269 }
|
|
270
|
|
271 return true;
|
|
272 }
|
|
273
|
|
274
|
|
275
|
3
|
276
|
|
277 /**
|
|
278 * Deal with existing objects, do nothing if md5 of stored metadata and metadata on the server is the same otherwise call {@link #updateObject(ECHOObject)}.
|
|
279 * @param objNew
|
|
280 * @param old
|
|
281 * @throws TransformerException
|
|
282 * @throws ESciDocXmlObjectException
|
|
283 */
|
|
284 private void handleExistingObject(ECHOObject objNew, ECHOObject old) throws TransformerException, ESciDocXmlObjectException {
|
|
285 ECHORessource objNewRes = (ECHORessource)objNew;
|
|
286 ECHORessource objOldRes = (ECHORessource)old;
|
|
287 String md5onServer = objNewRes.getIndexMetaMD5onServer();
|
|
288 String md5=objOldRes.getIndexMetaMD5stored();
|
8
|
289
|
|
290 objNewRes.writeEsciDocIDToIndexMeta(objOldRes.eScidocId);
|
|
291
|
3
|
292 if (md5onServer.equals(md5))
|
|
293 return;
|
|
294 else {
|
8
|
295
|
3
|
296 updateObject(objNew, old);
|
8
|
297
|
|
298
|
3
|
299 }
|
8
|
300
|
3
|
301 }
|
|
302
|
|
303 private void updateObject(ECHOObject objNew, ECHOObject objOld) throws TransformerException, ESciDocXmlObjectException {
|
6
|
304 objNew.context = context;
|
8
|
305
|
|
306 //erzeuge erst einmal ein neues XML Object aus den neuen Daten.
|
3
|
307 eSciDocXmlObject escidocItem = transformer.transform(objNew);
|
|
308 String lastModificationDateOld = objOld.lastModificationDate;
|
8
|
309
|
|
310 //jetzt das alte ModeificationDate dort rein (wegen, optimitistic locking)
|
3
|
311 escidocItem.setLastModificationDate(lastModificationDateOld);
|
|
312 try {
|
8
|
313 HttpResponse ret = connector.eScidocPut("/ir/item/"+objOld.eScidocId, EScidocBasicHandler.convertStringToStream(escidocItem.printXML()));
|
3
|
314 HttpEntity ent = ret.getEntity();
|
|
315 if (ret.getStatusLine().getStatusCode() != 200) {
|
|
316 logger.debug("Can not update:" + objOld.eScidocId);
|
|
317 // res.getEntity().consumeContent(); // necessary to release
|
|
318 // the conneciton
|
|
319 ent.consumeContent();
|
|
320
|
|
321 }
|
|
322 InputStream restream = ret.getEntity().getContent();
|
|
323 logger.debug(EScidocBasicHandler.convertStreamToString(restream));
|
|
324 } catch (UnsupportedEncodingException e) {
|
|
325 // TODO Auto-generated catch block
|
|
326 e.printStackTrace();
|
|
327 } catch (IOException e) {
|
|
328 // TODO Auto-generated catch block
|
|
329 e.printStackTrace();
|
|
330 }
|
8
|
331
|
5
|
332 addedFile.debug("updated:"+objOld.eScidocId);
|
3
|
333 }
|
|
334
|
2
|
335 /**
|
|
336 * @param command
|
|
337 * @param objectXPath
|
8
|
338 * @param comment der in escidoc bei der Operation abgespeichert wird.
|
2
|
339 * @param mode 0 : only submit, 1:only release, 2:release and submit
|
|
340 * @throws Exception
|
|
341 */
|
8
|
342 public void releaseAndSubmitObjects(String command, String objectXPath,String comment,int mode)
|
2
|
343 throws Exception {
|
|
344
|
|
345 Integer numberOfHits = connector.getNumberOfHitsFromFilterResult(
|
|
346 command, objectXPath,mode);
|
8
|
347
|
10
|
348 logger.info(String.format("%s objects found.", numberOfHits));
|
2
|
349 int tausend = ((numberOfHits-1) / MAX_REC);
|
8
|
350
|
2
|
351 String queryRestrict="";
|
|
352 if(mode==0 | mode==2){
|
|
353 queryRestrict="query=%22/properties/version/status%22=pending";
|
|
354 } else {
|
|
355 queryRestrict="query=%22/properties/version/status%22=submitted";
|
|
356 }
|
10
|
357 int counter=0;
|
2
|
358 for (int t = 0; t <= tausend; t++) {
|
|
359 int start = t * MAX_REC+1;
|
|
360 // int max=Math.min((t+1)*1000, numberOfHits);
|
|
361 String query = "?maximumRecords="+String.valueOf(MAX_REC)+"&startRecord="
|
|
362 + String.valueOf(start)+"&"+queryRestrict;
|
|
363 for (eSciDocXmlObject obj : connector
|
4
|
364 .getObjectsFromFilterResult(command+query, objectXPath)) {
|
8
|
365
|
10
|
366 logger.info(String.format("%s/%s", counter,numberOfHits));
|
|
367 counter+=1;
|
|
368
|
2
|
369 //TODO is the following really necessary, currently the obj in the list is sometimes not the current one.
|
|
370 try{
|
8
|
371 HttpResponse resObj = connector.eScidocGet(obj.getESciDocId());
|
|
372 HttpEntity ent = resObj.getEntity();
|
|
373 if (ent!=null){
|
|
374 obj= new eSciDocXmlObject(ent.getContent());
|
|
375 } else {
|
|
376 logger.debug("Can not retrieve:" + obj.getESciDocId());
|
|
377 continue;
|
|
378 }
|
2
|
379 } catch (Exception e){
|
|
380 logger.debug("Can not retrieve:" + obj.getESciDocId());
|
|
381 continue;
|
|
382 }
|
|
383 if (mode==0 | mode==2){
|
|
384 HttpResponse res = connector.submitAnObject(obj,
|
8
|
385 comment);
|
2
|
386 logger.debug(res.getStatusLine());
|
8
|
387
|
2
|
388 if (res.getStatusLine().getStatusCode() != 200) {
|
|
389 logger.debug("Can not submit:" + obj.getESciDocId());
|
|
390 // res.getEntity().consumeContent(); // necessary to release
|
|
391 // the conneciton
|
8
|
392
|
2
|
393 }
|
|
394 InputStream restream = res.getEntity().getContent();
|
|
395 logger.debug(EScidocBasicHandler.convertStreamToString(restream));
|
|
396 //res.getEntity().consumeContent(); // necessary to release the
|
8
|
397 // conneciton
|
|
398
|
2
|
399 if (!connector.upDateObject(obj)) {
|
|
400 logger.debug("Can not update:" + obj.getESciDocId());
|
|
401 // continue;
|
8
|
402
|
2
|
403 }
|
|
404 }
|
8
|
405
|
2
|
406 if (mode==1 | mode==2){
|
8
|
407 HttpResponse res = connector.releaseAnObject(obj, comment);
|
2
|
408 logger.debug(res.getStatusLine());
|
|
409 if (res.getStatusLine().getStatusCode() != 200) {
|
|
410 logger.debug("Can not release:" + obj.getESciDocId());
|
|
411 res.getEntity().consumeContent(); // necessary to release
|
8
|
412 // the conneciton
|
2
|
413 continue;
|
|
414 }
|
|
415 addedFile.debug("RELEASED:" + obj.getESciDocId());
|
|
416 res.getEntity().consumeContent(); // necessary to release the
|
8
|
417 // connecito
|
2
|
418 }
|
0
|
419 }
|
|
420 }
|
2
|
421
|
0
|
422 }
|
2
|
423
|
|
424 public static void main(String[] args) throws Exception {
|
|
425
|
0
|
426 Logger rl = Logger.getRootLogger();
|
8
|
427 DOMConfigurator.configure("/etc/escidocImportConfig.xml");
|
2
|
428 rl.setLevel(Level.DEBUG);
|
0
|
429
|
8
|
430
|
9
|
431 EScidocBasicHandler connector = new EScidocBasicHandler("escidoc.mpiwg-berlin.mpg.de",8080,"dwinter","weikiki7");
|
8
|
432
|
|
433
|
|
434 //ECHOImporter newimporter = new ECHOImporter(new URL(
|
|
435 // "file:///Users/dwinter/libcoll.rdf"));
|
|
436
|
2
|
437 ECHOImporter newimporter = new ECHOImporter(new URL(
|
8
|
438 "http://xserve09.mpiwg-berlin.mpg.de:19280/echo_nav/echo_pages/content/showRDF"));
|
2
|
439 ESciDocDataHarvester hv = new ESciDocDataHarvester(newimporter,
|
|
440 new ECHOTransformer(), connector, "/ir/context/escidoc:1001");
|
|
441
|
|
442 // hv.readObjectsFromInstance("ECHO_collection");
|
10
|
443 //hv.readObjectsFromInstance("ECHO_resource");
|
2
|
444
|
|
445 hv.releaseAndSubmitObjects(
|
|
446 "/ir/context/escidoc:1001/resources/members",
|
8
|
447 "//escidocItem:item","first release",0);
|
|
448 hv.releaseAndSubmitObjects(
|
|
449 "/ir/context/escidoc:1001/resources/members",
|
|
450 "//escidocItem:item","first release",1);
|
|
451
|
2
|
452
|
|
453 // newimporter.organizeRessourcesInCollections(connector,
|
|
454 // "/ir/context/escidoc:1001");
|
|
455 // hv.releaseAndSubmitObjects("/ir/containers","//container:container");
|
0
|
456 }
|
|
457 }
|