source: main/trunk/greenstone3/src/java/org/greenstone/gsdl3/service/DocumentBasket.java@ 24976

Last change on this file since 24976 was 24976, checked in by sjm84, 12 years ago

Reformatting this file ahead of some changes

  • Property svn:executable set to *
File size: 19.4 KB
Line 
1/*
2 * DocumentBasket.java
3 * Copyright (C) 2006 New Zealand Digital Library, http://www.nzdl.org
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20package org.greenstone.gsdl3.service;
21
22import java.sql.Statement;
23import java.util.Hashtable;
24import java.util.HashMap;
25import java.util.ArrayList;
26import java.util.HashSet;
27import java.util.Iterator;
28
29import org.w3c.dom.Element;
30import org.w3c.dom.NodeList;
31
32import org.greenstone.util.GlobalProperties;
33import org.greenstone.gsdl3.util.GSXML;
34import org.greenstone.gsdl3.util.GSPath;
35
36import java.net.InetAddress;
37import java.util.Properties;
38import java.util.Date;
39
40import javax.mail.*;
41import javax.mail.internet.*;
42
43import java.awt.event.ActionEvent;
44import java.awt.event.ActionListener;
45import javax.swing.Timer;
46
47import java.io.FileOutputStream;
48import java.io.*;
49import java.io.IOException;
50
51import org.apache.log4j.*;
52
53public class DocumentBasket extends ServiceRack
54{
55
56 static Logger logger = Logger.getLogger(org.greenstone.gsdl3.service.DocumentBasket.class.getName());
57
58 // the services on offer
59 // these strings must match what is found in the properties file
60 protected static final String ADD_ITEM_SERVICE = "AddDocument";
61 protected static final String DISPLAY_ITEMS_SERVICE = "DisplayDocumentList";
62 protected static final String ITEM_NUM_SERVICE = "GetDocuments";
63 protected static final String DELETE_ITEMS_SERVICE = "DeleteDocuments";
64 protected static final String DELETE_ITEM_SERVICE = "DeleteDocument";
65 //added
66 protected static final String MERGE_ITEM_SERVICE = "MergeDocument";
67 protected static final String ITEM_PARAM = "item";
68 protected static final String delimiter = "|";
69 protected static final int delay = 1800000;
70
71 protected static final String BASKET_BOOK = "documentBasketBook";
72
73 protected Hashtable userMap = null;
74 protected Hashtable timerMap = null;
75 protected String username = "";
76 protected String password = "";
77
78 /** constructor */
79 public DocumentBasket()
80 {
81 userMap = new Hashtable();
82 timerMap = new Hashtable();
83 }
84
85 private Hashtable updateDocMap(Element request)
86 {
87 String id = request.getAttribute("uid");
88
89 if (userMap.containsKey(id))
90 {
91 if (timerMap.containsKey(id))
92 {
93 UserTimer timer = (UserTimer) timerMap.get(id);
94 timer.restart();
95 }
96 return (Hashtable) userMap.get(id);
97 }
98 else
99 {
100 UserTimer timer = new UserTimer(delay, id);
101 timerMap.put(id, timer);
102 timer.start();
103 Hashtable newDocs = new Hashtable();
104 userMap.put(id, newDocs);
105 return newDocs;
106 }
107 }
108
109 /** configure this service */
110 public boolean configure(Element info, Element extra_info)
111 {
112 logger.info("Configuring DocumentBasket...");
113 this.config_info = info;
114
115 // set up short_service_info_ - for now just has name and type
116 Element add_service = this.doc.createElement(GSXML.SERVICE_ELEM);
117 add_service.setAttribute(GSXML.TYPE_ATT, "gather"); // what??
118 add_service.setAttribute(GSXML.NAME_ATT, ADD_ITEM_SERVICE);
119 this.short_service_info.appendChild(add_service);
120
121 // set up short_service_info_ - for now just has name and type
122 Element disp_service = this.doc.createElement(GSXML.SERVICE_ELEM);
123 disp_service.setAttribute(GSXML.TYPE_ATT, "gather"); // what??
124 disp_service.setAttribute(GSXML.NAME_ATT, DISPLAY_ITEMS_SERVICE);
125 this.short_service_info.appendChild(disp_service);
126
127 // set up short_service_info_ - for now just has name and type
128 Element num_service = this.doc.createElement(GSXML.SERVICE_ELEM);
129 num_service.setAttribute(GSXML.TYPE_ATT, "gather"); // what??
130 num_service.setAttribute(GSXML.NAME_ATT, ITEM_NUM_SERVICE);
131 this.short_service_info.appendChild(num_service);
132
133 // set up short_service_info_ - for now just has name and type
134 Element delete_service = this.doc.createElement(GSXML.SERVICE_ELEM);
135 delete_service.setAttribute(GSXML.TYPE_ATT, "gather"); // what??
136 delete_service.setAttribute(GSXML.NAME_ATT, DELETE_ITEMS_SERVICE);
137 this.short_service_info.appendChild(delete_service);
138
139 // set up short_service_info_ - for now just has name and type
140 Element deleteone_service = this.doc.createElement(GSXML.SERVICE_ELEM);
141 deleteone_service.setAttribute(GSXML.TYPE_ATT, "gather"); // what??
142 deleteone_service.setAttribute(GSXML.NAME_ATT, DELETE_ITEM_SERVICE);
143 this.short_service_info.appendChild(deleteone_service);
144
145 // set up short_service_info_ - for now just has name and type
146 Element merge_service = this.doc.createElement(GSXML.SERVICE_ELEM);
147 merge_service.setAttribute(GSXML.TYPE_ATT, "gather"); // what??
148 merge_service.setAttribute(GSXML.NAME_ATT, MERGE_ITEM_SERVICE);
149 this.short_service_info.appendChild(merge_service);
150
151 return true;
152 }
153
154 /** returns a specific service description */
155 protected Element getServiceDescription(String service_id, String lang, String subset)
156 {
157 if (service_id.equals(ADD_ITEM_SERVICE))
158 {
159 Element add_service = this.doc.createElement(GSXML.SERVICE_ELEM);
160 add_service.setAttribute(GSXML.TYPE_ATT, "gather"); // what??
161 add_service.setAttribute(GSXML.NAME_ATT, ADD_ITEM_SERVICE);
162 return add_service;
163 }
164 if (service_id.equals(DISPLAY_ITEMS_SERVICE))
165 {
166
167 Element disp_service = this.doc.createElement(GSXML.SERVICE_ELEM);
168 disp_service.setAttribute(GSXML.TYPE_ATT, "gather"); // what??
169 disp_service.setAttribute(GSXML.NAME_ATT, DISPLAY_ITEMS_SERVICE);
170 return disp_service;
171 }
172
173 if (service_id.equals(ITEM_NUM_SERVICE))
174 {
175
176 Element num_service = this.doc.createElement(GSXML.SERVICE_ELEM);
177 num_service.setAttribute(GSXML.TYPE_ATT, "gather"); // what??
178 num_service.setAttribute(GSXML.NAME_ATT, ITEM_NUM_SERVICE);
179 return num_service;
180 }
181
182 if (service_id.equals(DELETE_ITEMS_SERVICE))
183 {
184
185 Element del_service = this.doc.createElement(GSXML.SERVICE_ELEM);
186 del_service.setAttribute(GSXML.TYPE_ATT, "gather"); // what??
187 del_service.setAttribute(GSXML.NAME_ATT, DELETE_ITEMS_SERVICE);
188 return del_service;
189 }
190
191 if (service_id.equals(DELETE_ITEM_SERVICE))
192 {
193
194 Element delone_service = this.doc.createElement(GSXML.SERVICE_ELEM);
195 delone_service.setAttribute(GSXML.TYPE_ATT, "gather"); // what??
196 delone_service.setAttribute(GSXML.NAME_ATT, DELETE_ITEM_SERVICE);
197 return delone_service;
198 }
199 if (service_id.equals(MERGE_ITEM_SERVICE))
200 {
201 Element merge_service = this.doc.createElement(GSXML.SERVICE_ELEM);
202 merge_service.setAttribute(GSXML.TYPE_ATT, "gather"); // what??
203 merge_service.setAttribute(GSXML.NAME_ATT, MERGE_ITEM_SERVICE);
204 return merge_service;
205 }
206 return null;
207 }
208
209 protected Element processAddDocument(Element request)
210 {
211 //System.err.println("REQUEST = " + GSXML.xmlNodeToString(request));
212 Hashtable docsMap = updateDocMap(request);
213 //System.err.println("DOCSMAP = " + docsMap);
214 // Create a new (empty) result message
215 Element result = this.doc.createElement(GSXML.RESPONSE_ELEM);
216
217 // Get the parameters of the request
218 Element param_list = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
219 if (param_list == null)
220 {
221 logger.error("DocumentBasket Error: AddDocument request had no paramList.");
222 return result; // Return the empty result
223 }
224
225 HashMap params = GSXML.extractParams(param_list, false);
226 //System.err.println("PARAMS = " + params);
227 String item = (String) params.get("item");
228
229 int startIndex = item.startsWith(BASKET_BOOK) ? BASKET_BOOK.length() : 0;
230
231 String collection = "";
232 int pos = item.indexOf(":");
233 if (pos != -1)
234 {
235 collection = item.substring(startIndex, pos);
236 item = item.substring(pos + 1);
237 }
238 //logger.error("COLLECTION = " + collection + " *** ITEM = " + item);
239 if (docsMap.containsKey(collection))
240 {
241 Hashtable items = (Hashtable) docsMap.get(collection);
242 if (!items.containsKey(item))
243 {
244 Item newItem = generateItem(collection, item);
245 items.put(item, newItem);
246 result.appendChild(newItem.wrapIntoElement());
247 }
248 }
249 else
250 {
251 Hashtable items = new Hashtable();
252 Item newItem = generateItem(collection, item);
253 items.put(item, newItem);
254 docsMap.put(collection, items);
255 result.appendChild(newItem.wrapIntoElement());
256 }
257
258 return result;
259 }
260
261 protected Element processMergeDocument(Element request)
262 {
263 // Get the parameters of the request
264 Element param_list = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
265 if (param_list == null)
266 {
267 logger.error("DocumentBasket Error: MergeDocument request had no paramList.");
268 return null; // Return the empty result
269 }
270
271 HashMap params = GSXML.extractParams(param_list, false);
272
273 String docString = (String) params.get("docs");
274 String[] docs = docString.split("-");
275
276 for (String d : docs)
277 {
278 logger.error("DOC = " + d);
279 }
280
281 /*
282 * try{ System.out.println("Concatenate Two PDF"); PdfReader reader1 =
283 * new PdfReader("3A01-01_Part_1-001.pdf"); PdfReader reader2 = new
284 * PdfReader("3A01-01_Part_1-017.pdf"); PdfCopyFields copy = new
285 * PdfCopyFields(new FileOutputStream("concatenatedPDF.pdf"));
286 * copy.addDocument(reader1); copy.addDocument(reader2); copy.close(); }
287 * catch(Exception ex) { ex.printStackTrace(); }
288 */
289 // added -->
290
291 try
292 {
293 {
294 PrintWriter pw = new PrintWriter(new FileOutputStream("G:/output1.xml"));
295 File file = new File("G:/greenstone3-svn/web/sites/localsite/collect/peij21/archives/HASH0189.dir/");
296 File[] files = file.listFiles();
297
298 for (int i = 0; i < files.length; i++)
299 {
300
301 //System.out.println(files[i].getName());
302 String fileName = files[i].getName();
303
304 if (fileName.equals("doc.xml"))
305 {
306
307 System.out.println("Processing " + files[i].getPath() + "... ");
308 BufferedReader br = new BufferedReader(new FileReader(files[i].getPath()));
309 String line = br.readLine();
310 while (line != null)
311 {
312 pw.println(line);
313 line = br.readLine();
314 }
315 br.close();
316
317 }
318
319 }
320
321 File file1 = new File("G:/greenstone3-svn/web/sites/localsite/collect/peij21/archives/HASHfdc0.dir/");
322 File[] files1 = file1.listFiles();
323
324 for (int i = 0; i < files1.length; i++)
325 {
326
327 //System.out.println(files[i].getName());
328 String fileName = files1[i].getName();
329
330 if (fileName.equals("doc.xml"))
331 {
332
333 System.out.println("Processing " + files1[i].getPath() + "... ");
334 BufferedReader br = new BufferedReader(new FileReader(files1[i].getPath()));
335 String line = br.readLine();
336 while (line != null)
337 {
338 pw.println(line);
339 line = br.readLine();
340 }
341 br.close();
342
343 }
344
345 }
346
347 pw.close();
348
349 System.out.println("All doc.xml files have been concatenated into output1.xml");
350 }
351 }
352 catch (Exception ex)
353 {
354 ex.printStackTrace();
355 }
356 return null;
357 }
358
359 //end
360 private Item generateItem(String collection, String id)
361 {
362 Item item = new Item(collection, id);
363 String to = GSPath.appendLink(collection, "DocumentMetadataRetrieve");
364 ArrayList tmp = new ArrayList();
365 tmp.add(id);
366 Element response = getDocumentMetadata(to, "en", "dumy", tmp.iterator());
367 Element doc_node = (Element) response.getElementsByTagName(GSXML.DOC_NODE_ELEM).item(0);
368
369 String node_id = doc_node.getAttribute(GSXML.NODE_ID_ATT);
370 Element metadata_list = (Element) doc_node.getElementsByTagName(GSXML.METADATA_ELEM + GSXML.LIST_MODIFIER).item(0);
371
372 //assign title metadata if any
373 Element metadata = GSXML.getNamedElement(metadata_list, "metadata", "name", "Title");
374 if (metadata != null)
375 {
376 item.title = GSXML.getNodeText(metadata).trim();
377 }
378 //assign date metadata if any
379 metadata = GSXML.getNamedElement(metadata_list, "metadata", "name", "Date");
380 if (metadata != null)
381 {
382 item.date = GSXML.getNodeText(metadata).trim();
383 }
384
385 //assign root title metadata if any
386 metadata = GSXML.getNamedElement(metadata_list, "metadata", "name", "root_Title");
387 if (metadata != null)
388 {
389 String rootTitle = GSXML.getNodeText(metadata).trim();
390 if (!rootTitle.equals(item.title))
391 {
392 item.rootTitle = rootTitle;
393 }
394
395 }
396
397 return item;
398 }
399
400 protected Element processDeleteDocuments(Element request)
401 {
402 Hashtable docsMap = updateDocMap(request);
403
404 // Create a new (empty) result message
405 Element result = this.doc.createElement(GSXML.RESPONSE_ELEM);
406
407 // Get the parameters of the request
408 Element param_list = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
409
410 //GSXML.printXMLNode(param_list);
411
412 if (param_list == null)
413 {
414 logger.error("DocumentBasket Error: DeleteDocument request had no paramList.");
415 return result; // Return the empty result
416 }
417
418 HashMap params = GSXML.extractParams(param_list, false);
419
420 String param = (String) params.get("items");
421
422 if (param == null)
423 return result;
424
425 String[] items = param.split("\\|");
426
427 for (int i = 0; i < items.length; i++)
428 {
429 String item = items[i];
430 if (item.trim().length() == 0)
431 continue;
432
433 String collection = "";
434 int pos = item.indexOf(":");
435 if (pos != -1)
436 {
437 collection = item.substring(0, pos);
438 item = item.substring(pos + 1);
439 }
440
441 if (docsMap.containsKey(collection))
442 {
443 Hashtable itemMap = (Hashtable) docsMap.get(collection);
444 if (itemMap.containsKey(item))
445 {
446 itemMap.remove(item);
447 }
448 if (itemMap.size() == 0)
449 {
450 docsMap.remove(collection);
451 }
452 }
453 }
454
455 return result;
456 }
457
458 protected Element processDeleteDocument(Element request)
459 {
460 Hashtable docsMap = updateDocMap(request);
461
462 // Create a new (empty) result message
463 Element result = this.doc.createElement(GSXML.RESPONSE_ELEM);
464
465 // Get the parameters of the request
466 Element param_list = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
467
468 //GSXML.printXMLNode(param_list);
469
470 if (param_list == null)
471 {
472 logger.error("DocumentBasket Error: DeleteDocument request had no paramList.");
473 return result; // Return the empty result
474 }
475
476 HashMap params = GSXML.extractParams(param_list, false);
477
478 String param = (String) params.get("item");
479
480 if (param == null)
481 return result;
482
483 String item = param;
484
485 String collection = "";
486 int pos = item.indexOf(":");
487
488 if (pos != -1)
489 {
490 collection = item.substring(0, pos);
491 item = item.substring(pos + 1);
492 }
493
494 if (docsMap.containsKey(collection))
495 {
496 Hashtable itemMap = (Hashtable) docsMap.get(collection);
497 if (itemMap.containsKey(item))
498 {
499 itemMap.remove(item);
500 }
501 if (itemMap.size() == 0)
502 {
503 docsMap.remove(collection);
504 }
505 }
506
507 return result;
508 }
509
510 protected Element processGetDocuments(Element request)
511 {
512 // GSXML.printXMLNode(request);
513 Hashtable docsMap = updateDocMap(request);
514
515 // Create a new (empty) result message
516 Element result = this.doc.createElement(GSXML.RESPONSE_ELEM);
517
518 int size = 0;
519 String ids = "";
520 Iterator keys = docsMap.keySet().iterator();
521
522 while (keys.hasNext())
523 {
524 Hashtable items = (Hashtable) docsMap.get((String) keys.next());
525 size += items.size();
526 Iterator values = items.values().iterator();
527 while (values.hasNext())
528 {
529 Item item = (Item) values.next();
530 result.appendChild(item.wrapIntoElement());
531 }
532 }
533
534 Element selement = this.doc.createElement("size");
535 selement.setAttribute("value", size + "");
536 result.appendChild(selement);
537
538 return result;
539 }
540
541 private Element getDocumentMetadata(String to, String lang, String uid, Iterator ids)
542 {
543
544 // Build a request to obtain some document metadata
545 Element dm_message = this.doc.createElement(GSXML.MESSAGE_ELEM);
546 Element dm_request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_PROCESS, to, lang, uid);
547 dm_message.appendChild(dm_request);
548
549 // Create a parameter list to specify the required metadata information
550 HashSet meta_names = new HashSet();
551 meta_names.add("Title"); // the default
552 meta_names.add("root_Title");
553 meta_names.add("Date");
554
555 Element param_list = this.doc.createElement(GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
556
557 Element param = null;
558 Iterator i = meta_names.iterator();
559 while (i.hasNext())
560 {
561 String name = (String) i.next();
562 param = this.doc.createElement(GSXML.PARAM_ELEM);
563 param_list.appendChild(param);
564 param.setAttribute(GSXML.NAME_ATT, "metadata");
565 param.setAttribute(GSXML.VALUE_ATT, name);
566 }
567
568 dm_request.appendChild(param_list);
569
570 // create the doc node list for the metadata request
571 Element dm_doc_list = this.doc.createElement(GSXML.DOC_NODE_ELEM + GSXML.LIST_MODIFIER);
572 dm_request.appendChild(dm_doc_list);
573
574 while (ids.hasNext())
575 {
576 // Add the documentNode to the list
577 Element dm_doc_node = this.doc.createElement(GSXML.DOC_NODE_ELEM);
578 dm_doc_list.appendChild(dm_doc_node);
579 dm_doc_node.setAttribute(GSXML.NODE_ID_ATT, (String) ids.next());
580 }
581
582 return (Element) this.router.process(dm_message);
583 }
584
585 protected Element processDisplayDocumentList(Element request)
586 {
587 Hashtable docsMap = updateDocMap(request);
588
589 // Create a new (empty) result message
590 Element result = this.doc.createElement(GSXML.RESPONSE_ELEM);
591
592 Iterator keys = docsMap.keySet().iterator();
593
594 while (keys.hasNext())
595 {
596 String collection = (String) keys.next();
597 Hashtable items = (Hashtable) docsMap.get(collection);
598 Iterator itemItr = items.values().iterator();
599
600 Element collectionNode = this.doc.createElement("documentList");
601 collectionNode.setAttribute("name", collection);
602 result.appendChild(collectionNode);
603
604 while (itemItr.hasNext())
605 {
606 Item item = (Item) itemItr.next();
607 Element itemElement = this.doc.createElement("item");
608
609 collectionNode.appendChild(itemElement);
610 itemElement.setAttribute("name", item.docid);
611 itemElement.setAttribute("collection", item.collection);
612 itemElement.setAttribute("title", item.title);
613 itemElement.setAttribute("date", item.date);
614 itemElement.setAttribute("root_title", item.rootTitle);
615 }
616 }
617
618 return result;
619 }
620
621 protected class Item
622 {
623 public String collection;
624 public String docid;
625 public String title = "";
626 public String query = "";
627 public String date = "";
628 public String rootTitle = "";
629
630 public Item(String coll, String id)
631 {
632 this.collection = coll;
633 this.docid = id;
634 }
635
636 public boolean equals(Object o)
637 {
638 if (!(o instanceof Item))
639 {
640 return false;
641 }
642
643 Item item = (Item) o;
644 String id = collection + ":" + docid;
645 String idin = item.collection + ":" + item.docid;
646 return id.equals(idin);
647 }
648
649 public String toString()
650 {
651 return collection + ":" + docid + ":" + "[" + ((!rootTitle.equals("")) ? (rootTitle + ":") : "") + title + "]";
652 }
653
654 public Element wrapIntoElement()
655 {
656 Element itemElement = doc.createElement("item");
657 itemElement.setAttribute("name", docid);
658 itemElement.setAttribute("collection", collection);
659 itemElement.setAttribute("title", title);
660 itemElement.setAttribute("date", date);
661 itemElement.setAttribute("root_title", rootTitle);
662 return itemElement;
663 }
664 }
665
666 private class UserTimer extends Timer implements ActionListener
667 {
668 String id = "";
669
670 public UserTimer(int delay, String id)
671 {
672 super(delay, (ActionListener) null);
673 addActionListener(this);
674 this.id = id;
675 }
676
677 public void actionPerformed(ActionEvent e)
678 {
679 userMap.remove(id);
680 timerMap.remove(id);
681 stop();
682 }
683 }
684}
Note: See TracBrowser for help on using the repository browser.