KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > roller > business > search > operations > IndexOperation


1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. The ASF licenses this file to You
4 * under the Apache License, Version 2.0 (the "License"); you may not
5 * use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License. For additional information regarding
15 * copyright in this work, please see the NOTICE file in the top level
16 * directory of this distribution.
17 */

18 /* Created on Jul 16, 2003 */
19 package org.apache.roller.business.search.operations;
20
21 import java.io.IOException JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.List JavaDoc;
24
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27 import org.apache.lucene.document.Document;
28 import org.apache.lucene.document.Field;
29 import org.apache.lucene.index.IndexReader;
30 import org.apache.lucene.index.IndexWriter;
31 import org.apache.roller.business.IndexManagerImpl;
32 import org.apache.roller.business.search.FieldConstants;
33 import org.apache.roller.pojos.CommentData;
34 import org.apache.roller.pojos.WeblogCategoryData;
35 import org.apache.roller.pojos.WeblogEntryData;
36 import org.apache.roller.util.Utilities;
37 import org.apache.roller.config.RollerConfig;
38
39 /**
40  * This is the base class for all index operation.
41  * These operations include:<br>
42  * SearchOperation<br>
43  * AddWeblogOperation<br>
44  * RemoveWeblogOperation<br>
45  * RebuildUserIndexOperation
46  *
47  * @author Mindaugas Idzelis (min@idzelis.com)
48  */

49 public abstract class IndexOperation implements Runnable JavaDoc {
50     private static Log mLogger = LogFactory.getFactory().getInstance(IndexOperation.class);
51
52     //~ Instance fields
53
// ========================================================
54
protected IndexManagerImpl manager;
55
56     private IndexReader reader;
57
58     private IndexWriter writer;
59
60     //~ Constructors
61
// ===========================================================
62

63     public IndexOperation(IndexManagerImpl manager) {
64         this.manager = manager;
65     }
66
67     //~ Methods
68
// ================================================================
69

70     protected Document getDocument(WeblogEntryData data) {
71
72         // Actual comment content is indexed only if search.index.comments
73
// is true or absent from the (static) configuration properties.
74
// If false in the configuration, comments are treated as if empty.
75
boolean indexComments = RollerConfig.getBooleanProperty("search.index.comments", true);
76
77         String JavaDoc commentContent = "";
78         String JavaDoc commentEmail = "";
79         String JavaDoc commentName = "";
80         if (indexComments) {
81             List JavaDoc comments = data.getComments();
82             if (comments != null) {
83                 StringBuffer JavaDoc commentEmailBuf = new StringBuffer JavaDoc();
84                 StringBuffer JavaDoc commentContentBuf = new StringBuffer JavaDoc();
85                 StringBuffer JavaDoc commentNameBuf = new StringBuffer JavaDoc();
86                 for (Iterator JavaDoc cItr = comments.iterator(); cItr.hasNext();) {
87                     CommentData comment = (CommentData) cItr.next();
88                     if (comment.getSpam() == null || !comment.getSpam().booleanValue()) {
89                         if (comment.getContent() != null) {
90                             commentContentBuf.append(comment.getContent());
91                             commentContentBuf.append(",");
92                         }
93                         if (comment.getEmail() != null) {
94                             commentEmailBuf.append(comment.getEmail());
95                             commentEmailBuf.append(",");
96                         }
97                         if (comment.getName() != null) {
98                             commentNameBuf.append(comment.getName());
99                             commentNameBuf.append(",");
100                         }
101                     }
102                 }
103                 commentEmail = commentEmailBuf.toString();
104                 commentContent = commentContentBuf.toString();
105                 commentName = commentNameBuf.toString();
106             }
107         }
108
109         Document doc = new Document();
110
111         doc.add(Field.Keyword(FieldConstants.ID, data.getId()));
112
113         doc.add(Field.Keyword(FieldConstants.WEBSITE_HANDLE, data.getWebsite().getHandle()));
114
115         doc.add(Field.UnIndexed(FieldConstants.ANCHOR, data.getAnchor()));
116         doc.add(Field.Text(FieldConstants.USERNAME, data.getCreator().getUserName()));
117         doc.add(Field.Text(FieldConstants.TITLE, data.getTitle()));
118
119         // index the entry text, but don't store it - moved to end of block
120
doc.add(Field.UnStored(FieldConstants.CONTENT, data.getText()));
121
122         // store an abbreviated version of the entry text, but don't index
123
doc.add(Field.UnIndexed(FieldConstants.CONTENT_STORED,
124             Utilities.truncateNicely(Utilities.removeHTML(data.getText()), 240, 260, "...")));
125
126         doc.add(Field.Keyword(FieldConstants.UPDATED, data.getUpdateTime()
127                 .toString()));
128         doc.add(Field.Keyword(FieldConstants.PUBLISHED, data.getPubTime()
129                 .toString()));
130
131         // index Comments
132
doc.add(Field.UnStored(FieldConstants.C_CONTENT, commentContent));
133         doc.add(Field.UnStored(FieldConstants.C_EMAIL, commentEmail));
134         doc.add(Field.UnStored(FieldConstants.C_NAME, commentName));
135
136         doc.add(Field.UnStored(FieldConstants.CONSTANT, FieldConstants.CONSTANT_V));
137
138         // index Category
139
WeblogCategoryData categorydata = data.getCategory();
140         Field category = (categorydata == null)
141            ? Field.UnStored(FieldConstants.CATEGORY, "")
142            : Field.Text(FieldConstants.CATEGORY, categorydata.getName());
143         doc.add(category);
144
145         return doc;
146     }
147
148     protected IndexReader beginDeleting() {
149         try {
150             reader = IndexReader.open(manager.getIndexDirectory());
151         } catch (IOException JavaDoc e) {
152         }
153
154         return reader;
155     }
156
157     protected void endDeleting() {
158         if (reader != null) {
159             try {
160                 reader.close();
161             } catch (IOException JavaDoc e) {
162                 mLogger.error("ERROR closing reader");
163             }
164         }
165     }
166
167     protected IndexWriter beginWriting() {
168         try {
169             writer = new IndexWriter(manager.getIndexDirectory(), IndexManagerImpl.getAnalyzer(), false);
170         } catch (IOException JavaDoc e) {
171             mLogger.error("ERROR creating writer");
172         }
173
174         return writer;
175     }
176
177     protected void endWriting() {
178         if (writer != null) {
179             try {
180                 writer.close();
181             } catch (IOException JavaDoc e) {
182                 mLogger.error("ERROR closing writer", e);
183             }
184         }
185     }
186
187     public void run() {
188         doRun();
189     }
190
191     protected abstract void doRun();
192 }
193
Popular Tags