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

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

Adding UserContext to replace the use of lang and uid

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