1 package com.knowgate.lucene; 2 3 import java.util.Date ; 4 import java.util.Properties ; 5 6 import java.io.IOException ; 7 import java.sql.SQLException ; 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 ; 20 import java.util.HashMap ; 21 import org.apache.lucene.index.Term; 22 import org.apache.lucene.index.IndexReader; 23 24 29 public class BugIndexer extends Indexer { 30 31 public BugIndexer() { } 32 33 34 54 public static void addBug(IndexWriter oIWrt, 55 String sGuid, int iNumber, String sWorkArea, 56 String sProject, String sTitle, String sWriter, 57 String sReportedBy, Date dtCreated, 58 String sType, Short oPriority, Short oSeverity, 59 String sStatus, String sComments, String sText) 60 throws ClassNotFoundException , IOException , IllegalArgumentException , 61 NoSuchFieldException , IllegalAccessException , InstantiationException , 62 NullPointerException { 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 } 91 111 public static void addBug(IndexWriter oIWrt, 112 String sGuid, int iNumber, String sWorkArea, 113 String sProject, String sTitle, 114 String sReportedBy, Date dtCreated, 115 String sComments, String sText) 116 throws ClassNotFoundException , IOException , IllegalArgumentException , 117 NoSuchFieldException , IllegalAccessException , InstantiationException , 118 NullPointerException { 119 addBug(oIWrt, sGuid, iNumber, sWorkArea, sProject, sTitle, null, sReportedBy, 120 dtCreated, null, null, null, null, sComments, sText); 121 } 123 137 public static void addBug(IndexWriter oIWrt, JDCConnection oCon, 138 String sWorkArea, Bug oBug) 139 throws SQLException ,IOException ,ClassNotFoundException ,NoSuchFieldException , 140 IllegalAccessException , InstantiationException , NullPointerException { 141 Short oSeverity; 142 Short oPriority; 143 144 if (null==oBug) throw new NullPointerException ("BugIndexer.addBug() Bug may not be null"); 145 if (null==oCon) throw new NullPointerException ("BugIndexer.addBug() JDBC Connection may not be null"); 146 if (oCon.isClosed()) throw new SQLException ("BugIndexer.addBug() JDBC connection is closed"); 147 148 if (oBug.isNull(DB.od_priority)) 149 oPriority = null; 150 else 151 oPriority = new Short (oBug.getShort(DB.od_priority)); 152 153 if (oBug.isNull(DB.od_severity)) 154 oSeverity = null; 155 else 156 oSeverity = new Short (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 182 public static void addBug(Properties oProps, JDCConnection oCon, 183 String sWorkArea, Bug oBug) 184 throws SQLException ,IOException ,ClassNotFoundException ,NoSuchFieldException , 185 IllegalAccessException , InstantiationException , NullPointerException , 186 NoSuchFieldException { 187 188 189 String sDirectory = oProps.getProperty("luceneindex"); 190 191 if (null==sDirectory) 192 throw new NoSuchFieldException ("Cannot find luceneindex property"); 193 194 sDirectory = Gadgets.chomp(sDirectory, File.separator) + DB.k_bugs + File.separator + sWorkArea; 195 File oDir = new File (sDirectory); 196 if (!oDir.exists()) { 197 FileSystem oFS = new FileSystem(); 198 try { oFS.mkdirs(sDirectory); } catch (Exception e) { throw new IOException (e.getClass().getName()+" "+e.getMessage()); } 199 } 201 Class 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 } 208 219 public static int deleteBug(String sWorkArea, Properties oProps, String sGuid) 220 throws IllegalArgumentException , NoSuchFieldException , 221 IllegalAccessException , IOException , NullPointerException { 222 return Indexer.delete(DB.k_bugs, sWorkArea, oProps, sGuid); 223 } } 225 | Popular Tags |