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 24/07/2007 10:27:23
040: *
041: * The JForum Project
042: * http://www.jforum.net
043: */
044: package net.jforum.dao.generic;
045:
046: import java.sql.PreparedStatement;
047: import java.sql.ResultSet;
048: import java.sql.SQLException;
049: import java.sql.Timestamp;
050: import java.util.ArrayList;
051: import java.util.Date;
052: import java.util.List;
053:
054: import net.jforum.JForumExecutionContext;
055: import net.jforum.dao.LuceneDAO;
056: import net.jforum.entities.Post;
057: import net.jforum.exceptions.DatabaseException;
058: import net.jforum.search.LuceneReindexArgs;
059: import net.jforum.search.SearchPost;
060: import net.jforum.util.DbUtils;
061: import net.jforum.util.preferences.SystemGlobals;
062:
063: /**
064: * @author Rafael Steil
065: * @version $Id: GenericLuceneDAO.java,v 1.12 2007/10/13 13:46:21 rafaelsteil Exp $
066: */
067: public class GenericLuceneDAO implements LuceneDAO {
068: /**
069: * @see net.jforum.dao.LuceneDAO#getPostsToIndex(LuceneReindexArgs, int, int)
070: */
071: public List getPostsToIndex(int fromPostId, int toPostId) {
072: List l = new ArrayList();
073:
074: PreparedStatement p = null;
075: ResultSet rs = null;
076:
077: try {
078: p = JForumExecutionContext
079: .getConnection()
080: .prepareStatement(
081: SystemGlobals
082: .getSql("SearchModel.getPostsToIndexForLucene"));
083:
084: p.setInt(1, fromPostId);
085: p.setInt(2, toPostId);
086:
087: rs = p.executeQuery();
088:
089: while (rs.next()) {
090: l.add(this .makePost(rs));
091: }
092: } catch (SQLException e) {
093: throw new DatabaseException(e);
094: } finally {
095: DbUtils.close(rs, p);
096: }
097:
098: return l;
099: }
100:
101: /**
102: * @see net.jforum.dao.LuceneDAO#firstPostIdByDate(java.util.Date)
103: */
104: public int firstPostIdByDate(Date date) {
105: return this .getPostIdByDate(date, SystemGlobals
106: .getSql("SearchModel.firstPostIdByDate"));
107: }
108:
109: /**
110: * @see net.jforum.dao.LuceneDAO#lastPostIdByDate(java.util.Date)
111: */
112: public int lastPostIdByDate(Date date) {
113: return this .getPostIdByDate(date, SystemGlobals
114: .getSql("SearchModel.lastPostIdByDate"));
115: }
116:
117: private int getPostIdByDate(Date date, String query) {
118: int postId = 0;
119:
120: PreparedStatement p = null;
121: ResultSet rs = null;
122:
123: try {
124: p = JForumExecutionContext.getConnection()
125: .prepareStatement(query);
126:
127: p.setTimestamp(1, new Timestamp(date.getTime()));
128:
129: rs = p.executeQuery();
130:
131: if (rs.next()) {
132: postId = rs.getInt(1);
133: }
134: } catch (SQLException e) {
135: throw new DatabaseException(e);
136: } finally {
137: DbUtils.close(rs, p);
138: }
139:
140: return postId;
141: }
142:
143: /**
144: * @see net.jforum.dao.LuceneDAO#getPostsData(int[])
145: */
146: public List getPostsData(int[] postIds) {
147: if (postIds.length == 0) {
148: return new ArrayList();
149: }
150:
151: List l = new ArrayList();
152:
153: PreparedStatement p = null;
154: ResultSet rs = null;
155:
156: try {
157: String sql = SystemGlobals
158: .getSql("SearchModel.getPostsDataForLucene");
159: sql = sql
160: .replaceAll(":posts:", this .buildInClause(postIds));
161:
162: p = JForumExecutionContext.getConnection()
163: .prepareStatement(sql);
164: rs = p.executeQuery();
165:
166: while (rs.next()) {
167: Post post = this .makePost(rs);
168: post.setPostUsername(rs.getString("username"));
169:
170: l.add(post);
171: }
172: } catch (SQLException e) {
173: throw new DatabaseException(e);
174: } finally {
175: DbUtils.close(rs, p);
176: }
177:
178: return l;
179: }
180:
181: private String buildInClause(int[] postIds) {
182: StringBuffer sb = new StringBuffer(128);
183:
184: for (int i = 0; i < postIds.length - 1; i++) {
185: sb.append(postIds[i]).append(',');
186: }
187:
188: sb.append(postIds[postIds.length - 1]);
189:
190: return sb.toString();
191: }
192:
193: private Post makePost(ResultSet rs) throws SQLException {
194: Post p = new SearchPost();
195:
196: p.setId(rs.getInt("post_id"));
197: p.setForumId(rs.getInt("forum_id"));
198: p.setTopicId(rs.getInt("topic_id"));
199: p.setUserId(rs.getInt("user_id"));
200: p.setTime(new Date(rs.getTimestamp("post_time").getTime()));
201: p.setText(this .readPostTextFromResultSet(rs));
202: p.setBbCodeEnabled(rs.getInt("enable_bbcode") == 1);
203: p.setSmiliesEnabled(rs.getInt("enable_smilies") == 1);
204:
205: String subject = rs.getString("post_subject");
206:
207: if (subject == null || subject.trim().length() == 0) {
208: subject = rs.getString("topic_title");
209: }
210:
211: p.setSubject(subject);
212:
213: return p;
214: }
215:
216: protected String readPostTextFromResultSet(ResultSet rs)
217: throws SQLException {
218: return rs.getString("post_text");
219: }
220: }
|