KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > claros > chat > controllers > QueueController


1 package org.claros.chat.controllers;
2
3 import java.sql.SQLException JavaDoc;
4 import java.sql.Timestamp JavaDoc;
5 import java.util.Calendar JavaDoc;
6 import java.util.Date JavaDoc;
7 import java.util.List JavaDoc;
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 JavaDoc QUEUE_IN = "in";
24     public static final String JavaDoc QUEUE_OUT = "out";
25     
26
27     /**
28      *
29      * @param from
30      * @param to
31      * @param body
32      * @param direction
33      * @throws Exception
34      */

35     public static void push(String JavaDoc from, String JavaDoc to, String JavaDoc body, String JavaDoc direction) {
36         IGenericDao dao = null;
37         try {
38             dao = Utility.getDbConnection();
39             
40             Queue q = new Queue();
41             q.setDelivered(new Integer JavaDoc(0));
42             q.setMsgDirection(direction);
43             q.setMsgBody(body);
44             q.setMsgFrom(from);
45             q.setMsgTime(new Timestamp JavaDoc(new Date JavaDoc().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 JavaDoc e) {
53             log.error("mesage couldn't be written to db", e);
54         } finally {
55             try {
56                 JdbcUtil.close(dao);
57             } catch (Exception JavaDoc e) {
58                 log.fatal("unable to close jdbc connection", e);
59             }
60             dao = null;
61         }
62     }
63
64     /**
65      *
66      * @param from
67      * @param to
68      * @param body
69      * @param direction
70      * @throws Exception
71      */

72     public static List JavaDoc fetchUserMessages(String JavaDoc user, String JavaDoc direction) throws Exception JavaDoc {
73         user = prepareName(user);
74         IGenericDao dao = null;
75         List JavaDoc myList = null;
76         try {
77             dao = Utility.getDbConnection();
78             if (direction.equals(QUEUE_IN)) {
79                 String JavaDoc 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 JavaDoc[] {user, direction});
81             } else {
82                 String JavaDoc 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 JavaDoc[] {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     /**
101      *
102      * @param id
103      * @throws Exception
104      */

105     public static void setDelivered(Long JavaDoc id) throws Exception JavaDoc {
106         IGenericDao dao = null;
107         try {
108             dao = Utility.getDbConnection();
109
110             String JavaDoc sql = "UPDATE QUEUE SET DELIVERED = 1 WHERE ID = ?";
111             dao.executeUpdate(sql, new Object JavaDoc[] {id});
112         } finally {
113             JdbcUtil.close(dao);
114             dao = null;
115         }
116     }
117     
118     /**
119      *
120      * @param user
121      * @param direction
122      * @return
123      * @throws Exception
124      */

125     public static List JavaDoc showAllMessages() throws Exception JavaDoc {
126         IGenericDao dao = null;
127         List JavaDoc myList = null;
128         try {
129             dao = Utility.getDbConnection();
130             String JavaDoc 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     /**
140      *
141      *
142      */

143     public static void clear() {
144         QueryRunner run = new QueryRunner(DbConfigList.getDataSourceById("file"));
145         try {
146             Calendar JavaDoc cal = Calendar.getInstance();
147             cal.add(Calendar.MINUTE, -10);
148             Timestamp JavaDoc ts = new Timestamp JavaDoc(cal.getTime().getTime());
149             
150             String JavaDoc sql = "DELETE FROM QUEUE WHERE DELIVERED = 1 AND MSG_TIME < ?";
151             run.update(sql, new Object JavaDoc[] {ts});
152         } catch (SQLException JavaDoc e) {
153             log.fatal("unable to clear queue", e);
154         }
155     }
156
157     /**
158      *
159      *
160      */

161     public static void fullClear() {
162         QueryRunner run = new QueryRunner(DbConfigList.getDataSourceById("file"));
163         try {
164             String JavaDoc sql = "DELETE FROM QUEUE";
165             run.update(sql);
166         } catch (SQLException JavaDoc e) {
167             log.fatal("unable to clear queue", e);
168         }
169     }
170
171     public static String JavaDoc prepareName(String JavaDoc 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