| 1 package org.claros.chat.controllers; 2 3 import java.sql.SQLException ; 4 import java.sql.Timestamp ; 5 import java.util.Calendar ; 6 import java.util.Date ; 7 import java.util.List ; 8 9 import org.apache.commons.dbutils.QueryRunner; 10 import org.apache.commons.logging.Log; 11 import org.apache.commons.logging.LogFactory; 12 import org.claros.chat.models.Queue; 13 import org.claros.chat.utility.Utility; 14 import org.claros.commons.db.DbConfigList; 15 16 import com.jenkov.mrpersister.impl.mapping.AutoGeneratedColumnsMapper; 17 import com.jenkov.mrpersister.itf.IGenericDao; 18 import com.jenkov.mrpersister.itf.mapping.IObjectMappingKey; 19 import com.jenkov.mrpersister.util.JdbcUtil; 20 21 public class QueueController { 22 private static Log log = LogFactory.getLog(QueueController.class); 23 public static final String QUEUE_IN = "in"; 24 public static final String QUEUE_OUT = "out"; 25 26 27 35 public static void push(String from, String to, String body, String direction) { 36 IGenericDao dao = null; 37 try { 38 dao = Utility.getDbConnection(); 39 40 Queue q = new Queue(); 41 q.setDelivered(new Integer (0)); 42 q.setMsgDirection(direction); 43 q.setMsgBody(body); 44 q.setMsgFrom(from); 45 q.setMsgTime(new Timestamp (new Date ().getTime())); 46 q.setMsgTo(to); 47 48 log.debug("new message " + direction + " from: " + from + " to: " + to + " body: " + body); 49 50 IObjectMappingKey myObj = Utility.persistMan.getObjectMappingFactory().createInstance(Queue.class, new AutoGeneratedColumnsMapper(true)); 51 dao.insert(myObj, q); 52 } catch (Exception e) { 53 log.error("mesage couldn't be written to db", e); 54 } finally { 55 try { 56 JdbcUtil.close(dao); 57 } catch (Exception e) { 58 log.fatal("unable to close jdbc connection", e); 59 } 60 dao = null; 61 } 62 } 63 64 72 public static List fetchUserMessages(String user, String direction) throws Exception { 73 user = prepareName(user); 74 IGenericDao dao = null; 75 List myList = null; 76 try { 77 dao = Utility.getDbConnection(); 78 if (direction.equals(QUEUE_IN)) { 79 String sql1 = "SELECT * FROM QUEUE WHERE MSG_TO = ? AND MSG_DIRECTION = ? AND DELIVERED = 0 ORDER BY MSG_TIME ASC"; 80 myList = dao.readList(Queue.class, sql1, new Object [] {user, direction}); 81 } else { 82 String sql2 = "SELECT * FROM QUEUE WHERE MSG_FROM = ? AND MSG_DIRECTION = ? AND DELIVERED = 0 ORDER BY MSG_TIME ASC"; 83 myList = dao.readList(Queue.class, sql2, new Object [] {user, direction}); 84 } 85 86 if (myList != null) { 87 Queue q = null; 88 for (int i=0; i <myList.size(); i++) { 89 q = (Queue)myList.get(i); 90 setDelivered(q.getId()); 91 } 92 } 93 } finally { 94 JdbcUtil.close(dao); 95 dao = null; 96 } 97 return myList; 98 } 99 100 105 public static void setDelivered(Long id) throws Exception { 106 IGenericDao dao = null; 107 try { 108 dao = Utility.getDbConnection(); 109 110 String sql = "UPDATE QUEUE SET DELIVERED = 1 WHERE ID = ?"; 111 dao.executeUpdate(sql, new Object [] {id}); 112 } finally { 113 JdbcUtil.close(dao); 114 dao = null; 115 } 116 } 117 118 125 public static List showAllMessages() throws Exception { 126 IGenericDao dao = null; 127 List myList = null; 128 try { 129 dao = Utility.getDbConnection(); 130 String sql = "SELECT * FROM QUEUE ORDER BY MSG_TIME ASC"; 131 myList = dao.readList(Queue.class, sql); 132 } finally { 133 JdbcUtil.close(dao); 134 dao = null; 135 } 136 return myList; 137 } 138 139 143 public static void clear() { 144 QueryRunner run = new QueryRunner(DbConfigList.getDataSourceById("file")); 145 try { 146 Calendar cal = Calendar.getInstance(); 147 cal.add(Calendar.MINUTE, -10); 148 Timestamp ts = new Timestamp (cal.getTime().getTime()); 149 150 String sql = "DELETE FROM QUEUE WHERE DELIVERED = 1 AND MSG_TIME < ?"; 151 run.update(sql, new Object [] {ts}); 152 } catch (SQLException e) { 153 log.fatal("unable to clear queue", e); 154 } 155 } 156 157 161 public static void fullClear() { 162 QueryRunner run = new QueryRunner(DbConfigList.getDataSourceById("file")); 163 try { 164 String sql = "DELETE FROM QUEUE"; 165 run.update(sql); 166 } catch (SQLException e) { 167 log.fatal("unable to clear queue", e); 168 } 169 } 170 171 public static String prepareName(String user) { 172 if (user.indexOf("/") > -1) { 173 user = user.substring(0, user.indexOf("/")); 174 } 175 176 if (user.indexOf("@") < 0) { 177 user = user + "@gmail.com"; 178 } 179 return user; 180 } 181 } 182 | Popular Tags |