1 6 package org.roller.business.search.operations; 7 8 import java.io.IOException ; 9 import java.text.MessageFormat ; 10 import java.util.Date ; 11 import java.util.Iterator ; 12 import java.util.List ; 13 14 import org.apache.commons.logging.Log; 15 import org.apache.commons.logging.LogFactory; 16 import org.apache.lucene.index.IndexReader; 17 import org.apache.lucene.index.IndexWriter; 18 import org.apache.lucene.index.Term; 19 import org.roller.RollerException; 20 import org.roller.business.IndexManagerImpl; 21 import org.roller.business.search.FieldConstants; 22 import org.roller.business.search.IndexUtil; 23 import org.roller.model.Roller; 24 import org.roller.model.RollerFactory; 25 import org.roller.model.WeblogManager; 26 import org.roller.pojos.WeblogEntryData; 27 import org.roller.pojos.WebsiteData; 28 29 30 35 public class RebuildUserIndexOperation extends WriteToIndexOperation 36 { 37 39 private static Log mLogger = 40 LogFactory.getFactory().getInstance(RebuildUserIndexOperation.class); 41 42 44 private WebsiteData website; 45 46 48 53 public RebuildUserIndexOperation(IndexManagerImpl mgr, WebsiteData website) 54 { 55 super(mgr); 56 this.website = website; 57 } 58 59 61 64 public void doRun() 65 { 66 Date start = new Date (); 67 68 IndexReader reader = beginDeleting(); 69 70 try 71 { 72 if (reader != null) 73 { 74 String userName = null; 75 if (website != null && website.getUser() != null) 76 { 77 userName = website.getUser().getUserName(); 78 } 79 Term tUsername = 80 IndexUtil.getTerm(FieldConstants.USERNAME, userName); 81 82 if (tUsername != null) 83 { 84 reader.delete(tUsername); 85 } 86 else 87 { 88 Term all = 89 IndexUtil.getTerm(FieldConstants.CONSTANT, 90 FieldConstants.CONSTANT_V); 91 reader.delete(all); 92 } 93 } 94 } 95 catch (IOException e) 96 { 97 mLogger.info("Problems deleting doc from index", e); 98 } 99 finally 100 { 101 endDeleting(); 102 } 103 104 IndexWriter writer = beginWriting(); 105 106 Roller roller = RollerFactory.getRoller(); 107 try 108 { 109 roller.begin(); 110 if (writer != null) 111 { 112 WeblogManager weblogManager = roller.getWeblogManager(); 113 114 List entries = weblogManager .getWeblogEntries( 115 website, null, new Date (), null, WeblogManager.PUB_ONLY, null); 121 122 for (Iterator wbItr = entries.iterator(); wbItr.hasNext();) 123 { 124 WeblogEntryData entry = (WeblogEntryData) wbItr.next(); 125 writer.addDocument(getDocument(entry)); 126 mLogger.debug( 127 MessageFormat.format("Indexed entry {0}: {1}", 128 new Object [] {entry.getPubTime(), entry.getAnchor()})); 129 } 130 } 131 } 132 catch (Exception e) 133 { 134 mLogger.error("ERROR adding doc to index", e); 135 } 136 finally 137 { 138 endWriting(); 139 if (roller != null) roller.release(); 140 } 141 142 Date end = new Date (); 143 double length = (end.getTime() - start.getTime()) / (double) 1000; 144 145 if (website == null) 146 { 147 mLogger.info( 148 "Completed rebuilding index for all users in '" + length + "' secs"); 149 } 150 else 151 { 152 mLogger.info("Completed rebuilding index for '" + 153 website.getUser().getUserName() + "' in '" + length + "' seconds"); 154 } 155 } 156 } 157 | Popular Tags |