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.plugin.std;
009:
010: import dtw.webmail.JwmaKernel;
011: import dtw.webmail.model.JwmaMailIdentity;
012: import dtw.webmail.model.JwmaMailIdentityImpl;
013: import dtw.webmail.model.JwmaPreferencesImpl;
014: import dtw.webmail.util.AbstractIdentifiable;
015: import dtw.webmail.util.AssociatedAbstractIdentifiable;
016: import dtw.webmail.util.Associator;
017: import net.wimpi.text.Processor;
018: import org.apache.log4j.Logger;
019: import org.exolab.castor.jdo.Database;
020: import org.exolab.castor.jdo.PersistenceException;
021: import org.exolab.castor.jdo.TimeStampable;
022:
023: import java.text.DateFormat;
024: import java.text.SimpleDateFormat;
025: import java.util.*;
026:
027: /**
028: * Class implementing a specialized <tt>JwmaPreferencesImpl</tt>
029: * for being persisted with the Castor Plugins.
030: *
031: * @author Dieter Wimberger
032: * @version 0.9.7 07/02/2003
033: */
034: public class CastorPreferences extends AbstractIdentifiable implements
035: JwmaPreferencesImpl, Associator, TimeStampable {
036:
037: //logging
038: private static Logger log = Logger
039: .getLogger(CastorPreferences.class);
040:
041: //instance attributes
042: private String m_UserIdentity;
043: private String m_Firstname = "firstname";
044: private String m_Lastname = "lastname";
045: private String m_LastLogin = "Never.";
046: private String m_Signature = "";
047: private String m_QuoteChar = ">";
048: private boolean m_AutoQuote = false;
049: private boolean m_AutoSign = true;
050:
051: //Folders
052: private String m_RootFolder = "";
053: private String m_Inbox = "Inbox";
054: private String m_SentMailArchive = "sent-mail";
055: private String m_ReadMailArchive = "read-mail";
056: private String m_TrashFolder = "trash";
057: private String m_DraftFolder = "draft";
058:
059: //Auto features
060: private boolean m_AutoArchiveSent = false;
061: private boolean m_AutoMoveRead = false;
062: private boolean m_AutoEmpty = false;
063:
064: private Processor m_MessageProcessor;
065: private Locale m_Locale = new Locale("en", "");
066:
067: private String m_ContactDatabase;
068: private SimpleDateFormat m_DateFormat = (SimpleDateFormat) DateFormat
069: .getDateInstance(DateFormat.SHORT, m_Locale);
070:
071: private String m_DefaultMailIdentity;
072: private List m_MailIdentities;
073: private boolean m_Expert = false;
074: private boolean m_DisplayingInlined;
075: private String m_Style = "";
076: public int m_MessageSortCriteria;
077:
078: private List m_RemovedAssociations;
079: private boolean m_Update = false;
080: private long m_Timestamp = TimeStampable.NO_TIMESTAMP;
081:
082: /**
083: * Constructs a JwmaPreferences instance.
084: */
085: public CastorPreferences() {
086: super ();
087: //ensure default processor to be set
088: setMessageProcessorName("");
089: m_MailIdentities = Collections
090: .synchronizedList(new ArrayList(5));
091: m_RemovedAssociations = Collections
092: .synchronizedList(new ArrayList(5));
093: }//constructor
094:
095: public String getUserIdentity() {
096: return m_UserIdentity;
097: }//getUserIdentity
098:
099: /**
100: * Sets the identity of this <tt>JwmaPreferences</tt>.
101: * <p>
102: * <em>Note</em>:<br>
103: * The format of the string has to be
104: * <tt><username>@<postofficehost></tt>.
105: * <br>
106: *
107: * @param id the identity to be set as <tt>String</tt>.
108: */
109: public void setUserIdentity(String userid) {
110: m_UserIdentity = userid;
111: }//setUserIdentity
112:
113: public String getFirstname() {
114: return m_Firstname;
115: }//getFirstname
116:
117: /**
118: * Sets the firstname of the owner of this
119: * <tt>JwmaPreferences</tt>.
120: *
121: * @param firstname the owner's firstname.
122: */
123: public void setFirstname(String firstname) {
124: m_Firstname = firstname;
125: }//setFirstname
126:
127: public String getLastname() {
128: return m_Lastname;
129: }//getLastname
130:
131: /**
132: * Sets the lastname of the owner of this
133: * <tt>JwmaPreferences</tt>.
134: *
135: * @param lastname the owner's lastname.
136: */
137: public void setLastname(String lastname) {
138: m_Lastname = lastname;
139: }//setLastname
140:
141: public String getLastLogin() {
142: return m_LastLogin;
143: }//getLastLogin
144:
145: /**
146: * Sets the the user's last login date and originating host.
147: *
148: * @param address the internet address of the last login.
149: */
150: public void setLastLogin(String lastlogin) {
151: m_LastLogin = lastlogin;
152: }//setLastLogin
153:
154: public String getQuoteChar() {
155: return m_QuoteChar;
156: }//getQuoteChars
157:
158: /**
159: * Sets the quoting character.
160: * <p>
161: * <i><b>Note</b>:
162: * only the first character is taken from the String.</i>
163: *
164: * @return the quote character as String.
165: */
166: public void setQuoteChar(String qc) {
167: m_QuoteChar = "" + qc.charAt(0);
168: }//setQuoteChars
169:
170: public boolean isAutoQuote() {
171: return m_AutoQuote;
172: }//isAutoQuote
173:
174: /**
175: * Sets the flag that controls wheter messages should be automatically
176: * quoted on reply.
177: *
178: * @param doquote true if messages being replied to should be automatically
179: * quoted, false otherwise.
180: */
181: public void setAutoQuote(boolean doquote) {
182: m_AutoQuote = doquote;
183: }//setAutoQuote
184:
185: public String getRootFolder() {
186: return m_RootFolder;
187: }//getRootFolder
188:
189: /**
190: * Sets the path of the mail root folder.
191: *
192: * @param path the path of root mail folder as
193: * <tt>String</tt>.
194: */
195: public void setRootFolder(String path) {
196: m_RootFolder = path;
197: }//setRootFolder
198:
199: public String getSentMailArchive() {
200: return m_SentMailArchive;
201: }//getSentMailArchive
202:
203: /**
204: * Sets the path of the sent-mail-archive.
205: *
206: * @param path the path of the sent-mail-archive as <tt>String</tt>.
207: */
208: public void setSentMailArchive(String path) {
209: m_SentMailArchive = path;
210: }//setSentMailArchive
211:
212: public String getReadMailArchive() {
213: return m_ReadMailArchive;
214: }//getReadMailArchive
215:
216: /**
217: * Sets the path of the read-mail-archive.
218: *
219: * @param path the path of the read-mail-archive as <tt>String</tt>.
220: */
221: public void setReadMailArchive(String path) {
222: m_ReadMailArchive = path;
223: }//setReadMailArchive
224:
225: public String getTrashFolder() {
226: return m_TrashFolder;
227: }//getTrashFolder
228:
229: /**
230: * Sets the path of the trashfolder.
231: *
232: * @param path the path of the trashfolder as <tt>String</tt>.
233: */
234: public void setTrashFolder(String path) {
235: m_TrashFolder = path;
236: }//setTrashFolder
237:
238: public String getDraftFolder() {
239: return m_DraftFolder;
240: }//getDraftFolder
241:
242: /**
243: * Sets the path of the draft folder.
244: *
245: * @param path the path of the draft folder as <tt>String</tt>.
246: */
247: public void setDraftFolder(String path) {
248: m_DraftFolder = path;
249: }//setDraftFolder
250:
251: /**
252: * Sets the flag that controls wheter messages should be automatically
253: * archived when sent.
254: *
255: * @param doarchive true if messages being sent should be automatically
256: * archived, false otherwise.
257: */
258: public void setAutoArchiveSent(boolean doarchive) {
259: m_AutoArchiveSent = doarchive;
260: }//setAutoArchiveSent
261:
262: public boolean isAutoArchiveSent() {
263: return m_AutoArchiveSent;
264: }//isAutoArchiveSent
265:
266: /**
267: * Sets the flag that controls wheter messages should be automatically
268: * moved when read.
269: *
270: * @param domoveread true if read messages should be automatically
271: * moved, false otherwise.
272: */
273: public void setAutoMoveRead(boolean domoveread) {
274: m_AutoMoveRead = domoveread;
275: }//setAutoMoveRead
276:
277: public boolean isAutoMoveRead() {
278: return m_AutoMoveRead;
279: }//isAutoMoveRead
280:
281: public boolean isAutoEmpty() {
282: return m_AutoEmpty;
283: }//isAutoEmptying
284:
285: /**
286: * Sets the flag that controls wheter messages should be automatically
287: * deleted from the trash on logout.
288: *
289: * @param b true if messages in trash should be automatically
290: * deleted on logout, false otherwise.
291: */
292: public void setAutoEmpty(boolean b) {
293: m_AutoEmpty = b;
294: }//setAutoEmpty
295:
296: public String getLanguage() {
297: return m_Locale.getLanguage();
298: }//getLanguage
299:
300: /**
301: * Sets the language of this <tt>JwmaPreferences</tt>.
302: *
303: * @param str the language locale as <tt>String</tt>.
304: */
305: public void setLanguage(String str) {
306: m_Locale = new Locale(str, "");
307: }//setLanguage
308:
309: public Locale getLocale() {
310: return m_Locale;
311: }//getLocale
312:
313: public void setLocale(Locale locale) {
314: m_Locale = locale;
315: m_DateFormat = (SimpleDateFormat) DateFormat.getDateInstance(
316: DateFormat.SHORT, m_Locale);
317: }//setLocale
318:
319: public String getContactDatabaseID() {
320: return m_ContactDatabase;
321: }//getContactDatabaseID
322:
323: public void setContactDatabaseID(String dbid) {
324: m_ContactDatabase = dbid;
325: }//setContactDatabaseID
326:
327: public void setMessageProcessorName(String name) {
328: //fetch the processor or pipe from the kernel
329: m_MessageProcessor = JwmaKernel.getReference()
330: .getMessageProcessor(name);
331: }//setMessageProcessorName
332:
333: public String getMessageProcessorName() {
334: if (m_MessageProcessor == null) {
335: return "";
336: } else {
337: return m_MessageProcessor.getName();
338: }
339: }//getMessageProcessorName
340:
341: public Processor getMessageProcessor() {
342: return m_MessageProcessor;
343: }//getMessageProcessor
344:
345: public void setMessageProcessor(Processor processor) {
346: if (processor != null) {
347: m_MessageProcessor = processor;
348: }
349: }//getMessageProcessor
350:
351: public DateFormat getDateFormat() {
352: return m_DateFormat;
353: }//getDateFormat
354:
355: public void setDateFormat(SimpleDateFormat dateformat) {
356: m_DateFormat = dateformat;
357: }//setDateFormat
358:
359: public String getDateFormatPattern() {
360: return m_DateFormat.toPattern();
361: }//getDateFormatPattern
362:
363: public void setDateFormatPattern(String pattern) {
364: try {
365: m_DateFormat.applyPattern(pattern);
366: } catch (Exception ex) {
367: //stick with the default
368: }
369: }//setDateFormatPattern
370:
371: public void setDefaultMailIdentity(String uid) {
372: m_DefaultMailIdentity = uid;
373: //log.debug("Set default mail identity="+uid);
374: }//setDefaultMailIdentity
375:
376: public String getDefaultMailIdentity() {
377:
378: //log.debug("Get default mail identity="+m_DefaultMailIdentity);
379: return m_DefaultMailIdentity;
380: }//getDefaultMailIdentity
381:
382: public JwmaMailIdentity getMailIdentity() {
383: return getMailIdentity(m_DefaultMailIdentity);
384: }//getMailIdentity
385:
386: public Collection getMailIdentityCollection() {
387: //log.debug("Getting mail identities.");
388: return m_MailIdentities;
389: }//getMailIdentityCollection
390:
391: public void setMailIdentityCollection(Collection collection) {
392: //log.debug("Setting mail identities.");
393: m_MailIdentities = Collections.synchronizedList(new ArrayList(
394: collection));
395: //ensure order
396: //Collections.sort(m_MailIdentities,SortingUtil.INDEX_INCREASING);
397: }//setMailIdentityCollection
398:
399: public List getMailIdentities() {
400: return m_MailIdentities;
401: }//getMailIdentities
402:
403: public JwmaMailIdentity[] listMailIdentities() {
404: JwmaMailIdentity[] addr = new JwmaMailIdentity[m_MailIdentities
405: .size()];
406: return (JwmaMailIdentity[]) m_MailIdentities.toArray(addr);
407: }//listMailIdentities
408:
409: public boolean existsMailIdentity(String uid) {
410: for (Iterator iter = m_MailIdentities.iterator(); iter
411: .hasNext();) {
412: CastorMailIdentity mid = (CastorMailIdentity) iter.next();
413: if (mid.equals(uid)) {
414: return true;
415: }
416: }
417: return false;
418: }//existsMailIdentity
419:
420: public JwmaMailIdentity getMailIdentity(String uid) {
421: for (Iterator iter = m_MailIdentities.iterator(); iter
422: .hasNext();) {
423: CastorMailIdentity mid = (CastorMailIdentity) iter.next();
424: if (mid.equals(uid)) {
425: return mid;
426: }
427: }
428: return getMailIdentity(m_DefaultMailIdentity);
429: }//getMailIdentity
430:
431: public void addMailIdentity(JwmaMailIdentity identity) {
432: m_MailIdentities.add(identity);
433: ((CastorMailIdentity) identity).setAssociatorUID(this .getUID());
434: }//addMailIdentity
435:
436: public void removeMailIdentity(String uid) {
437: for (Iterator iter = m_MailIdentities.iterator(); iter
438: .hasNext();) {
439: CastorMailIdentity mid = (CastorMailIdentity) iter.next();
440: if (mid.equals(uid)) {
441: iter.remove();
442: mid.resetAssociatorUID();
443: m_RemovedAssociations.add(mid);
444: }
445: }
446: }//removeMailIdentity
447:
448: public int getMailIdentityCount() {
449: return m_MailIdentities.size();
450: }//getMailIdentityCount
451:
452: public boolean isExpert() {
453: return m_Expert;
454: }//isExpert
455:
456: public void setExpert(boolean b) {
457: m_Expert = b;
458: }//setExpert
459:
460: public void setStyle(String style) {
461: m_Style = style;
462: }//setStyle
463:
464: public String getStyle() {
465: return m_Style;
466: }//getStyle
467:
468: public boolean isDisplayingInlined() {
469: return m_DisplayingInlined;
470: }//isDisplayingInlined
471:
472: public void setDisplayingInlined(boolean b) {
473: m_DisplayingInlined = b;
474: }//setDisplayingInlined
475:
476: public int getMessageSortCriteria() {
477: return m_MessageSortCriteria;
478: }//getMessageSortCriteria
479:
480: public void setMessageSortCriteria(int messageSortCriteria) {
481: m_MessageSortCriteria = messageSortCriteria;
482: }//setMessageSortCriteria
483:
484: public JwmaMailIdentityImpl createMailIdentity() {
485: return new CastorMailIdentity();
486: }//createMailIdentity
487:
488: /**
489: * Returns a clone of this preferences.
490: *
491: * @return the clone, or null if the cloning process failed.
492: */
493: public JwmaPreferencesImpl getClone() {
494: try {
495: CastorPreferences pref = (CastorPreferences) this .clone();
496: //assign it it's own unique identifier
497: pref.setUID("");
498: //clear the mail identities
499: m_MailIdentities.clear();
500: //add up a default mail identity
501: CastorMailIdentity mid = new CastorMailIdentity();
502: //with some sense making values
503: mid.setName("Default");
504: mid.setFrom(getUserIdentity());
505: pref.addMailIdentity(mid);
506: pref.setDefaultMailIdentity(mid.getUID());
507: //FIX: ensures a set message processor
508: pref
509: .setMessageProcessorName(pref
510: .getMessageProcessorName());
511: return pref;
512: } catch (Exception ex) {
513: return null;
514: }
515: }//getClone
516:
517: public List getRemovedAssociations() {
518: return m_RemovedAssociations;
519: }//getRemovedAssociations
520:
521: public void updatePreferences(Database db)
522: throws PersistenceException {
523: m_Update = true;
524: try {
525: persistPreferences(db);
526: } finally {
527: m_Update = false;
528: }
529: }//updateDatabase
530:
531: public void persistPreferences(Database db)
532: throws PersistenceException {
533:
534: //1. store this
535: storeObject(db, this );
536:
537: //3. iterate over identities, ensure order by indexing
538: int i = 0;
539: for (Iterator iter = m_MailIdentities.listIterator(); iter
540: .hasNext(); i++) {
541: //Object next=iter.next();
542: //((Indexable)next).setIndex(i);
543: storeObject(db, iter.next());
544: }
545: }//persistPreferences
546:
547: private void storeObject(Database db, Object o)
548: throws PersistenceException {
549: if (db == null || o == null) {
550: return;
551: } else if (db.isPersistent(o) || m_Update) {
552: log.debug(JwmaKernel.getReference().getLogMessage(
553: "jwma.plugin.castor.objupdate")
554: + o.toString());
555: if (o instanceof Associator) {
556: cleanupAssociations(db, ((Associator) o)
557: .getRemovedAssociations());
558: }
559: db.update(o);
560: } else {
561: log.debug(JwmaKernel.getReference().getLogMessage(
562: "jwma.plugin.castor.objcreate")
563: + o.toString());
564: db.create(o);
565: }
566: }//storeObject
567:
568: private void cleanupAssociations(Database db, List l)
569: throws PersistenceException {
570:
571: Object o = null;
572: for (Iterator iter = l.iterator(); iter.hasNext();) {
573: AssociatedAbstractIdentifiable assoc = (AssociatedAbstractIdentifiable) iter
574: .next();
575: //check if object has an association
576: if (!assoc.isAssociated()) {
577: log.debug(JwmaKernel.getReference().getLogMessage(
578: "jwma.plugin.castor.objremove")
579: + assoc.getClass() + " " + assoc.toString());
580: //needs to be loaded in this transaction :( hack but works
581: try {
582: o = db.load(assoc.getClass(), assoc.getUID());
583: } catch (PersistenceException pex) {
584: /*more hack, required to work*/
585: }
586: db.remove(o);
587: }
588: }
589: //and definately, remove the references
590: l.clear();
591: }//cleanupAssociations
592:
593: public long jdoGetTimeStamp() {
594: return m_Timestamp;
595: }//jdoGetTimeStamp
596:
597: public void jdoSetTimeStamp(long timeStamp) {
598: m_Timestamp = timeStamp;
599: }//jdoSetTimeStamp
600:
601: }//class JwmaPreferencesImpl
|