KickJava   Java API By Example, From Geeks To Geeks.

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


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

6 package org.roller.business.search.operations;
7
8 import java.io.IOException JavaDoc;
9 import java.text.MessageFormat JavaDoc;
10 import java.util.Date JavaDoc;
11 import java.util.Iterator JavaDoc;
12 import java.util.List JavaDoc;
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 /**
31  * @author aim4min
32  *
33  * An index operation that rebuilds a given users index (or all indexes.)
34  */

35 public class RebuildUserIndexOperation extends WriteToIndexOperation
36 {
37     //~ Static fields/initializers =============================================
38

39     private static Log mLogger =
40         LogFactory.getFactory().getInstance(RebuildUserIndexOperation.class);
41
42     //~ Instance fields ========================================================
43

44     private WebsiteData website;
45
46     //~ Constructors ===========================================================
47

48     /**
49      * Create a new operation that will recreate an index.
50      *
51      * @param website The website to rebuild the index for, or null for all users.
52      */

53     public RebuildUserIndexOperation(IndexManagerImpl mgr, WebsiteData website)
54     {
55         super(mgr);
56         this.website = website;
57     }
58
59     //~ Methods ================================================================
60

61     /* (non-Javadoc)
62      * @see java.lang.Runnable#run()
63      */

64     public void doRun()
65     {
66         Date JavaDoc start = new Date JavaDoc();
67
68         IndexReader reader = beginDeleting();
69
70         try
71         {
72             if (reader != null)
73             {
74                 String JavaDoc 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 JavaDoc 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 JavaDoc entries = weblogManager .getWeblogEntries(
115                     website, // userName
116
null, // startDate
117
new Date JavaDoc(), // endDate // don't index 'future' entries
118
null, // catName
119
WeblogManager.PUB_ONLY, // status
120
null);
121
122                 for (Iterator JavaDoc 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 JavaDoc[] {entry.getPubTime(), entry.getAnchor()}));
129                 }
130             }
131         }
132         catch (Exception JavaDoc 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 JavaDoc end = new Date JavaDoc();
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