1 18 19 package org.apache.roller.business.search.operations; 20 21 import java.io.IOException ; 22 import java.util.Iterator ; 23 import java.util.List ; 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 49 public abstract class IndexOperation implements Runnable { 50 private static Log mLogger = LogFactory.getFactory().getInstance(IndexOperation.class); 51 52 protected IndexManagerImpl manager; 55 56 private IndexReader reader; 57 58 private IndexWriter writer; 59 60 63 public IndexOperation(IndexManagerImpl manager) { 64 this.manager = manager; 65 } 66 67 70 protected Document getDocument(WeblogEntryData data) { 71 72 boolean indexComments = RollerConfig.getBooleanProperty("search.index.comments", true); 76 77 String commentContent = ""; 78 String commentEmail = ""; 79 String commentName = ""; 80 if (indexComments) { 81 List comments = data.getComments(); 82 if (comments != null) { 83 StringBuffer commentEmailBuf = new StringBuffer (); 84 StringBuffer commentContentBuf = new StringBuffer (); 85 StringBuffer commentNameBuf = new StringBuffer (); 86 for (Iterator 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 doc.add(Field.UnStored(FieldConstants.CONTENT, data.getText())); 121 122 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 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 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 e) { 152 } 153 154 return reader; 155 } 156 157 protected void endDeleting() { 158 if (reader != null) { 159 try { 160 reader.close(); 161 } catch (IOException 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 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 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 |