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

Last change on this file since 1102 was 1102, checked in by davidb, 6 years ago

Reworking of the code-base to separate logic from graphics. This version of Expeditee now supports a JFX graphics as an alternative to SWING

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