Source Code Cross Referenced for GenericKarmaDAO.java in  » Forum » JForum-2.1.8 » net » jforum » dao » generic » 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 » Forum » JForum 2.1.8 » net.jforum.dao.generic 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright (c) JForum Team
003:         * All rights reserved.
004:         * 
005:         * Redistribution and use in source and binary forms, 
006:         * with or without modification, are permitted provided 
007:         * that the following conditions are met:
008:         * 
009:         * 1) Redistributions of source code must retain the above 
010:         * copyright notice, this list of conditions and the 
011:         * following  disclaimer.
012:         * 2)  Redistributions in binary form must reproduce the 
013:         * above copyright notice, this list of conditions and 
014:         * the following disclaimer in the documentation and/or 
015:         * other materials provided with the distribution.
016:         * 3) Neither the name of "Rafael Steil" nor 
017:         * the names of its contributors may be used to endorse 
018:         * or promote products derived from this software without 
019:         * specific prior written permission.
020:         * 
021:         * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT 
022:         * HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 
023:         * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, 
024:         * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 
025:         * MERCHANTABILITY AND FITNESS FOR A PARTICULAR 
026:         * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 
027:         * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE 
028:         * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
029:         * EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
030:         * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
031:         * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, 
032:         * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
033:         * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER 
034:         * IN CONTRACT, STRICT LIABILITY, OR TORT 
035:         * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
036:         * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 
037:         * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
038:         * 
039:         * Created on Jan 11, 2005 11:22:19 PM
040:         * The JForum Project
041:         * http://www.jforum.net
042:         */
043:        package net.jforum.dao.generic;
044:
045:        import java.sql.PreparedStatement;
046:        import java.sql.ResultSet;
047:        import java.sql.SQLException;
048:        import java.sql.Timestamp;
049:        import java.util.ArrayList;
050:        import java.util.Date;
051:        import java.util.HashMap;
052:        import java.util.List;
053:        import java.util.Map;
054:
055:        import net.jforum.JForumExecutionContext;
056:        import net.jforum.entities.Karma;
057:        import net.jforum.entities.KarmaStatus;
058:        import net.jforum.entities.User;
059:        import net.jforum.exceptions.DatabaseException;
060:        import net.jforum.util.DbUtils;
061:        import net.jforum.util.preferences.SystemGlobals;
062:
063:        /**
064:         * @author Rafael Steil
065:         * @version $Id: GenericKarmaDAO.java,v 1.12 2007/08/01 22:30:03 rafaelsteil Exp $
066:         */
067:        public class GenericKarmaDAO implements  net.jforum.dao.KarmaDAO {
068:            /**
069:             * @see net.jforum.dao.KarmaDAO#addKarma(net.jforum.entities.Karma)
070:             */
071:            public void addKarma(Karma karma) {
072:                PreparedStatement p = null;
073:                try {
074:                    p = JForumExecutionContext.getConnection()
075:                            .prepareStatement(
076:                                    SystemGlobals.getSql("KarmaModel.add"));
077:                    p.setInt(1, karma.getPostId());
078:                    p.setInt(2, karma.getPostUserId());
079:                    p.setInt(3, karma.getFromUserId());
080:                    p.setInt(4, karma.getPoints());
081:                    p.setInt(5, karma.getTopicId());
082:                    p.setTimestamp(6, new Timestamp((new Date()).getTime()));
083:                    p.executeUpdate();
084:
085:                    this .updateUserKarma(karma.getPostUserId());
086:                } catch (SQLException e) {
087:                    throw new DatabaseException(e);
088:                } finally {
089:                    DbUtils.close(p);
090:                }
091:            }
092:
093:            /**
094:             * @see net.jforum.dao.KarmaDAO#selectKarmaStatus(int)
095:             */
096:            public KarmaStatus getUserKarma(int userId) {
097:                KarmaStatus status = new KarmaStatus();
098:
099:                PreparedStatement p = null;
100:                ResultSet rs = null;
101:                try {
102:                    p = JForumExecutionContext.getConnection()
103:                            .prepareStatement(
104:                                    SystemGlobals
105:                                            .getSql("KarmaModel.getUserKarma"));
106:                    p.setInt(1, userId);
107:
108:                    rs = p.executeQuery();
109:                    if (rs.next()) {
110:                        status.setKarmaPoints(Math.round(rs
111:                                .getDouble("user_karma")));
112:                    }
113:
114:                    return status;
115:                } catch (SQLException e) {
116:                    throw new DatabaseException(e);
117:                } finally {
118:                    DbUtils.close(rs, p);
119:                }
120:            }
121:
122:            /**
123:             * @see net.jforum.dao.KarmaDAO#updateUserKarma(int)
124:             */
125:            public void updateUserKarma(int userId) {
126:                PreparedStatement p = null;
127:                ResultSet rs = null;
128:                try {
129:                    p = JForumExecutionContext
130:                            .getConnection()
131:                            .prepareStatement(
132:                                    SystemGlobals
133:                                            .getSql("KarmaModel.getUserKarmaPoints"));
134:                    p.setInt(1, userId);
135:
136:                    int totalRecords = 0;
137:                    double totalPoints = 0;
138:                    rs = p.executeQuery();
139:
140:                    while (rs.next()) {
141:                        int points = rs.getInt("points");
142:                        int votes = rs.getInt("votes");
143:
144:                        totalPoints += ((double) points / votes);
145:                        totalRecords++;
146:                    }
147:
148:                    rs.close();
149:                    rs = null;
150:                    p.close();
151:                    p = null;
152:
153:                    p = JForumExecutionContext
154:                            .getConnection()
155:                            .prepareStatement(
156:                                    SystemGlobals
157:                                            .getSql("KarmaModel.updateUserKarma"));
158:
159:                    double karmaPoints = totalPoints / totalRecords;
160:
161:                    if (Double.isNaN(karmaPoints)) {
162:                        karmaPoints = 0;
163:                    }
164:
165:                    p.setDouble(1, karmaPoints);
166:                    p.setInt(2, userId);
167:                    p.executeUpdate();
168:                } catch (SQLException e) {
169:                    throw new DatabaseException(e);
170:                } finally {
171:                    DbUtils.close(rs, p);
172:                }
173:            }
174:
175:            /**
176:             * @see net.jforum.dao.KarmaDAO#update(net.jforum.entities.Karma)
177:             */
178:            public void update(Karma karma) {
179:                PreparedStatement p = null;
180:                try {
181:                    p = JForumExecutionContext.getConnection()
182:                            .prepareStatement(
183:                                    SystemGlobals.getSql("KarmaModel.update"));
184:                    p.setInt(1, karma.getPoints());
185:                    p.setInt(2, karma.getId());
186:                    p.executeUpdate();
187:                } catch (SQLException e) {
188:                    throw new DatabaseException(e);
189:                } finally {
190:                    DbUtils.close(p);
191:                }
192:            }
193:
194:            /**
195:             * @see net.jforum.dao.KarmaDAO#getPostKarma(int)
196:             */
197:            public KarmaStatus getPostKarma(int postId) {
198:                KarmaStatus karma = new KarmaStatus();
199:
200:                PreparedStatement p = null;
201:                ResultSet rs = null;
202:                try {
203:                    p = JForumExecutionContext.getConnection()
204:                            .prepareStatement(
205:                                    SystemGlobals
206:                                            .getSql("KarmaModel.getPostKarma"));
207:                    p.setInt(1, postId);
208:
209:                    rs = p.executeQuery();
210:                    if (rs.next()) {
211:                        karma.setKarmaPoints(Math.round(rs.getDouble(1)));
212:                    }
213:
214:                    return karma;
215:                } catch (SQLException e) {
216:                    throw new DatabaseException(e);
217:                } finally {
218:                    DbUtils.close(rs, p);
219:                }
220:            }
221:
222:            /**
223:             * @see net.jforum.dao.KarmaDAO#userCanAddKarma(int, int)
224:             */
225:            public boolean userCanAddKarma(int userId, int postId) {
226:                boolean status = true;
227:
228:                PreparedStatement p = null;
229:                ResultSet rs = null;
230:                try {
231:                    p = JForumExecutionContext
232:                            .getConnection()
233:                            .prepareStatement(
234:                                    SystemGlobals
235:                                            .getSql("KarmaModel.userCanAddKarma"));
236:                    p.setInt(1, postId);
237:                    p.setInt(2, userId);
238:
239:                    rs = p.executeQuery();
240:                    if (rs.next()) {
241:                        status = rs.getInt(1) < 1;
242:                    }
243:
244:                    return status;
245:                } catch (SQLException e) {
246:                    throw new DatabaseException(e);
247:                } finally {
248:                    DbUtils.close(rs, p);
249:                }
250:            }
251:
252:            /**
253:             * @see net.jforum.dao.KarmaDAO#getUserVotes(int, int)
254:             */
255:            public Map getUserVotes(int topicId, int userId) {
256:                Map m = new HashMap();
257:
258:                PreparedStatement p = null;
259:                ResultSet rs = null;
260:                try {
261:                    p = JForumExecutionContext.getConnection()
262:                            .prepareStatement(
263:                                    SystemGlobals
264:                                            .getSql("KarmaModel.getUserVotes"));
265:                    p.setInt(1, topicId);
266:                    p.setInt(2, userId);
267:
268:                    rs = p.executeQuery();
269:                    while (rs.next()) {
270:                        m.put(new Integer(rs.getInt("post_id")), new Integer(rs
271:                                .getInt("points")));
272:                    }
273:
274:                    return m;
275:                } catch (SQLException e) {
276:                    throw new DatabaseException(e);
277:                } finally {
278:                    DbUtils.close(rs, p);
279:                }
280:            }
281:
282:            public void getUserTotalKarma(User user) {
283:                PreparedStatement p = null;
284:                ResultSet rs = null;
285:                try {
286:                    p = JForumExecutionContext
287:                            .getConnection()
288:                            .prepareStatement(
289:                                    SystemGlobals
290:                                            .getSql("KarmaModel.getUserTotalVotes"));
291:                    p.setInt(1, user.getId());
292:
293:                    rs = p.executeQuery();
294:
295:                    user.setKarma(new KarmaStatus());
296:
297:                    if (rs.next()) {
298:                        user.getKarma().setTotalPoints(rs.getInt("points"));
299:                        user.getKarma().setVotesReceived(rs.getInt("votes"));
300:                    }
301:
302:                    if (user.getKarma().getVotesReceived() != 0)// prevetns division by
303:                        // zero.
304:                        user.getKarma().setKarmaPoints(
305:                                user.getKarma().getTotalPoints()
306:                                        / user.getKarma().getVotesReceived());
307:
308:                    this .getVotesGiven(user);
309:                } catch (SQLException e) {
310:                    throw new DatabaseException(e);
311:                } finally {
312:                    DbUtils.close(rs, p);
313:                }
314:            }
315:
316:            private void getVotesGiven(User user) {
317:                PreparedStatement p = null;
318:                ResultSet rs = null;
319:                try {
320:                    p = JForumExecutionContext
321:                            .getConnection()
322:                            .prepareStatement(
323:                                    SystemGlobals
324:                                            .getSql("KarmaModel.getUserGivenVotes"));
325:                    p.setInt(1, user.getId());
326:
327:                    rs = p.executeQuery();
328:
329:                    if (rs.next()) {
330:                        user.getKarma().setVotesGiven(rs.getInt("votes"));
331:                    }
332:                } catch (SQLException e) {
333:                    throw new DatabaseException(e);
334:                } finally {
335:                    DbUtils.close(rs, p);
336:                }
337:            }
338:
339:            /**
340:             * @see net.jforum.dao.KarmaDAO#getMostRatedUserByPeriod(int, java.util.Date, java.util.Date,
341:             *      String)
342:             */
343:            public List getMostRatedUserByPeriod(int start, Date firstPeriod,
344:                    Date lastPeriod, String orderField) {
345:                String sql = SystemGlobals
346:                        .getSql("KarmaModel.getMostRatedUserByPeriod");
347:                sql += " ORDER BY " + orderField + " DESC";
348:
349:                return this .getMostRatedUserByPeriod(sql, firstPeriod,
350:                        lastPeriod);
351:            }
352:
353:            /**
354:             * 
355:             * @param sql String
356:             * @param firstPeriod Date
357:             * @param lastPeriod Date
358:             * @return List
359:             */
360:            protected List getMostRatedUserByPeriod(String sql,
361:                    Date firstPeriod, Date lastPeriod) {
362:                if (firstPeriod.after(lastPeriod)) {
363:                    throw new DatabaseException(
364:                            "First Date needs to be before the Last Date");
365:                }
366:
367:                PreparedStatement p = null;
368:                ResultSet rs = null;
369:
370:                try {
371:                    p = JForumExecutionContext.getConnection()
372:                            .prepareStatement(sql);
373:                    p.setTimestamp(1, new Timestamp(firstPeriod.getTime()));
374:                    p.setTimestamp(2, new Timestamp(lastPeriod.getTime()));
375:
376:                    rs = p.executeQuery();
377:                    return this .fillUser(rs);
378:                } catch (SQLException e) {
379:                    throw new DatabaseException(e);
380:                } finally {
381:                    DbUtils.close(rs, p);
382:                }
383:            }
384:
385:            protected List fillUser(ResultSet rs) throws SQLException {
386:                List usersAndPoints = new ArrayList();
387:                KarmaStatus karma = null;
388:                while (rs.next()) {
389:                    User user = new User();
390:                    karma = new KarmaStatus();
391:                    karma.setTotalPoints(rs.getInt("total"));
392:                    karma.setVotesReceived(rs.getInt("votes_received"));
393:                    karma.setKarmaPoints(rs.getDouble("user_karma"));
394:                    karma.setVotesGiven(rs.getInt("votes_given"));
395:                    user.setUsername(rs.getString("username"));
396:                    user.setId(rs.getInt("user_id"));
397:                    user.setKarma(karma);
398:                    usersAndPoints.add(user);
399:                }
400:                return usersAndPoints;
401:            }
402:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.