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 30, 2005 11:38:30 AM
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.Statement;
049: import java.sql.Timestamp;
050: import java.util.ArrayList;
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.dao.ModerationDAO;
057: import net.jforum.entities.ModerationPendingInfo;
058: import net.jforum.entities.Post;
059: import net.jforum.entities.TopicModerationInfo;
060: import net.jforum.exceptions.DatabaseException;
061: import net.jforum.util.DbUtils;
062: import net.jforum.util.preferences.SystemGlobals;
063:
064: /**
065: * @author Rafael Steil
066: * @version $Id: GenericModerationDAO.java,v 1.9 2007/07/30 16:31:29 rafaelsteil Exp $
067: */
068: public class GenericModerationDAO implements ModerationDAO {
069: /**
070: * @see net.jforum.dao.ModerationDAO#aprovePost(int)
071: */
072: public void aprovePost(int postId) {
073: PreparedStatement p = null;
074: try {
075: p = JForumExecutionContext
076: .getConnection()
077: .prepareStatement(
078: SystemGlobals
079: .getSql("ModerationModel.aprovePost"));
080: p
081: .setTimestamp(1, new Timestamp(System
082: .currentTimeMillis()));
083: p.setInt(2, postId);
084: p.executeUpdate();
085: } catch (SQLException e) {
086: throw new DatabaseException(e);
087: } finally {
088: DbUtils.close(p);
089: }
090: }
091:
092: /**
093: * @see net.jforum.dao.ModerationDAO#topicsByForum(int)
094: */
095: public Map topicsByForum(int forumId) {
096: Map m = new HashMap();
097:
098: PreparedStatement p = null;
099: ResultSet rs = null;
100:
101: try {
102: p = JForumExecutionContext
103: .getConnection()
104: .prepareStatement(
105: SystemGlobals
106: .getSql("ModerationModel.topicsByForum"));
107: p.setInt(1, forumId);
108:
109: int lastId = 0;
110: TopicModerationInfo info = null;
111:
112: rs = p.executeQuery();
113:
114: while (rs.next()) {
115: int id = rs.getInt("topic_id");
116:
117: if (id != lastId) {
118: lastId = id;
119:
120: if (info != null) {
121: m.put(new Integer(info.getTopicId()), info);
122: }
123:
124: info = new TopicModerationInfo();
125: info.setTopicId(id);
126: info.setTopicReplies(rs.getInt("topic_replies"));
127: info.setTopicTitle(rs.getString("topic_title"));
128: }
129:
130: if (info != null) {
131: info.addPost(this .getPost(rs));
132: }
133: }
134:
135: if (info != null) {
136: m.put(new Integer(info.getTopicId()), info);
137: }
138:
139: return m;
140: } catch (SQLException e) {
141: throw new DatabaseException(e);
142: } finally {
143: DbUtils.close(rs, p);
144: }
145: }
146:
147: protected Post getPost(ResultSet rs) throws SQLException {
148: Post p = new Post();
149:
150: p.setPostUsername(rs.getString("username"));
151: p.setId(rs.getInt("post_id"));
152: p.setUserId(rs.getInt("user_id"));
153: p.setBbCodeEnabled(rs.getInt("enable_bbcode") == 1);
154: p.setHtmlEnabled(rs.getInt("enable_html") == 1);
155: p.setSmiliesEnabled(rs.getInt("enable_smilies") == 1);
156: p.setSubject(rs.getString("post_subject"));
157: p.setText(this .getPostTextFromResultSet(rs));
158:
159: return p;
160: }
161:
162: protected String getPostTextFromResultSet(ResultSet rs)
163: throws SQLException {
164: return rs.getString("post_text");
165: }
166:
167: /**
168: * @see net.jforum.dao.ModerationDAO#categoryPendingModeration()
169: */
170: public List categoryPendingModeration() {
171: List l = new ArrayList();
172: int lastId = 0;
173: ModerationPendingInfo info = null;
174: Statement s = null;
175: ResultSet rs = null;
176:
177: try {
178: s = JForumExecutionContext.getConnection()
179: .createStatement();
180: rs = s
181: .executeQuery(SystemGlobals
182: .getSql("ModerationModel.categoryPendingModeration"));
183:
184: while (rs.next()) {
185: int id = rs.getInt("categories_id");
186:
187: if (id != lastId) {
188: lastId = id;
189:
190: if (info != null) {
191: l.add(info);
192: }
193:
194: info = new ModerationPendingInfo();
195: info.setCategoryName(rs.getString("title"));
196: info.setCategoryId(id);
197: }
198:
199: if (info != null) {
200: info.addInfo(rs.getString("forum_name"), rs
201: .getInt("forum_id"), rs.getInt("total"));
202: }
203: }
204:
205: if (info != null) {
206: l.add(info);
207: }
208:
209: return l;
210: } catch (SQLException e) {
211: throw new DatabaseException(e);
212: } finally {
213: DbUtils.close(rs, s);
214: }
215: }
216: }
|