001: /***
002: * jwma Java WebMail
003: * Copyright (c) 2000-2003 jwma team
004: *
005: * jwma is free software; you can distribute and use this source
006: * under the terms of the BSD-style license received along with
007: * the distribution.
008: ***/package dtw.webmail;
009:
010: import java.io.*;
011: import java.util.*;
012: import java.text.*;
013:
014: import javax.servlet.*;
015: import javax.servlet.http.*;
016:
017: import org.apache.log4j.Logger;
018: import org.apache.log4j.NDC;
019:
020: import dtw.webmail.util.*;
021: import dtw.webmail.model.*;
022:
023: public class JwmaContactsController extends HttpServlet {
024:
025: //logging
026: private static Logger log = Logger
027: .getLogger(JwmaContactsController.class);
028:
029: /**
030: * Handles the incoming requests.
031: * <p>
032: * This implementation ensures authenticated session
033: * existence, retrieves the <tt>acton</tt>
034: * and <tt>todo</tt> parameters, and dispatches all
035: * valid actions to the target dispatchers.
036: * <p>
037: * Application related errors/exceptions are handled
038: * by forwarding the request to an error page, or the actual page
039: * in case of an inlined error.
040: *
041: * @param req a reference to the actual <tt>HttpServletRequest</tt>
042: * instance.
043: * @param res a reference to the actual <tt>HttpServletResponse</tt>
044: * instance.
045: *
046: * @throws ServletException if servlet related operations fail.
047: * @throws IOException if i/o operations fail.
048: */
049: public void service(HttpServletRequest req, HttpServletResponse res)
050: throws ServletException, IOException {
051: try {
052: NDC.push(req.getRemoteHost());
053: //1. Handle session
054: HttpSession websession = req.getSession(true);
055: Object o = websession.getValue("jwma.session");
056: Object c = websession.getValue("jwma.contacts");
057: String acton = req.getParameter("acton");
058: String dome = req.getParameter("todo");
059:
060: //2. update session information
061: JwmaSession session = ((o == null) ? new JwmaSession(
062: websession) : ((JwmaSession) o));
063:
064: session.setRequest(req);
065: session.setResponse(res);
066: session.setWebSession(websession);
067:
068: //redirect to login view if no valid websession
069: if (!session.isValidWebSession()) {
070: session.redirect(JwmaKernel.LOGIN_VIEW);
071: return;
072: }
073:
074: //3. prepare the contact database
075: JwmaContactsImpl ctdb = ((c == null) ? null
076: : ((JwmaContactsImpl) c));
077:
078: //dispatch actions
079: try {
080: if (acton == null || acton.equals("")) {
081: throw new JwmaException("request.target.missing");
082: } else if (dome == null || dome.equals("")) {
083: throw new JwmaException("request.action.missing");
084: } else {
085: //ensure authentication
086: session.ensureAuthenticated();
087: //session.ensurePostOfficeConnection();
088:
089: if (acton.equals("database")) {
090: doDispatchDatabaseActions(session, dome, ctdb);
091: } else if (acton.equals("group")) {
092: doDispatchGroupActions(session, dome, ctdb);
093: } else if (acton.equals("contact")) {
094: doDispatchContactActions(session, dome, ctdb);
095: } else {
096: throw new JwmaException(
097: "request.target.invalid");
098: }
099:
100: }
101: } catch (JwmaException ex) {
102: //1. if not authenticated forward to login page
103: if (!session.isAuthenticated()) {
104: session.redirect(JwmaKernel.LOGIN_VIEW);
105: } else {
106: String message = ex.getMessage();
107: //oneliner resolving of key to message in
108: message = JwmaKernel.getReference().getLogMessage(
109: "jwma.usererror")
110: + JwmaKernel.getReference()
111: .getErrorMessage(message);
112: //log exception with description and trace if inlined exception
113: //available, else with stacktrace.
114: if (ex.hasException()) {
115: log.error(message, ex.getException());
116: } else {
117: log.error(message, ex);
118: }
119: //store error
120: session.storeBean("jwma.error", ex);
121: //redirect last view with inlined error or
122: //to error page.
123: if (ex.isInlineError()) {
124: session.redirectToActual();
125: } else {
126: session.redirect(JwmaKernel.ERROR_VIEW);
127: }
128: }
129: }
130: } finally {
131: NDC.pop();
132: }
133: }//service
134:
135: /*** Dispatchers ***************************************************/
136:
137: /**
138: * Dispatches actions targeting a <tt>JwmaContactsImpl</tt>.
139: *
140: * @param session a <tt>JwmaSession</tt> instance.
141: * @param dome the task as <tt>String</tt>.
142: * @param ctdb a <tt>JwmaContactsImpl</tt> instance.
143: *
144: * @throws JwmaException if it fails to dispatch the request to a
145: * method (i.e. invalid request), or the action method fails
146: * to execute the task.
147: */
148: private void doDispatchDatabaseActions(JwmaSession session,
149: String dome, JwmaContactsImpl ctdb) throws JwmaException {
150:
151: if (dome.equals("display")) {
152: doDisplayDatabase(session, ctdb);
153: } else {
154: //ensure ctdb loaded
155: if (ctdb == null) {
156: throw new JwmaException("database.action.notloaded");
157: }
158:
159: if (dome.equals("store")) {
160: doStoreDatabase(session, ctdb);
161: } else if (dome.equals("export")) {
162: //check type
163: //do export in type
164: } else if (dome.equals("setfilter")) {
165: String filtertype = session
166: .getRequestParameter("filtertype");
167: if (filtertype == null || filtertype.length() == 0) {
168: throw new JwmaException(
169: "database.action.filtertypemissing");
170: }
171: String filter = session.getRequestParameter("filter");
172: if (filter == null || filter.length() == 0) {
173: throw new JwmaException(
174: "database.action.filtermissing");
175: }
176: doSetContactsFilter(session, ctdb, filtertype, filter);
177: } else {
178: throw new JwmaException("database.action.invalid");
179: }
180: }
181: }//doDispatchDatabaseActions
182:
183: /**
184: * Dispatches actions targeting a <tt>ContactGroup</tt>.
185: *
186: * @param session a <tt>JwmaSession</tt> instance.
187: * @param dome the task as <tt>String</tt>.
188: * @param ctdb a <tt>JwmaContactsImpl</tt> instance.
189: *
190: * @throws JwmaException if it fails to dispatch the request to a
191: * method (i.e. invalid request), or the action method fails
192: * to execute the task.
193: */
194: private void doDispatchGroupActions(JwmaSession session,
195: String dome, JwmaContactsImpl ctdb) throws JwmaException {
196: log.debug("Dispatching group action=" + dome);
197: String[] gids = session.getRequestParameters("group.id");
198:
199: if (dome.equals("display")) {
200: if (gids == null || gids[0] == null || gids[0].equals("")) {
201: throw new JwmaException("group.action.gidmissing");
202: } else {
203: doDisplayGroup(session, ctdb, gids[0]);
204: }
205: } else if (dome.equals("edit")) {
206: if (gids == null || gids[0] == null || gids[0].equals("")) {
207: throw new JwmaException("group.action.gidmissing");
208: } else {
209: doEditGroup(session, ctdb, gids[0]);
210: }
211: } else if (dome.equals("update")) {
212: if (gids == null || gids[0] == null || gids[0].equals("")) {
213: throw new JwmaException("group.action.gidmissing");
214: }
215: doUpdateGroup(session, ctdb, gids[0]);
216: } else if (dome.equals("create")) {
217: doCreateGroup(session, ctdb);
218: } else if (dome.equals("delete")) {
219: if (gids == null || gids.length == 0) {
220: throw new JwmaException("group.action.gidmissing");
221: }
222: doDeleteGroups(session, ctdb, gids);
223: } else if (dome.equals("removecontacts")) {
224: if (gids == null || gids[0] == null || gids[0].equals("")) {
225: throw new JwmaException("group.action.gidmissing");
226: }
227: String[] cids = session
228: .getRequestParameters("contact.member.id");
229: if (cids == null || cids.length == 0) {
230: throw new JwmaException("group.action.cidmissing");
231: }
232: doRemoveContacts(session, ctdb, gids[0], cids);
233: } else if (dome.equals("addcontacts")) {
234: if (gids == null || gids[0] == null || gids[0].equals("")) {
235: throw new JwmaException("group.action.gidmissing");
236: }
237: String[] cids = session
238: .getRequestParameters("contact.nomember.id");
239: if (cids == null || cids.length == 0) {
240: throw new JwmaException("group.action.cidmissing");
241: }
242: doAddContacts(session, ctdb, gids[0], cids);
243: } else {
244: throw new JwmaException("group.action.invalid");
245: }
246: }//doDispatchGroupActions
247:
248: /**
249: * Dispatches actions targeting a <tt>Contact</tt>.
250: *
251: * @param session a <tt>JwmaSession</tt> instance.
252: * @param dome the task as <tt>String</tt>.
253: * @param ctdb a <tt>JwmaContactsImpl</tt> instance.
254: *
255: * @throws JwmaException if it fails to dispatch the request to a
256: * method (i.e. invalid request), or the action method fails
257: * to execute the task.
258: */
259: private void doDispatchContactActions(JwmaSession session,
260: String dome, JwmaContactsImpl ctdb) throws JwmaException {
261:
262: if (dome.equals("create")) {
263: doUpdateContact(session, ctdb, null);
264: } else if (dome.equals("import")) {
265: Object ct = session.getWebSession().getValue(
266: "jwma.contacts.import");
267: if (ct == null) {
268: throw new JwmaException("contact.action.importmissing");
269: }
270: doImportContact(session, ctdb, (JwmaContactImpl) ct);
271: } else {
272: String[] cids = session.getRequestParameters("contact.id");
273: if (cids == null || cids[0] == null || cids[0].equals("")) {
274: throw new JwmaException("contact.action.idmissing");
275: }
276: if (dome.equals("display")) {
277: doDisplayContact(session, ctdb, cids[0]);
278: } else if (dome.equals("edit")) {
279: doEditContact(session, ctdb, cids[0]);
280: } else if (dome.equals("update")) {
281: doUpdateContact(session, ctdb, cids[0]);
282: } else if (dome.equals("delete")) {
283: doDeleteContacts(session, ctdb, cids);
284: } else {
285: throw new JwmaException("contact.action.invalid");
286: }
287: }
288: }//doDispatchContactActions
289:
290: /*** BEGIN database actions **********************************************************/
291:
292: /**
293: * Prepares the user's contact database and redirects him to the contacts
294: * view.
295: *
296: * @param session a <tt>JwmaSession</tt> instance.
297: * @param ctdb a <tt>JwmaContactsImpl</tt> instance.
298: *
299: * @throws JwmaException if it fails to execute properly.
300: */
301: private void doDisplayDatabase(JwmaSession session,
302: JwmaContactsImpl ctdb) throws JwmaException {
303:
304: session.redirect(JwmaKernel.CONTACTS_VIEW);
305: return;
306: }//doDisplayDatabase
307:
308: /**
309: * Stores the user's contact database and redirects him to the contacts
310: * view.
311: *
312: * @param session a <tt>JwmaSession</tt> instance.
313: * @param ctdb a <tt>JwmaContactsImpl</tt> instance.
314: *
315: * @throws JwmaException if it fails to execute properly.
316: */
317: private void doStoreDatabase(JwmaSession session,
318: JwmaContactsImpl ctdb) throws JwmaException {
319:
320: JwmaKernel.getReference().getContactManagementPlugin()
321: .saveContacts(ctdb);
322:
323: session.redirect(JwmaKernel.CONTACTS_VIEW);
324: return;
325: }//doStoreDatabase
326:
327: private void doSetContactsFilter(JwmaSession session,
328: JwmaContactsImpl ctdb, String filtertype, String filter)
329: throws JwmaException {
330:
331: //log.debug("Setting filter type="+filtertype+" value="+filter);
332: if (filtertype.equals("alphabetic")) {
333: if (filter.equals("none")) {
334: ctdb.setContactFilter(null);
335: //log.debug("Alphabetic filter reset.");
336: } else {
337: ctdb.setContactFilter(new LastnameStartsWithFilter(
338: filter));
339: //log.debug("Alphabetic filter set to "+filter);
340: }
341:
342: } else if (filtertype.equals("category")) {
343: if (filter.equals("none")) {
344: ctdb.setCategoryFilter("");
345: } else {
346: ctdb.setCategoryFilter(filter);
347: }
348: } else {
349: throw new JwmaException("database.setfilter.uknowntype");
350: }
351: session.redirectToActual();
352: //redirect(JwmaKernel.CONTACTS_VIEW);
353: return;
354:
355: }//doSetContactsFilter
356:
357: /*** END database actions ************************************************************/
358:
359: /*** BEGIN group actions ************************************************************/
360:
361: /**
362: * Displays a contact group entry from the contact database.
363: *
364: * @param session a <tt>JwmaSession</tt> instance.
365: * @param ctdb a <tt>JwmaContactsImpl</tt> instance.
366: * @param cid a contact identifier as <tt>String</tt>.
367: *
368: * @throws JwmaException if it fails to execute properly.
369: */
370: private void doDisplayGroup(JwmaSession session,
371: JwmaContactsImpl ctdb, String cid) throws JwmaException {
372:
373: if (ctdb.containsContactGroup(cid)) {
374: session.storeBean("jwma.contacts.group", ctdb
375: .getContactGroup(cid));
376: } else {
377: throw new JwmaException("group.action.invalidgroup", true);
378: }
379: session.redirect(JwmaKernel.CONTACTGROUP_VIEW);
380: return;
381: }//doDisplayGroup
382:
383: /**
384: * Displays a contact entry from the contact database for editing.
385: *
386: * @param session a <tt>JwmaSession</tt> instance.
387: * @param ctdb a <tt>JwmaContactsImpl</tt> instance.
388: * @param cid a contact identifier as <tt>String</tt>.
389: *
390: * @throws JwmaException if it fails to execute properly.
391: */
392: private void doEditGroup(JwmaSession session,
393: JwmaContactsImpl ctdb, String cid) throws JwmaException {
394:
395: if (ctdb.containsContactGroup(cid)) {
396: session.storeBean("jwma.contacts.group", ctdb
397: .getContactGroup(cid));
398: } else {
399: throw new JwmaException("group.action.invalidgroup", true);
400: }
401: session.redirect(JwmaKernel.CONTACTGROUP_EDIT_VIEW);
402: return;
403: }//doEditGroup
404:
405: /**
406: * Updates a group entry in the contact database.
407: *
408: * @param session a <tt>JwmaSession</tt> instance.
409: * @param ctdb a <tt>JwmaContactsImpl</tt> instance.
410: *
411: * @throws JwmaException if it fails to execute properly.
412: */
413: private void doUpdateGroup(JwmaSession session,
414: JwmaContactsImpl ctdb, String id) throws JwmaException {
415:
416: //check if id exists
417: if (ctdb.containsContactGroup(id)) {
418: String name = session.getRequestParameter("group.name");
419: String comments = session
420: .getRequestParameter("group.comments");
421:
422: //check if groupname already exists
423: if (ctdb.containsContactGroupName(name)
424: && !name.equals(ctdb.getContactGroup(id).getName())) {
425: throw new JwmaException("group.name.duplicate");
426: } else {
427: JwmaContactGroupImpl group = (JwmaContactGroupImpl) ctdb
428: .getContactGroup(id);
429: //set name if not null, or empty string
430: if (name != null && !name.equals("")
431: && !name.equals(group.getName())) {
432: group.setName(name);
433: }
434: group.setComments(comments);
435: }
436: } else {
437: throw new JwmaException("group.action.invalidgroup", true);
438: }
439: session.redirect(JwmaKernel.CONTACTGROUP_VIEW);
440: return;
441: }//doUpdateGroup
442:
443: /**
444: * Creates a group entry in the contact database.
445: *
446: * @param session a <tt>JwmaSession</tt> instance.
447: * @param ctdb a <tt>JwmaContactsImpl</tt> instance.
448: *
449: * @throws JwmaException if it fails to execute properly.
450: */
451: private void doCreateGroup(JwmaSession session,
452: JwmaContactsImpl ctdb) throws JwmaException {
453:
454: //1. retrieve name and note
455: String name = session.getRequestParameter("group.name");
456: String comments = session.getRequestParameter("group.comments");
457: log.debug("Creating group name=" + name + " " + " comments="
458: + comments);
459: //check if groupname already exists
460: if (ctdb.containsContactGroupName(name)) {
461: throw new JwmaException("group.name.duplicate", true);
462: } else {
463: //check name if not null, or empty string
464: if (name != null && !name.equals("")) {
465: //create a new group in the users database
466: JwmaContactGroupImpl group = ctdb
467: .createContactGroup(name);
468: group.setComments(comments);
469: ctdb.addContactGroup(group);
470: session.storeBean("jwma.contacts.group", group);
471: }
472: }
473: JwmaKernel.getReference().getContactManagementPlugin()
474: .saveContacts(ctdb);
475:
476: session.redirect(JwmaKernel.CONTACTGROUP_EDIT_VIEW);
477: return;
478: }//doCreateGroup
479:
480: /**
481: * Deletes group entries from the contact database.
482: *
483: * @param session a <tt>JwmaSession</tt> instance.
484: * @param ctdb a <tt>JwmaContactsImpl</tt> instance.
485: *
486: * @throws JwmaException if it fails to execute properly.
487: */
488: private void doDeleteGroups(JwmaSession session,
489: JwmaContactsImpl ctdb, String[] gids) throws JwmaException {
490:
491: log.debug("Deleting groups...");
492: for (int i = 0; i < gids.length; i++) {
493: JwmaContactGroupImpl group = (JwmaContactGroupImpl) ctdb
494: .getContactGroup(gids[i]);
495: if (group != null) {
496: log.debug("Deleting " + gids[i]);
497: ctdb.removeContactGroup(group);
498: } else {
499: throw new JwmaException("group.action.invalidgroup",
500: true);
501: }
502: }
503: JwmaKernel.getReference().getContactManagementPlugin()
504: .saveContacts(ctdb);
505: session.redirect(JwmaKernel.CONTACTS_VIEW);
506: return;
507: }//doDeleteGroups
508:
509: /**
510: * Adds contacts to a group entry in the contact database.
511: *
512: * @param session a <tt>JwmaSession</tt> instance.
513: * @param ctdb a <tt>JwmaContactsImpl</tt> instance.
514: *
515: * @throws JwmaException if it fails to execute properly.
516: */
517: private void doAddContacts(JwmaSession session,
518: JwmaContactsImpl ctdb, String gid, String[] cids)
519: throws JwmaException {
520:
521: //check if id exists
522: if (ctdb.containsContactGroup(gid)) {
523: //retrieve group
524: JwmaContactGroupImpl group = (JwmaContactGroupImpl) ctdb
525: .getContactGroup(gid);
526: //add contacts
527: for (int i = 0; i < cids.length; i++) {
528: log.debug("Trying to add #" + i + " is in database="
529: + ctdb.containsContact(cids[i]) + " with id="
530: + ctdb.getContact(cids[i]));
531: if (ctdb.containsContact(cids[i])) {
532: group.addContact((JwmaContactImpl) ctdb
533: .getContact(cids[i]));
534: } else {
535: throw new JwmaException(
536: "contact.action.invalidcontact", true);
537: }
538: }
539: } else {
540: throw new JwmaException("group.action.invalidgroup", true);
541: }
542:
543: session.redirect(JwmaKernel.CONTACTGROUP_EDIT_VIEW);
544: return;
545: }//doAddContacts
546:
547: /**
548: * Removes contacts from a group entry in the contact database.
549: *
550: * @param session a <tt>JwmaSession</tt> instance.
551: * @param ctdb a <tt>JwmaContactsImpl</tt> instance.
552: *
553: * @throws JwmaException if it fails to execute properly.
554: */
555: private void doRemoveContacts(JwmaSession session,
556: JwmaContactsImpl ctdb, String gid, String[] cids)
557: throws JwmaException {
558:
559: //check if id exists
560: if (ctdb.containsContactGroup(gid)) {
561: //retrieve group
562: JwmaContactGroupImpl group = (JwmaContactGroupImpl) ctdb
563: .getContactGroup(gid);
564: //add contacts
565: for (int i = 0; i < cids.length; i++) {
566: log.debug("Trying to remove #" + i + " is in database="
567: + ctdb.containsContact(cids[i]) + " with id="
568: + cids[i]);
569: if (ctdb.containsContact(cids[i])
570: && group.containsContact(cids[i])) {
571: group.removeContact((JwmaContactImpl) ctdb
572: .getContact(cids[i]));
573: } else {
574: throw new JwmaException(
575: "contact.action.invalidcontact");
576: }
577: }
578: } else {
579: throw new JwmaException("group.action.invalidgroup", true);
580: }
581: session.redirect(JwmaKernel.CONTACTGROUP_EDIT_VIEW);
582: return;
583: }//doDeleteContacts
584:
585: /*** END group actions ************************************************************/
586:
587: /*** BEGIN contact actions **********************************************************/
588:
589: /**
590: * Displays a contact entry from the contact database.
591: *
592: * @param session a <tt>JwmaSession</tt> instance.
593: * @param ctdb a <tt>JwmaContactsImpl</tt> instance.
594: * @param cid a contact identifier as <tt>String</tt>.
595: *
596: * @throws JwmaException if it fails to execute properly.
597: */
598: private void doDisplayContact(JwmaSession session,
599: JwmaContactsImpl ctdb, String cid) throws JwmaException {
600:
601: if (ctdb.containsContact(cid)) {
602: session.storeBean("jwma.contacts.contact", ctdb
603: .getContact(cid));
604: } else {
605: throw new JwmaException("contact.action.invalidcontact",
606: true);
607: }
608: session.redirect(JwmaKernel.CONTACT_VIEW);
609: return;
610: }//doDisplayContact
611:
612: /**
613: * Displays a contact entry from the contact database for editing.
614: *
615: * @param session a <tt>JwmaSession</tt> instance.
616: * @param ctdb a <tt>JwmaContactsImpl</tt> instance.
617: * @param cid a contact identifier as <tt>String</tt>.
618: *
619: * @throws JwmaException if it fails to execute properly.
620: */
621: private void doEditContact(JwmaSession session,
622: JwmaContactsImpl ctdb, String cid) throws JwmaException {
623:
624: if (ctdb.containsContact(cid)) {
625: session.storeBean("jwma.contacts.contact", ctdb
626: .getContact(cid));
627: } else {
628: throw new JwmaException("contact.action.invalidcontact",
629: true);
630: }
631: session.redirect(JwmaKernel.CONTACT_EDIT_VIEW);
632: return;
633: }//doEditContact
634:
635: /**
636: * Updates or creates a contact entry in the contact database.
637: *
638: * @param session a <tt>JwmaSession</tt> instance.
639: * @param ctdb a <tt>JwmaContactsImpl</tt> instance.
640: * @param cid a contact identifier as <tt>String</tt>.
641: *
642: * @throws JwmaException if it fails to execute properly.
643: */
644: private void doUpdateContact(JwmaSession session,
645: JwmaContactsImpl ctdb, String cid) throws JwmaException {
646:
647: JwmaContactImpl ct = null;
648: boolean create = false;
649:
650: if (cid == null) {
651: ct = ctdb.createContact();
652: log.debug("Created new contact=" + ct.getUID());
653: create = true;
654: } else {
655: if (ctdb.containsContact(cid)) {
656: ct = (JwmaContactImpl) ctdb.getContact(cid);
657: } else {
658: throw new JwmaException("contact.action.invalidcontact");
659: }
660: }
661:
662: String category = session.getRequestParameter("category");
663: String newcategory = session.getRequestParameter("_category");
664: if (category != null && category.length() > 0
665: && newcategory != null
666: && !ctdb.existsContactCategory(category)
667: && newcategory.length() > 0) {
668: category = newcategory;
669: ctdb.addContactCategory(newcategory);
670: }
671:
672: String firstname = session.getRequestParameter("firstname");
673: String lastname = session.getRequestParameter("lastname");
674: String middlename = session.getRequestParameter("middlename");
675: String nickname = session.getRequestParameter("nickname");
676:
677: String company = session.getRequestParameter("company");
678: String title = session.getRequestParameter("title");
679: String role = session.getRequestParameter("role");
680:
681: //phone numbers
682: String homenum = session.getRequestParameter("phone.home");
683: String worknum = session.getRequestParameter("phone.work");
684: String pagernum = session.getRequestParameter("phone.pager");
685: String faxnum = session.getRequestParameter("phone.fax");
686: String mobilenum = session.getRequestParameter("phone.mobile");
687:
688: //primary location
689: boolean primlocation = new Boolean(session
690: .getRequestParameter("primary.location"))
691: .booleanValue();
692:
693: //work
694: String workstreet = session.getRequestParameter("work.street");
695: String workcity = session.getRequestParameter("work.city");
696: String workregion = session.getRequestParameter("work.region");
697: String workcountry = session
698: .getRequestParameter("work.country");
699: String workzip = session.getRequestParameter("work.zip");
700:
701: //home
702: String homestreet = session.getRequestParameter("home.street");
703: String homecity = session.getRequestParameter("home.city");
704: String homeregion = session.getRequestParameter("home.region");
705: String homecountry = session
706: .getRequestParameter("home.country");
707: String homezip = session.getRequestParameter("home.zip");
708:
709: //internet
710: String email = session.getRequestParameter("email.primary");
711: String altemail = session
712: .getRequestParameter("email.alternate");
713: String url = session.getRequestParameter("personal.url");
714: String compurl = session.getRequestParameter("company.url");
715: //misc
716: String birthday = session.getRequestParameter("birthdate");
717: String comments = session.getRequestParameter("comments");
718: boolean frequent = new Boolean(session
719: .getRequestParameter("frequent")).booleanValue();
720:
721: ct.setCategory(category);
722: ct.setFirstname(firstname);
723: ct.setLastname(lastname);
724: ct.setMiddlename(middlename);
725: if (nickname != null && nickname.length() > 0) {
726: log.debug("after testing nick");
727: //Check if already existing nickname:
728: JwmaContact ct_n = ctdb.getContactByNickname(nickname);
729: if (ct_n != null && !ct_n.getUID().equals(ct.getUID())) {
730: log.debug("contact=" + ct.getUID() + " nickct="
731: + ct_n.getUID());
732: throw new JwmaException(
733: "contact.update.duplicatenickname", true);
734: } else {
735: ct.setNickname(nickname);
736: }
737: } else {
738: ct.setNickname("");
739: }
740:
741: ct.setCompany(company);
742: ct.setTitle(title);
743: ct.setRole(role);
744: ct.setCompanyURL(compurl);
745: ct.setHomePhoneNumber(homenum);
746: ct.setWorkPhoneNumber(worknum);
747: ct.setPagerNumber(pagernum);
748: ct.setFaxNumber(faxnum);
749: ct.setMobileNumber(mobilenum);
750: ct.setPrimarilyWorkContact(primlocation);
751: ct.setWorkStreet(workstreet);
752: ct.setWorkCity(workcity);
753: ct.setWorkRegion(workregion);
754: ct.setWorkCountry(workcountry);
755: ct.setWorkZIP(workzip);
756: ct.setHomeStreet(homestreet);
757: ct.setHomeCity(homecity);
758: ct.setHomeRegion(homeregion);
759: ct.setHomeCountry(homecountry);
760: ct.setHomeZIP(homezip);
761: ct.setEmail(email);
762: ct.setAlternateEmail(altemail);
763: ct.setURL(url);
764: if (!ct.isFrequentRecipient() && frequent) {
765: ctdb.addFrequentRecipient(ct);
766: }
767: if (ct.isFrequentRecipient() && !frequent) {
768: ctdb.removeFrequentRecipient(ct);
769: }
770: ct.setFrequentRecipient(frequent);
771: ct.setComments(comments);
772:
773: try {
774: if (birthday != null && birthday.length() > 0) {
775: ct.setBirthDate(session.getPreferences()
776: .getDateFormat().parse(birthday));
777: }
778: } catch (Exception ex) {
779: //ignore now throw new JwmaException("");
780: log.error("doUpdateContact()", ex);
781: }
782: if (create) {
783: ctdb.addContact(ct);
784: }
785: saveContactDatabase(ctdb);
786: session.storeBean("jwma.contacts.contact", ct);
787:
788: session.redirect(JwmaKernel.CONTACT_EDIT_VIEW);
789: return;
790: }//doUpdateContact
791:
792: /**
793: * Deletes contact entries from the contact database.
794: *
795: * @param session a <tt>JwmaSession</tt> instance.
796: * @param ctdb a <tt>JwmaContactsImpl</tt> instance.
797: * @param cid a contact identifier as <tt>String</tt>.
798: *
799: * @throws JwmaException if it fails to execute properly.
800: */
801: private void doDeleteContacts(JwmaSession session,
802: JwmaContactsImpl ctdb, String[] cids) throws JwmaException {
803:
804: for (int i = 0; i < cids.length; i++) {
805: if (ctdb.containsContact(cids[i])) {
806: ctdb.removeContact((JwmaContactImpl) ctdb
807: .getContact(cids[i]));
808: } else {
809: throw new JwmaException(
810: "contact.action.invalidcontact", true);
811: }
812: }
813:
814: session.redirect(JwmaKernel.CONTACTS_VIEW);
815: return;
816: }//doDeleteContacts
817:
818: /**
819: * Imports a contact buffered from a message.
820: *
821: * @param session a <tt>JwmaSession</tt> instance.
822: * @param ctdb a <tt>JwmaContactsImpl</tt> instance.
823: * @param ct a contact as <tt>JwmaContactImpl</tt>.
824: *
825: * @throws JwmaException if it fails to execute properly.
826: */
827: private void doImportContact(JwmaSession session,
828: JwmaContactsImpl ctdb, JwmaContactImpl ct)
829: throws JwmaException {
830: if (!ctdb.containsContact(ct.getUID())) {
831: ctdb.addContact(ct);
832: } else {
833: throw new JwmaException("contact.action.invalidcontact",
834: true);
835: }
836:
837: session.storeBean("jwma.contacts.contact", ct);
838:
839: session.redirect(JwmaKernel.CONTACT_EDIT_VIEW);
840: return;
841: }//doDeleteContacts
842:
843: /*** END contact actions ************************************************************/
844:
845: private final void saveContactDatabase(JwmaContactsImpl ctdb)
846: throws JwmaException {
847:
848: JwmaKernel.getReference().getContactManagementPlugin()
849: .saveContacts(ctdb);
850: }//saveContactDatabase
851:
852: }//class JwmaContactsController
|