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