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

Last change on this file since 24509 was 24509, checked in by sjm84, 13 years ago

Adding in the DocumentBasket functionality and also the new cascading way of handling xsl files which will look in the collect/site/interface/base interface places in that order

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