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