KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > jforum > dao > generic > GenericScheduledSearchIndexerDAO


1 /*
2  * Copyright (c) Rafael Steil
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 Mar 11, 2005 5:33:54 PM
40  * The JForum Project
41  * http://www.jforum.net
42  */

43 package net.jforum.dao.generic;
44
45 import java.sql.Connection JavaDoc;
46 import java.sql.PreparedStatement JavaDoc;
47 import java.sql.ResultSet JavaDoc;
48 import java.sql.Timestamp JavaDoc;
49 import java.util.ArrayList JavaDoc;
50 import java.util.List JavaDoc;
51
52 import net.jforum.dao.DataAccessDriver;
53 import net.jforum.dao.SearchIndexerDAO;
54 import net.jforum.entities.Post;
55 import net.jforum.util.preferences.SystemGlobals;
56
57 import org.apache.log4j.Logger;
58
59 /**
60  * @author Rafael Steil
61  * @version $Id: GenericScheduledSearchIndexerDAO.java,v 1.8 2005/12/23 00:01:17 rafaelsteil Exp $
62  */

63 public class GenericScheduledSearchIndexerDAO implements net.jforum.dao.ScheduledSearchIndexerDAO
64 {
65     private static Logger logger = Logger.getLogger(GenericScheduledSearchIndexerDAO.class);
66     
67     /**
68      * @see net.jforum.dao.ScheduledSearchIndexerDAO#index(int)
69      */

70     public int index(int step, Connection JavaDoc conn) throws Exception JavaDoc
71     {
72         Timestamp JavaDoc timestamp = new Timestamp JavaDoc(System.currentTimeMillis());
73         
74         // Get the last post id so far
75
PreparedStatement JavaDoc p = conn.prepareStatement(SystemGlobals.getSql("SearchModel.maxPostIdUntilNow"));
76         p.setTimestamp(1, timestamp);
77
78         ResultSet JavaDoc rs = p.executeQuery();
79         int maxPostId = -1;
80         
81         if (rs.next()) {
82             maxPostId = rs.getInt(1);
83         }
84         
85         // Get the latest indexed post
86
rs.close();
87         p.close();
88         
89         int latestIndexedPostId = -1;
90         
91         p = conn.prepareStatement(SystemGlobals.getSql("SearchModel.lastIndexedPostId"));
92         rs = p.executeQuery();
93         
94         if (rs.next()) {
95             latestIndexedPostId = rs.getInt(1);
96         }
97         
98         rs.close();
99         p.close();
100         
101         if (maxPostId == -1 || latestIndexedPostId == -1 || maxPostId <= latestIndexedPostId) {
102             logger.info("No posts found to index. Leaving...");
103             return 0;
104         }
105         
106         if (logger.isInfoEnabled()) {
107             logger.info("Going to index posts from " + latestIndexedPostId + " to " + maxPostId);
108         }
109         
110         // Count how many posts we have to index
111
p = conn.prepareStatement(SystemGlobals.getSql("SearchModel.howManyToIndex"));
112         p.setTimestamp(1, timestamp);
113         p.setInt(2, latestIndexedPostId);
114         
115         rs = p.executeQuery();
116         rs.next();
117         
118         int total = rs.getInt(1);
119         
120         rs.close();
121         p.close();
122         
123         // Do the dirty job
124
SearchIndexerDAO sim = DataAccessDriver.getInstance().newSearchIndexerDAO();
125         sim.setConnection(conn);
126         
127         int start = 0;
128         while (true) {
129             List JavaDoc posts = this.getPosts(start, step, latestIndexedPostId, maxPostId, conn);
130             
131             if (posts.size() == 0) {
132                 break;
133             }
134             
135             logger.info("Indexing range [" + start + ", " + (start + step) + "] from a total of " + total);
136             
137             sim.insertSearchWords(posts);
138             
139             start += step;
140         }
141         
142         return maxPostId;
143     }
144     
145     public List JavaDoc getPosts(int start, int count, int minPostId, int maxPostId, Connection JavaDoc conn) throws Exception JavaDoc
146     {
147         List JavaDoc l = new ArrayList JavaDoc();
148         
149         PreparedStatement JavaDoc p = conn.prepareStatement(SystemGlobals.getSql("SearchModel.getPostsToIndex"));
150         p.setInt(1, minPostId);
151         p.setInt(2, maxPostId);
152         p.setInt(3, start);
153         p.setInt(4, count);
154         
155         ResultSet JavaDoc rs = p.executeQuery();
156         while (rs.next()) {
157             l.add(this.makePost(rs));
158         }
159         
160         rs.close();
161         p.close();
162         
163         return l;
164     }
165     
166     protected String JavaDoc getPostTextFromResultSet(ResultSet JavaDoc rs) throws Exception JavaDoc
167     {
168         return rs.getString("post_text");
169     }
170     
171     protected Post makePost(ResultSet JavaDoc rs) throws Exception JavaDoc
172     {
173         Post p = new Post();
174         p.setId(rs.getInt("post_id"));
175         p.setText(this.getPostTextFromResultSet(rs));
176         p.setSubject("post_subject");
177         
178         return p;
179     }
180 }
181
Popular Tags