001: package org.claros.chat.controllers;
002:
003: import java.sql.SQLException;
004: import java.sql.Timestamp;
005: import java.util.Calendar;
006: import java.util.Date;
007: import java.util.List;
008:
009: import org.apache.commons.dbutils.QueryRunner;
010: import org.apache.commons.logging.Log;
011: import org.apache.commons.logging.LogFactory;
012: import org.claros.chat.models.Queue;
013: import org.claros.chat.utility.Utility;
014: import org.claros.commons.db.DbConfigList;
015:
016: import com.jenkov.mrpersister.impl.mapping.AutoGeneratedColumnsMapper;
017: import com.jenkov.mrpersister.itf.IGenericDao;
018: import com.jenkov.mrpersister.itf.mapping.IObjectMappingKey;
019: import com.jenkov.mrpersister.util.JdbcUtil;
020:
021: public class QueueController {
022: private static Log log = LogFactory.getLog(QueueController.class);
023: public static final String QUEUE_IN = "in";
024: public static final String QUEUE_OUT = "out";
025:
026: /**
027: *
028: * @param from
029: * @param to
030: * @param body
031: * @param direction
032: * @throws Exception
033: */
034: @SuppressWarnings("deprecation")
035: public static void push(String from, String to, String body,
036: String direction) {
037: IGenericDao dao = null;
038: try {
039: dao = Utility.getDbConnection();
040:
041: Queue q = new Queue();
042: q.setDelivered(new Integer(0));
043: q.setMsgDirection(direction);
044: q.setMsgBody(body.trim());
045: q.setMsgFrom(from);
046: q.setMsgTime(new Timestamp(new Date().getTime()));
047: q.setMsgTo(to);
048:
049: log.debug("new message " + direction + " from: " + from
050: + " to: " + to + " body: " + body);
051:
052: IObjectMappingKey myObj = Utility.persistMan
053: .getObjectMappingFactory().createInstance(
054: Queue.class,
055: new AutoGeneratedColumnsMapper(true));
056: dao.insert(myObj, q);
057: } catch (Exception e) {
058: log.error("mesage couldn't be written to db", e);
059: } finally {
060: try {
061: JdbcUtil.close(dao);
062: } catch (Exception e) {
063: log.fatal("unable to close jdbc connection", e);
064: }
065: dao = null;
066: }
067: }
068:
069: /**
070: *
071: * @param from
072: * @param to
073: * @param body
074: * @param direction
075: * @throws Exception
076: */
077: public static List fetchUserMessages(String user, String direction,
078: String defaultDomain) throws Exception {
079: user = prepareName(user, defaultDomain);
080: IGenericDao dao = null;
081: List myList = null;
082: try {
083: dao = Utility.getDbConnection();
084: if (direction.equals(QUEUE_IN)) {
085: String sql1 = "SELECT * FROM QUEUE WHERE MSG_TO = ? AND MSG_DIRECTION = ? AND DELIVERED = 0 ORDER BY MSG_TIME ASC";
086: myList = dao.readList(Queue.class, sql1, new Object[] {
087: user, direction });
088: } else {
089: String sql2 = "SELECT * FROM QUEUE WHERE MSG_FROM = ? AND MSG_DIRECTION = ? AND DELIVERED = 0 ORDER BY MSG_TIME ASC";
090: myList = dao.readList(Queue.class, sql2, new Object[] {
091: user, direction });
092:
093: if (myList != null) {
094: Queue q = null;
095: for (int i = 0; i < myList.size(); i++) {
096: q = (Queue) myList.get(i);
097: setDelivered(q.getId());
098: }
099: }
100:
101: }
102: } finally {
103: JdbcUtil.close(dao);
104: dao = null;
105: }
106: return myList;
107: }
108:
109: /**
110: *
111: * @param id
112: * @throws Exception
113: */
114: public static void setDelivered(String user, Long id,
115: String defaultDomain) throws Exception {
116: user = prepareName(user, defaultDomain);
117: IGenericDao dao = null;
118: try {
119: dao = Utility.getDbConnection();
120:
121: String sql = "UPDATE QUEUE SET DELIVERED = 1 WHERE MSG_TO = ? AND ID = ?";
122: dao.executeUpdate(sql, new Object[] { user, id });
123: } finally {
124: JdbcUtil.close(dao);
125: dao = null;
126: }
127: }
128:
129: /**
130: *
131: * @param id
132: * @throws Exception
133: */
134: public static void setDelivered(Long id) throws Exception {
135: IGenericDao dao = null;
136: try {
137: dao = Utility.getDbConnection();
138:
139: String sql = "UPDATE QUEUE SET DELIVERED = 1 WHERE ID = ?";
140: dao.executeUpdate(sql, new Object[] { id });
141: } finally {
142: JdbcUtil.close(dao);
143: dao = null;
144: }
145: }
146:
147: /**
148: *
149: * @param user
150: * @param direction
151: * @return
152: * @throws Exception
153: */
154: public static List showAllMessages() throws Exception {
155: IGenericDao dao = null;
156: List myList = null;
157: try {
158: dao = Utility.getDbConnection();
159: String sql = "SELECT * FROM QUEUE ORDER BY MSG_TIME ASC";
160: myList = dao.readList(Queue.class, sql);
161: } finally {
162: JdbcUtil.close(dao);
163: dao = null;
164: }
165: return myList;
166: }
167:
168: /**
169: *
170: *
171: */
172: public static void clear() {
173: QueryRunner run = new QueryRunner(DbConfigList
174: .getDataSourceById("file"));
175: try {
176: Calendar cal = Calendar.getInstance();
177: cal.add(Calendar.MINUTE, -10);
178: Timestamp ts = new Timestamp(cal.getTime().getTime());
179:
180: String sql = "DELETE FROM QUEUE WHERE DELIVERED = 1 AND MSG_TIME < ?";
181: run.update(sql, new Object[] { ts });
182: } catch (SQLException e) {
183: log.fatal("unable to clear queue", e);
184: }
185: }
186:
187: /**
188: *
189: *
190: */
191: public static void fullClear() {
192: QueryRunner run = new QueryRunner(DbConfigList
193: .getDataSourceById("file"));
194: try {
195: String sql = "DELETE FROM QUEUE";
196: run.update(sql);
197: } catch (SQLException e) {
198: log.fatal("unable to clear queue", e);
199: }
200: }
201:
202: /**
203: *
204: * @param user
205: * @param isGmail
206: * @param defaultDomain
207: * @return
208: */
209: public static String prepareName(String user, String defaultDomain) {
210: if (user.indexOf("/") > -1) {
211: user = user.substring(0, user.indexOf("/"));
212: }
213:
214: if (user.indexOf("@") < 0) {
215: if (defaultDomain != null) {
216: user = user + "@" + defaultDomain;
217: }
218: }
219: return user;
220: }
221: }
|