KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Created on Jul 16, 2003
3  * Authored by: Mindaugas Idzelis (min@idzelis.com)
4  */

5 package org.roller.business.search.operations;
6
7 import java.io.IOException JavaDoc;
8 import java.util.Iterator JavaDoc;
9 import java.util.List JavaDoc;
10
11 import org.apache.commons.logging.Log;
12 import org.apache.commons.logging.LogFactory;
13 import org.apache.lucene.document.Document;
14 import org.apache.lucene.document.Field;
15 import org.apache.lucene.index.IndexReader;
16 import org.apache.lucene.index.IndexWriter;
17 import org.roller.business.IndexManagerImpl;
18 import org.roller.business.search.FieldConstants;
19 import org.roller.pojos.CommentData;
20 import org.roller.pojos.WeblogCategoryData;
21 import org.roller.pojos.WeblogEntryData;
22 import org.roller.util.Utilities;
23
24 /**
25  * @author aim4min
26  *
27  * This is the base class for all index operation. These operations include:
28  *
29  * SearchOperation AddWeblogOperation RemoveWeblogOperation
30  * RebuildUserIndexOperation
31  *
32  */

33 public abstract class IndexOperation implements Runnable JavaDoc
34 {
35     private static Log mLogger =
36         LogFactory.getFactory().getInstance(IndexOperation.class);
37
38     //~ Instance fields
39
// ========================================================
40
protected IndexManagerImpl manager;
41
42     private IndexReader reader;
43
44     private IndexWriter writer;
45
46     //~ Constructors
47
// ===========================================================
48

49     public IndexOperation(IndexManagerImpl manager)
50     {
51         this.manager = manager;
52     }
53
54     //~ Methods
55
// ================================================================
56

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