Source Code Cross Referenced for QueueController.java in  » Web-Mail » claros-intouch2-2.2-beta » org » claros » chat » controllers » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Web Mail » claros intouch2 2.2 beta » org.claros.chat.controllers 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.