source: trunk/src/org/expeditee/agents/SearchGreenstone.java@ 376

Last change on this file since 376 was 376, checked in by ra33, 16 years ago
File size: 10.5 KB
Line 
1package org.expeditee.agents;
2
3import java.util.HashMap;
4import java.util.Map;
5import java.util.Vector;
6
7import org.expeditee.greenstone.Greenstone3Connection;
8import org.expeditee.greenstone.Query;
9import org.expeditee.greenstone.QueryOutcome;
10import org.expeditee.greenstone.Result;
11import org.expeditee.greenstone.ResultDocument;
12import org.expeditee.gui.AttributeValuePair;
13import org.expeditee.gui.DisplayIO;
14import org.expeditee.gui.Frame;
15import org.expeditee.gui.FrameCreator;
16import org.expeditee.gui.FrameGraphics;
17import org.expeditee.gui.FrameMouseActions;
18import org.expeditee.gui.MessageBay;
19import org.expeditee.items.Text;
20
21public class SearchGreenstone extends SearchAgent {
22
23 private static String _fullCaseSearchQuery = null;
24
25 private static boolean _doCasefolding = true;
26
27 private static boolean _doStemming = false;
28
29 protected static Greenstone3Connection _gsdl = null;
30
31 public static Greenstone3Connection getConnection() {
32 return _gsdl;
33 }
34
35 private static String _maxResults = "10";
36
37 private static boolean _showAbstract = false;
38
39 private static boolean _showKeywords = false;
40
41 private static boolean _showAuthors = false;
42
43 private static boolean _showDate = false;
44
45 private String _thisMaxResults = "10";
46
47 private int _indexChoice = 1;
48
49 private static boolean _getAllMetadata = true;
50
51 private static int _locationChoice = 1;
52
53 private static String[] _indexKeys = { "TX", "TI", "JO", "BO", "CR", "KE" };
54
55 protected static Vector<Result> _currentResultSet = null;
56
57 private boolean _useLastSearchResults = false;
58
59 /**
60 * dateMap is a hash table. The keys are year values. the data associated
61 * with each key is a Vector of document IDs therefore, for the current
62 * result set you can get the set of years in which the results were
63 * published, and for each year you can get the set of documents published
64 * in that year
65 *
66 * If you want to introduce additional mappings (eg document written by
67 * authors) you should introduce additional structures here (HashMap used in
68 * the same way as dateMap will probably suffice
69 *
70 */
71 protected static Map<String, Vector<String>> _dateMap = new HashMap<String, Vector<String>>();
72
73 protected static Map<Integer, Vector<String>> _pageCountMap = new HashMap<Integer, Vector<String>>();
74
75 protected static Map<String, String> _titleMap = new HashMap<String, String>();
76
77 public SearchGreenstone(int resultsCount, String searchText) {
78 super(searchText);
79 _thisMaxResults = resultsCount + "";
80 _fullCaseSearchQuery = searchText;
81 }
82
83 public SearchGreenstone(String searchText) {
84 super(searchText);
85 _thisMaxResults = _maxResults;
86 _fullCaseSearchQuery = searchText;
87 }
88
89 public SearchGreenstone(int resultsCount) {
90 this(null);
91 _thisMaxResults = resultsCount + "";
92 }
93
94 public SearchGreenstone() {
95 super(null);
96 _useLastSearchResults = true;
97 }
98
99 public static void init(Frame settings) {
100 if (settings == null)
101 return;
102
103 _maxResults = "10";
104 _showAbstract = false;
105 _showKeywords = false;
106 _showAuthors = false;
107 _showDate = false;
108 _locationChoice = 1;
109
110 // Set the settings
111 for (Text item : settings.getBodyTextItems(false)) {
112
113 AttributeValuePair avp = new AttributeValuePair(item.getText());
114 if (avp.isAnnotation())
115 continue;
116
117 String attribute = avp.getAttributeOrValue().toLowerCase();
118
119 if (attribute.equals("campus"))
120 _locationChoice = 0;
121 else if (attribute.equals("autoconnect"))
122 connect();
123 else if (attribute.equals("maxresults")) {
124 try {
125 _maxResults = avp.getValue();
126 } catch (Exception e) {
127 }
128 } else if (attribute.equals("dostemming"))
129 _doStemming = true;
130 else if (attribute.startsWith("showabstract"))
131 _showAbstract = true;
132 else if (attribute.startsWith("showauthor"))
133 _showAuthors = true;
134 else if (attribute.startsWith("showkeyword"))
135 _showKeywords = true;
136 else if (attribute.startsWith("showdate"))
137 _showDate = true;
138 }
139 }
140
141 public static void connect() {
142 if (_gsdl == null)
143 _gsdl = new Greenstone3Connection(_locationChoice);
144 }
145
146 protected String getResultsTitle() {
147 return this.getClass().getSimpleName() + "[" + getCursorText() + "]";
148 }
149
150 @Override
151 protected Frame process(Frame frame) {
152 String resultsTitle = getResultsTitle();
153 _results.setTitle(resultsTitle);
154
155 if (!_useLastSearchResults) {
156 connect();
157 doQuery(_pattern);
158 } else if (_currentResultSet != null) {
159 Text newText = DisplayIO.getCurrentFrame().createNewText(
160 getCursorText());
161 _clicked = newText;
162 FrameMouseActions.pickup(newText);
163 }
164
165 if (_currentResultSet == null || _currentResultSet.size() == 0) {
166 MessageBay.errorMessage(getNoResultsMessage());
167 return null;
168 }
169
170 createResults();
171
172 _results.save();
173
174 String resultFrameName = _results.getName();
175 if (_clicked != null) {
176 _clicked.setLink(resultFrameName);
177 _clicked.setText(resultsTitle);
178 }
179
180 return _results.getFirstFrame();
181 }
182
183 protected String getNoResultsMessage() {
184 return "Could not find Greenstone query text";
185 }
186
187 /**
188 * @return
189 */
190 protected String getCursorText() {
191 return _fullCaseSearchQuery;
192 }
193
194 protected void createResults() {
195 viewByScore(_currentResultSet, _results);
196 }
197
198 /**
199 * TODO make this more efficient so the maps are loaded on demand...
200 *
201 * @param queryText
202 */
203 protected void doQuery(String queryText) {
204 _pageCountMap.clear();
205 _dateMap.clear();
206 _titleMap.clear();
207
208 Query query = createQuery(queryText);
209 QueryOutcome queryOutcome = _gsdl.issueQueryToServer(query);
210 if (queryOutcome != null)
211 _currentResultSet = getResultSetMetadata(queryOutcome);
212 }
213
214 private Query createQuery(String queryText) {
215
216 Query query = new Query();
217 // set the query options
218 query.setQueryText(queryText);
219 query.setIndex(_indexKeys[_indexChoice]);
220 query.setMaxDocsToReturn(_thisMaxResults);
221
222 if (_doStemming) {
223 query.setStemming("1");
224 } else {
225 query.setStemming("0");
226 }
227
228 if (_doCasefolding) {
229 query.setCasefolding("1");
230 } else {
231 query.setCasefolding("0");
232 }
233
234 return query;
235 }
236
237 public Vector<Result> getResultSetMetadata(QueryOutcome queryOutcome) {
238
239 Vector<Result> results = queryOutcome.getResults();
240 for (Result result : results) {
241 getResultMetadata(result);
242 }
243 return results;
244 }
245
246 private void getResultMetadata(Result result) {
247 String docID = result.getDocID();
248
249 _gsdl.getDocumentMetadataFromServer(docID, "Title");
250 _gsdl.getDocumentMetadataFromServer(docID, "Date");
251
252 if (_getAllMetadata) {
253 _gsdl.getDocumentMetadataFromServer(docID, "Date");
254 _gsdl.getDocumentMetadataFromServer(docID, "Booktitle");
255 _gsdl.getDocumentMetadataFromServer(docID, "Journal");
256 _gsdl.getDocumentMetadataFromServer(docID, "Creator");
257 _gsdl.getDocumentMetadataFromServer(docID, "Keywords");
258 _gsdl.getDocumentMetadataFromServer(docID, "Publisher");
259 _gsdl.getDocumentMetadataFromServer(docID, "Abstract");
260 _gsdl.getDocumentMetadataFromServer(docID, "Pages");
261 _gsdl.getDocumentMetadataFromServer(docID, "Number");
262 _gsdl.getDocumentMetadataFromServer(docID, "Volume");
263 }
264
265 }
266
267 /*
268 * given the Vector of result items (ordered by descending relevance to the
269 * query) this method iterates through them and constructs an HTML document
270 * and has it rendered in the result window
271 *
272 * This is the default presentation for results
273 *
274 * You can modify this method as you wish to change the HTML for the default
275 * presentation
276 *
277 * Some useful method calls are illustrated here - if you have the ID of a
278 * document you can get all data stored for it with ResultDocument rd =
279 * gsdl.getDocument(docID); - a ResultDocument object has a set of methods
280 * for getting metadata values for that document most metadata is a single
281 * value, but authors and keywords can have multiple values and these are
282 * stored in a Vector
283 *
284 * The IF condition for 'Title' shows how to construct a link that can be
285 * clicked on and then dealt with by the handleLinkClick() method
286 *
287 */
288 public void viewByScore(Vector<Result> results, FrameCreator resultsCreator) {
289
290 for (Result result : results) {
291 String docID = result.getDocID();
292 ResultDocument rd = _gsdl.getDocument(docID);
293 int docRank = result.getRank();
294
295 addText(rd, resultsCreator, (docRank + 1) + ". " + rd.getTitle());
296 }
297 }
298
299 protected String getDetails(ResultDocument rd) {
300 StringBuffer resultText = new StringBuffer("");
301
302 if (rd.metadataExists("Title")) {
303 resultText.append("title: " + rd.getTitle()).append('\n');
304 }
305 if (rd.metadataExists("Date")) {
306 resultText.append("date: " + rd.getDate()).append('\n');
307 }
308 if (rd.metadataExists("Booktitle")) {
309 resultText.append("booktitle: " + rd.getBooktitle()).append('\n');
310 }
311 if (rd.metadataExists("Pages")) {
312 resultText.append("pages: " + rd.getPages()).append('\n');
313 }
314 if (rd.metadataExists("Journal")) {
315 resultText.append("journal: " + rd.getJournal()).append('\n');
316 }
317 if (rd.metadataExists("Volume")) {
318 resultText.append("volume: " + rd.getVolume()).append('\n');
319 }
320 if (rd.metadataExists("Number")) {
321 resultText.append("number: " + rd.getNumber()).append('\n');
322 }
323 if (rd.metadataExists("Editor")) {
324 resultText.append("editor: " + rd.getEditor()).append('\n');
325 }
326 if (rd.metadataExists("Publisher")) {
327 resultText.append("publisher: " + rd.getPublisher()).append('\n');
328 }
329 if (rd.metadataExists("Abstract")) {
330 resultText.append("abstract: " + rd.getAbstract()).append('\n');
331 }
332 for (String author : rd.getAuthors()) {
333 resultText.append("author: " + author).append('\n');
334 }
335
336 for (String keyword : rd.getKeywords()) {
337 resultText.append("keyword: " + keyword).append('\n');
338 }
339
340 resultText.deleteCharAt(resultText.length() - 1);
341
342 return resultText.toString();
343 }
344
345 @Override
346 public Frame getResultFrame() {
347 if (_useLastSearchResults && _currentResultSet == null)
348 return null;
349
350 return super.getResultFrame();
351 }
352
353 protected void addText(ResultDocument rd, FrameCreator results, String text) {
354 // Put the details on a separate frame
355 FrameCreator details = new FrameCreator(rd.getTitle());
356 details.addText(getDetails(rd), null, null, null, true);
357
358 if (_showDate && rd.metadataExists("Date"))
359 text += ", " + rd.getDate();
360
361 if (_showAbstract && rd.metadataExists("Abstract"))
362 text += "\n " + rd.getAbstract();
363
364 if (_showAuthors && rd.metadataExists("Creator"))
365 text += "\nAuthors" + rd.getAuthors().toString();
366
367 if (_showKeywords && rd.getKeywords().size() > 0)
368 text += "\nKeywords" + rd.getKeywords().toString();
369
370 results.addText(text, null, details.getName(), "getTextFromChildFrame",
371 false);
372
373 FrameGraphics.requestRefresh(true);
374 }
375
376 public static void clearSession() {
377 getConnection().getSessionResults().clear();
378 _currentResultSet = null;
379 }
380
381}
Note: See TracBrowser for help on using the repository browser.