001: package org.claros.intouch.webmail.controllers;
002:
003: import org.claros.commons.auth.models.AuthProfile;
004: import org.claros.commons.mail.models.ConnectionMetaHandler;
005: import org.claros.commons.mail.models.ConnectionProfile;
006:
007: /**
008: * @author Umut Gokbayrak
009: */
010: public class DbInboxControllerImpl extends InboxControllerBase
011: implements InboxController {
012: // private static Log log = LogFactory.getLog(DbInboxControllerImpl.class);
013: // private static Locale loc = new Locale("en", "US");
014: /**
015: * @param profile
016: * @param auth
017: * @param handler
018: */
019: public DbInboxControllerImpl(AuthProfile auth,
020: ConnectionProfile profile, ConnectionMetaHandler handler) {
021: super (auth, profile, handler);
022: }
023:
024: /* (non-Javadoc)
025: * @see org.claros.groupware.webmail.controllers.InboxController#checkEmail()
026: */
027: public ConnectionMetaHandler checkEmail() throws Exception {
028: //ProtocolFactory factory = new ProtocolFactory(profile, auth, handler);
029: //Protocol protocol = factory.getProtocol(null);
030: try {
031: // fetch all messages from the remote pop3 server
032: // protocol.disconnect();
033: // handler = protocol.connect(org.claros.commons.mail.utility.Constants.CONNECTION_READ_WRITE);
034: /*
035: ArrayList headers = protocol.fetchAllHeaders();
036: ArrayList toBeDeleted = new ArrayList();
037: if (headers != null) {
038: EmailHeader header = null;
039: for (int i=0;i<headers.size();i++) {
040: header = (EmailHeader)headers.get(i);
041: int msgId = header.getMessageId();
042: try {
043: ByteArrayOutputStream bos = new ByteArrayOutputStream();
044: ObjectOutputStream os = new ObjectOutputStream(bos);
045: os.writeObject(header);
046: byte bHeader[] = bos.toByteArray();
047: String md5Header = MD5.getHashString(bHeader);
048:
049: MailControllerFactory mailFact = new MailControllerFactory(auth, profile, handler, null);
050: MailController mailCont = mailFact.getMailController();
051: DbMailControllerImpl dbMailCont = (DbMailControllerImpl)mailCont;
052: if (!dbMailCont.mailAlreadyFetched(md5Header)) {
053: Message msg = protocol.getMessage(msgId);
054: if (!msg.getFolder().isOpen()) {
055: msg.getFolder().open(Folder.READ_ONLY);
056: }
057:
058: // find the destionation folderId for the message
059: String folderId = findDestinationFolderId(msg);
060:
061: // if message should be directly deleted it shouldn't be
062: // stored in DB.
063: if (folderId != null) {
064: // create a byte array from the message content.
065: bos = new ByteArrayOutputStream();
066: msg.writeTo(bos);
067: byte bMsg[] = bos.toByteArray();
068:
069: // serialize the message byte array
070: os = new ObjectOutputStream(bos);
071: os.writeObject(bMsg);
072:
073: // create an email db item
074: MsgDbOject item = new MsgDbOject();
075: item.setEmail(bos.toByteArray());
076: item.setUniqueId(md5Header);
077: item.setFolderId(new Long(folderId));
078: item.setUnread(new Boolean(true));
079: item.setUsername(auth.getUsername());
080: item.setMsgSize(new Long(bMsg.length));
081:
082: // save the email db item.
083: mailCont.appendEmail(item);
084: }
085: toBeDeleted.add(new Integer(msgId));
086: }
087: } catch (Exception e) {
088: toBeDeleted.add(new Integer(msgId));
089: log.error("Error while processing mail.", e);
090: }
091: }
092: }
093:
094: // fetched messages are deleted if the user requested so.
095: // String deleteFetched = UserPrefsController.getUserSetting(auth, UserPrefConstants.deleteFetched);
096: String deleteFetched = "no";
097: if (deleteFetched != null && deleteFetched.equals("yes")) {
098: if (toBeDeleted.size() > 0) {
099: int ids[] = new int[toBeDeleted.size()];
100: for (int i=0;i<toBeDeleted.size();i++) {
101: Integer id = (Integer)toBeDeleted.get(i);
102: ids[i] = id.intValue();
103: }
104: protocol.deleteMessages(ids);
105: }
106: }
107: */
108: } finally {
109: // protocol.disconnect();
110: }
111: return handler;
112: }
113:
114: }
|