1 43 package net.jforum.dao.generic; 44 45 import java.sql.PreparedStatement ; 46 import java.sql.ResultSet ; 47 import java.sql.Timestamp ; 48 import java.text.SimpleDateFormat ; 49 import java.util.ArrayList ; 50 import java.util.Iterator ; 51 import java.util.List ; 52 53 import net.jforum.JForumExecutionContext; 54 import net.jforum.dao.DataAccessDriver; 55 import net.jforum.entities.Post; 56 import net.jforum.util.preferences.ConfigKeys; 57 import net.jforum.util.preferences.SystemGlobals; 58 import net.jforum.util.search.SearchFacade; 59 60 65 public class GenericPostDAO extends AutoKeys implements net.jforum.dao.PostDAO 66 { 67 70 public Post selectById(int postId) throws Exception 71 { 72 PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("PostModel.selectById")); 73 p.setInt(1, postId); 74 75 ResultSet rs = p.executeQuery(); 76 77 Post post = new Post(); 78 79 if (rs.next()) { 80 post = this.makePost(rs); 81 } 82 83 rs.close(); 84 p.close(); 85 86 return post; 87 } 88 89 protected Post makePost(ResultSet rs) throws Exception 90 { 91 Post post = new Post(); 92 post.setId(rs.getInt("post_id")); 93 post.setTopicId(rs.getInt("topic_id")); 94 post.setForumId(rs.getInt("forum_id")); 95 post.setUserId(rs.getInt("user_id")); 96 Timestamp postTime = rs.getTimestamp("post_time"); 97 post.setTime(postTime); 98 post.setUserIp(rs.getString("poster_ip")); 99 post.setBbCodeEnabled(rs.getInt("enable_bbcode") > 0); 100 post.setHtmlEnabled(rs.getInt("enable_html") > 0); 101 post.setSmiliesEnabled(rs.getInt("enable_smilies") > 0); 102 post.setSignatureEnabled(rs.getInt("enable_sig") > 0); 103 post.setEditTime(rs.getTimestamp("post_edit_time")); 104 post.setEditCount(rs.getInt("post_edit_count")); 105 post.setSubject(rs.getString("post_subject")); 106 post.setText(this.getPostTextFromResultSet(rs)); 107 post.setPostUsername(rs.getString("username")); 108 post.hasAttachments(rs.getInt("attach") > 0); 109 post.setModerate(rs.getInt("need_moderate") == 1); 110 111 SimpleDateFormat df = new SimpleDateFormat (SystemGlobals.getValue(ConfigKeys.DATE_TIME_FORMAT)); 112 post.setFormatedTime(df.format(postTime)); 113 114 post.setKarma(DataAccessDriver.getInstance().newKarmaDAO().getPostKarma(post.getId())); 115 116 return post; 117 } 118 119 128 protected String getPostTextFromResultSet(ResultSet rs) throws Exception 129 { 130 return rs.getString("post_text"); 131 } 132 133 136 public void delete(Post post) throws Exception 137 { 138 List l = new ArrayList (); 139 l.add(post); 140 this.removePosts(l); 141 } 142 143 private void removePosts(List posts) throws Exception 144 { 145 PreparedStatement post = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("PostModel.deletePost")); 146 PreparedStatement text = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("PostModel.deletePostText")); 147 PreparedStatement search = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("PostModel.deleteWordMatch")); 148 149 for (Iterator iter = posts.iterator(); iter.hasNext(); ) { 150 Post p = (Post)iter.next(); 151 152 post.setInt(1, p.getId()); 153 text.setInt(1, p.getId()); 154 search.setInt(1, p.getId()); 155 156 text.executeUpdate(); 157 search.executeUpdate(); 158 post.executeUpdate(); 159 } 160 161 search.close(); 162 post.close(); 163 text.close(); 164 } 165 166 169 public void deleteByTopic(int topicId) throws Exception 170 { 171 PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("PostModel.deleteByTopic")); 172 p.setInt(1, topicId); 173 ResultSet rs = p.executeQuery(); 174 175 List posts = new ArrayList (); 176 while (rs.next()) { 177 Post post = new Post(); 178 post.setId(rs.getInt("post_id")); 179 post.setUserId(rs.getInt("user_id")); 180 181 posts.add(post); 182 } 183 184 rs.close(); 185 p.close(); 186 187 this.removePosts(posts); 188 } 189 190 193 public void update(Post post) throws Exception 194 { 195 this.updatePostsTable(post); 196 this.updatePostsTextTable(post); 197 198 SearchFacade.index(post); 199 } 200 201 protected void updatePostsTextTable(Post post) throws Exception 202 { 203 PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("PostModel.updatePostText")); 204 p.setString(1, post.getText()); 205 p.setString(2, post.getSubject()); 206 p.setInt(3, post.getId()); 207 208 p.executeUpdate(); 209 p.close(); 210 } 211 212 protected void updatePostsTable(Post post) throws Exception 213 { 214 PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("PostModel.updatePost")); 215 p.setInt(1, post.getTopicId()); 216 p.setInt(2, post.getForumId()); 217 p.setInt(3, post.isBbCodeEnabled() ? 1 : 0); 218 p.setInt(4, post.isHtmlEnabled() ? 1 : 0); 219 p.setInt(5, post.isSmiliesEnabled() ? 1 : 0); 220 p.setInt(6, post.isSignatureEnabled() ? 1 : 0); 221 p.setTimestamp(7, new Timestamp (System.currentTimeMillis())); 222 p.setString(8, post.getUserIp()); 223 p.setInt(9, post.getId()); 224 225 p.executeUpdate(); 226 p.close(); 227 } 228 229 232 public int addNew(Post post) throws Exception 233 { 234 this.addNewPost(post); 235 this.addNewPostText(post); 236 237 SearchFacade.index(post); 239 240 return post.getId(); 241 } 242 243 protected void addNewPostText(Post post) throws Exception 244 { 245 PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("PostModel.addNewPostText")); 246 p.setInt(1, post.getId()); 247 p.setString(2, post.getText()); 248 p.setString(3, post.getSubject()); 249 p.executeUpdate(); 250 p.close(); 251 } 252 253 protected void addNewPost(Post post) throws Exception 254 { 255 PreparedStatement p = this.getStatementForAutoKeys("PostModel.addNewPost"); 256 257 p.setInt(1, post.getTopicId()); 258 p.setInt(2, post.getForumId()); 259 p.setLong(3, post.getUserId()); 260 p.setTimestamp(4, new Timestamp (post.getTime().getTime())); 261 p.setString(5, post.getUserIp()); 262 p.setInt(6, post.isBbCodeEnabled() ? 1 : 0); 263 p.setInt(7, post.isHtmlEnabled() ? 1 : 0); 264 p.setInt(8, post.isSmiliesEnabled() ? 1 : 0); 265 p.setInt(9, post.isSignatureEnabled() ? 1 : 0); 266 p.setInt(10, post.isModerationNeeded() ? 1 : 0); 267 268 this.setAutoGeneratedKeysQuery(SystemGlobals.getSql("PostModel.lastGeneratedPostId")); 269 int postId = this.executeAutoKeysQuery(p); 270 post.setId(postId); 271 272 p.close(); 273 } 274 275 278 public List selectAllByTopic(int topicId) throws Exception 279 { 280 return this.selectAllByTopicByLimit(topicId, 0, Integer.MAX_VALUE - 1); 281 } 282 283 286 public List selectAllByTopicByLimit(int topicId, int startFrom, int count) throws Exception 287 { 288 List l = new ArrayList (); 289 290 PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("PostModel.selectAllByTopicByLimit")); 291 p.setInt(1, topicId); 292 p.setInt(2, startFrom); 293 p.setInt(3, count); 294 295 ResultSet rs = p.executeQuery(); 296 297 while (rs.next()) { 298 l.add(this.makePost(rs)); 299 } 300 301 rs.close(); 302 p.close(); 303 304 return l; 305 } 306 307 310 public List selectByUserByLimit(int userId,int startFrom, int count) throws Exception 311 { 312 PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement( 313 SystemGlobals.getSql("PostModel.selectByUserByLimit")); 314 315 p.setInt(1,userId); 316 p.setInt(2, startFrom); 317 p.setInt(3, count); 318 319 ResultSet rs = p.executeQuery(); 320 List l = new ArrayList (); 321 322 while (rs.next()) { 323 l.add(this.makePost(rs)); 324 } 325 326 rs.close(); 327 p.close(); 328 329 return l; 330 } 331 332 335 public int countPreviousPosts(int postId) throws Exception 336 { 337 int total = 0; 338 339 PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement( 340 SystemGlobals.getSql("PostModel.countPreviousPosts")); 341 p.setInt(1, postId); 342 p.setInt(2, postId); 343 344 ResultSet rs = p.executeQuery(); 345 346 if (rs.next()) { 347 total = rs.getInt(1); 348 } 349 350 rs.close(); 351 p.close(); 352 353 return total; 354 } 355 } 356 | Popular Tags |