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