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.model;
009:
010: import java.util.*;
011: import javax.mail.*;
012: import javax.mail.internet.*;
013:
014: import org.apache.log4j.Logger;
015:
016: import dtw.webmail.util.EntityHandler;
017:
018: //import dtw.webmail.JwmaKernel;
019:
020: /**
021: * Class implementing the JwmaMessageInfo model.
022: * It is designed to wrap the minimum information of
023: * a mail message necessary to be listed in a list view.
024: *
025: * @author Dieter Wimberger
026: * @version 0.9.7 07/02/2003
027: */
028: public class JwmaMessageInfoImpl implements JwmaMessageInfo {
029:
030: //logging
031: private static Logger log = Logger
032: .getLogger(JwmaMessageInfoImpl.class);
033:
034: //instance attributes
035: private Integer m_Number;
036: private boolean m_Read;
037: private boolean m_Answered;
038: private boolean m_Recent;
039: private boolean m_Deleted;
040: private boolean m_Draft;
041: private boolean m_Received;
042: private boolean m_Singlepart;
043: private Date m_ReceivedDate;
044: private Date m_SentDate;
045: private String m_From;
046: private String m_To;
047: private String m_Subject;
048: private int m_Size;
049:
050: /**
051: * Constructs a new <tt>JwmaMessageInfoImpl</tt>.
052: */
053: private JwmaMessageInfoImpl(int number) {
054: setMessageNumber(number);
055: }//constructor
056:
057: public int getMessageNumber() {
058: return m_Number.intValue();
059: }//getMessageNumber
060:
061: /**
062: * Returns the message's number as <tt>Integer</tt>.
063: *
064: * This method is only used internally, and supposed
065: * to save a lot of resources, due to the fact that the
066: * standard collection sort <tt>Comparator</tt> does not
067: * need to create the wrapper instances on the fly for every
068: * comparison.
069: *
070: * @returns the message's number as <tt>Integer</tt>.
071: *
072: * @see dtw.webmail.util.MessageSortingUtil#NUMBER_NUMERICAL
073: * @see dtw.webmail.util.MessageSortingUtil#NUMBER_REVERSE_NUMERICAL
074: */
075: public Integer getNumberForSort() {
076: return m_Number;
077: }//getNumberForSort
078:
079: /**
080: * Sets the message number of this MessageInfo.
081: * This method is public to allow caching in the <tt>JwmaMessageInfoListImpl</tt>.
082: * The number should reflect the number of the wrapped <tt>javax.mail.Message</tt>
083: * instance.
084: *
085: * @param num the number as <tt>int</tt>.
086: *
087: * @see dtw.webmail.model.JwmaMessageInfoListImpl#renumber()
088: */
089: public void setMessageNumber(int num) {
090: m_Number = new Integer(num);
091: }//setMessageNumber
092:
093: public boolean isRead() {
094: return m_Read;
095: }//isRead
096:
097: /**
098: * Sets the read flag of this MessageInfo.
099: * It flags if the wrapped message was already read.
100: *
101: * @param b true if read, false otherwise.
102: */
103: private void setRead(boolean b) {
104: m_Read = b;
105: }//setRead
106:
107: public boolean isDraft() {
108: return m_Draft;
109: }//isDraft
110:
111: /**
112: * Sets the draft flag of this MessageInfo.
113: * It flags if the wrapped message is a draft.
114: *
115: * @param b true if draft, false otherwise.
116: */
117: private void setDraft(boolean b) {
118: m_Draft = b;
119: }//setDraft
120:
121: public boolean isAnswered() {
122: return m_Answered;
123: }//isAnswered
124:
125: /**
126: * Sets the answered flag of this MessageInfo.
127: * It flags if the wrapped message was answered.
128: *
129: * @param b true if answered, false otherwise.
130: */
131: private void setAnswered(boolean b) {
132: m_Answered = b;
133: }//setAnswered
134:
135: public boolean isDeleted() {
136: return m_Deleted;
137: }//isDeleted
138:
139: /**
140: * Sets the deleted flag of this MessageInfo.
141: * It flags if the wrapped message was deleted.
142: *
143: * @param b true if deleted, false otherwise.
144: */
145: private void setDeleted(boolean b) {
146: m_Deleted = b;
147: }//setDeleted
148:
149: public boolean isNew() {
150: return m_Recent;
151: }//isNew
152:
153: /**
154: * Sets the new flag of this MessageInfo.
155: * Flags if the wrapped message is new.
156: *
157: * @param b true if new, false otherwise.
158: */
159: private void setNew(boolean b) {
160: m_Recent = b;
161: }//setNew
162:
163: public boolean isReceived() {
164: return m_Received;
165: }//isReceived
166:
167: /**
168: * Sets the received flag of this MessageInfo.
169: * Flags if the wrapped message was received.
170: *
171: * @param true if received, false otherwise.
172: */
173: private void setReceived(boolean b) {
174: m_Received = b;
175: }//setReceived
176:
177: public boolean isSent() {
178: return !m_Received;
179: }//isSent
180:
181: public Date getDate() {
182: if (isReceived() && m_ReceivedDate != null) {
183: return m_ReceivedDate;
184: } else if (isSent() && m_SentDate != null) {
185: return m_SentDate;
186: } else {
187: return new Date();
188: }
189: }//getDate
190:
191: public Date getReceivedDate() {
192: return m_ReceivedDate;
193: }//getReceivedDate
194:
195: /**
196: * Sets the received date of this MessageInfo.
197: *
198: * @param d the date when the wrapped message was received.
199: */
200: private void setReceivedDate(Date d) {
201: m_ReceivedDate = d;
202: }//setReceivedDate
203:
204: public Date getSentDate() {
205: return m_SentDate;
206: }//getSentDate
207:
208: /**
209: * Sets the sent date of this MessageInfo.
210: *
211: * @param d the date when the wrapped message was sent.
212: */
213: private void setSentDate(Date d) {
214: m_SentDate = d;
215: }//setSentDate
216:
217: public String getWho() {
218: if (isReceived()) {
219: return getFrom();
220: } else {
221: return "<i>" + getTo() + "</i>";
222: }
223: }//getWho
224:
225: public String getFrom() {
226: return m_From;
227: }//getFrom
228:
229: /**
230: * Sets the author's address of this MessageInfo.
231: *
232: * @param from the address of the author as <tt>String</tt>.
233: */
234: private void setFrom(String from) {
235: m_From = from;
236: }//setFrom
237:
238: public String getTo() {
239: return m_To;
240: }//getTo
241:
242: /**
243: * Sets the receiver's address(es) of this MessageInfo.
244: *
245: * @param from the address(es) of the receiver(s) as <tt>String</tt>.
246: */
247: private void setTo(String to) {
248: m_To = to;
249: }//setTo
250:
251: public String getSubject() {
252: return m_Subject;
253: }//getSubject
254:
255: /**
256: * Sets the subject of this MessageInfo.
257: * Note that the subject will be set to an empty string, if the
258: * given String is null, or if it cannot be decoded.
259: *
260: * @param from the address of the author as <tt>String</tt>.
261: */
262: private void setSubject(String subject) {
263: if (subject == null) {
264: m_Subject = "";
265: } else {
266: m_Subject = subject;
267: }
268: }//setSubject
269:
270: public boolean isSinglepart() {
271: return m_Singlepart;
272: }//isSinglepart
273:
274: public boolean isMultipart() {
275: return !m_Singlepart;
276: }//isMultipart
277:
278: /**
279: * Sets the singlepart flag of this MessageInfo.
280: * It flags if the message contains attachments.
281: *
282: * @param b true if singlepart, false otherwise.
283: */
284: private void setSinglepart(boolean b) {
285: m_Singlepart = b;
286: }//setSinglepart
287:
288: public int getSize() {
289: return m_Size;
290: }//getSize
291:
292: /**
293: * Sets the message size for this MessageInfo.
294: *
295: * @param bytes the size of the message in bytes.
296: */
297: private void setSize(int bytes) {
298: m_Size = bytes;
299: }//setSize
300:
301: /**
302: * Method that prepares the given <tt>String</tt>
303: * by decoding it through the <tt>MimeUtility</tt>
304: * and encoding it through the <tt>EntitiyHandler</tt>.
305: */
306: private static String prepareString(String str) throws Exception {
307:
308: if (str == null) {
309: return "";
310: } else {
311: return (EntityHandler.encode(MimeUtility.decodeText(str)));
312: }
313: }//prepareString
314:
315: /**
316: * Factoy method that creates a new <tt>JwmaMessageInfoImpl</tt> instance.
317: * The passed in message should have been loaded with a slim profile. Values are
318: * extracted and set in the newly created instance.
319: *
320: * @param msg the message to be wrapped as <tt>javax.mail.Message</tt>.
321: */
322: public static JwmaMessageInfoImpl createJwmaMessageInfoImpl(
323: Message msg) throws JwmaException {
324:
325: JwmaMessageInfoImpl messageinfo = null;
326:
327: try {
328: //create instance with number
329: messageinfo = new JwmaMessageInfoImpl(msg
330: .getMessageNumber());
331:
332: //set flags
333: messageinfo.setNew(msg.isSet(Flags.Flag.RECENT));
334: messageinfo.setRead(msg.isSet(Flags.Flag.SEEN));
335: messageinfo.setAnswered(msg.isSet(Flags.Flag.ANSWERED));
336: messageinfo.setDeleted(msg.isSet(Flags.Flag.DELETED));
337: messageinfo.setDraft(msg.isSet(Flags.Flag.DRAFT));
338:
339: //determine if received, will have a header named Received
340: messageinfo
341: .setReceived((msg.getHeader("Received") != null));
342:
343: //senders and receivers
344: messageinfo.setFrom(prepareString(InternetAddress
345: .toString(msg.getFrom())));
346:
347: messageinfo.setTo(prepareString(InternetAddress
348: .toString(msg
349: .getRecipients(Message.RecipientType.TO))));
350:
351: //Dates
352: messageinfo.setReceivedDate(msg.getReceivedDate());
353: messageinfo.setSentDate(msg.getSentDate());
354:
355: //subject
356: messageinfo.setSubject(prepareString(msg.getSubject()));
357:
358: //size
359: messageinfo.setSize(msg.getSize());
360:
361: //attachments or none
362: messageinfo.setSinglepart(!msg.isMimeType("multipart/*"));
363:
364: } catch (Exception ex) {
365: throw new JwmaException("jwma.messageinfo.failedcreation");
366: }
367: return messageinfo;
368: }//createJwmaMessageInfoImpl
369:
370: }//class JwmaMessageInfoImpl
|