1 /*
2 * (C) 2002 David Carr david@carr.name
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18
19 package net.sourceforge.mflow.impl;
20
21 import java.awt.datatransfer.DataFlavor;
22 import java.awt.datatransfer.UnsupportedFlavorException;
23 import java.io.IOException;
24 import java.io.Reader;
25 import java.util.ArrayList;
26 import java.util.Arrays;
27 import java.util.Date;
28 import java.util.List;
29 import java.util.logging.Logger;
30
31 import net.sourceforge.mflow.api.Contact;
32 import net.sourceforge.mflow.api.ContactMethod;
33 import net.sourceforge.mflow.api.Msg;
34 import net.sourceforge.mflow.api.ContentHandler;
35 import net.sourceforge.mflow.util.IOUtil;
36
37 /***
38 * This class models a generic message. Msg contains a set of attributes and a "content".
39 *
40 * @author <a href="mailto:david@carr.name">David Carr</a>
41 * @version $Revision: 1.4 $
42 */
43 public class MsgImpl implements Msg {
44 /***
45 * Stores the ContactMethods that the Msg was received from
46 */
47 private List from = new ArrayList();
48 /***
49 * Stores the ContactMethods that the Msg is to be sent to
50 */
51 private List to = new ArrayList();
52 /***
53 * Stores the ContactMethods that the Msg should be replied to
54 */
55 private List replyTo = new ArrayList();
56 /***
57 * The Date for when the Msg was received (null if it wasn't)
58 */
59 private Date received = null;
60 /***
61 * The Date for when the Msg was sent (null if it wasn't)
62 */
63 private Date sent = null;
64 /***
65 * The subject of the Msg
66 */
67 private String subject = "";
68 /***
69 * The content of the message, stored as a vector of ContentHandlers
70 */
71 private List contentHandlers = new ArrayList();
72 /***
73 * Whether or not the Msg is "new"
74 */
75 private boolean isNew = false;
76 /***
77 * Whether or not the Msg is "unread"
78 */
79 private boolean isUnread = false;
80
81 /***
82 * Private storage for a reference to the Logger
83 */
84 private static Logger log = Logger.getLogger(Msg.class.getName());
85
86 /***
87 * No-arg version of the constructor, makes a blank Msg
88 */
89 public MsgImpl() {
90 //No data needed
91 }
92
93 /***
94 * Copy constructor, fills a new Msg with the values of an existing Msg
95 *
96 * @param m the Msg to copy attributes from
97 */
98 public MsgImpl(Msg m) {
99 this.from = Arrays.asList(m.getFrom());
100 this.to = Arrays.asList(m.getRecipients());
101 this.replyTo = Arrays.asList(m.getReplyTo());
102 this.received = m.getReceivedDate();
103 this.sent = m.getSentDate();
104 this.subject = m.getSubject();
105 this.contentHandlers = Arrays.asList(m.getContentHandlers());
106 this.isNew = m.isNew();
107 this.isUnread = m.isUnread();
108 }
109
110 /***
111 * Add this recipient ContactMethod to the existing ones.
112 *
113 * @param cm the recipient
114 */
115 public void addRecipient(ContactMethod cm) {
116 if (!this.to.contains(cm)) {
117 this.to.add(cm);
118 }
119 }
120
121 /***
122 * Add these recipient ContactMethods to the existing ones.
123 *
124 * @param cms the recipients
125 */
126 public void addRecipients(ContactMethod[] cms) {
127 for (int i = 0; i < cms.length; i++) {
128 addRecipient(cms[i]);
129 }
130 }
131
132 /***
133 * Add this ContactMethod to be replied to
134 *
135 * @param cm the reply ContactMethod
136 */
137 public void addReplyTo(ContactMethod cm) {
138 if (!this.replyTo.contains(cm)) {
139 this.replyTo.add(cm);
140 }
141 }
142
143 /***
144 * Get all the recipient ContactMethods for the Msg.
145 *
146 * @return an array of the recipients for this Msg
147 */
148 public ContactMethod[] getRecipients() {
149 return (ContactMethod[]) this.to.toArray(new ContactMethod[this.to.size()]);
150 }
151
152 /***
153 * Return the ContentHandlers for the content
154 */
155 public ContentHandler[] getContentHandlers() {
156 return (ContentHandler[]) this.contentHandlers.toArray(
157 new ContentHandler[this.contentHandlers.size()]);
158 }
159
160 /***
161 * Get the sender(s).
162 *
163 * @return the sender(s) of this Msg.
164 */
165 public Contact[] getFrom() {
166 return (Contact[]) this.from.toArray(new Contact[this.from.size()]);
167 }
168
169 /***
170 * Get the Date this Msg was received.
171 *
172 * @return the Date this Msg was received, or null if it wasn't received
173 */
174 public Date getReceivedDate() {
175 return this.received;
176 }
177
178 /***
179 * Get the ContactMethods to which replies should be directed. This will usually be the sender of the message, but some messages may direct replies to a different ContactMethod.
180 *
181 * @return an array of the ContactMethods to which replies to this Msg should be directed
182 */
183 public ContactMethod[] getReplyTo() {
184 return (ContactMethod[]) this.replyTo.toArray(
185 new ContactMethod[this.replyTo.size()]);
186 }
187
188 /***
189 * Get the Date this Msg was sent.
190 *
191 * @return the Date this Msg was sent, or null if it wasn't sent
192 */
193 public Date getSentDate() {
194 return this.sent;
195 }
196
197 /***
198 * Get the subject of this Msg
199 *
200 * @return the subject of this Msg
201 */
202 public String getSubject() {
203 return this.subject;
204 }
205
206 /***
207 * Get a new Msg suitable for a reply to this message. The new Msg will have its attributes set up appropriately. Note that this new Msg object will be empty, that is, it will <b>not</b> have a "content". These will have to be suitably filled in by the client.
208 * If replyToAll is set, the new Msg will be addressed to all recipients of this message. Otherwise, the reply will be addressed to only the sender of this message (using the value of the getReplyTo method).
209 * The "Subject" field is filled in with the original subject prefixed with "Re: " (unless it already starts with "Re:" (case insensitive)).
210 *
211 * @param replyToAll whether to reply to all recipients of the original message, or just the reply-to ContactMethods
212 */
213 public Msg reply(boolean replyToAll) {
214 MsgImpl r = new MsgImpl();
215 if (this.subject.length() < 3
216 || this.subject.substring(0, 3).toLowerCase().equals("re:")) {
217 r.subject = this.subject;
218 } else {
219 r.subject = "Re: " + this.subject;
220 }
221 if (replyToAll) {
222 r.addRecipients(getReplyTo());
223 r.addRecipients(getRecipients());
224 } else {
225 r.addRecipients(getReplyTo());
226 }
227 return r;
228 }
229
230 /***
231 * Adds a new ContentHandler
232 *
233 * @param ch the new ContentHandler
234 */
235 public void addContentHandler(ContentHandler ch) {
236 if (ch != null && !this.contentHandlers.contains(ch)) {
237 this.contentHandlers.add(ch);
238 }
239 }
240
241 /***
242 * Sets a new ContentHandler
243 *
244 * @param ch the new ContentHandler
245 */
246 public void setContentHandler(ContentHandler ch) {
247 this.contentHandlers.clear();
248 addContentHandler(ch);
249 }
250
251 /***
252 * Sets the content of the Msg
253 *
254 * @param chs the new ContentHandlers
255 */
256 public void setContentHandlers(ContentHandler[] chs) {
257 this.contentHandlers.clear();
258 for (int i = 0; i < chs.length; i++) {
259 addContentHandler(chs[i]);
260 }
261 }
262
263 /***
264 * Set the "from" Contact for this Msg.
265 *
266 * @param c the Contact for the sender
267 */
268 public void setFrom(Contact c) {
269 this.from.clear();
270 this.from.add(c);
271 }
272
273 /***
274 * Set the "from" Contact(s) for this Msg.
275 *
276 * @param cs the new sender Contact(s)
277 */
278 public void setFrom(Contact[] cs) {
279 this.from.clear();
280 for (int i = 0; i < cs.length; i++) {
281 if (!this.from.contains(cs[i])) {
282 this.from.add(cs[i]);
283 }
284 }
285 }
286
287 /***
288 * Set the received Date
289 *
290 * @param d the Date the Msg was received
291 */
292 public void setReceivedDate(Date d) {
293 this.received = d;
294 }
295
296 /***
297 * Set the recipient ContactMethod. All ContactMethodare replaced by the ContactMethod parameter.
298 *
299 * @param cm the recipient
300 */
301 public void setRecipient(ContactMethod cm) {
302 this.to.clear();
303 addRecipient(cm);
304 }
305
306 /***
307 * Set these recipient ContactMethods. Eliminates all previous recipients.
308 *
309 * @param cms the recipients
310 */
311 public void setRecipients(ContactMethod[] cms) {
312 this.to.clear();
313 addRecipients(cms);
314 }
315
316 /***
317 * Set the address to which replies should be directed.
318 *
319 * @param cm the reply ContactMethod
320 */
321 public void setReplyTo(ContactMethod cm) {
322 this.replyTo.clear();
323 this.replyTo.add(cm);
324 }
325
326 /***
327 * Set the addresses to which replies should be directed. (Normally only a single address will be specified.)
328 *
329 * @param cms the reply ContactMethods
330 */
331 public void setReplyTo(ContactMethod[] cms) {
332 this.replyTo.clear();
333 for (int i = 0; i < cms.length; i++) {
334 addReplyTo(cms[i]);
335 }
336 }
337
338 /***
339 * Set the sent Date
340 *
341 * @param d the Date the Msg was sent
342 */
343 public void setSentDate(Date d) {
344 this.sent = d;
345 }
346
347 /***
348 * Set the subject
349 *
350 * @param s the subject
351 */
352 public void setSubject(String s) {
353 this.subject = s;
354 }
355
356 /***
357 * Convenience method to get the content of the Msg as a String
358 *
359 * @return a String content
360 */
361 public String getText() {
362 StringBuffer sb = new StringBuffer();
363 for (int i = 0; i < this.contentHandlers.size(); i++) {
364 Reader r = null;
365 try {
366 ContentHandler ch = (ContentHandler) this.contentHandlers.get(i);
367 DataFlavor[] flavors = ch.getTransferDataFlavors();
368 DataFlavor flavor = DataFlavor.selectBestTextFlavor(flavors);
369 if (flavor != null) {
370 Object o = ch.getContent();
371 if (o instanceof byte[]) {
372 sb.append(new String((byte[]) o));
373 } else {
374 sb.append(o);
375 }
376 } else {
377 r = flavor.getReaderForText(ch.getDataHandler());
378 for (int c;(c = r.read()) != -1;) {
379 sb.append((char) c);
380 }
381 }
382 } catch (IOException ioe) {
383 log.throwing("Msg", "getText()", ioe);
384 } catch (UnsupportedFlavorException ufe) {
385 log.throwing("Msg", "getText()", ufe);
386 } finally {
387 IOUtil.closeReader(r);
388 }
389 }
390 return sb.toString();
391 }
392
393 /***
394 * Convenience method to set the content of the Msg to a String
395 *
396 * @param body the text to set as the content
397 */
398 public void setText(String body) {
399 setContentHandler(new ContentHandlerImpl(body, "text/plain"));
400 }
401
402 /***
403 * Mutator for the unread state
404 *
405 * @param isUnread whether the Msg has not been read
406 */
407 public void setUnread(boolean isUnread) {
408 this.isUnread = isUnread;
409 }
410
411 /***
412 * Mutator for the "new" state
413 *
414 * @param isNew whether the Msg is "new"
415 */
416 public void setNew(boolean isNew) {
417 this.isNew = isNew;
418 }
419
420 /***
421 * Accessor for the unread state
422 *
423 * @return whether the Msg has not been read
424 */
425 public boolean isUnread() {
426 return this.isUnread;
427 }
428
429 /***
430 * Accessor for the "new" state
431 *
432 * @return whether the Msg is "new"
433 */
434 public boolean isNew() {
435 return this.isNew;
436 }
437 }
This page was automatically generated by Maven