001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. The ASF licenses this file to You
004: * under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License. For additional information regarding
015: * copyright in this work, please see the NOTICE file in the top level
016: * directory of this distribution.
017: */
018: /* Created on Jul 16, 2003 */
019: package org.apache.roller.business.search.operations;
020:
021: import java.io.IOException;
022: import java.util.Iterator;
023: import java.util.List;
024:
025: import org.apache.commons.logging.Log;
026: import org.apache.commons.logging.LogFactory;
027: import org.apache.lucene.document.Document;
028: import org.apache.lucene.document.Field;
029: import org.apache.lucene.index.IndexReader;
030: import org.apache.lucene.index.IndexWriter;
031: import org.apache.roller.business.search.IndexManagerImpl;
032: import org.apache.roller.business.search.FieldConstants;
033: import org.apache.roller.pojos.CommentData;
034: import org.apache.roller.pojos.WeblogCategoryData;
035: import org.apache.roller.pojos.WeblogEntryData;
036: import org.apache.roller.util.Utilities;
037: import org.apache.roller.config.RollerConfig;
038:
039: /**
040: * This is the base class for all index operation.
041: * These operations include:<br>
042: * SearchOperation<br>
043: * AddWeblogOperation<br>
044: * RemoveWeblogOperation<br>
045: * RebuildUserIndexOperation
046: *
047: * @author Mindaugas Idzelis (min@idzelis.com)
048: */
049: public abstract class IndexOperation implements Runnable {
050: private static Log mLogger = LogFactory.getFactory().getInstance(
051: IndexOperation.class);
052:
053: //~ Instance fields
054: // ========================================================
055: protected IndexManagerImpl manager;
056:
057: private IndexReader reader;
058:
059: private IndexWriter writer;
060:
061: //~ Constructors
062: // ===========================================================
063:
064: public IndexOperation(IndexManagerImpl manager) {
065: this .manager = manager;
066: }
067:
068: //~ Methods
069: // ================================================================
070:
071: protected Document getDocument(WeblogEntryData data) {
072:
073: // Actual comment content is indexed only if search.index.comments
074: // is true or absent from the (static) configuration properties.
075: // If false in the configuration, comments are treated as if empty.
076: boolean indexComments = RollerConfig.getBooleanProperty(
077: "search.index.comments", true);
078:
079: String commentContent = "";
080: String commentEmail = "";
081: String commentName = "";
082: if (indexComments) {
083: List comments = data.getComments();
084: if (comments != null) {
085: StringBuffer commentEmailBuf = new StringBuffer();
086: StringBuffer commentContentBuf = new StringBuffer();
087: StringBuffer commentNameBuf = new StringBuffer();
088: for (Iterator cItr = comments.iterator(); cItr
089: .hasNext();) {
090: CommentData comment = (CommentData) cItr.next();
091: if (comment.getSpam() == null
092: || !comment.getSpam().booleanValue()) {
093: if (comment.getContent() != null) {
094: commentContentBuf.append(comment
095: .getContent());
096: commentContentBuf.append(",");
097: }
098: if (comment.getEmail() != null) {
099: commentEmailBuf.append(comment.getEmail());
100: commentEmailBuf.append(",");
101: }
102: if (comment.getName() != null) {
103: commentNameBuf.append(comment.getName());
104: commentNameBuf.append(",");
105: }
106: }
107: }
108: commentEmail = commentEmailBuf.toString();
109: commentContent = commentContentBuf.toString();
110: commentName = commentNameBuf.toString();
111: }
112: }
113:
114: Document doc = new Document();
115:
116: doc.add(Field.Keyword(FieldConstants.ID, data.getId()));
117:
118: doc.add(Field.Keyword(FieldConstants.WEBSITE_HANDLE, data
119: .getWebsite().getHandle()));
120:
121: doc.add(Field
122: .UnIndexed(FieldConstants.ANCHOR, data.getAnchor()));
123: doc.add(Field.Text(FieldConstants.USERNAME, data.getCreator()
124: .getUserName()));
125: doc.add(Field.Text(FieldConstants.TITLE, data.getTitle()));
126:
127: // index the entry text, but don't store it - moved to end of block
128: doc.add(Field.UnStored(FieldConstants.CONTENT, data.getText()));
129:
130: // store an abbreviated version of the entry text, but don't index
131: doc.add(Field.UnIndexed(FieldConstants.CONTENT_STORED,
132: Utilities.truncateNicely(Utilities.removeHTML(data
133: .getText()), 240, 260, "...")));
134:
135: doc.add(Field.Keyword(FieldConstants.UPDATED, data
136: .getUpdateTime().toString()));
137: doc.add(Field.Keyword(FieldConstants.PUBLISHED, data
138: .getPubTime().toString()));
139:
140: // index Comments
141: doc.add(Field
142: .UnStored(FieldConstants.C_CONTENT, commentContent));
143: doc.add(Field.UnStored(FieldConstants.C_EMAIL, commentEmail));
144: doc.add(Field.UnStored(FieldConstants.C_NAME, commentName));
145:
146: doc.add(Field.UnStored(FieldConstants.CONSTANT,
147: FieldConstants.CONSTANT_V));
148:
149: // index Category
150: WeblogCategoryData categorydata = data.getCategory();
151: Field category = (categorydata == null) ? Field.UnStored(
152: FieldConstants.CATEGORY, "") : Field.Text(
153: FieldConstants.CATEGORY, categorydata.getName());
154: doc.add(category);
155:
156: return doc;
157: }
158:
159: protected IndexReader beginDeleting() {
160: try {
161: reader = IndexReader.open(manager.getIndexDirectory());
162: } catch (IOException e) {
163: }
164:
165: return reader;
166: }
167:
168: protected void endDeleting() {
169: if (reader != null) {
170: try {
171: reader.close();
172: } catch (IOException e) {
173: mLogger.error("ERROR closing reader");
174: }
175: }
176: }
177:
178: protected IndexWriter beginWriting() {
179: try {
180: writer = new IndexWriter(manager.getIndexDirectory(),
181: IndexManagerImpl.getAnalyzer(), false);
182: } catch (IOException e) {
183: mLogger.error("ERROR creating writer");
184: }
185:
186: return writer;
187: }
188:
189: protected void endWriting() {
190: if (writer != null) {
191: try {
192: writer.close();
193: } catch (IOException e) {
194: mLogger.error("ERROR closing writer", e);
195: }
196: }
197: }
198:
199: public void run() {
200: doRun();
201: }
202:
203: protected abstract void doRun();
204: }
|