001: package org.claros.mini.controllers;
002:
003: import java.io.ByteArrayInputStream;
004: import java.sql.SQLException;
005: import java.util.Properties;
006:
007: import javax.mail.Session;
008: import javax.mail.internet.MimeMessage;
009:
010: import org.apache.commons.dbutils.QueryRunner;
011: import org.apache.commons.logging.Log;
012: import org.apache.commons.logging.LogFactory;
013: import org.claros.commons.db.DbConfigList;
014: import org.claros.commons.exception.NoPermissionException;
015: import org.claros.commons.mail.exception.MailboxActionException;
016: import org.claros.commons.mail.models.ConnectionMetaHandler;
017: import org.claros.commons.mail.models.ConnectionProfile;
018: import org.claros.commons.mail.models.Email;
019: import org.claros.commons.mail.parser.MessageParser;
020: import org.claros.commons.models.AuthProfile;
021: import org.claros.mini.models.EmailDbItem;
022: import org.claros.mini.utility.Constants;
023: import org.claros.mini.utility.Utility;
024:
025: import com.jenkov.mrpersister.impl.mapping.AutoGeneratedColumnsMapper;
026: import com.jenkov.mrpersister.itf.IGenericDao;
027: import com.jenkov.mrpersister.itf.mapping.IObjectMappingKey;
028: import com.jenkov.mrpersister.util.JdbcUtil;
029:
030: /**
031: * @author Umut Gokbayrak
032: */
033: public class DbMailControllerImpl implements MailController {
034: private static Log log = LogFactory
035: .getLog(DbMailControllerImpl.class);
036: private AuthProfile auth;
037: private ConnectionProfile profile;
038: private ConnectionMetaHandler handler;
039:
040: private DbMailControllerImpl() {
041: super ();
042: }
043:
044: public DbMailControllerImpl(AuthProfile auth,
045: ConnectionProfile profile, ConnectionMetaHandler handler) {
046: this .auth = auth;
047: this .profile = profile;
048: this .handler = handler;
049: }
050:
051: /* (non-Javadoc)
052: * @see org.claros.groupware.webmail.controllers.MailController#getEmailById(java.lang.Long)
053: */
054: public Email getEmailById(Long emailId) throws Exception {
055: Email email = null;
056: EmailDbItem item = getEmailDbItemById(emailId);
057:
058: Properties props = new Properties();
059: Session session = Session.getDefaultInstance(props);
060: ByteArrayInputStream bis = new ByteArrayInputStream(item
061: .getEmail());
062: MimeMessage msg = new MimeMessage(session, bis);
063:
064: try {
065: email = MessageParser.parseMessage(msg);
066: email.setMsgId(item.getId());
067: } catch (Exception e) {
068: log.error("Message could not be parsed to an email object",
069: e);
070: throw new MailboxActionException(e);
071: }
072: if (email != null) {
073: email.setMsgId(item.getId());
074: }
075: return email;
076: }
077:
078: /**
079: *
080: * @param auth
081: * @param emailId
082: * @return
083: * @throws Exception
084: */
085: private EmailDbItem getEmailDbItemById(Long emailId)
086: throws Exception {
087: EmailDbItem email = null;
088: IGenericDao dao = null;
089: try {
090: dao = Utility.getDbConnection();
091: String username = auth.getUsername();
092:
093: String sql = "SELECT * FROM EMAIL_DB_ITEMS WHERE USER=? AND ID = ?";
094: email = (EmailDbItem) dao.read(EmailDbItem.class, sql,
095: new Object[] { username, emailId });
096: } finally {
097: JdbcUtil.close(dao);
098: dao = null;
099: }
100: return email;
101: }
102:
103: /* (non-Javadoc)
104: * @see org.claros.groupware.webmail.controllers.MailController#deleteEmail(java.lang.Long)
105: */
106: public void deleteEmail(Long emailId) throws Exception {
107: EmailDbItem tmp = getEmailDbItemById(emailId);
108: if (tmp != null) {
109: String user = tmp.getUser();
110: if (!auth.getUsername().equals(user)) {
111: throw new NoPermissionException();
112: }
113:
114: IGenericDao dao = null;
115: try {
116: dao = Utility.getDbConnection();
117: dao.deleteByPrimaryKey(EmailDbItem.class, emailId);
118: } catch (Exception e) {
119: // do nothing sier
120: } finally {
121: JdbcUtil.close(dao);
122: dao = null;
123: }
124: }
125: }
126:
127: /* (non-Javadoc)
128: * @see org.claros.groupware.webmail.controllers.MailController#moveEmail(java.lang.Long, java.lang.String)
129: */
130: public void moveEmail(Long msgId, String destFolder)
131: throws Exception {
132: EmailDbItem item = getEmailDbItemById(msgId);
133: Long destId = new Long(destFolder);
134: item.setFolderId(destId);
135: updateEmail(item);
136: }
137:
138: /**
139: *
140: * @param item
141: * @throws Exception
142: */
143: private void updateEmail(EmailDbItem item) throws Exception {
144: EmailDbItem tmp = getEmailDbItemById(item.getId());
145: String user = tmp.getUser();
146: if (!auth.getUsername().equals(user)) {
147: throw new NoPermissionException();
148: }
149:
150: IGenericDao dao = null;
151: try {
152: dao = Utility.getDbConnection();
153: dao.update(EmailDbItem.class, item);
154: } finally {
155: JdbcUtil.close(dao);
156: dao = null;
157: }
158: }
159:
160: /* (non-Javadoc)
161: * @see org.claros.groupware.webmail.controllers.MailController#appendEmail(org.claros.groupware.webmail.models.EmailDbItem)
162: */
163: public void appendEmail(EmailDbItem item) throws Exception {
164: IGenericDao dao = null;
165: try {
166: dao = Utility.getDbConnection();
167:
168: IObjectMappingKey myObj = Constants.persistMan
169: .getObjectMappingFactory().createInstance(
170: EmailDbItem.class,
171: new AutoGeneratedColumnsMapper(true));
172: dao.insert(myObj, item);
173: } finally {
174: JdbcUtil.close(dao);
175: dao = null;
176: }
177: }
178:
179: /* (non-Javadoc)
180: * @see org.claros.groupware.webmail.controllers.MailController#markAsRead(java.lang.Long)
181: */
182: public void markAsRead(Long msgId) throws Exception {
183: QueryRunner run = new QueryRunner(DbConfigList
184: .getDataSourceById("file"));
185: String username = auth.getUsername();
186: try {
187: String sql = "UPDATE EMAIL_DB_ITEMS SET UNREAD = ? where user=? and id=?";
188: run.update(sql, new Object[] { new Boolean(false),
189: username, msgId });
190: } catch (SQLException e) {
191: throw e;
192: }
193: }
194:
195: /**
196: *
197: * @param auth
198: * @param md5Header
199: * @return
200: * @throws Exception
201: */
202: public boolean mailAlreadyFetched(String md5Header)
203: throws Exception {
204: IGenericDao dao = null;
205: boolean result = false;
206: try {
207: dao = Utility.getDbConnection();
208: String username = auth.getUsername();
209:
210: String sql = "SELECT * FROM EMAIL_DB_ITEMS WHERE USER=? AND UNIQUE_ID = ?";
211: EmailDbItem email = (EmailDbItem) dao.read(
212: EmailDbItem.class, sql, new Object[] { username,
213: md5Header });
214: if (email != null) {
215: result = true;
216: }
217: } finally {
218: JdbcUtil.close(dao);
219: dao = null;
220: }
221: return result;
222: }
223:
224: /* (non-Javadoc)
225: * @see org.claros.groupware.webmail.controllers.MailController#deleteEmails(int)
226: */
227: public void deleteEmails(int msgs[]) throws Exception {
228: for (int i = 0; i < msgs.length; i++) {
229: deleteEmail(new Long(msgs[i]));
230: }
231: }
232: }
|