source: main/trunk/greenstone3/src/java/org/greenstone/gsdl3/service/FavouriteBasket.java@ 37546

Last change on this file since 37546 was 37546, checked in by kjdon, 14 months ago

FavouriteBasket needs to add format info for DisplayItems service

  • Property svn:keywords set to Author Date Id Revision
File size: 16.9 KB
Line 
1/*
2 * FavouriteBasket.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;
28import java.util.Set;
29
30import org.w3c.dom.Document;
31import org.w3c.dom.Element;
32import org.w3c.dom.NodeList;
33
34import org.greenstone.util.GlobalProperties;
35import org.greenstone.gsdl3.util.GSXML;
36import org.greenstone.gsdl3.util.GSPath;
37import org.greenstone.gsdl3.util.UserContext;
38import org.greenstone.gsdl3.util.XMLConverter;
39
40import java.io.Serializable;
41import java.net.InetAddress;
42import java.util.Properties;
43import java.util.Date;
44
45import javax.mail.*;
46import javax.mail.internet.*;
47
48import java.awt.event.ActionEvent;
49import java.awt.event.ActionListener;
50import javax.swing.Timer;
51
52import org.apache.log4j.*;
53
54public class FavouriteBasket extends ServiceRack
55{
56
57 static Logger logger = Logger.getLogger(org.greenstone.gsdl3.service.FavouriteBasket.class.getName());
58
59 // the services on offer
60 // these strings must match what is found in the properties file
61 protected static final String ADD_ITEM_SERVICE = "AddFavourite";
62 protected static final String DISPLAY_ITEMS_SERVICE = "GetFavouritesList";
63 protected static final String ITEM_NUM_SERVICE = "GetNumFavourites";
64 protected static final String DELETE_ITEMS_SERVICE = "DeleteFavourites";
65 protected static final String SEND_MAIL_SERVICE = "SendFavouritesMail";
66 //protected static final String DELETE_ITEM_SERVICE = "DeleteItem";
67
68 // for AddFavourite
69 protected static final String ITEM_PARAM = "item";
70 // for DeleteFavouritess
71 protected static final String ITEMS_PARAM = "items";
72 // for SendMail
73 protected static final String ADDRESS_PARAM = "address";
74 protected static final String SUBJECT_PARAM = "subject";
75 protected static final String CONTENT_PARAM = "content";
76 protected static final String CC_PARAM = "cc";
77 protected static final String BCC_PARAM = "bcc";
78
79
80 protected static final String delimiter = "|";
81 protected static final int delay = 1800000;
82
83 protected Hashtable<String, Hashtable<String, Hashtable<String, Item>>> userMap = null;
84 protected Hashtable<String, UserTimer> timerMap = null;
85 protected String username = "";
86 protected String password = "";
87
88 HashSet<String> meta_names = null;
89
90 /** constructor */
91 public FavouriteBasket()
92 {
93 userMap = new Hashtable<String, Hashtable<String, Hashtable<String, Item>>>();
94 timerMap = new Hashtable<String, UserTimer>();
95 }
96
97 private Hashtable<String, Hashtable<String, Item>> updateDocMap(Element request)
98 {
99
100 UserContext userContext = new UserContext(request);
101 String id = null;
102 String user_name =userContext.getUsername();
103 if (user_name == null || user_name.equals("")) {
104 id = userContext.getUserID();
105 } else {
106 id = user_name;
107 }
108 if (userMap.containsKey(id))
109 {
110 if (timerMap.containsKey(id))
111 {
112 UserTimer timer = timerMap.get(id);
113 timer.restart();
114 }
115 return userMap.get(id);
116 }
117 else
118 {
119 UserTimer timer = new UserTimer(delay, id);
120 timerMap.put(id, timer);
121 timer.start();
122 Hashtable<String, Hashtable<String, Item>> newDocs = new Hashtable<String, Hashtable<String, Item>>();
123 userMap.put(id, newDocs);
124 return newDocs;
125 }
126 }
127
128 /** configure this service */
129 public boolean configure(Element info, Element extra_info)
130 {
131 logger.info("Configuring FavouriteBasket...");
132 this.config_info = info;
133
134 // set up short_service_info_ - for now just has name and type
135 Element add_service = this.desc_doc.createElement(GSXML.SERVICE_ELEM);
136 add_service.setAttribute(GSXML.TYPE_ATT, "gather"); // what??
137 add_service.setAttribute(GSXML.NAME_ATT, ADD_ITEM_SERVICE);
138 this.short_service_info.appendChild(add_service);
139
140 // set up short_service_info_ - for now just has name and type
141 Element disp_service = this.desc_doc.createElement(GSXML.SERVICE_ELEM);
142 disp_service.setAttribute(GSXML.TYPE_ATT, "gather"); // what??
143 disp_service.setAttribute(GSXML.NAME_ATT, DISPLAY_ITEMS_SERVICE);
144 this.short_service_info.appendChild(disp_service);
145
146 // set up short_service_info_ - for now just has name and type
147 Element num_service = this.desc_doc.createElement(GSXML.SERVICE_ELEM);
148 num_service.setAttribute(GSXML.TYPE_ATT, "gather"); // what??
149 num_service.setAttribute(GSXML.NAME_ATT, ITEM_NUM_SERVICE);
150 this.short_service_info.appendChild(num_service);
151
152 // set up short_service_info_ - for now just has name and type
153 Element delete_service = this.desc_doc.createElement(GSXML.SERVICE_ELEM);
154 delete_service.setAttribute(GSXML.TYPE_ATT, "gather"); // what??
155 delete_service.setAttribute(GSXML.NAME_ATT, DELETE_ITEMS_SERVICE);
156 this.short_service_info.appendChild(delete_service);
157
158 // set up short_service_info_ - for now just has name and type
159 // Element deleteone_service = this.desc_doc.createElement(GSXML.SERVICE_ELEM);
160 // deleteone_service.setAttribute(GSXML.TYPE_ATT, "gather"); // what??
161 // deleteone_service.setAttribute(GSXML.NAME_ATT, DELETE_ITEM_SERVICE);
162 // this.short_service_info.appendChild(deleteone_service);
163
164 // set up short_service_info_ - for now just has name and type
165 Element mail_service = this.desc_doc.createElement(GSXML.SERVICE_ELEM);
166 mail_service.setAttribute(GSXML.TYPE_ATT, "gather"); // what??
167 mail_service.setAttribute(GSXML.NAME_ATT, SEND_MAIL_SERVICE);
168 this.short_service_info.appendChild(mail_service);
169
170 Element format_info = (Element) GSXML.getChildByTagName(info, GSXML.FORMAT_ELEM);
171 if (format_info != null)
172 {
173 this.format_info_map.put(DISPLAY_ITEMS_SERVICE, this.desc_doc.importNode(format_info, true));
174 }
175
176 return true;
177
178 }
179
180 /** returns a specific service description */
181 protected Element getServiceDescription(Document doc, String service_id, String lang, String subset)
182 {
183
184 if (service_id.equals(ADD_ITEM_SERVICE))
185 {
186 Element add_service = doc.createElement(GSXML.SERVICE_ELEM);
187 add_service.setAttribute(GSXML.TYPE_ATT, "gather"); // what??
188 add_service.setAttribute(GSXML.NAME_ATT, ADD_ITEM_SERVICE);
189 return add_service;
190 }
191 if (service_id.equals(DISPLAY_ITEMS_SERVICE))
192 {
193
194 Element disp_service = doc.createElement(GSXML.SERVICE_ELEM);
195 disp_service.setAttribute(GSXML.TYPE_ATT, "gather"); // what??
196 disp_service.setAttribute(GSXML.NAME_ATT, DISPLAY_ITEMS_SERVICE);
197 return disp_service;
198 }
199
200 if (service_id.equals(ITEM_NUM_SERVICE))
201 {
202
203 Element num_service = doc.createElement(GSXML.SERVICE_ELEM);
204 num_service.setAttribute(GSXML.TYPE_ATT, "gather"); // what??
205 num_service.setAttribute(GSXML.NAME_ATT, ITEM_NUM_SERVICE);
206 return num_service;
207 }
208
209 if (service_id.equals(DELETE_ITEMS_SERVICE))
210 {
211
212 Element del_service = doc.createElement(GSXML.SERVICE_ELEM);
213 del_service.setAttribute(GSXML.TYPE_ATT, "gather"); // what??
214 del_service.setAttribute(GSXML.NAME_ATT, DELETE_ITEMS_SERVICE);
215 return del_service;
216 }
217
218 if (service_id.equals(SEND_MAIL_SERVICE))
219 {
220
221 Element mail_service = doc.createElement(GSXML.SERVICE_ELEM);
222 mail_service.setAttribute(GSXML.TYPE_ATT, "gather"); // what??
223 mail_service.setAttribute(GSXML.NAME_ATT, SEND_MAIL_SERVICE);
224 return mail_service;
225 }
226
227 return null;
228 }
229
230 protected Element processAddFavourite(Element request)
231 {
232
233 Hashtable<String, Hashtable<String, Item>> docsMap = updateDocMap(request);
234
235 // Create a new (empty) result message
236 Document result_doc = XMLConverter.newDOM();
237 Element result = result_doc.createElement(GSXML.RESPONSE_ELEM);
238
239 // Get the parameters of the request
240 Element param_list = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
241 if (param_list == null)
242 {
243 logger.error("FavouriteBasket Error: AddFavourite request had no paramList.");
244 return result; // Return the empty result
245 }
246
247 HashMap<String, Serializable> params = GSXML.extractParams(param_list, false);
248
249 String item = (String) params.get(ITEM_PARAM);
250 String collection = "";
251 int pos = item.indexOf(":");
252 if (pos != -1)
253 {
254 collection = item.substring(0, pos);
255 item = item.substring(pos + 1);
256 }
257
258 if (docsMap.containsKey(collection))
259 {
260 Hashtable<String, Item> items = docsMap.get(collection);
261 if (!items.containsKey(item))
262 {
263 Item newItem = new Item(collection, item);
264 items.put(item, newItem);
265 result.appendChild(newItem.wrapIntoElement(result_doc));
266 }
267 }
268 else
269 {
270 Hashtable<String, Item> items = new Hashtable<String, Item>();
271 Item newItem = new Item(collection, item);
272 items.put(item, newItem);
273 docsMap.put(collection, items);
274 result.appendChild(newItem.wrapIntoElement(result_doc));
275 }
276
277 return result;
278 }
279
280
281 protected Element processDeleteFavouritess(Element request)
282 {
283
284 Hashtable<String, Hashtable<String, Item>> docsMap = updateDocMap(request);
285
286 // Create a new (empty) result message
287 Document result_doc = XMLConverter.newDOM();
288 Element result = result_doc.createElement(GSXML.RESPONSE_ELEM);
289
290 // Get the parameters of the request
291 Element param_list = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
292
293 if (param_list == null)
294 {
295 logger.error("FavouriteBasket Error: DeleteItem request had no paramList.");
296 return result; // Return the empty result
297 }
298
299 HashMap<String, Serializable> params = GSXML.extractParams(param_list, false);
300
301 String param = (String) params.get(ITEMS_PARAM);
302
303 if (param == null)
304 return result;
305
306 String[] items = param.split("\\|");
307
308 for (int i = 0; i < items.length; i++)
309 {
310 String item = items[i];
311 if (item.trim().length() == 0)
312 continue;
313 String collection = "";
314 int pos = item.indexOf(":");
315 if (pos != -1)
316 {
317 collection = item.substring(0, pos);
318 item = item.substring(pos + 1);
319 }
320
321 if (docsMap.containsKey(collection))
322 {
323 Hashtable itemMap = docsMap.get(collection);
324 if (itemMap.containsKey(item))
325 {
326 itemMap.remove(item);
327 }
328 if (itemMap.size() == 0)
329 {
330 docsMap.remove(collection);
331 }
332 }
333
334 }
335
336 return result;
337 }
338
339
340 protected Element processGetNumFavourites(Element request)
341 {
342 Hashtable<String, Hashtable<String, Item>> docsMap = updateDocMap(request);
343
344 // Create a new (empty) result message
345 Document result_doc = XMLConverter.newDOM();
346 Element result = result_doc.createElement(GSXML.RESPONSE_ELEM);
347
348 int size = 0;
349 String ids = "";
350 Iterator<String> keys = docsMap.keySet().iterator();
351
352 while (keys.hasNext())
353 {
354 Hashtable items = docsMap.get(keys.next());
355 size += items.size();
356 Iterator values = items.values().iterator();
357 while (values.hasNext())
358 {
359 Item item = (Item) values.next();
360 result.appendChild(item.wrapIntoElement(result_doc));
361 }
362 }
363
364 Element selement = result_doc.createElement("size");
365 selement.setAttribute("value", size + "");
366 result.appendChild(selement);
367
368 return result;
369 }
370
371
372 protected Element processGetFavouritesList(Element request)
373 {
374 Hashtable<String, Hashtable<String, Item>> docsMap = updateDocMap(request);
375
376 // Create a new (empty) result message
377 Document result_doc = XMLConverter.newDOM();
378 Element result = result_doc.createElement(GSXML.RESPONSE_ELEM);
379
380 Iterator<String> keys = docsMap.keySet().iterator();
381
382 while (keys.hasNext())
383 {
384 String collection = keys.next();
385 Hashtable items = docsMap.get(collection);
386 Iterator itemItr = items.values().iterator();
387
388 Element collectionNode = result_doc.createElement("favouriteList");
389 collectionNode.setAttribute("name", collection);
390 result.appendChild(collectionNode);
391
392 while (itemItr.hasNext())
393 {
394 Item item = (Item) itemItr.next();
395 collectionNode.appendChild(item.wrapIntoElement(result_doc));
396 }
397 }
398
399 return result;
400 }
401
402
403
404
405 public Element processSendFavouritesMail(Element request)
406 {
407 // Create a new (empty) result message
408 Document result_doc = XMLConverter.newDOM();
409 Element result = result_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 if (param_list == null)
415 {
416 logger.error("FavouriteBasket Error: SendMail request had no paramList.");
417 return result; // Return the empty result
418 }
419
420 HashMap<String, Serializable> params = GSXML.extractParams(param_list, false);
421
422 String to = (String) params.get(ADDRESS_PARAM);
423 String subject = (String) params.get(SUBJECT_PARAM);
424 String content = (String) params.get(CONTENT_PARAM);
425 String cc = (String) params.get(CC_PARAM);
426 String bcc = (String) params.get(BCC_PARAM);
427
428 String mailhost = GlobalProperties.getProperty("mail.smtp.host");
429 username = GlobalProperties.getProperty("mail.smtp.username");
430 password = GlobalProperties.getProperty("mail.smtp.password");
431 String from = GlobalProperties.getProperty("mail.from");
432 String port = GlobalProperties.getProperty("mail.smtp.port");
433 String mail_security = GlobalProperties.getProperty("mail.security");
434 String mailer = "msgsend";
435
436 try
437 {
438
439 Properties props = System.getProperties();
440
441 //Setup smtp host and from address
442 // XXX - could use Session.getTransport() and Transport.connect()
443 // XXX - assume we're using SMTP
444 if (mailhost != null && !mailhost.trim().equals(""))
445 {
446 props.put("mail.smtp.host", mailhost);
447 }
448 else
449 {
450 props.put("mail.smtp.host", "localhost");
451 }
452 if (from != null && !from.trim().equals(""))
453 {
454 props.put("mail.from", from);
455 }
456
457
458 if (port != null && !port.trim().equals("")) {
459 props.put("mail.smtp.port", port);
460 }
461
462 if (mail_security != null) {
463 mail_security = mail_security.trim();
464 if (mail_security.equals("ssl")) {
465 props.put("mail.smtp.ssl.enable", "true");
466 } else if (mail_security.equals("tls")) {
467 props.put("mail.smtp.starttls.enable", "true");
468 }
469 else if (mail_security.equals("")) {
470 logger.error("unknown security protocol "+mail_security +", should be ssl or tls");
471 }
472 }
473
474 // this doesn't seem to matter having this when
475 // username and password are empty
476 props.put("mail.smtp.auth", "true");
477
478 //setup username and password to the smtp server
479
480 if (username == null || username.trim().equals(""))
481 username = "";
482 if (password == null || password.trim().equals(""))
483 password = "";
484
485 Authenticator auth = new Authenticator()
486 {
487 protected PasswordAuthentication getPasswordAuthentication()
488 {
489 return new PasswordAuthentication(username, password);
490 }
491 };
492
493 Session session = Session.getInstance(props, auth);
494
495 Message msg = new MimeMessage(session);
496 msg.setFrom();
497 msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to, false));
498 if (cc != null)
499 {
500 msg.setRecipients(Message.RecipientType.CC, InternetAddress.parse(cc, false));
501 }
502 if (bcc != null)
503 {
504 msg.setRecipients(Message.RecipientType.BCC, InternetAddress.parse(bcc, false));
505 }
506 msg.setSubject(subject);
507 msg.setText(content.replaceAll("-------", "&"));
508 msg.setHeader("X-Mailer", mailer);
509 msg.setSentDate(new Date());
510
511 // send the thing off
512 Transport.send(msg);
513
514 logger.info("\nMail was sent successfully.");
515 result.appendChild(result_doc.createTextNode("Mail was sent successfully."));
516 }
517 catch (Exception e)
518 {
519 logger.error("Error sending mail!");
520 e.printStackTrace();
521 result.appendChild(result_doc.createTextNode(e.getMessage()));
522 }
523
524 return result;
525 }
526
527 protected class Item
528 {
529 public String collection;
530 public String docid;
531
532 public Item(String coll, String id)
533 {
534 this.collection = coll;
535 this.docid = id;
536 }
537
538 public boolean equals(Object o)
539 {
540 if (!(o instanceof Item))
541 {
542 return false;
543 }
544 Item item = (Item) o;
545 String id = collection + ":" + docid;
546 String idin = item.collection + ":" + item.docid;
547 return id.equals(idin);
548
549 }
550
551 public Element wrapIntoElement(Document doc)
552 {
553 Element itemElement = doc.createElement("documentNode");
554 itemElement.setAttribute("nodeID", docid);
555 itemElement.setAttribute("collection", collection);
556
557 return itemElement;
558 }
559 }
560
561 private class UserTimer extends Timer implements ActionListener
562 {
563 String id = "";
564
565 public UserTimer(int delay, String id)
566 {
567 super(delay, (ActionListener) null);
568 addActionListener(this);
569 this.id = id;
570 }
571
572 public void actionPerformed(ActionEvent e)
573 {
574 userMap.remove(id);
575 timerMap.remove(id);
576 stop();
577 }
578
579 }
580
581}
Note: See TracBrowser for help on using the repository browser.