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) {
|
|
243 addedObjects.add(escidocItem.getESciDocId());
|
|
244 addedFile.debug(escidocItem.getESciDocId() + "\n");
|
|
245
|
|
246 } else {
|
|
247 notAddedObjects.add(obj.echoUrl);
|
|
248 notAddedFile.debug(obj.echoUrl);
|
|
249
|
|
250 }
|
|
251
|
|
252 } catch (IOException e) {
|
|
253 // TODO Auto-generated catch block
|
|
254 e.printStackTrace();
|
|
255 throw new ESciDocXmlObjectException();
|
|
256 } catch (JDOMException e) {
|
|
257 // TODO Auto-generated catch block
|
|
258 e.printStackTrace();
|
|
259 throw new ESciDocXmlObjectException();
|
|
260 }
|
|
261
|
|
262 }
|
|
263 }
|
|
264 if (logger.getLevel() == Level.DEBUG) {
|
|
265 for (String addedObject : addedObjects) {
|
|
266 logger.debug(addedObject);
|
|
267 }
|
|
268 }
|
|
269
|
|
270 return true;
|
|
271 }
|
|
272
|
|
273
|
|
274
|
3
|
275
|
|
276 /**
|
|
277 * Deal with existing objects, do nothing if md5 of stored metadata and metadata on the server is the same otherwise call {@link #updateObject(ECHOObject)}.
|
|
278 * @param objNew
|
|
279 * @param old
|
|
280 * @throws TransformerException
|
|
281 * @throws ESciDocXmlObjectException
|
|
282 */
|
|
283 private void handleExistingObject(ECHOObject objNew, ECHOObject old) throws TransformerException, ESciDocXmlObjectException {
|
|
284 ECHORessource objNewRes = (ECHORessource)objNew;
|
|
285 ECHORessource objOldRes = (ECHORessource)old;
|
|
286 String md5onServer = objNewRes.getIndexMetaMD5onServer();
|
|
287 String md5=objOldRes.getIndexMetaMD5stored();
|
8
|
288
|
|
289 objNewRes.writeEsciDocIDToIndexMeta(objOldRes.eScidocId);
|
|
290
|
3
|
291 if (md5onServer.equals(md5))
|
|
292 return;
|
|
293 else {
|
8
|
294
|
3
|
295 updateObject(objNew, old);
|
8
|
296
|
|
297
|
3
|
298 }
|
8
|
299
|
3
|
300 }
|
|
301
|
|
302 private void updateObject(ECHOObject objNew, ECHOObject objOld) throws TransformerException, ESciDocXmlObjectException {
|
6
|
303 objNew.context = context;
|
8
|
304
|
|
305 //erzeuge erst einmal ein neues XML Object aus den neuen Daten.
|
3
|
306 eSciDocXmlObject escidocItem = transformer.transform(objNew);
|
|
307 String lastModificationDateOld = objOld.lastModificationDate;
|
8
|
308
|
|
309 //jetzt das alte ModeificationDate dort rein (wegen, optimitistic locking)
|
3
|
310 escidocItem.setLastModificationDate(lastModificationDateOld);
|
|
311 try {
|
8
|
312 HttpResponse ret = connector.eScidocPut("/ir/item/"+objOld.eScidocId, EScidocBasicHandler.convertStringToStream(escidocItem.printXML()));
|
3
|
313 HttpEntity ent = ret.getEntity();
|
|
314 if (ret.getStatusLine().getStatusCode() != 200) {
|
|
315 logger.debug("Can not update:" + objOld.eScidocId);
|
|
316 // res.getEntity().consumeContent(); // necessary to release
|
|
317 // the conneciton
|
|
318 ent.consumeContent();
|
|
319
|
|
320 }
|
|
321 InputStream restream = ret.getEntity().getContent();
|
|
322 logger.debug(EScidocBasicHandler.convertStreamToString(restream));
|
|
323 } catch (UnsupportedEncodingException e) {
|
|
324 // TODO Auto-generated catch block
|
|
325 e.printStackTrace();
|
|
326 } catch (IOException e) {
|
|
327 // TODO Auto-generated catch block
|
|
328 e.printStackTrace();
|
|
329 }
|
8
|
330
|
5
|
331 addedFile.debug("updated:"+objOld.eScidocId);
|
3
|
332 }
|
|
333
|
2
|
334 /**
|
|
335 * @param command
|
|
336 * @param objectXPath
|
8
|
337 * @param comment der in escidoc bei der Operation abgespeichert wird.
|
2
|
338 * @param mode 0 : only submit, 1:only release, 2:release and submit
|
|
339 * @throws Exception
|
|
340 */
|
8
|
341 public void releaseAndSubmitObjects(String command, String objectXPath,String comment,int mode)
|
2
|
342 throws Exception {
|
|
343
|
|
344 Integer numberOfHits = connector.getNumberOfHitsFromFilterResult(
|
|
345 command, objectXPath,mode);
|
8
|
346
|
|
347
|
2
|
348 int tausend = ((numberOfHits-1) / MAX_REC);
|
8
|
349
|
2
|
350 String queryRestrict="";
|
|
351 if(mode==0 | mode==2){
|
|
352 queryRestrict="query=%22/properties/version/status%22=pending";
|
|
353 } else {
|
|
354 queryRestrict="query=%22/properties/version/status%22=submitted";
|
|
355 }
|
8
|
356
|
2
|
357 for (int t = 0; t <= tausend; t++) {
|
|
358 int start = t * MAX_REC+1;
|
|
359 // int max=Math.min((t+1)*1000, numberOfHits);
|
|
360 String query = "?maximumRecords="+String.valueOf(MAX_REC)+"&startRecord="
|
|
361 + String.valueOf(start)+"&"+queryRestrict;
|
|
362 for (eSciDocXmlObject obj : connector
|
4
|
363 .getObjectsFromFilterResult(command+query, objectXPath)) {
|
8
|
364
|
2
|
365 //TODO is the following really necessary, currently the obj in the list is sometimes not the current one.
|
|
366 try{
|
8
|
367 HttpResponse resObj = connector.eScidocGet(obj.getESciDocId());
|
|
368 HttpEntity ent = resObj.getEntity();
|
|
369 if (ent!=null){
|
|
370 obj= new eSciDocXmlObject(ent.getContent());
|
|
371 } else {
|
|
372 logger.debug("Can not retrieve:" + obj.getESciDocId());
|
|
373 continue;
|
|
374 }
|
2
|
375 } catch (Exception e){
|
|
376 logger.debug("Can not retrieve:" + obj.getESciDocId());
|
|
377 continue;
|
|
378 }
|
|
379 if (mode==0 | mode==2){
|
|
380 HttpResponse res = connector.submitAnObject(obj,
|
8
|
381 comment);
|
2
|
382 logger.debug(res.getStatusLine());
|
8
|
383
|
2
|
384 if (res.getStatusLine().getStatusCode() != 200) {
|
|
385 logger.debug("Can not submit:" + obj.getESciDocId());
|
|
386 // res.getEntity().consumeContent(); // necessary to release
|
|
387 // the conneciton
|
8
|
388
|
2
|
389 }
|
|
390 InputStream restream = res.getEntity().getContent();
|
|
391 logger.debug(EScidocBasicHandler.convertStreamToString(restream));
|
|
392 //res.getEntity().consumeContent(); // necessary to release the
|
8
|
393 // conneciton
|
|
394
|
2
|
395 if (!connector.upDateObject(obj)) {
|
|
396 logger.debug("Can not update:" + obj.getESciDocId());
|
|
397 // continue;
|
8
|
398
|
2
|
399 }
|
|
400 }
|
8
|
401
|
2
|
402 if (mode==1 | mode==2){
|
8
|
403 HttpResponse res = connector.releaseAnObject(obj, comment);
|
2
|
404 logger.debug(res.getStatusLine());
|
|
405 if (res.getStatusLine().getStatusCode() != 200) {
|
|
406 logger.debug("Can not release:" + obj.getESciDocId());
|
|
407 res.getEntity().consumeContent(); // necessary to release
|
8
|
408 // the conneciton
|
2
|
409 continue;
|
|
410 }
|
|
411 addedFile.debug("RELEASED:" + obj.getESciDocId());
|
|
412 res.getEntity().consumeContent(); // necessary to release the
|
8
|
413 // connecito
|
2
|
414 }
|
0
|
415 }
|
|
416 }
|
2
|
417
|
0
|
418 }
|
2
|
419
|
|
420 public static void main(String[] args) throws Exception {
|
|
421
|
0
|
422 Logger rl = Logger.getRootLogger();
|
8
|
423 DOMConfigurator.configure("/etc/escidocImportConfig.xml");
|
2
|
424 rl.setLevel(Level.DEBUG);
|
0
|
425
|
8
|
426
|
9
|
427 EScidocBasicHandler connector = new EScidocBasicHandler("escidoc.mpiwg-berlin.mpg.de",8080,"dwinter","weikiki7");
|
8
|
428
|
|
429
|
|
430 //ECHOImporter newimporter = new ECHOImporter(new URL(
|
|
431 // "file:///Users/dwinter/libcoll.rdf"));
|
|
432
|
2
|
433 ECHOImporter newimporter = new ECHOImporter(new URL(
|
8
|
434 "http://xserve09.mpiwg-berlin.mpg.de:19280/echo_nav/echo_pages/content/showRDF"));
|
2
|
435 ESciDocDataHarvester hv = new ESciDocDataHarvester(newimporter,
|
|
436 new ECHOTransformer(), connector, "/ir/context/escidoc:1001");
|
|
437
|
|
438 // hv.readObjectsFromInstance("ECHO_collection");
|
8
|
439 hv.readObjectsFromInstance("ECHO_resource");
|
2
|
440
|
|
441 hv.releaseAndSubmitObjects(
|
|
442 "/ir/context/escidoc:1001/resources/members",
|
8
|
443 "//escidocItem:item","first release",0);
|
|
444 hv.releaseAndSubmitObjects(
|
|
445 "/ir/context/escidoc:1001/resources/members",
|
|
446 "//escidocItem:item","first release",1);
|
|
447
|
2
|
448
|
|
449 // newimporter.organizeRessourcesInCollections(connector,
|
|
450 // "/ir/context/escidoc:1001");
|
|
451 // hv.releaseAndSubmitObjects("/ir/containers","//container:container");
|
0
|
452 }
|
|
453 }
|