001: /*
002: * This file is part of PFIXCORE.
003: *
004: * PFIXCORE is free software; you can redistribute it and/or modify it under the
005: * terms of the GNU Lesser General Public License as published by the Free
006: * Software Foundation; either version 2 of the License, or (at your option) any
007: * later version.
008: *
009: * PFIXCORE is distributed in the hope that it will be useful, but WITHOUT ANY
010: * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
011: * A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012: * details.
013: *
014: * You should have received a copy of the GNU Lesser General Public License
015: * along with PFIXCORE; if not, write to the Free Software Foundation, Inc., 59
016: * Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: *
018: */
019:
020: package de.schlund.pfixcore.lucefix;
021:
022: import java.io.IOException;
023: import java.util.Vector;
024:
025: import org.apache.lucene.analysis.PerFieldAnalyzerWrapper;
026: import org.apache.lucene.document.Document;
027: import org.apache.lucene.index.IndexReader;
028: import org.apache.lucene.queryParser.ParseException;
029: import org.apache.lucene.queryParser.QueryParser;
030: import org.apache.lucene.search.BooleanQuery;
031: import org.apache.lucene.search.Hits;
032: import org.apache.lucene.search.IndexSearcher;
033: import org.apache.lucene.search.Query;
034: import org.w3c.dom.Element;
035:
036: import de.schlund.pfixcore.editor2.core.dom.Project;
037: import de.schlund.pfixcore.editor2.frontend.resources.ProjectsResource;
038: import de.schlund.pfixcore.editor2.frontend.util.EditorResourceLocator;
039: import de.schlund.pfixcore.workflow.Context;
040: import de.schlund.pfixxml.ResultDocument;
041:
042: public class ContextSearchImpl implements ContextSearch {
043:
044: private final static PerFieldAnalyzerWrapper analyzer = PreDoc.ANALYZER;
045:
046: private Context context;
047: private Query lastQuery;
048: private Hit[] hits;
049:
050: String content, tags, attribkey, attribvalue, comments;
051:
052: public void resetData() {
053: content = null;
054: tags = null;
055: attribkey = null;
056: attribvalue = null;
057: comments = null;
058: hits = null;
059: lastQuery = null;
060: }
061:
062: public String getAttribkey() {
063: return attribkey;
064: }
065:
066: public String getAttribvalue() {
067: return attribvalue;
068: }
069:
070: public String getComments() {
071: return comments;
072: }
073:
074: public String getContent() {
075: return content;
076: }
077:
078: public String getTags() {
079: return tags;
080: }
081:
082: public void search(String content, String tags, String attribkey,
083: String attribvalue, String comments) throws IOException,
084: ParseException {
085: IndexReader reader = IndexReader
086: .open(PfixQueueManager.lucene_data_path);
087: this .content = content;
088: this .tags = tags;
089: this .attribkey = attribkey;
090: this .attribvalue = attribvalue;
091: this .comments = comments;
092:
093: IndexSearcher searcher = new IndexSearcher(reader);
094: BooleanQuery query = new BooleanQuery();
095: if (content != null)
096: query.add(QueryParser.parse(content, PreDoc.CONTENTS,
097: analyzer), true, false);
098: if (tags != null)
099: query.add(QueryParser.parse(tags, PreDoc.TAGS, analyzer),
100: true, false);
101: if (attribkey != null)
102: query.add(QueryParser.parse(attribkey, PreDoc.ATTRIBKEYS,
103: analyzer), true, false);
104: if (attribvalue != null)
105: query.add(QueryParser.parse(attribvalue,
106: PreDoc.ATTRIBVALUES, analyzer), true, false);
107: if (comments != null)
108: query.add(QueryParser.parse(comments, PreDoc.COMMENTS,
109: analyzer), true, false);
110: Hits hits = searcher.search(query);
111: transformHits(hits);
112: lastQuery = query;
113: }
114:
115: private static String[] splitPath(String input) {
116: String[] retval = new String[3];
117: int letzterSlash = input.lastIndexOf("/");
118: int vorletzterSlash = input.lastIndexOf("/", letzterSlash - 1);
119: retval[0] = input.substring(0, vorletzterSlash);
120: retval[1] = input.substring(vorletzterSlash + 1, letzterSlash);
121: retval[2] = input.substring(letzterSlash + 1);
122: return retval;
123: }
124:
125: public void init(Context context) throws Exception {
126: this .context = context;
127: reset();
128: }
129:
130: private void transformHits(Hits hits) throws IOException {
131: ProjectsResource pcon = EditorResourceLocator
132: .getProjectsResource(context);
133: Project currentProject = pcon.getSelectedProject();
134:
135: Document doc;
136: Vector<Hit> temp = new Vector<Hit>();
137: String[] token;
138: for (int i = 0; i < hits.length(); i++) {
139: doc = hits.doc(i);
140:
141: token = splitPath(doc.get(PreDoc.PATH));
142: if (currentProject != null
143: && currentProject.hasIncludePart(token[0],
144: token[1], token[2]) == false) {
145: continue;
146: }
147: temp.add(new Hit(doc, hits.score(i)));
148: }
149: this .hits = temp.toArray(new Hit[0]);
150: }
151:
152: public void insertStatus(ResultDocument resdoc, Element elem)
153: throws Exception {
154:
155: if (hits != null) {
156: Element newelem;
157: for (Hit doc : hits) {
158: newelem = resdoc.createSubNode(elem, "hit");
159: newelem.setAttribute("score", doc.getScore() + "");
160: newelem.setAttribute("filename", doc.getFilename());
161: newelem.setAttribute("part", doc.getPartname());
162: newelem.setAttribute("product", doc.getProductname());
163: newelem.setAttribute("path", doc.getPath());
164:
165: }
166: }
167: if (lastQuery != null)
168: elem.setAttribute("lastQuery", lastQuery.toString());
169: }
170:
171: public void reset() throws Exception {
172: hits = null;
173: lastQuery = null;
174: }
175:
176: private class Hit {
177: private double score;
178: private String filename;
179: private String partname;
180: private String productname;
181:
182: /**
183: * @param filename
184: * @param partname
185: * @param productname
186: * @param score
187: */
188: public Hit(String filename, String partname,
189: String productname, double score) {
190: this .filename = filename;
191: this .partname = partname;
192: this .productname = productname;
193: this .score = score;
194: }
195:
196: public Hit(Document lucenedoc, double score) {
197: String[] token = splitPath(lucenedoc.get(PreDoc.PATH));
198: this .filename = token[0];
199: this .partname = token[1];
200: this .productname = token[2];
201: this .score = score;
202: }
203:
204: public String getFilename() {
205: return filename;
206: }
207:
208: public void setFilename(String filename) {
209: this .filename = filename;
210: }
211:
212: public String getPartname() {
213: return partname;
214: }
215:
216: public void setPartname(String partname) {
217: this .partname = partname;
218: }
219:
220: public String getProductname() {
221: return productname;
222: }
223:
224: public void setProductname(String productname) {
225: this .productname = productname;
226: }
227:
228: public double getScore() {
229: return score;
230: }
231:
232: public void setScore(double score) {
233: this .score = score;
234: }
235:
236: public String getPath() {
237: StringBuffer sb = new StringBuffer(getFilename());
238: sb.append("/");
239: sb.append(getPartname());
240: sb.append("/");
241: sb.append(getProductname());
242: return sb.toString();
243: }
244: }
245: }
|