KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > knowgate > lucene > BugIndexer


1 package com.knowgate.lucene;
2
3 import java.util.Date JavaDoc;
4 import java.util.Properties JavaDoc;
5
6 import java.io.IOException JavaDoc;
7 import java.sql.SQLException JavaDoc;
8
9 import org.apache.lucene.index.IndexWriter;
10 import org.apache.lucene.document.Document;
11 import org.apache.lucene.document.Field;
12
13 import com.knowgate.jdc.JDCConnection;
14 import com.knowgate.dataobjs.DB;
15 import com.knowgate.projtrack.Bug;
16 import com.knowgate.misc.Gadgets;
17 import com.knowgate.dfs.FileSystem;
18 import org.apache.lucene.analysis.Analyzer;
19 import java.io.File JavaDoc;
20 import java.util.HashMap JavaDoc;
21 import org.apache.lucene.index.Term;
22 import org.apache.lucene.index.IndexReader;
23
24 /**
25  * Indexer subclass for hipergate bugs
26  * @author Sergio Montoro Ten
27  * @version 3.0
28  */

29 public class BugIndexer extends Indexer {
30
31   public BugIndexer() { }
32
33
34   /**
35    * Add bug to index
36    * @param oIWrt IndexWriter
37    * @param sGuid String Bug GUID
38    * @param iNumber int Bug Number
39    * @param sWorkArea String GUID of WorkArea to which bug belongs
40    * @param sProject String GUID of project to which bug belongs
41    * @param sTitle String Title
42    * @param sReportedBy String Author
43    * @param dtCreated Date Created
44    * @param sComments String Comments
45    * @param sText String Bug Description
46    * @throws ClassNotFoundException
47    * @throws IOException
48    * @throws IllegalArgumentException
49    * @throws NoSuchFieldException
50    * @throws IllegalAccessException
51    * @throws InstantiationException
52    * @throws NullPointerException
53    */

54   public static void addBug(IndexWriter oIWrt,
55                             String JavaDoc sGuid, int iNumber, String JavaDoc sWorkArea,
56                             String JavaDoc sProject, String JavaDoc sTitle, String JavaDoc sWriter,
57                             String JavaDoc sReportedBy, Date JavaDoc dtCreated,
58                             String JavaDoc sType, Short JavaDoc oPriority, Short JavaDoc oSeverity,
59                             String JavaDoc sStatus, String JavaDoc sComments, String JavaDoc sText)
60     throws ClassNotFoundException JavaDoc, IOException JavaDoc, IllegalArgumentException JavaDoc,
61              NoSuchFieldException JavaDoc, IllegalAccessException JavaDoc, InstantiationException JavaDoc,
62              NullPointerException JavaDoc {
63
64     Document oDoc = new Document();
65     oDoc.add (Field.Keyword ("workarea" , sWorkArea));
66     oDoc.add (Field.Keyword ("container", sProject));
67     oDoc.add (Field.Keyword ("guid" , sGuid));
68     oDoc.add (Field.Keyword ("number" , String.valueOf(iNumber)));
69     oDoc.add (Field.Text ("title" , Gadgets.ASCIIEncode(sTitle)));
70     oDoc.add (Field.Keyword ("created" , dtCreated));
71     oDoc.add (Field.Keyword ("writer" , sWriter));
72     if (null!=sStatus) oDoc.add (Field.Keyword ("status" , sStatus));
73     if (null!=sType) oDoc.add (Field.Keyword ("type" , sType));
74     if (null!=oPriority) oDoc.add (Field.Keyword ("priority" , oPriority.toString()));
75     if (null!=oSeverity) oDoc.add (Field.Keyword ("severity" , oSeverity.toString()));
76     if (null!=sReportedBy) oDoc.add (Field.Text ("author" , Gadgets.ASCIIEncode(sReportedBy)));
77     oDoc.add (Field.UnStored("comments" , Gadgets.ASCIIEncode(sComments)));
78     if (null==sText) {
79       oDoc.add (Field.UnStored("text", ""));
80       oDoc.add (Field.UnStored("abstract", ""));
81     } else {
82       oDoc.add (Field.UnStored("text", Gadgets.ASCIIEncode(sText)));
83       if (sText.length()>80)
84         oDoc.add (new Field("abstract", sText.substring(0,80).replace('\n',' '), true, false, false));
85       else
86         oDoc.add (new Field("abstract", sText.replace('\n',' '), true, false, false));
87     }
88     oIWrt.addDocument(oDoc);
89   } // addBug
90

91   /**
92    * Add bug to index
93    * @param oIWrt IndexWriter
94    * @param sGuid String Bug GUID
95    * @param iNumber int Bug Number
96    * @param sWorkArea String GUID of WorkArea to which bug belongs
97    * @param sProject String GUID of project to which bug belongs
98    * @param sTitle String Title
99    * @param sReportedBy String Author
100    * @param dtCreated Date Created
101    * @param sComments String Comments
102    * @param sText String Bug Description
103    * @throws ClassNotFoundException
104    * @throws IOException
105    * @throws IllegalArgumentException
106    * @throws NoSuchFieldException
107    * @throws IllegalAccessException
108    * @throws InstantiationException
109    * @throws NullPointerException
110    */

111   public static void addBug(IndexWriter oIWrt,
112                             String JavaDoc sGuid, int iNumber, String JavaDoc sWorkArea,
113                             String JavaDoc sProject, String JavaDoc sTitle,
114                             String JavaDoc sReportedBy, Date JavaDoc dtCreated,
115                             String JavaDoc sComments, String JavaDoc sText)
116     throws ClassNotFoundException JavaDoc, IOException JavaDoc, IllegalArgumentException JavaDoc,
117              NoSuchFieldException JavaDoc, IllegalAccessException JavaDoc, InstantiationException JavaDoc,
118              NullPointerException JavaDoc {
119     addBug(oIWrt, sGuid, iNumber, sWorkArea, sProject, sTitle, null, sReportedBy,
120            dtCreated, null, null, null, null, sComments, sText);
121   } // addBug
122

123   /**
124    * Add bug to index
125    * @param oIWrt IndexWriter
126    * @param oCon JDCConnection
127    * @param sWorkArea String GUID of WorkArea where bug must be added
128    * @param oBug Bug
129    * @throws SQLException
130    * @throws IOException
131    * @throws ClassNotFoundException
132    * @throws NoSuchFieldException
133    * @throws IllegalAccessException
134    * @throws InstantiationException
135    * @throws NullPointerException
136    */

137   public static void addBug(IndexWriter oIWrt, JDCConnection oCon,
138                             String JavaDoc sWorkArea, Bug oBug)
139     throws SQLException JavaDoc,IOException JavaDoc,ClassNotFoundException JavaDoc,NoSuchFieldException JavaDoc,
140            IllegalAccessException JavaDoc, InstantiationException JavaDoc, NullPointerException JavaDoc {
141     Short JavaDoc oSeverity;
142     Short JavaDoc oPriority;
143
144     if (null==oBug) throw new NullPointerException JavaDoc ("BugIndexer.addBug() Bug may not be null");
145     if (null==oCon) throw new NullPointerException JavaDoc ("BugIndexer.addBug() JDBC Connection may not be null");
146     if (oCon.isClosed()) throw new SQLException JavaDoc("BugIndexer.addBug() JDBC connection is closed");
147
148     if (oBug.isNull(DB.od_priority))
149       oPriority = null;
150     else
151       oPriority = new Short JavaDoc(oBug.getShort(DB.od_priority));
152
153     if (oBug.isNull(DB.od_severity))
154       oSeverity = null;
155     else
156       oSeverity = new Short JavaDoc(oBug.getShort(DB.od_severity));
157
158     addBug(oIWrt, oBug.getString(DB.gu_bug), oBug.getInt(DB.pg_bug),
159            oBug.getString(sWorkArea), oBug.getString(DB.gu_project),
160            oBug.getStringNull(DB.tl_bug,""), oBug.getString(DB.gu_writer),
161            oBug.getStringNull(DB.nm_reporter,""), oBug.getCreationDate(oCon),
162            oBug.getStringNull(DB.tp_bug,null), oPriority, oSeverity,
163            oBug.getStringNull(DB.tx_status, null), oBug.getStringNull(DB.tx_comments,null),
164            oBug.getStringNull(DB.tx_bug_brief,null));
165   }
166
167   /**
168    * Add bug to index
169    * @param oProps Properties
170    * @param oCon JDCConnection
171    * @param sWorkArea String
172    * @param oBug Bug
173    * @throws SQLException
174    * @throws IOException
175    * @throws ClassNotFoundException
176    * @throws NoSuchFieldException
177    * @throws IllegalAccessException
178    * @throws InstantiationException
179    * @throws NullPointerException
180    * @throws NoSuchFieldException
181    */

182   public static void addBug(Properties JavaDoc oProps, JDCConnection oCon,
183                             String JavaDoc sWorkArea, Bug oBug)
184     throws SQLException JavaDoc,IOException JavaDoc,ClassNotFoundException JavaDoc,NoSuchFieldException JavaDoc,
185            IllegalAccessException JavaDoc, InstantiationException JavaDoc, NullPointerException JavaDoc,
186            NoSuchFieldException JavaDoc {
187
188
189     String JavaDoc sDirectory = oProps.getProperty("luceneindex");
190
191     if (null==sDirectory)
192      throw new NoSuchFieldException JavaDoc ("Cannot find luceneindex property");
193
194      sDirectory = Gadgets.chomp(sDirectory, File.separator) + DB.k_bugs + File.separator + sWorkArea;
195      File JavaDoc oDir = new File JavaDoc(sDirectory);
196      if (!oDir.exists()) {
197        FileSystem oFS = new FileSystem();
198        try { oFS.mkdirs(sDirectory); } catch (Exception JavaDoc e) { throw new IOException JavaDoc(e.getClass().getName()+" "+e.getMessage()); }
199     } // fi
200

201     Class JavaDoc oAnalyzer = Class.forName(oProps.getProperty("analyzer" , DEFAULT_ANALYZER));
202
203     IndexWriter oIWrt = new IndexWriter(sDirectory, (Analyzer) oAnalyzer.newInstance(), true);
204     addBug(oIWrt, oCon, sWorkArea, oBug);
205     oIWrt.close();
206   } // addBug
207

208   /**
209    * Delete a bug with a given GUID
210    * @param oProps Properties Collection containing luceneindex directory
211    * @param sGuid Bug GUID
212    * @return Number of documents deleted
213    * @throws IllegalArgumentException If sTableName is not one of { k_bugs, k_newsmsgs, k_mime_msgs }
214    * @throws NoSuchFieldException If luceneindex property is not found at oProps
215    * @throws IllegalAccessException
216    * @throws IOException
217    * @throws NullPointerException If sGuid is <b>null</b>
218    */

219   public static int deleteBug(String JavaDoc sWorkArea, Properties JavaDoc oProps, String JavaDoc sGuid)
220     throws IllegalArgumentException JavaDoc, NoSuchFieldException JavaDoc,
221            IllegalAccessException JavaDoc, IOException JavaDoc, NullPointerException JavaDoc {
222       return Indexer.delete(DB.k_bugs, sWorkArea, oProps, sGuid);
223   } // delete
224
}
225
Popular Tags