001: // Copyright 04/11/01 Sun Microsystems, Inc. All Rights Reserved.
002: // "@(#)MailContext.java 1.12 01/04/11 Sun Microsystems"
003:
004: package com.sun.portal.wireless.taglibs.mail;
005:
006: import javax.mail.*;
007: import javax.mail.internet.*;
008: import javax.mail.search.*;
009: import javax.servlet.jsp.PageContext;
010:
011: import java.util.*;
012: import java.text.*;
013:
014: class MailUtils {
015:
016: private static int direction = 1;
017:
018: public static void setSortDirection(String sd) {
019: direction = 1;
020:
021: if (sd != null && sd.equals("down")) {
022: direction = -1;
023: }
024: }
025:
026: public static MsgBean[] toBeanArray(Message[] msgs, String from,
027: TimeZone tz, PageContext pageContext) {
028:
029: MsgBean[] tmp = new MsgBean[msgs.length];
030:
031: for (int i = 0; i < msgs.length; i++) {
032: tmp[i] = new MsgBean(msgs[i], from, tz);
033: tmp[i].setPageContext(pageContext);
034: }
035:
036: return tmp;
037: }
038:
039: public static FolderBean[] toFolderBeanArray(Folder[] folders) {
040:
041: FolderBean[] tmp = new FolderBean[folders.length];
042:
043: for (int i = 0; i < folders.length; i++) {
044: tmp[i] = new FolderBean(folders[i]);
045: }
046:
047: return tmp;
048: }
049:
050: public static Object[] inboxOrder(Object[] in) {
051: if (direction < 0) {
052: Object tmp;
053: int len = in.length - 1;
054: int hlen = (in.length / 2);
055: for (int i = 0; i < hlen; i++) {
056: tmp = in[i];
057: in[i] = in[len - i];
058: in[len - i] = tmp;
059: }
060: }
061: return in;
062: }
063:
064: public static void prefetch(MsgBean[] msgs, String field) {
065: FetchProfile fp = new FetchProfile();
066: fp.add(field);
067: prefetch(msgs, fp);
068: }
069:
070: public static void prefetch(MsgBean[] msgs, FetchProfile fp) {
071: try {
072: Message[] m = new Message[msgs.length];
073:
074: for (int i = 0; i < msgs.length; i++) {
075: m[i] = msgs[i].getObject();
076: }
077:
078: m[0].getFolder().fetch(m, fp);
079: } catch (Exception e) {
080: // Dont really care what the error was
081: }
082: }
083:
084: public static class FolderComparator implements Comparator {
085: Collator collate = null;
086:
087: public FolderComparator(Collator c) {
088: collate = c;
089: }
090:
091: public int compare(Object a, Object b) {
092: Folder f1 = ((FolderBean) a).getFolder();
093: Folder f2 = ((FolderBean) b).getFolder();
094:
095: String tmp1 = f1.getName();
096: String tmp2 = f2.getName();
097:
098: return direction * (collate.compare(tmp1, tmp2));
099: }
100: }
101:
102: public static class MsgFromComparator implements Comparator {
103: Collator collate = null;
104:
105: public MsgFromComparator(Collator c) {
106: collate = c;
107: }
108:
109: public int compare(Object a, Object b) {
110: MsgBean b1 = (MsgBean) a;
111: MsgBean b2 = (MsgBean) b;
112: String tmp1 = "";
113: String tmp2 = "";
114:
115: try {
116: tmp1 = b1.getFrom();
117: } catch (MessagingException ex) {
118: }
119:
120: try {
121: tmp2 = b2.getFrom();
122: } catch (MessagingException ex) {
123: }
124:
125: return direction * (collate.compare(tmp1, tmp2));
126: }
127: }
128:
129: public static class MsgSubjectComparator implements Comparator {
130: Collator collate = null;
131:
132: public MsgSubjectComparator(Collator c) {
133: collate = c;
134: }
135:
136: public int compare(Object a, Object b) {
137: MsgBean b1 = (MsgBean) a;
138: MsgBean b2 = (MsgBean) b;
139: String tmp1 = "";
140: String tmp2 = "";
141:
142: try {
143: tmp1 = b1.getSubject();
144: } catch (MessagingException ex) {
145: }
146:
147: try {
148: tmp2 = b2.getSubject();
149: } catch (MessagingException ex) {
150: }
151:
152: return direction * (collate.compare(tmp1, tmp2));
153: }
154: }
155:
156: public static class MsgDateComparator implements Comparator {
157: public int compare(Object a, Object b) {
158: MsgBean b1 = (MsgBean) a;
159: MsgBean b2 = (MsgBean) b;
160: Message m1 = b1.getObject();
161: Message m2 = b2.getObject();
162: Date d1;
163: Date d2;
164: try {
165: d1 = m1.getReceivedDate();
166: } catch (MessagingException ex) {
167: return direction * 1;
168: }
169:
170: try {
171: d2 = m2.getReceivedDate();
172: } catch (MessagingException ex) {
173: return direction * (-1);
174: }
175:
176: return direction * (d1.compareTo(d2));
177: }
178: }
179:
180: public static class MsgIdComparator implements Comparator {
181: public int compare(Object a, Object b) {
182: MsgBean b1 = (MsgBean) a;
183: MsgBean b2 = (MsgBean) b;
184: int i = b1.getMsgid();
185: int j = b2.getMsgid();
186:
187: return direction * (i - j);
188: }
189: }
190:
191: public static class MsgSeenComparator implements Comparator {
192: public int compare(Object a, Object b) {
193: MsgBean b1 = (MsgBean) a;
194: MsgBean b2 = (MsgBean) b;
195:
196: int i = b1.getMsgid();
197: int j = b2.getMsgid();
198:
199: boolean s1;
200: try {
201: s1 = b1.isSeen();
202: } catch (Exception e) {
203: s1 = false;
204: }
205:
206: boolean s2;
207: try {
208: s2 = b2.isSeen();
209: } catch (Exception e) {
210: s2 = false;
211: }
212:
213: if (s1 == s2) {
214: return direction * (i - j);
215: }
216:
217: if (s1 == true) {
218: return direction;
219: }
220:
221: return -direction;
222: }
223: }
224:
225: public static class MsgRecentComparator implements Comparator {
226: public int compare(Object a, Object b) {
227: MsgBean b1 = (MsgBean) a;
228: MsgBean b2 = (MsgBean) b;
229:
230: int i = b1.getMsgid();
231: int j = b2.getMsgid();
232:
233: boolean s1;
234: try {
235: s1 = b1.isRecent();
236: } catch (Exception e) {
237: s1 = false;
238: }
239:
240: boolean s2;
241: try {
242: s2 = b2.isRecent();
243: } catch (Exception e) {
244: s2 = false;
245: }
246:
247: if (s1 == s2) {
248: return direction * (i - j);
249: }
250:
251: if (s1 == true) {
252: return direction;
253: }
254:
255: return -direction;
256: }
257: }
258:
259: }
|