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

Last change on this file since 336 was 336, checked in by ra33, 16 years ago

Fixed minor bugs

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