KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2   Copyright (C) 2003 Know Gate S.L. All rights reserved.
3                       C/Oņa, 107 1š2 28050 Madrid (Spain)
4
5   Redistribution and use in source and binary forms, with or without
6   modification, are permitted provided that the following conditions
7   are met:
8
9   1. Redistributions of source code must retain the above copyright
10      notice, this list of conditions and the following disclaimer.
11
12   2. The end-user documentation included with the redistribution,
13      if any, must include the following acknowledgment:
14      "This product includes software parts from hipergate
15      (http://www.hipergate.org/)."
16      Alternately, this acknowledgment may appear in the software itself,
17      if and wherever such third-party acknowledgments normally appear.
18
19   3. The name hipergate must not be used to endorse or promote products
20      derived from this software without prior written permission.
21      Products derived from this software may not be called hipergate,
22      nor may hipergate appear in their name, without prior written
23      permission.
24
25   This library is distributed in the hope that it will be useful,
26   but WITHOUT ANY WARRANTY; without even the implied warranty of
27   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
28
29   You should have received a copy of hipergate License with this code;
30   if not, visit http://www.hipergate.org or mail to info@hipergate.org
31 */

32
33 package com.knowgate.lucene;
34
35 import java.math.BigDecimal JavaDoc;
36
37 import java.util.Date JavaDoc;
38 import java.util.Map JavaDoc;
39 import java.util.HashMap JavaDoc;
40 import java.util.Iterator JavaDoc;
41 import java.util.LinkedList JavaDoc;
42 import java.util.ListIterator JavaDoc;
43 import java.text.SimpleDateFormat JavaDoc;
44
45 import java.sql.DriverManager JavaDoc;
46 import java.sql.Connection JavaDoc;
47 import java.sql.Statement JavaDoc;
48 import java.sql.PreparedStatement JavaDoc;
49 import java.sql.ResultSet JavaDoc;
50 import java.sql.SQLException JavaDoc;
51
52 import java.util.Properties JavaDoc;
53 import java.io.IOException JavaDoc;
54 import java.io.FileNotFoundException JavaDoc;
55 import java.io.InputStream JavaDoc;
56 import java.io.FileInputStream JavaDoc;
57 import java.io.File JavaDoc;
58
59 import javax.mail.MessagingException JavaDoc;
60 import javax.mail.internet.MimeBodyPart JavaDoc;
61
62 import org.apache.lucene.analysis.Analyzer;
63 import org.apache.lucene.index.*;
64 import org.apache.lucene.document.*;
65
66 import org.htmlparser.beans.StringBean;
67
68 import com.knowgate.debug.DebugFile;
69 import com.knowgate.misc.Gadgets;
70 import com.knowgate.dfs.FileSystem;
71
72 import com.knowgate.hipermail.DBMimePart;
73
74 /**
75  * <p>Data Feeder from hipergate tables for Lucene</p>
76  * @author Sergio Montoro Ten
77  * @version 3.0
78  * @see http://jakarta.apache.org/lucene/docs/index.html
79  */

80
81 public class Indexer {
82
83   public final static String JavaDoc DEFAULT_ANALYZER = "org.apache.lucene.analysis.StopAnalyzer";
84
85   // ---------------------------------------------------------------------------
86

87   private static String JavaDoc IfNull(Connection JavaDoc oConn) throws SQLException JavaDoc {
88     String JavaDoc sDBMS = oConn.getMetaData().getDatabaseProductName();
89
90     if (sDBMS.equals("PostgreSQL"))
91       return "COALESCE";
92     else if (sDBMS.equals("Microsoft SQL Server"))
93       return "ISNULL";
94     else if (sDBMS.equals("Oracle"))
95       return "NVL";
96     else if (sDBMS.equals("MySQL"))
97       return "COALESCE";
98     else
99       return null;
100   }
101
102   // ---------------------------------------------------------------------------
103

104   private static boolean allowedTable(String JavaDoc sTableName) {
105     return sTableName.equalsIgnoreCase("k_bugs") || sTableName.equalsIgnoreCase("k_newsmsgs") || sTableName.equalsIgnoreCase("k_mime_msgs");
106   }
107
108   // ---------------------------------------------------------------------------
109

110   /**
111    * Optimize a given index
112    * @param oProps Properties Collection (typically loaded from hipergate.cnf)
113    * containing luceneindex property and (optionally) analyzer
114    * @param sTableName String Name of table to be indexed (currently only k_bugs, k_newsmsgs or k_mime_msgs are permitted)
115    * @param sWorkArea GUID of WorkArea to be optimized
116    * @throws NoSuchFieldException
117    * @throws IllegalArgumentException
118    * @throws ClassNotFoundException
119    * @throws IOException
120    * @throws InstantiationException
121    * @throws IllegalAccessException
122    */

123   public static void optimize(Properties JavaDoc oProps, String JavaDoc sTableName, String JavaDoc sWorkArea)
124     throws NoSuchFieldException JavaDoc,IllegalArgumentException JavaDoc, ClassNotFoundException JavaDoc,
125            FileNotFoundException JavaDoc,IOException JavaDoc,InstantiationException JavaDoc,IllegalAccessException JavaDoc {
126
127     if (!allowedTable(sTableName))
128       throw new IllegalArgumentException JavaDoc("Table name must be k_bugs or k_newsmsgs or k_mime_msgs");
129
130     if (DebugFile.trace) {
131       DebugFile.writeln("Begin Indexer.rebuild([Properties]" + sTableName);
132       DebugFile.incIdent();
133     }
134
135     String JavaDoc sDirectory = oProps.getProperty("luceneindex");
136
137     if (null==sDirectory) {
138       if (DebugFile.trace) DebugFile.decIdent();
139       throw new NoSuchFieldException JavaDoc ("Cannot find luceneindex property");
140     }
141
142     sDirectory = Gadgets.chomp(sDirectory, File.separator) + sTableName.toLowerCase();
143     if (null!=sWorkArea) sDirectory += File.separator + sWorkArea;
144
145     if (DebugFile.trace) DebugFile.writeln("index directory is " + sDirectory);
146
147     File JavaDoc oDir = new File JavaDoc(sDirectory);
148     if (!oDir.exists()) {
149       if (DebugFile.trace) DebugFile.decIdent();
150       throw new FileNotFoundException JavaDoc("Directory " + sDirectory + " does not exist");
151     }
152
153     if (DebugFile.trace)
154       DebugFile.writeln("Class.forName(" + oProps.getProperty("analyzer" , DEFAULT_ANALYZER) + ")");
155
156     Class JavaDoc oAnalyzer = Class.forName(oProps.getProperty("analyzer" , DEFAULT_ANALYZER));
157
158     if (DebugFile.trace)
159       DebugFile.writeln("new IndexWriter(...)");
160
161     IndexWriter oIWrt = new IndexWriter(sDirectory, (Analyzer) oAnalyzer.newInstance(), true);
162
163     if (DebugFile.trace) DebugFile.writeln("IndexWriter.optimize()");
164
165     oIWrt.optimize();
166
167     if (DebugFile.trace) DebugFile.writeln("IndexWriter.close()");
168
169     oIWrt.close();
170
171     if (DebugFile.trace) {
172       DebugFile.decIdent();
173       DebugFile.writeln("End Indexer.optimize()");
174     }
175   } // optimize
176

177   // ---------------------------------------------------------------------------
178

179   /**
180    * <p>Rebuild Full Text Index for a table</p>
181    * Indexed documents have the following fields:<br>
182    * <table border=1 cellpadding=4>
183    * <tr><td><b>Field Name</b></td><td><b>Description</b></td><td><b>Indexed</b></td><td><b>Stored</b></td></tr>
184    * <tr><td>workarea</td><td>GUID of WorkArea</td><td align=middle>Yes</td><td align=middle>Yes</td></tr>
185    * <tr><td>container</td><td>Name of Container (NewsGroup, Project, etc)</td><td align=middle>Yes</td><td align=middle>Yes</td></tr>
186    * <tr><td>guid</td><td>GUID for Retrieved Object</td><td align=middle>Yes</td><td align=middle>Yes</td></tr>
187    * <tr><td>number</td><td>Object Ordinal Identifier</td><td align=middle>Yes</td><td align=middle>Yes</td></tr>
188    * <tr><td>title</td><td>Title or Subject</td><td align=middle>Yes</td><td align=middle>Yes</td></tr>
189    * <tr><td>author</td><td>Author</td><td align=middle>Yes</td><td align=middle>Yes</td></tr>
190    * <tr><td>text</td><td>Document Text</td><td align=middle>Yes</td><td align=middle>No</td></tr>
191    * <tr><td>abstract</td><td>First 80 characters of text</td><td align=middle>No</td><td align=middle>Yes</td></tr>
192    * </table>
193    * @param oProps Properties Collection (typically loaded from hipergate.cnf) containing:<br>
194    * <b>driver</b> : Class name for JDBC driver<br>
195    * <b>dburl</b> : Database Connection URL<br>
196    * <b>dbuser</b> : Database User<br>
197    * <b>dbpassword</b> : Database User Password<br>
198    * <b>luceneindex</b> : Base path for Lucene index directories,
199    * the rebuilded index will be stored at a subdirectory called as the table name.<br>
200    * @param sTableName Name of table to be indexed (currently only k_bugs, k_newsmsgs or k_mime_msgs are permitted)
201    * <b>analyzer</b> : org.apache.lucene.analysis.Analyzer subclass name
202    * @param sWorkArea GUID of WorkArea to be rebuilt
203    * @throws NoSuchFieldException If any of the requiered properties of oProps is not found
204    * @throws ClassNotFoundException If JDBC driver or analyzer classes are not found
205    * @throws SQLException
206    * @throws IOException
207    * @throws IllegalArgumentException
208    * @throws IllegalAccessException
209    * @throws InstantiationException
210    */

211   public static void rebuild(Properties JavaDoc oProps, String JavaDoc sTableName, String JavaDoc sWorkArea)
212     throws SQLException JavaDoc, IOException JavaDoc, ClassNotFoundException JavaDoc,
213            IllegalArgumentException JavaDoc, NoSuchFieldException JavaDoc,
214            IllegalAccessException JavaDoc, InstantiationException JavaDoc {
215
216     String JavaDoc sGuid, sContainer, sTitle, sAuthor, sComments, sText;
217     Date JavaDoc dtCreated;
218     BigDecimal JavaDoc dNumber;
219     int iNumber, iSize;
220
221     final BigDecimal JavaDoc dZero = new BigDecimal JavaDoc(0);
222
223     // Check whether table name is any of the allowed ones
224
if (!allowedTable(sTableName))
225       throw new IllegalArgumentException JavaDoc("Table name must be k_bugs or k_newsmsgs or k_mime_msgs");
226
227     if (DebugFile.trace) {
228       DebugFile.writeln("Begin Indexer.rebuild([Properties]," + sTableName + "," + sWorkArea + ")");
229       DebugFile.incIdent();
230     }
231
232     // Get physical base path to index files from luceneindex property
233
String JavaDoc sDirectory = oProps.getProperty("luceneindex");
234
235     if (null==sDirectory) {
236       if (DebugFile.trace) DebugFile.decIdent();
237       throw new NoSuchFieldException JavaDoc ("Cannot find luceneindex property");
238     }
239
240     // Append WorkArea and table name to luceneindex base path
241
sDirectory = Gadgets.chomp(sDirectory, File.separator) + sTableName.toLowerCase();
242     if (null!=sWorkArea) sDirectory += File.separator + sWorkArea;
243
244     if (DebugFile.trace) DebugFile.writeln("index directory is " + sDirectory);
245
246     if (null==oProps.getProperty("driver")) {
247       if (DebugFile.trace) DebugFile.decIdent();
248       throw new NoSuchFieldException JavaDoc ("Cannot find driver property");
249     }
250
251     if (null==oProps.getProperty("dburl")) {
252       if (DebugFile.trace) DebugFile.decIdent();
253       throw new NoSuchFieldException JavaDoc ("Cannot find dburl property");
254     }
255
256     if (DebugFile.trace) DebugFile.writeln("Class.forName(" + oProps.getProperty("analyzer" , DEFAULT_ANALYZER) + ")");
257
258     Class JavaDoc oAnalyzer = Class.forName(oProps.getProperty("analyzer" , DEFAULT_ANALYZER));
259
260     if (DebugFile.trace) DebugFile.writeln("Class.forName(" + oProps.getProperty("driver") + ")");
261
262     Class JavaDoc oDriver = Class.forName(oProps.getProperty("driver"));
263
264     if (DebugFile.trace) DebugFile.writeln("IndexReader.open("+sDirectory+")");
265
266     // *********************************************************************
267
// Delete every document from this table and WorkArea before re-indexing
268
File JavaDoc oDir = new File JavaDoc(sDirectory);
269     if (oDir.exists()) {
270       IndexReader oReader = IndexReader.open(sDirectory);
271       int iDeleted = oReader.delete(new Term("workarea", sWorkArea));
272       oReader.close();
273     } else {
274       FileSystem oFS = new FileSystem();
275       try { oFS.mkdirs(sDirectory); } catch (Exception JavaDoc e) { throw new IOException JavaDoc(e.getClass().getName()+" "+e.getMessage()); }
276     }
277     // *********************************************************************
278

279     if (DebugFile.trace) DebugFile.writeln("new IndexWriter("+sDirectory+",[Analyzer], true)");
280
281     IndexWriter oIWrt = new IndexWriter(sDirectory, (Analyzer) oAnalyzer.newInstance(), true);
282
283     if (DebugFile.trace)
284       DebugFile.writeln("DriverManager.getConnection(" + oProps.getProperty("dburl") + ", ...)");
285
286     Connection JavaDoc oConn = DriverManager.getConnection(oProps.getProperty("dburl"), oProps.getProperty("dbuser"),oProps.getProperty("dbpassword"));
287     oConn.setAutoCommit(true);
288
289     Statement JavaDoc oStmt = oConn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
290     ResultSet JavaDoc oRSet;
291
292     if (sTableName.equalsIgnoreCase("k_bugs")) {
293
294       if (DebugFile.trace)
295         DebugFile.writeln("Statement.executeQuery(SELECT p.gu_workarea,p.nm_project,b.gu_bug,b.tl_bug,b.dt_modified," + IfNull(oConn) + "(b.nm_reporter,'')," + IfNull(oConn) + "(b.tx_bug_brief,'')," + IfNull(oConn) + "(b.tx_comments,'') FROM k_bugs b, k_projects p WHERE b.gu_project=p.gu_project AND p.gu_owner='"+sWorkArea+"')");
296
297       oRSet = oStmt.executeQuery("SELECT p.gu_owner,p.nm_project,b.gu_bug,b.pg_bug,b.tl_bug,b.dt_modified," + IfNull(oConn) + "(b.nm_reporter,'')," + IfNull(oConn) + "(b.tx_comments,'')," + IfNull(oConn) + "(b.tx_bug_brief,'') FROM k_bugs b, k_projects p WHERE b.gu_project=p.gu_project AND p.gu_owner='"+sWorkArea+"'");
298
299       while (oRSet.next()) {
300         sWorkArea = oRSet.getString(1);
301         sContainer = oRSet.getString(2);
302         sGuid = oRSet.getString(3);
303         iNumber = oRSet.getInt(4);
304         sTitle = oRSet.getString(5);
305         dtCreated = oRSet.getDate(6);
306         sAuthor = oRSet.getString(7);
307         sComments = oRSet.getString(8);
308         if (null==sComments) sComments = "";
309         sText = oRSet.getString(9);
310         if (null==sText) sText = "";
311         BugIndexer.addBug(oIWrt, sGuid, iNumber, sWorkArea, sContainer, sTitle, sAuthor, dtCreated, sComments, sText);
312       } // wend
313
oRSet.close();
314     }
315
316     else if (sTableName.equalsIgnoreCase("k_newsmsgs")) {
317
318       if (DebugFile.trace)
319         DebugFile.writeln("Statement.executeQuery(SELECT g.gu_workarea,c.nm_category,m.gu_msg,m.tx_subject,m.dt_published," + IfNull(oConn) + "(b.nm_author,'')," + IfNull(oConn) + "(b.tx_msg,'') FROM k_newsmsgs m, k_categories c, k_newsgroups g, k_x_cat_objs x WHERE m.id_status=0 AND m.gu_msg=x.gu_object AND x.gu_category=g.gu_newsgrp AND c.gu_category=g.gu_newsgrp AND g.gu_workarea='"+sWorkArea+"')");
320
321       oRSet = oStmt.executeQuery("SELECT g.gu_workarea,c.nm_category,m.gu_msg,m.tx_subject,m.dt_published," + IfNull(oConn) + "(m.nm_author,'')," + IfNull(oConn) + "(m.tx_msg,'') FROM k_newsmsgs m, k_categories c, k_newsgroups g, k_x_cat_objs x WHERE m.id_status=0 AND m.gu_msg=x.gu_object AND x.gu_category=g.gu_newsgrp AND c.gu_category=g.gu_newsgrp AND g.gu_workarea='"+sWorkArea+"'");
322
323       while (oRSet.next()) {
324         sWorkArea = oRSet.getString(1);
325         sContainer = oRSet.getString(2);
326         sGuid = oRSet.getString(3);
327         sTitle = oRSet.getString(4);
328         dtCreated = oRSet.getDate(5);
329         sAuthor = oRSet.getString(6);
330         sText = oRSet.getString(7);
331         NewsMessageIndexer.addNewsMessage(oIWrt, sGuid, sWorkArea, sContainer, sTitle, sAuthor, dtCreated, sText);
332       } // wend
333
oRSet.close();
334     }
335     else if (sTableName.equalsIgnoreCase("k_mime_msgs")) {
336
337       LinkedList JavaDoc oIndexedGuids = new LinkedList JavaDoc();
338
339       PreparedStatement JavaDoc oRecp = oConn.prepareStatement("SELECT tx_personal,tx_email FROM k_inet_addrs WHERE tp_recipient<>'to' AND gu_mimemsg=?", ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
340
341       if (DebugFile.trace)
342         DebugFile.writeln("Statement.executeQuery(SELECT g.gu_workarea,c.nm_category,m.gu_mimemsg,m.tx_subject,m.nm_from,m.tx_mail_from,m.pg_mimemsg,m.de_mimemsg,m.dt_sent,m.len_mimemsg,m.by_content FROM k_mime_msgs m, k_categories c WHERE m.bo_deleted<>0 AND m.bo_draft<>0 AND m.gu_category=c.gu_category AND m.gu_workarea='"+sWorkArea+"')");
343
344       oRSet = oStmt.executeQuery("SELECT g.gu_workarea,c.nm_category,m.gu_mimemsg,m.tx_subject,m.nm_from,m.tx_mail_from,m.pg_mimemsg,m.de_mimemsg,m.dt_sent,m.len_mimemsg,m.by_content FROM k_mime_msgs m, k_categories c WHERE m.bo_deleted<>0 AND m.bo_draft<>0 AND m.gu_category=c.gu_category AND m.gu_workarea='"+sWorkArea+"'");
345
346       while (oRSet.next()) {
347
348         sWorkArea = oRSet.getString(1);
349         sContainer = oRSet.getString(2);
350         sGuid = oRSet.getString(3);
351         sTitle = oRSet.getString(4);
352         sAuthor = oRSet.getString(5);
353         if (oRSet.wasNull()) sAuthor = "";
354         sAuthor += " " + oRSet.getString(6);
355         dNumber = oRSet.getBigDecimal(7);
356         if (oRSet.wasNull()) dNumber = dZero;
357         sComments = oRSet.getString(8);
358         dtCreated = oRSet.getDate(9);
359         iSize = oRSet.getInt(10);
360
361         if (DebugFile.trace) DebugFile.writeln("Indexing message "+sGuid+" - "+sTitle);
362
363         InputStream JavaDoc oStrm = oRSet.getBinaryStream(11);
364
365         String JavaDoc sRecipients = "";
366         oRecp.setString(1, sGuid);
367         ResultSet JavaDoc oRecs = oRecp.executeQuery();
368         while (oRecs.next()) {
369           sRecipients += oRecs.getString(1)+" "+oRecs.getString(2)+" ";
370         } // wend
371
oRecs.close();
372
373         MailIndexer.addMail(oIWrt, sGuid, dNumber, sWorkArea, sContainer, sTitle,
374                             sAuthor, sRecipients, dtCreated, sComments, oStrm, iSize);
375
376         oIndexedGuids.add(sGuid);
377       } // wend
378
oRSet.close();
379       oRecp.close();
380
381       PreparedStatement JavaDoc oUpdt = oConn.prepareStatement("UPDATE k_mime_msgs SET bo_indexed=1 WHERE gu_mimemsg=?");
382       ListIterator JavaDoc oIter = oIndexedGuids.listIterator();
383       while (oIter.hasNext()) {
384         oUpdt.setObject(1, oIter.next(), java.sql.Types.CHAR);
385         oUpdt.executeUpdate();
386       } // wend
387
oUpdt.close();
388     } // fi
389

390     oStmt.close();
391     oConn.close();
392
393     if (DebugFile.trace) DebugFile.writeln("IndexWriter.optimize()");
394
395     oIWrt.optimize();
396
397     if (DebugFile.trace) DebugFile.writeln("IndexWriter.close()");
398
399     oIWrt.close();
400
401     if (DebugFile.trace) {
402       DebugFile.decIdent();
403       DebugFile.writeln("End Indexer.rebuild()");
404     }
405   } // rebuild
406

407   /**
408    *
409    * @param oProps
410    * @param sTableName
411    * @throws SQLException
412    * @throws IOException
413    * @throws ClassNotFoundException
414    * @throws IllegalArgumentException
415    * @throws NoSuchFieldException
416    * @throws IllegalAccessException
417    * @throws InstantiationException
418    */

419   public static void rebuild(Properties JavaDoc oProps, String JavaDoc sTableName)
420     throws SQLException JavaDoc, IOException JavaDoc, ClassNotFoundException JavaDoc,
421            IllegalArgumentException JavaDoc, NoSuchFieldException JavaDoc,
422            IllegalAccessException JavaDoc, InstantiationException JavaDoc {
423   }
424
425   public static void add(IndexWriter oIWrt,
426                          Map JavaDoc oKeywords, Map JavaDoc oTexts, Map JavaDoc oUnStored)
427     throws ClassNotFoundException JavaDoc, IOException JavaDoc,
428            IllegalArgumentException JavaDoc, NoSuchFieldException JavaDoc,
429            IllegalAccessException JavaDoc, InstantiationException JavaDoc,
430            NullPointerException JavaDoc {
431
432     String JavaDoc sFieldName;
433     Object JavaDoc oFieldValue;
434     Document oDoc = new Document();
435     Iterator JavaDoc oKeys = oKeywords.keySet().iterator();
436     while (oKeys.hasNext()) {
437       sFieldName = (String JavaDoc) oKeys.next();
438       oFieldValue = oKeywords.get(sFieldName);
439       if (null==oFieldValue) oFieldValue = "";
440       if (oFieldValue.getClass().getName().equals("java.util.Date"))
441         oDoc.add (Field.Keyword(sFieldName, (Date JavaDoc) oFieldValue));
442       else
443         oDoc.add (Field.Keyword(sFieldName, (String JavaDoc) oFieldValue));
444     } // wend
445
Iterator JavaDoc oTxts = oTexts.keySet().iterator();
446     while (oTxts.hasNext()) {
447       sFieldName = (String JavaDoc) oTxts.next();
448       oFieldValue = oTexts.get(sFieldName);
449       if (null==oFieldValue) oFieldValue = "";
450       oDoc.add (Field.Text(sFieldName, (String JavaDoc) oFieldValue));
451     } // wend
452
Iterator JavaDoc oUnStor = oUnStored.keySet().iterator();
453     while (oUnStor.hasNext()) {
454       sFieldName = (String JavaDoc) oUnStor.next();
455       oFieldValue = oUnStored.get(sFieldName);
456       if (null==oFieldValue) oFieldValue = "";
457       oDoc.add (Field.Text(sFieldName, (String JavaDoc) oFieldValue));
458     } // wend
459
oIWrt.addDocument(oDoc);
460   } // add
461

462   public static void add(String JavaDoc sTableName, String JavaDoc sDirectory, String JavaDoc sAnalyzer,
463                          Map JavaDoc oKeywords, Map JavaDoc oTexts, Map JavaDoc oUnStored)
464       throws ClassNotFoundException JavaDoc, IOException JavaDoc,
465              IllegalArgumentException JavaDoc, NoSuchFieldException JavaDoc,
466              IllegalAccessException JavaDoc, InstantiationException JavaDoc,
467              NullPointerException JavaDoc {
468
469     if (!allowedTable(sTableName))
470       throw new IllegalArgumentException JavaDoc("Table name must be k_bugs or k_newsmsgs or k_mime_msgs");
471
472     if (null==sDirectory)
473       throw new NoSuchFieldException JavaDoc ("Cannot find luceneindex property");
474
475     File JavaDoc oDir = new File JavaDoc(sDirectory);
476     if (!oDir.exists()) {
477       FileSystem oFS = new FileSystem();
478       try { oFS.mkdirs(sDirectory); } catch (Exception JavaDoc e) { throw new IOException JavaDoc(e.getClass().getName()+" "+e.getMessage()); }
479     }
480
481     Class JavaDoc oAnalyzer = Class.forName((sAnalyzer==null) ? DEFAULT_ANALYZER : sAnalyzer);
482
483     IndexWriter oIWrt = new IndexWriter(sDirectory, (Analyzer) oAnalyzer.newInstance(), true);
484
485     add (oIWrt, oKeywords, oTexts, oUnStored);
486
487     oIWrt.close();
488   } // add
489

490   /**
491    * Add a document to the index
492    * @param sTableName k_bugs, k_newsmsgs or k_mime_msgs
493    * @param oProps Properties Collection containing luceneindex directory
494    * @param sWorkArea WorkArea for document
495    * @param sContainer GUID of Category or NewsGroup to which documento belongs
496    * @param sGUID Document GUID
497    * @param iNumber Document number (optional, may be zero)
498    * @param sTitle Document Title (optional, may be <b>null</b>)
499    * @param sText Document text (optional, may be <b>null</b>)
500    * @param sAuthor Document author (optional, may be <b>null</b>)
501    * @param sAbstract Document abstract (optional, may be <b>null</b>)
502    * @param sComments Document comments (optional, may be <b>null</b>)
503    * @throws ClassNotFoundException
504    * @throws IOException
505    * @throws IllegalArgumentException If sTableName is not one of { k_bugs, k_newsmsgs, k_mime_msgs }
506    * @throws NoSuchFieldException If luceneindex property is not found at oProps
507    * @throws IllegalAccessException
508    * @throws InstantiationException
509    * @throws NullPointerException
510    * @deprecated Use add method from Indexer subclasses instead
511    */

512
513   public static void add(String JavaDoc sTableName, Properties JavaDoc oProps,
514                          String JavaDoc sGUID, int iNumber, String JavaDoc sWorkArea,
515                          String JavaDoc sContainer, String JavaDoc sTitle,
516                          String JavaDoc sText, String JavaDoc sAuthor,
517                          String JavaDoc sAbstract, String JavaDoc sComments)
518       throws ClassNotFoundException JavaDoc, IOException JavaDoc,
519              IllegalArgumentException JavaDoc, NoSuchFieldException JavaDoc,
520              IllegalAccessException JavaDoc, InstantiationException JavaDoc,
521              NullPointerException JavaDoc {
522
523     if (null==sGUID)
524       throw new NullPointerException JavaDoc ("Document GUID may not be null");
525
526     if (!sTableName.equalsIgnoreCase("k_bugs") && !sTableName.equalsIgnoreCase("k_newsmsgs") && !sTableName.equalsIgnoreCase("k_mime_msgs"))
527       throw new IllegalArgumentException JavaDoc("Table name must be k_bugs or k_newsmsgs or k_mime_msgs");
528
529     String JavaDoc sDirectory = oProps.getProperty("luceneindex");
530
531     if (null==sDirectory)
532       throw new NoSuchFieldException JavaDoc ("Cannot find luceneindex property");
533
534     sDirectory = Gadgets.chomp(sDirectory, File.separator) + sTableName.toLowerCase() + File.separator + sWorkArea;
535     File JavaDoc oDir = new File JavaDoc(sDirectory);
536     if (!oDir.exists()) {
537       FileSystem oFS = new FileSystem();
538       try { oFS.mkdirs(sDirectory); } catch (Exception JavaDoc e) { throw new IOException JavaDoc(e.getClass().getName()+" "+e.getMessage()); }
539     }
540
541     Class JavaDoc oAnalyzer = Class.forName(oProps.getProperty("analyzer" , DEFAULT_ANALYZER));
542
543     HashMap JavaDoc oKeys = new HashMap JavaDoc(11);
544     oKeys.put("workarea" , sWorkArea==null ? "" : sWorkArea);
545     oKeys.put("container", sContainer==null ? "" : sContainer);
546     oKeys.put("guid", sGUID);
547     oKeys.put("number", String.valueOf(iNumber));
548     HashMap JavaDoc oTexts = new HashMap JavaDoc(11);
549     oTexts.put("title", sTitle==null ? "" : sTitle);
550     oTexts.put("author", sAuthor==null ? "" : sAuthor);
551     oTexts.put("abstract", sAbstract==null ? "" : Gadgets.left(sAbstract, 80));
552     HashMap JavaDoc oUnstor = new HashMap JavaDoc(11);
553     oUnstor.put("comments", sComments==null ? "" : sComments);
554     oUnstor.put("text", sText==null ? "" : sText);
555
556     IndexWriter oIWrt = new IndexWriter(sDirectory, (Analyzer) oAnalyzer.newInstance(), true);
557     add(oIWrt, oKeys, oTexts, oUnstor);
558     oIWrt.close();
559   } // add
560

561   // ---------------------------------------------------------------------------
562

563   /**
564    * Delete a document with a given GUID
565    * @param sTableName k_bugs, k_newsmsgs or k_mime_msgs
566    * @param oProps Properties Collection containing luceneindex directory
567    * @param sGuid Document GUID
568    * @return Number of documents deleted
569    * @throws IllegalArgumentException If sTableName is not one of { k_bugs, k_newsmsgs, k_mime_msgs }
570    * @throws NoSuchFieldException If luceneindex property is not found at oProps
571    * @throws IllegalAccessException
572    * @throws IOException
573    * @throws NullPointerException If sGuid is <b>null</b>
574    */

575   public static int delete(String JavaDoc sTableName, String JavaDoc sWorkArea, Properties JavaDoc oProps, String JavaDoc sGuid)
576       throws IllegalArgumentException JavaDoc, NoSuchFieldException JavaDoc,
577              IllegalAccessException JavaDoc, IOException JavaDoc, NullPointerException JavaDoc {
578
579     if (null==sGuid)
580       throw new NullPointerException JavaDoc ("Document GUID may not be null");
581
582     if (!allowedTable(sTableName))
583       throw new IllegalArgumentException JavaDoc("Table name must be k_bugs or k_newsmsgs or k_mime_msgs");
584
585     String JavaDoc sDirectory = oProps.getProperty("luceneindex");
586
587     if (null==sDirectory)
588       throw new NoSuchFieldException JavaDoc ("Cannot find luceneindex property");
589
590     sDirectory = Gadgets.chomp(sDirectory, File.separator) + sTableName.toLowerCase() + File.separator + sWorkArea;
591     File JavaDoc oDir = new File JavaDoc(sDirectory);
592     if (!oDir.exists()) {
593       FileSystem oFS = new FileSystem();
594       try { oFS.mkdirs(sDirectory); } catch (Exception JavaDoc e) { throw new IOException JavaDoc(e.getClass().getName()+" "+e.getMessage()); }
595     }
596
597     IndexReader oReader = IndexReader.open(sDirectory);
598
599     int iDeleted = oReader.delete(new Term("guid", sGuid));
600
601     oReader.close();
602
603     return iDeleted;
604   } // delete
605

606   // ---------------------------------------------------------------------------
607

608   private static void printUsage() {
609     System.out.println("");
610     System.out.println("Usage:");
611     System.out.println("Indexer cnf_path rebuild {k_bugs|k_newsmsgs|k_mime_msgs}");
612     System.out.println("cnf_path : Full path to hipergate.cnf file");
613   }
614
615   // ---------------------------------------------------------------------------
616

617   /**
618    * <p>Static method for calling indexer from the command line</p>
619    * @param argv String[] Must have two arguments, the first one is the full path
620    * to hipergate.cnf or other properties file containing database connection parameters.<br>
621    * The second argument must be "rebuild".<br>
622    * The third argument is one of {k_bugs|k_newsmsgs|k_mime_msgs} indicating which table index is to be rebuilt.<br>
623    * Command line example: java -cp ... com.knowgate.lucene.Indexer /etc/hipergate.cnf rebuild k_mime_msgs
624    * @throws SQLException
625    * @throws IOException
626    * @throws ClassNotFoundException
627    * @throws IllegalArgumentException
628    * @throws NoSuchFieldException
629    * @throws IllegalAccessException
630    * @throws InstantiationException
631    */

632   public static void main(String JavaDoc[] argv)
633     throws SQLException JavaDoc, IOException JavaDoc, ClassNotFoundException JavaDoc,
634     IllegalArgumentException JavaDoc, NoSuchFieldException JavaDoc,
635     IllegalAccessException JavaDoc, InstantiationException JavaDoc {
636
637     if (argv.length!=3)
638       printUsage();
639     else if (!argv[1].equals("rebuild")) {
640       printUsage();
641     } else if (!allowedTable(argv[2])) {
642       printUsage();
643     }
644     else {
645       Properties JavaDoc oProps = new Properties JavaDoc();
646       FileInputStream JavaDoc oCNF = new FileInputStream JavaDoc(argv[0]);
647       oProps.load(oCNF);
648       oCNF.close();
649       rebuild (oProps, argv[2]);
650     }
651   } // main
652
}
653
Popular Tags