001: package org.claros.mini.controllers;
002:
003: import java.io.ByteArrayInputStream;
004: import java.sql.SQLException;
005: import java.util.ArrayList;
006: import java.util.HashMap;
007: import java.util.List;
008: import java.util.Properties;
009:
010: import javax.mail.Message;
011: import javax.mail.MessagingException;
012: import javax.mail.Session;
013: import javax.mail.internet.MimeMessage;
014:
015: import org.apache.commons.dbutils.QueryRunner;
016: import org.apache.commons.dbutils.handlers.MapHandler;
017: import org.apache.commons.logging.Log;
018: import org.apache.commons.logging.LogFactory;
019: import org.claros.commons.db.DbConfigList;
020: import org.claros.commons.exception.NoPermissionException;
021: import org.claros.commons.mail.models.EmailHeader;
022: import org.claros.commons.models.AuthProfile;
023: import org.claros.commons.utility.Formatter;
024: import org.claros.mini.models.EmailDbItem;
025: import org.claros.mini.models.FolderDbItem;
026: import org.claros.mini.models.FolderDbItemWrapper;
027: import org.claros.mini.utility.Constants;
028: import org.claros.mini.utility.Utility;
029:
030: import com.jenkov.mrpersister.impl.mapping.AutoGeneratedColumnsMapper;
031: import com.jenkov.mrpersister.itf.IGenericDao;
032: import com.jenkov.mrpersister.itf.mapping.IObjectMappingKey;
033: import com.jenkov.mrpersister.util.JdbcUtil;
034:
035: /**
036: * @author Umut Gokbayrak
037: */
038: public class DbFolderControllerImpl implements FolderController {
039: private static Log log = LogFactory
040: .getLog(DbFolderControllerImpl.class);
041: private AuthProfile auth;
042:
043: /**
044: * @param auth
045: */
046: public DbFolderControllerImpl(AuthProfile auth) {
047: this .auth = auth;
048: }
049:
050: /**
051: * used to disable it.
052: *
053: */
054: private DbFolderControllerImpl() {
055: super ();
056: }
057:
058: /**
059: * only meaningful for inbox, sent, junk, trash
060: * @param auth
061: * @return
062: */
063: private FolderDbItem getSpecialFolderByType(Integer folderType)
064: throws Exception {
065: IGenericDao dao = null;
066: FolderDbItem folder = null;
067: try {
068: dao = Utility.getDbConnection();
069: String username = auth.getUsername();
070:
071: String sql = "select * from FOLDER_DB_ITEMS where user=? and type = ?";
072: folder = (FolderDbItem) dao.read(FolderDbItem.class, sql,
073: new Object[] { username, folderType });
074: } finally {
075: JdbcUtil.close(dao);
076: dao = null;
077: }
078: return folder;
079: }
080:
081: /**
082: * @param auth
083: * @return
084: */
085: public FolderDbItem getJunkFolder() throws Exception {
086: return getSpecialFolderByType(Constants.FOLDER_TYPE_JUNK);
087: }
088:
089: /**
090: * @param auth
091: * @return
092: */
093: public FolderDbItem getInboxFolder() throws Exception {
094: return getSpecialFolderByType(Constants.FOLDER_TYPE_INBOX);
095: }
096:
097: /**
098: * @param auth
099: * @return
100: */
101: public FolderDbItem getSentItems() throws Exception {
102: return getSpecialFolderByType(Constants.FOLDER_TYPE_SENT);
103: }
104:
105: /* (non-Javadoc)
106: * @see org.claros.groupware.webmail.controllers.FolderController#getFolders(org.claros.commons.models.AuthProfile)
107: */
108: public List getFolders() throws Exception {
109: IGenericDao dao = null;
110: ArrayList myList = null;
111: try {
112: dao = Utility.getDbConnection();
113: String username = auth.getUsername();
114:
115: String sql = "select * from FOLDER_DB_ITEMS where user=?";
116: List folders = dao.readList(FolderDbItem.class, sql,
117: new Object[] { username });
118:
119: myList = new ArrayList();
120: if (folders != null) {
121: FolderDbItem tmp = null;
122: for (int i = 0; i < folders.size(); i++) {
123: tmp = (FolderDbItem) folders.get(i);
124: FolderDbItemWrapper fd = new FolderDbItemWrapper(
125: tmp);
126: fd.setUnreadItemCount(countUnreadMessages(tmp
127: .getId().toString()));
128: fd.setTotalItemCount(countTotalMessages(tmp.getId()
129: .toString()));
130: myList.add(fd);
131: }
132: }
133: } finally {
134: JdbcUtil.close(dao);
135: dao = null;
136: }
137: return myList;
138: }
139:
140: /* (non-Javadoc)
141: * @see org.claros.groupware.webmail.controllers.FolderController#getFolder(org.claros.commons.models.AuthProfile, java.lang.String)
142: */
143: public FolderDbItem getFolder(String folder) throws Exception {
144: IGenericDao dao = null;
145: FolderDbItem fld = null;
146: try {
147: Long lFolder = new Long(folder);
148:
149: dao = Utility.getDbConnection();
150: String username = auth.getUsername();
151:
152: String sql = "select * from FOLDER_DB_ITEMS where user=? and id = ?";
153: fld = (FolderDbItem) dao.read(FolderDbItem.class, sql,
154: new Object[] { username, lFolder });
155: } finally {
156: JdbcUtil.close(dao);
157: dao = null;
158: }
159: return fld;
160: }
161:
162: /* (non-Javadoc)
163: * @see org.claros.groupware.webmail.controllers.FolderController#getMailsByFolder(org.claros.commons.models.AuthProfile, java.lang.String)
164: */
165: public List getMailsByFolder(String folder) throws Exception {
166: IGenericDao dao = null;
167: List msgs = null;
168: try {
169: Long folderId = new Long(folder);
170: dao = Utility.getDbConnection();
171: String username = auth.getUsername();
172:
173: String sql = "select * from EMAIL_DB_ITEMS where user=? and folder_id = ?";
174: msgs = dao.readList(EmailDbItem.class, sql, new Object[] {
175: username, folderId });
176: } finally {
177: JdbcUtil.close(dao);
178: dao = null;
179: }
180: return msgs;
181: }
182:
183: /* (non-Javadoc)
184: * @see org.claros.groupware.webmail.controllers.FolderController#createFolder(org.claros.groupware.webmail.models.FolderDbItem)
185: */
186: public void createFolder(FolderDbItem item) throws Exception {
187: IGenericDao dao = null;
188: try {
189: dao = Utility.getDbConnection();
190:
191: IObjectMappingKey myObj = Constants.persistMan
192: .getObjectMappingFactory().createInstance(
193: FolderDbItem.class,
194: new AutoGeneratedColumnsMapper(true));
195: dao.insert(myObj, item);
196: } finally {
197: JdbcUtil.close(dao);
198: dao = null;
199: }
200: }
201:
202: /* (non-Javadoc)
203: * @see org.claros.groupware.webmail.controllers.FolderController#countUnreadMessages(org.claros.commons.models.AuthProfile, java.lang.Long)
204: */
205: public Integer countUnreadMessages(String folder) throws Exception {
206: QueryRunner run = new QueryRunner(DbConfigList
207: .getDataSourceById("file"));
208: HashMap result = null;
209: String username = auth.getUsername();
210: try {
211: Long folderId = new Long(folder);
212: String sql = "select COUNT(*) as NUMBER from EMAIL_DB_ITEMS where user=? and FOLDER_ID = ? and unread = ?";
213: result = (HashMap) run.query(sql, new Object[] { username,
214: folderId, new Boolean(true) }, new MapHandler());
215: } catch (SQLException e) {
216: return new Integer(0);
217: }
218: if (result != null) {
219: return new Integer(result.get("number").toString());
220: }
221: return new Integer(0);
222: }
223:
224: /* (non-Javadoc)
225: * @see org.claros.groupware.webmail.controllers.FolderController#countTotalMessages(org.claros.commons.models.AuthProfile, java.lang.String)
226: */
227: public Integer countTotalMessages(String folder) throws Exception {
228: QueryRunner run = new QueryRunner(DbConfigList
229: .getDataSourceById("file"));
230: HashMap result = null;
231: String username = auth.getUsername();
232: try {
233: Long folderId = new Long(folder);
234: String sql = "select COUNT(*) as NUMBER from EMAIL_DB_ITEMS where user=? and FOLDER_ID = ? ";
235: result = (HashMap) run.query(sql, new Object[] { username,
236: folderId }, new MapHandler());
237: } catch (SQLException e) {
238: return new Integer(0);
239: }
240: if (result != null) {
241: return new Integer(result.get("number").toString());
242: }
243: return new Integer(0);
244: }
245:
246: /* (non-Javadoc)
247: * @see org.claros.groupware.webmail.controllers.FolderController#emptyFolder(org.claros.commons.models.AuthProfile, java.lang.String)
248: */
249: public void emptyFolder(String folder) throws Exception {
250: QueryRunner run = new QueryRunner(DbConfigList
251: .getDataSourceById("file"));
252: String username = auth.getUsername();
253: try {
254: Long folderId = new Long(folder);
255: String sql = "DELETE FROM EMAIL_DB_ITEMS where user=? and folder_id=?";
256: run.update(sql, new Object[] { username, folderId });
257: } catch (SQLException e) {
258: throw e;
259: }
260: }
261:
262: /* (non-Javadoc)
263: * @see org.claros.groupware.webmail.controllers.FolderController#deleteFolder(org.claros.commons.models.AuthProfile, java.lang.String)
264: */
265: public void deleteFolder(String folder) throws Exception {
266: IGenericDao dao = null;
267: try {
268: dao = Utility.getTxnDbConnection();
269: String username = auth.getUsername();
270:
271: Long folderId = new Long(folder);
272: FolderDbItem fld = getFolder(folder);
273: if (!fld.getUser().equals(auth.getUsername())) {
274: throw new NoPermissionException();
275: }
276:
277: String sql = "delete from EMAIL_DB_ITEMS where user=? and folder_id = ?";
278: // delete the emails under folder
279: dao.executeUpdate(sql, new Object[] { username, folderId });
280:
281: sql = "delete from FILTERS where user=? and DESTINATION = ?";
282: // delete the filters targeting the deleted folder
283: dao.executeUpdate(sql, new Object[] { username, folderId });
284:
285: // delete the folder
286: dao.deleteByPrimaryKey(FolderDbItem.class, folderId);
287: dao.commit();
288:
289: } catch (Exception e) {
290: // dao.rollback();
291: // throw e;
292: } finally {
293: JdbcUtil.close(dao);
294: dao = null;
295: }
296: }
297:
298: /* (non-Javadoc)
299: * @see org.claros.groupware.webmail.controllers.FolderController#getHeadersByFolder(org.claros.commons.models.AuthProfile, java.lang.String)
300: */
301: public ArrayList getHeadersByFolder(String folder) throws Exception {
302: ArrayList headers = new ArrayList();
303:
304: List mails = getMailsByFolder(folder);
305: if (mails != null) {
306: EmailDbItem item = null;
307: byte bEmail[] = null;
308: Properties props = new Properties();
309: Session session = Session.getDefaultInstance(props);
310: EmailHeader header = null;
311:
312: for (int i = 0; i < mails.size(); i++) {
313: item = (EmailDbItem) mails.get(i);
314: bEmail = item.getEmail();
315:
316: ByteArrayInputStream bis = new ByteArrayInputStream(
317: bEmail);
318: MimeMessage msg = new MimeMessage(session, bis);
319:
320: try {
321: header = new EmailHeader();
322:
323: header
324: .setMultipart((msg
325: .isMimeType("multipart/*")) ? true
326: : false);
327: header.setMessageId(item.getId().intValue());
328: header.setFrom(msg.getFrom());
329: header.setTo(msg
330: .getRecipients(Message.RecipientType.TO));
331: header.setCc(msg
332: .getRecipients(Message.RecipientType.CC));
333: header.setBcc(msg
334: .getRecipients(Message.RecipientType.BCC));
335: header.setDate(msg.getSentDate());
336: header.setReplyTo(msg.getReplyTo());
337: header.setSize(item.getSize().longValue());
338: header.setSubject(msg.getSubject());
339: header.setUnread(item.getUnread());
340:
341: // now set the human readables.
342: header.setDateShown(Formatter.formatDate(header
343: .getDate(), "dd.MM.yyyy HH:mm"));
344: header
345: .setFromShown(org.claros.commons.mail.utility.Utility
346: .addressArrToString(header
347: .getFrom()));
348: header
349: .setToShown(org.claros.commons.mail.utility.Utility
350: .addressArrToString(header.getTo()));
351: header
352: .setCcShown(org.claros.commons.mail.utility.Utility
353: .addressArrToString(header.getCc()));
354: header
355: .setSizeShown(org.claros.commons.mail.utility.Utility
356: .sizeToHumanReadable(header
357: .getSize()));
358:
359: // it is time to add it to the arraylist
360: headers.add(header);
361: } catch (MessagingException e1) {
362: log
363: .error(
364: "Could not parse headers of e-mail. Message might be defuncted or illegal formatted.",
365: e1);
366: }
367: }
368: }
369: return headers;
370: }
371:
372: /* (non-Javadoc)
373: * @see org.claros.groupware.webmail.controllers.FolderController#createDefaultFolders(org.claros.commons.models.AuthProfile)
374: */
375: public void createDefaultFolders() throws Exception {
376: if (getInboxFolder() == null) {
377: createFolder(new FolderDbItem(
378: null,
379: new Long(0),
380: auth.getUsername(),
381: org.claros.commons.mail.utility.Constants.FOLDER_INBOX,
382: Constants.FOLDER_TYPE_INBOX));
383: }
384: if (getJunkFolder() == null) {
385: createFolder(new FolderDbItem(
386: null,
387: new Long(0),
388: auth.getUsername(),
389: org.claros.commons.mail.utility.Constants.FOLDER_JUNK,
390: Constants.FOLDER_TYPE_JUNK));
391: }
392: if (getSentItems() == null) {
393: createFolder(new FolderDbItem(
394: null,
395: new Long(0),
396: auth.getUsername(),
397: org.claros.commons.mail.utility.Constants.FOLDER_SENT,
398: Constants.FOLDER_TYPE_SENT));
399: }
400: }
401:
402: /* (non-Javadoc)
403: * @see org.claros.groupware.webmail.controllers.FolderController#renameFolder(java.lang.String, java.lang.String)
404: */
405: public void renameFolder(String oldName, String newName)
406: throws Exception {
407: List folders = getFolders();
408: FolderDbItemWrapper tmp = null;
409: String n = null;
410: for (int i = 0; i < folders.size(); i++) {
411: tmp = (FolderDbItemWrapper) folders.get(i);
412: n = tmp.getFolderName();
413: if (n.equals(oldName)) {
414:
415: IGenericDao dao = null;
416: try {
417: dao = Utility.getDbConnection();
418:
419: FolderDbItem item = getFolder(tmp.getId()
420: .toString());
421: item.setFolderName(newName);
422:
423: dao.update(item);
424: } finally {
425: JdbcUtil.close(dao);
426: dao = null;
427: }
428: return;
429: }
430: }
431: throw new NoPermissionException();
432: }
433: }
|