1 5 package org.roller.business.search.operations; 6 7 import java.io.IOException ; 8 import java.util.Iterator ; 9 import java.util.List ; 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 33 public abstract class IndexOperation implements Runnable 34 { 35 private static Log mLogger = 36 LogFactory.getFactory().getInstance(IndexOperation.class); 37 38 protected IndexManagerImpl manager; 41 42 private IndexReader reader; 43 44 private IndexWriter writer; 45 46 49 public IndexOperation(IndexManagerImpl manager) 50 { 51 this.manager = manager; 52 } 53 54 57 protected Document getDocument(WeblogEntryData data) 58 { 59 StringBuffer commentEmail = new StringBuffer (); 60 StringBuffer commentContent = new StringBuffer (); 61 StringBuffer commentName = new StringBuffer (); 62 63 List comments = data.getComments(); 64 if (comments != null) 65 { 66 for (Iterator 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 doc.add(Field.UnStored(FieldConstants.CONTENT, data.getText())); 100 101 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 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 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 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 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 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 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 |