001: package org.claros.intouch.webmail.controllers;
002:
003: import java.util.ArrayList;
004: import java.util.Collections;
005: import java.util.Comparator;
006: import java.util.List;
007:
008: import javax.mail.Folder;
009: import javax.mail.MessagingException;
010:
011: import org.apache.commons.logging.Log;
012: import org.apache.commons.logging.LogFactory;
013: import org.claros.commons.auth.models.AuthProfile;
014: import org.claros.commons.exception.SystemException;
015: import org.claros.commons.mail.models.ConnectionMetaHandler;
016: import org.claros.commons.mail.models.ConnectionProfile;
017: import org.claros.commons.mail.protocols.ImapProtocolImpl;
018: import org.claros.commons.mail.protocols.Protocol;
019: import org.claros.commons.mail.protocols.ProtocolFactory;
020: import org.claros.intouch.common.utility.Constants;
021: import org.claros.intouch.webmail.models.FolderDbObject;
022: import org.claros.intouch.webmail.models.FolderDbObjectWrapper;
023:
024: /**
025: * @author Umut Gokbayrak
026: */
027: public class ImapFolderControllerImpl implements FolderController {
028: private AuthProfile auth;
029: private ConnectionProfile profile;
030: private ConnectionMetaHandler handler;
031: private static Log log = LogFactory
032: .getLog(ImapFolderControllerImpl.class);
033:
034: /**
035: * @param auth
036: * @param profile
037: * @param handler
038: */
039: public ImapFolderControllerImpl(AuthProfile auth,
040: ConnectionProfile profile, ConnectionMetaHandler handler) {
041: this .auth = auth;
042: this .profile = profile;
043: this .handler = handler;
044: }
045:
046: /**
047: * used to disable it.
048: */
049: @SuppressWarnings("unused")
050: private ImapFolderControllerImpl() {
051: super ();
052: }
053:
054: /**
055: *
056: * @param folder
057: * @return
058: * @throws Exception
059: */
060: public Folder getFolderObj(String folder, boolean useCache)
061: throws Exception {
062: ProtocolFactory factory = new ProtocolFactory(profile, auth,
063: handler);
064: ImapProtocolImpl protocol = (ImapProtocolImpl) factory
065: .getImap(folder);
066: Folder f = protocol.getImapFolder(useCache);
067: return f;
068: }
069:
070: /* (non-Javadoc)
071: * @see org.claros.groupware.webmail.controllers.FolderController#getFolders()
072: */
073: public List getFolders() throws Exception {
074: ProtocolFactory factory = new ProtocolFactory(profile, auth,
075: handler);
076: ImapProtocolImpl protocol = (ImapProtocolImpl) factory
077: .getImap(null);
078: Folder folders[] = protocol.listFolders();
079:
080: ArrayList res = new ArrayList();
081: boolean inboxAdded = false;
082: if (folders != null) {
083: Folder tmp = null;
084: String n, fn = null;
085: for (int i = 0; i < folders.length; i++) {
086: try {
087: tmp = folders[i];
088: if (tmp != null) {
089: n = tmp.getName();
090: fn = tmp.getFullName();
091: // if (fn != null && fn.startsWith("INBOX.")) {
092: // n = fn;
093: // }
094: Integer type = determineFolderType(fn);
095: if (type.equals(Constants.FOLDER_TYPE_INBOX)) {
096: inboxAdded = true;
097: }
098: FolderDbObject item = new FolderDbObject(null,
099: null, auth.getUsername(), n, type);
100: FolderDbObjectWrapper wr = new FolderDbObjectWrapper(
101: item);
102: wr.setUnreadItemCount(new Integer(tmp
103: .getUnreadMessageCount()));
104: wr.setTotalItemCount(new Integer(tmp
105: .getMessageCount()));
106: res.add(wr);
107: }
108: } catch (MessagingException e) {
109: // do not worry about this. folder might be deleted but still subscribed.
110: log.debug(e);
111: }
112: }
113: }
114:
115: // inbox is not added in this server implementation(weird!!!) . Please add it.
116: if (!inboxAdded) {
117: FolderDbObject item = new FolderDbObject(null, null, auth
118: .getUsername(),
119: org.claros.commons.mail.utility.Constants
120: .FOLDER_INBOX(profile).toUpperCase(),
121: Constants.FOLDER_TYPE_INBOX);
122: FolderDbObjectWrapper wr = new FolderDbObjectWrapper(item);
123: Folder inbox = protocol.getFolder();
124: if (inbox.exists()) {
125: wr.setUnreadItemCount(new Integer(inbox
126: .getUnreadMessageCount()));
127: wr.setTotalItemCount(new Integer(inbox
128: .getMessageCount()));
129: res.add(wr);
130: }
131: }
132:
133: Collections.sort(res, new Comparator() {
134: public int compare(Object f1, Object f2) {
135: FolderDbObjectWrapper fw1 = (FolderDbObjectWrapper) f1;
136: FolderDbObjectWrapper fw2 = (FolderDbObjectWrapper) f2;
137:
138: Integer t1 = fw1.getFolderType();
139: Integer t2 = fw2.getFolderType();
140:
141: if (t1.equals(Constants.FOLDER_TYPE_CUSTOM)
142: && t2.equals(Constants.FOLDER_TYPE_CUSTOM)) {
143: return fw1.getFolderName().compareTo(
144: fw2.getFolderName());
145: } else {
146: return t1.compareTo(t2);
147: }
148: }
149: });
150:
151: return res;
152: }
153:
154: /**
155: *
156: * @param folderName
157: * @return
158: */
159: private Integer determineFolderType(String folderName) {
160: if (folderName == null) {
161: return null;
162: }
163: String ns = profile.getFolderNameSpace();
164: if (!folderName.equals("INBOX") && !folderName.startsWith(ns)) {
165: folderName = ns + folderName;
166: }
167: if (folderName.toUpperCase().equals(
168: org.claros.commons.mail.utility.Constants.FOLDER_INBOX(
169: profile).toUpperCase())) {
170: return Constants.FOLDER_TYPE_INBOX;
171: } else if (folderName.toUpperCase().equals(
172: org.claros.commons.mail.utility.Constants.FOLDER_SENT(
173: profile).toUpperCase())) {
174: return Constants.FOLDER_TYPE_SENT;
175: } else if (folderName.toUpperCase().equals(
176: org.claros.commons.mail.utility.Constants.FOLDER_JUNK(
177: profile).toUpperCase())) {
178: return Constants.FOLDER_TYPE_JUNK;
179: } else if (folderName.toUpperCase().equals(
180: org.claros.commons.mail.utility.Constants.FOLDER_TRASH(
181: profile).toUpperCase())) {
182: return Constants.FOLDER_TYPE_TRASH;
183: } else if (folderName.toUpperCase().equals(
184: org.claros.commons.mail.utility.Constants
185: .FOLDER_DRAFTS(profile).toUpperCase())) {
186: return Constants.FOLDER_TYPE_DRAFTS;
187: } else {
188: return Constants.FOLDER_TYPE_CUSTOM;
189: }
190: }
191:
192: /* (non-Javadoc)
193: * @see org.claros.groupware.webmail.controllers.FolderController#getFolder(java.lang.String)
194: */
195: public FolderDbObject getFolder(String folder) throws Exception {
196: ProtocolFactory factory = new ProtocolFactory(profile, auth,
197: handler);
198: ImapProtocolImpl protocol = (ImapProtocolImpl) factory
199: .getImap(folder);
200: Folder f = protocol.getFolder();
201:
202: FolderDbObject item = null;
203: if (f.exists()) {
204: item = new FolderDbObject(null, null, auth.getUsername(), f
205: .getName(), determineFolderType(f.getName()));
206: }
207: // ImapProtocolImpl.closeFolder(f);
208: return item;
209: }
210:
211: /* (non-Javadoc)
212: * @see org.claros.groupware.webmail.controllers.FolderController#getMailsByFolder(java.lang.String)
213: */
214: public List getMailsByFolder(String folder) throws Exception {
215: throw new SystemException(
216: "Method not supported at this protocol");
217: }
218:
219: /* (non-Javadoc)
220: * @see org.claros.groupware.webmail.controllers.FolderController#createFolder(org.claros.groupware.webmail.models.FolderDbItem)
221: */
222: public void createFolder(FolderDbObject item) throws Exception {
223: String name = item.getFolderName();
224: ProtocolFactory factory = new ProtocolFactory(profile, auth,
225: handler);
226: ImapProtocolImpl protocol = (ImapProtocolImpl) factory
227: .getImap(name);
228: protocol.createFolder();
229: }
230:
231: /* (non-Javadoc)
232: * @see org.claros.groupware.webmail.controllers.FolderController#countUnreadMessages(java.lang.String)
233: */
234: public Integer countUnreadMessages(String folder) throws Exception {
235: ProtocolFactory factory = new ProtocolFactory(profile, auth,
236: handler);
237: ImapProtocolImpl protocol = (ImapProtocolImpl) factory
238: .getImap(folder);
239: int count = protocol.getUnreadMessageCount();
240: return new Integer(count);
241: }
242:
243: /* (non-Javadoc)
244: * @see org.claros.groupware.webmail.controllers.FolderController#countTotalMessages(java.lang.String)
245: */
246: public Integer countTotalMessages(String folder) throws Exception {
247: ProtocolFactory factory = new ProtocolFactory(profile, auth,
248: handler);
249: ImapProtocolImpl protocol = (ImapProtocolImpl) factory
250: .getImap(folder);
251: int count = protocol.getTotalMessageCount();
252: return new Integer(count);
253: }
254:
255: /* (non-Javadoc)
256: * @see org.claros.groupware.webmail.controllers.FolderController#emptyFolder(java.lang.String)
257: */
258: public void emptyFolder(String folder) throws Exception {
259: ProtocolFactory factory = new ProtocolFactory(profile, auth,
260: handler);
261: ImapProtocolImpl protocol = (ImapProtocolImpl) factory
262: .getImap(folder);
263: protocol.emptyFolder();
264: }
265:
266: /* (non-Javadoc)
267: * @see org.claros.groupware.webmail.controllers.FolderController#deleteFolder(java.lang.String)
268: */
269: public void deleteFolder(String folder) throws Exception {
270: ProtocolFactory factory = new ProtocolFactory(profile, auth,
271: handler);
272: ImapProtocolImpl protocol = (ImapProtocolImpl) factory
273: .getImap(folder);
274: protocol.deleteFolder();
275: }
276:
277: /* (non-Javadoc)
278: * @see org.claros.groupware.webmail.controllers.FolderController#renameFolder(java.lang.String, java.lang.String)
279: */
280: public void renameFolder(String oldName, String newName)
281: throws Exception {
282: ProtocolFactory factory = new ProtocolFactory(profile, auth,
283: handler);
284: ImapProtocolImpl protocol = (ImapProtocolImpl) factory
285: .getImap(oldName);
286: protocol.renameFolder(newName);
287: }
288:
289: /* (non-Javadoc)
290: * @see org.claros.groupware.webmail.controllers.FolderController#getHeadersByFolder(java.lang.String)
291: */
292: public ArrayList getHeadersByFolder(String folder) throws Exception {
293: ProtocolFactory factory = new ProtocolFactory(profile, auth,
294: handler);
295: Protocol protocol = factory.getProtocol(folder);
296: ArrayList headers = protocol.fetchAllHeaders();
297:
298: return headers;
299: }
300:
301: /* (non-Javadoc)
302: * @see org.claros.groupware.webmail.controllers.FolderController#createDefaultFolders()
303: */
304: public void createDefaultFolders() throws Exception {
305: ProtocolFactory factory = new ProtocolFactory(profile, auth,
306: handler);
307:
308: ImapProtocolImpl imapJunk = (ImapProtocolImpl) factory
309: .getImap(org.claros.commons.mail.utility.Constants
310: .FOLDER_JUNK(profile));
311: imapJunk.createFolder();
312:
313: ImapProtocolImpl imapSent = (ImapProtocolImpl) factory
314: .getImap(org.claros.commons.mail.utility.Constants
315: .FOLDER_SENT(profile));
316: imapSent.createFolder();
317:
318: ImapProtocolImpl imapTrash = (ImapProtocolImpl) factory
319: .getImap(org.claros.commons.mail.utility.Constants
320: .FOLDER_TRASH(profile));
321: imapTrash.createFolder();
322:
323: ImapProtocolImpl imapDrafts = (ImapProtocolImpl) factory
324: .getImap(org.claros.commons.mail.utility.Constants
325: .FOLDER_DRAFTS(profile));
326: imapDrafts.createFolder();
327: }
328:
329: /**
330: * @return
331: * @throws Exception
332: */
333: public FolderDbObject getJunkFolder() throws Exception {
334: return new FolderDbObject(null, null, null,
335: org.claros.commons.mail.utility.Constants
336: .FOLDER_JUNK(profile), null);
337: }
338:
339: /**
340: * @param auth
341: * @return
342: */
343: public FolderDbObject getInboxFolder() throws Exception {
344: return new FolderDbObject(null, null, null,
345: org.claros.commons.mail.utility.Constants
346: .FOLDER_INBOX(profile), null);
347: }
348:
349: /**
350: * @param auth
351: * @return
352: */
353: public FolderDbObject getSentItems() throws Exception {
354: return new FolderDbObject(null, null, null,
355: org.claros.commons.mail.utility.Constants
356: .FOLDER_SENT(profile), null);
357: }
358:
359: /**
360: * @param auth
361: * @return
362: */
363: public FolderDbObject getTrashFolder() throws Exception {
364: return new FolderDbObject(null, null, null,
365: org.claros.commons.mail.utility.Constants
366: .FOLDER_TRASH(profile), null);
367: }
368:
369: /**
370: * @param auth
371: * @return
372: */
373: public FolderDbObject getDraftsFolder() throws Exception {
374: return new FolderDbObject(null, null, null,
375: org.claros.commons.mail.utility.Constants
376: .FOLDER_DRAFTS(profile), null);
377: }
378:
379: public List getHeadersByFolder(String folder, int[] msgs)
380: throws Exception {
381: ProtocolFactory factory = new ProtocolFactory(profile, auth,
382: handler);
383: Protocol protocol = factory.getProtocol(folder);
384: ArrayList headers = protocol.fetchHeaders(msgs);
385: return headers;
386: }
387: }
|