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);
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: throws Exception {
078: user = prepareName(user);
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:
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: } finally {
101: JdbcUtil.close(dao);
102: dao = null;
103: }
104: return myList;
105: }
106:
107: /**
108: *
109: * @param id
110: * @throws Exception
111: */
112: public static void setDelivered(Long id) throws Exception {
113: IGenericDao dao = null;
114: try {
115: dao = Utility.getDbConnection();
116:
117: String sql = "UPDATE QUEUE SET DELIVERED = 1 WHERE ID = ?";
118: dao.executeUpdate(sql, new Object[] { id });
119: } finally {
120: JdbcUtil.close(dao);
121: dao = null;
122: }
123: }
124:
125: /**
126: *
127: * @param user
128: * @param direction
129: * @return
130: * @throws Exception
131: */
132: public static List showAllMessages() throws Exception {
133: IGenericDao dao = null;
134: List myList = null;
135: try {
136: dao = Utility.getDbConnection();
137: String sql = "SELECT * FROM QUEUE ORDER BY MSG_TIME ASC";
138: myList = dao.readList(Queue.class, sql);
139: } finally {
140: JdbcUtil.close(dao);
141: dao = null;
142: }
143: return myList;
144: }
145:
146: /**
147: *
148: *
149: */
150: public static void clear() {
151: QueryRunner run = new QueryRunner(DbConfigList
152: .getDataSourceById("file"));
153: try {
154: Calendar cal = Calendar.getInstance();
155: cal.add(Calendar.MINUTE, -10);
156: Timestamp ts = new Timestamp(cal.getTime().getTime());
157:
158: String sql = "DELETE FROM QUEUE WHERE DELIVERED = 1 AND MSG_TIME < ?";
159: run.update(sql, new Object[] { ts });
160: } catch (SQLException e) {
161: log.fatal("unable to clear queue", e);
162: }
163: }
164:
165: /**
166: *
167: *
168: */
169: public static void fullClear() {
170: QueryRunner run = new QueryRunner(DbConfigList
171: .getDataSourceById("file"));
172: try {
173: String sql = "DELETE FROM QUEUE";
174: run.update(sql);
175: } catch (SQLException e) {
176: log.fatal("unable to clear queue", e);
177: }
178: }
179:
180: public static String prepareName(String user) {
181: if (user.indexOf("/") > -1) {
182: user = user.substring(0, user.indexOf("/"));
183: }
184:
185: if (user.indexOf("@") < 0) {
186: user = user + "@gmail.com";
187: }
188: return user;
189: }
190: }
|