GenericLuceneDAO.java
Upload User: gdxydsw
Upload Date: 2019-01-29
Package Size: 16721k
Code Size: 6k
Category:

Java Develop

Development Platform:

Java

  1. /*
  2.  * Copyright (c) JForum Team
  3.  * All rights reserved.
  4.  * 
  5.  * Redistribution and use in source and binary forms, 
  6.  * with or without modification, are permitted provided 
  7.  * that the following conditions are met:
  8.  * 
  9.  * 1) Redistributions of source code must retain the above 
  10.  * copyright notice, this list of conditions and the 
  11.  * following  disclaimer.
  12.  * 2)  Redistributions in binary form must reproduce the 
  13.  * above copyright notice, this list of conditions and 
  14.  * the following disclaimer in the documentation and/or 
  15.  * other materials provided with the distribution.
  16.  * 3) Neither the name of "Rafael Steil" nor 
  17.  * the names of its contributors may be used to endorse 
  18.  * or promote products derived from this software without 
  19.  * specific prior written permission.
  20.  * 
  21.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT 
  22.  * HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 
  23.  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, 
  24.  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 
  25.  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR 
  26.  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 
  27.  * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE 
  28.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
  29.  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
  30.  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
  31.  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, 
  32.  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
  33.  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER 
  34.  * IN CONTRACT, STRICT LIABILITY, OR TORT 
  35.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
  36.  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 
  37.  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
  38.  * 
  39.  * Created on 24/07/2007 10:27:23
  40.  * 
  41.  * The JForum Project
  42.  * http://www.jforum.net
  43.  */
  44. package net.jforum.dao.generic;
  45. import java.sql.PreparedStatement;
  46. import java.sql.ResultSet;
  47. import java.sql.SQLException;
  48. import java.sql.Timestamp;
  49. import java.util.ArrayList;
  50. import java.util.Date;
  51. import java.util.List;
  52. import net.jforum.JForumExecutionContext;
  53. import net.jforum.dao.LuceneDAO;
  54. import net.jforum.entities.Post;
  55. import net.jforum.exceptions.DatabaseException;
  56. import net.jforum.search.LuceneReindexArgs;
  57. import net.jforum.search.SearchPost;
  58. import net.jforum.util.DbUtils;
  59. import net.jforum.util.preferences.SystemGlobals;
  60. /**
  61.  * @author Rafael Steil
  62.  * @version $Id: GenericLuceneDAO.java,v 1.12 2007/10/13 13:46:21 rafaelsteil Exp $
  63.  */
  64. public class GenericLuceneDAO implements LuceneDAO
  65. {
  66. /**
  67.  * @see net.jforum.dao.LuceneDAO#getPostsToIndex(LuceneReindexArgs, int, int)
  68.  */
  69. public List getPostsToIndex(int fromPostId, int toPostId)
  70. {
  71. List l = new ArrayList();
  72. PreparedStatement p = null;
  73. ResultSet rs = null;
  74. try {
  75. p = JForumExecutionContext.getConnection().prepareStatement(
  76. SystemGlobals.getSql("SearchModel.getPostsToIndexForLucene"));
  77. p.setInt(1, fromPostId);
  78. p.setInt(2, toPostId);
  79. rs = p.executeQuery();
  80. while (rs.next()) {
  81. l.add(this.makePost(rs));
  82. }
  83. }
  84. catch (SQLException e) {
  85. throw new DatabaseException(e);
  86. }
  87. finally {
  88. DbUtils.close(rs, p);
  89. }
  90. return l;
  91. }
  92. /**
  93.  * @see net.jforum.dao.LuceneDAO#firstPostIdByDate(java.util.Date)
  94.  */
  95. public int firstPostIdByDate(Date date) 
  96. {
  97. return this.getPostIdByDate(date, SystemGlobals.getSql("SearchModel.firstPostIdByDate"));
  98. }
  99. /**
  100.  * @see net.jforum.dao.LuceneDAO#lastPostIdByDate(java.util.Date)
  101.  */
  102. public int lastPostIdByDate(Date date) 
  103. {
  104. return this.getPostIdByDate(date, SystemGlobals.getSql("SearchModel.lastPostIdByDate"));
  105. }
  106. private int getPostIdByDate(Date date, String query)
  107. {
  108. int postId = 0;
  109. PreparedStatement p = null;
  110. ResultSet rs = null;
  111. try {
  112. p = JForumExecutionContext.getConnection().prepareStatement(query);
  113. p.setTimestamp(1, new Timestamp(date.getTime()));
  114. rs = p.executeQuery();
  115. if (rs.next()) {
  116. postId = rs.getInt(1);
  117. }
  118. }
  119. catch (SQLException e) {
  120. throw new DatabaseException(e);
  121. }
  122. finally {
  123. DbUtils.close(rs, p);
  124. }
  125. return postId;
  126. }
  127. /**
  128.  * @see net.jforum.dao.LuceneDAO#getPostsData(int[])
  129.  */
  130. public List getPostsData(int[] postIds)
  131. {
  132. if (postIds.length == 0) {
  133. return new ArrayList();
  134. }
  135. List l = new ArrayList();
  136. PreparedStatement p = null;
  137. ResultSet rs = null;
  138. try {
  139. String sql = SystemGlobals.getSql("SearchModel.getPostsDataForLucene");
  140. sql = sql.replaceAll(":posts:", this.buildInClause(postIds));
  141. p = JForumExecutionContext.getConnection().prepareStatement(sql);
  142. rs = p.executeQuery();
  143. while (rs.next()) {
  144. Post post = this.makePost(rs);
  145. post.setPostUsername(rs.getString("username"));
  146. l.add(post);
  147. }
  148. }
  149. catch (SQLException e) {
  150. throw new DatabaseException(e);
  151. }
  152. finally {
  153. DbUtils.close(rs, p);
  154. }
  155. return l;
  156. }
  157. private String buildInClause(int[] postIds)
  158. {
  159. StringBuffer sb = new StringBuffer(128);
  160. for (int i = 0; i < postIds.length - 1; i++) {
  161. sb.append(postIds[i]).append(',');
  162. }
  163. sb.append(postIds[postIds.length - 1]);
  164. return sb.toString();
  165. }
  166. private Post makePost(ResultSet rs) throws SQLException
  167. {
  168. Post p = new SearchPost();
  169. p.setId(rs.getInt("post_id"));
  170. p.setForumId(rs.getInt("forum_id"));
  171. p.setTopicId(rs.getInt("topic_id"));
  172. p.setUserId(rs.getInt("user_id"));
  173. p.setTime(new Date(rs.getTimestamp("post_time").getTime()));
  174. p.setText(this.readPostTextFromResultSet(rs));
  175. p.setBbCodeEnabled(rs.getInt("enable_bbcode") == 1);
  176. p.setSmiliesEnabled(rs.getInt("enable_smilies") == 1);
  177. String subject = rs.getString("post_subject");
  178. if (subject == null || subject.trim().length() == 0) {
  179. subject = rs.getString("topic_title");
  180. }
  181. p.setSubject(subject);
  182. return p;
  183. }
  184. protected String readPostTextFromResultSet(ResultSet rs) throws SQLException
  185. {
  186. return rs.getString("post_text");
  187. }
  188. }