KickJava   Java API By Example, From Geeks To Geeks.

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


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
10 import org.apache.commons.logging.Log;
11 import org.apache.commons.logging.LogFactory;
12 import org.apache.lucene.index.IndexReader;
13 import org.apache.lucene.index.IndexWriter;
14 import org.apache.lucene.index.Term;
15 import org.roller.business.IndexManagerImpl;
16 import org.roller.business.search.FieldConstants;
17 import org.roller.model.Roller;
18 import org.roller.model.RollerFactory;
19 import org.roller.pojos.WeblogEntryData;
20
21 /**
22  * @author aim4min
23  *
24  * An operation that adds a new log entry into the index.
25  */

26 public class ReIndexEntryOperation extends WriteToIndexOperation
27 {
28     //~ Static fields/initializers =============================================
29

30     private static Log mLogger =
31         LogFactory.getFactory().getInstance(AddEntryOperation.class);
32
33     //~ Instance fields ========================================================
34

35     private WeblogEntryData data;
36
37     //~ Constructors ===========================================================
38

39     /**
40      * Adds a web log entry into the index.
41      */

42     public ReIndexEntryOperation(IndexManagerImpl mgr, WeblogEntryData data)
43     {
44         super(mgr);
45         this.data = data;
46     }
47
48     //~ Methods ================================================================
49

50     /* (non-Javadoc)
51      * @see java.lang.Runnable#run()
52      */

53     public void doRun()
54     {
55         IndexReader reader = beginDeleting();
56         try
57         {
58             if (reader != null)
59             {
60                 Term term = new Term(FieldConstants.ID, data.getId());
61                 reader.delete(term);
62             }
63         }
64         catch (IOException JavaDoc e)
65         {
66             mLogger.error("Error deleting doc from index", e);
67         }
68         finally
69         {
70             endDeleting();
71         }
72         
73         IndexWriter writer = beginWriting();
74         Roller roller = RollerFactory.getRoller();
75         try
76         {
77             if (writer != null)
78             {
79                 writer.addDocument(getDocument(data));
80             }
81         }
82         catch (IOException JavaDoc e)
83         {
84             mLogger.error("Problems adding doc to index", e);
85         }
86         finally
87         {
88             if (roller != null) roller.release();
89             endWriting();
90         }
91     }
92 }
93
Popular Tags