001: package com.knowgate.lucene;
002:
003: import java.util.Date;
004: import java.util.Properties;
005:
006: import java.io.IOException;
007: import java.sql.SQLException;
008:
009: import org.apache.lucene.index.IndexWriter;
010: import org.apache.lucene.document.Document;
011: import org.apache.lucene.document.Field;
012:
013: import com.knowgate.jdc.JDCConnection;
014: import com.knowgate.dataobjs.DB;
015: import com.knowgate.projtrack.Bug;
016: import com.knowgate.misc.Gadgets;
017: import com.knowgate.dfs.FileSystem;
018: import org.apache.lucene.analysis.Analyzer;
019: import java.io.File;
020: import java.util.HashMap;
021: import org.apache.lucene.index.Term;
022: import org.apache.lucene.index.IndexReader;
023:
024: /**
025: * Indexer subclass for hipergate bugs
026: * @author Sergio Montoro Ten
027: * @version 3.0
028: */
029: public class BugIndexer extends Indexer {
030:
031: public BugIndexer() {
032: }
033:
034: /**
035: * Add bug to index
036: * @param oIWrt IndexWriter
037: * @param sGuid String Bug GUID
038: * @param iNumber int Bug Number
039: * @param sWorkArea String GUID of WorkArea to which bug belongs
040: * @param sProject String GUID of project to which bug belongs
041: * @param sTitle String Title
042: * @param sReportedBy String Author
043: * @param dtCreated Date Created
044: * @param sComments String Comments
045: * @param sText String Bug Description
046: * @throws ClassNotFoundException
047: * @throws IOException
048: * @throws IllegalArgumentException
049: * @throws NoSuchFieldException
050: * @throws IllegalAccessException
051: * @throws InstantiationException
052: * @throws NullPointerException
053: */
054: public static void addBug(IndexWriter oIWrt, String sGuid,
055: int iNumber, String sWorkArea, String sProject,
056: String sTitle, String sWriter, String sReportedBy,
057: Date dtCreated, String sType, Short oPriority,
058: Short oSeverity, String sStatus, String sComments,
059: String sText) throws ClassNotFoundException, IOException,
060: IllegalArgumentException, NoSuchFieldException,
061: IllegalAccessException, InstantiationException,
062: NullPointerException {
063:
064: Document oDoc = new Document();
065: oDoc.add(Field.Keyword("workarea", sWorkArea));
066: oDoc.add(Field.Keyword("container", sProject));
067: oDoc.add(Field.Keyword("guid", sGuid));
068: oDoc.add(Field.Keyword("number", String.valueOf(iNumber)));
069: oDoc.add(Field.Text("title", Gadgets.ASCIIEncode(sTitle)));
070: oDoc.add(Field.Keyword("created", dtCreated));
071: oDoc.add(Field.Keyword("writer", sWriter));
072: if (null != sStatus)
073: oDoc.add(Field.Keyword("status", sStatus));
074: if (null != sType)
075: oDoc.add(Field.Keyword("type", sType));
076: if (null != oPriority)
077: oDoc.add(Field.Keyword("priority", oPriority.toString()));
078: if (null != oSeverity)
079: oDoc.add(Field.Keyword("severity", oSeverity.toString()));
080: if (null != sReportedBy)
081: oDoc.add(Field.Text("author", Gadgets
082: .ASCIIEncode(sReportedBy)));
083: oDoc.add(Field.UnStored("comments", Gadgets
084: .ASCIIEncode(sComments)));
085: if (null == sText) {
086: oDoc.add(Field.UnStored("text", ""));
087: oDoc.add(Field.UnStored("abstract", ""));
088: } else {
089: oDoc
090: .add(Field.UnStored("text", Gadgets
091: .ASCIIEncode(sText)));
092: if (sText.length() > 80)
093: oDoc.add(new Field("abstract", sText.substring(0, 80)
094: .replace('\n', ' '), true, false, false));
095: else
096: oDoc.add(new Field("abstract",
097: sText.replace('\n', ' '), true, false, false));
098: }
099: oIWrt.addDocument(oDoc);
100: } // addBug
101:
102: /**
103: * Add bug to index
104: * @param oIWrt IndexWriter
105: * @param sGuid String Bug GUID
106: * @param iNumber int Bug Number
107: * @param sWorkArea String GUID of WorkArea to which bug belongs
108: * @param sProject String GUID of project to which bug belongs
109: * @param sTitle String Title
110: * @param sReportedBy String Author
111: * @param dtCreated Date Created
112: * @param sComments String Comments
113: * @param sText String Bug Description
114: * @throws ClassNotFoundException
115: * @throws IOException
116: * @throws IllegalArgumentException
117: * @throws NoSuchFieldException
118: * @throws IllegalAccessException
119: * @throws InstantiationException
120: * @throws NullPointerException
121: */
122: public static void addBug(IndexWriter oIWrt, String sGuid,
123: int iNumber, String sWorkArea, String sProject,
124: String sTitle, String sReportedBy, Date dtCreated,
125: String sComments, String sText)
126: throws ClassNotFoundException, IOException,
127: IllegalArgumentException, NoSuchFieldException,
128: IllegalAccessException, InstantiationException,
129: NullPointerException {
130: addBug(oIWrt, sGuid, iNumber, sWorkArea, sProject, sTitle,
131: null, sReportedBy, dtCreated, null, null, null, null,
132: sComments, sText);
133: } // addBug
134:
135: /**
136: * Add bug to index
137: * @param oIWrt IndexWriter
138: * @param oCon JDCConnection
139: * @param sWorkArea String GUID of WorkArea where bug must be added
140: * @param oBug Bug
141: * @throws SQLException
142: * @throws IOException
143: * @throws ClassNotFoundException
144: * @throws NoSuchFieldException
145: * @throws IllegalAccessException
146: * @throws InstantiationException
147: * @throws NullPointerException
148: */
149: public static void addBug(IndexWriter oIWrt, JDCConnection oCon,
150: String sWorkArea, Bug oBug) throws SQLException,
151: IOException, ClassNotFoundException, NoSuchFieldException,
152: IllegalAccessException, InstantiationException,
153: NullPointerException {
154: Short oSeverity;
155: Short oPriority;
156:
157: if (null == oBug)
158: throw new NullPointerException(
159: "BugIndexer.addBug() Bug may not be null");
160: if (null == oCon)
161: throw new NullPointerException(
162: "BugIndexer.addBug() JDBC Connection may not be null");
163: if (oCon.isClosed())
164: throw new SQLException(
165: "BugIndexer.addBug() JDBC connection is closed");
166:
167: if (oBug.isNull(DB.od_priority))
168: oPriority = null;
169: else
170: oPriority = new Short(oBug.getShort(DB.od_priority));
171:
172: if (oBug.isNull(DB.od_severity))
173: oSeverity = null;
174: else
175: oSeverity = new Short(oBug.getShort(DB.od_severity));
176:
177: addBug(oIWrt, oBug.getString(DB.gu_bug),
178: oBug.getInt(DB.pg_bug), oBug.getString(sWorkArea), oBug
179: .getString(DB.gu_project), oBug.getStringNull(
180: DB.tl_bug, ""), oBug.getString(DB.gu_writer),
181: oBug.getStringNull(DB.nm_reporter, ""), oBug
182: .getCreationDate(oCon), oBug.getStringNull(
183: DB.tp_bug, null), oPriority, oSeverity, oBug
184: .getStringNull(DB.tx_status, null), oBug
185: .getStringNull(DB.tx_comments, null), oBug
186: .getStringNull(DB.tx_bug_brief, null));
187: }
188:
189: /**
190: * Add bug to index
191: * @param oProps Properties
192: * @param oCon JDCConnection
193: * @param sWorkArea String
194: * @param oBug Bug
195: * @throws SQLException
196: * @throws IOException
197: * @throws ClassNotFoundException
198: * @throws NoSuchFieldException
199: * @throws IllegalAccessException
200: * @throws InstantiationException
201: * @throws NullPointerException
202: * @throws NoSuchFieldException
203: */
204: public static void addBug(Properties oProps, JDCConnection oCon,
205: String sWorkArea, Bug oBug) throws SQLException,
206: IOException, ClassNotFoundException, NoSuchFieldException,
207: IllegalAccessException, InstantiationException,
208: NullPointerException, NoSuchFieldException {
209:
210: String sDirectory = oProps.getProperty("luceneindex");
211:
212: if (null == sDirectory)
213: throw new NoSuchFieldException(
214: "Cannot find luceneindex property");
215:
216: sDirectory = Gadgets.chomp(sDirectory, File.separator)
217: + DB.k_bugs + File.separator + sWorkArea;
218: File oDir = new File(sDirectory);
219: if (!oDir.exists()) {
220: FileSystem oFS = new FileSystem();
221: try {
222: oFS.mkdirs(sDirectory);
223: } catch (Exception e) {
224: throw new IOException(e.getClass().getName() + " "
225: + e.getMessage());
226: }
227: } // fi
228:
229: Class oAnalyzer = Class.forName(oProps.getProperty("analyzer",
230: DEFAULT_ANALYZER));
231:
232: IndexWriter oIWrt = new IndexWriter(sDirectory,
233: (Analyzer) oAnalyzer.newInstance(), true);
234: addBug(oIWrt, oCon, sWorkArea, oBug);
235: oIWrt.close();
236: } // addBug
237:
238: /**
239: * Delete a bug with a given GUID
240: * @param oProps Properties Collection containing luceneindex directory
241: * @param sGuid Bug GUID
242: * @return Number of documents deleted
243: * @throws IllegalArgumentException If sTableName is not one of { k_bugs, k_newsmsgs, k_mime_msgs }
244: * @throws NoSuchFieldException If luceneindex property is not found at oProps
245: * @throws IllegalAccessException
246: * @throws IOException
247: * @throws NullPointerException If sGuid is <b>null</b>
248: */
249: public static int deleteBug(String sWorkArea, Properties oProps,
250: String sGuid) throws IllegalArgumentException,
251: NoSuchFieldException, IllegalAccessException, IOException,
252: NullPointerException {
253: return Indexer.delete(DB.k_bugs, sWorkArea, oProps, sGuid);
254: } // delete
255: }
|