[919] | 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 |
|
---|
[312] | 19 | package org.expeditee.agents;
|
---|
| 20 |
|
---|
| 21 | import java.util.HashMap;
|
---|
[313] | 22 | import java.util.Map;
|
---|
[312] | 23 | import java.util.Vector;
|
---|
| 24 |
|
---|
[1102] | 25 | import org.expeditee.gio.gesture.StandardGestureActions;
|
---|
[312] | 26 | import org.expeditee.greenstone.Greenstone3Connection;
|
---|
| 27 | import org.expeditee.greenstone.Query;
|
---|
| 28 | import org.expeditee.greenstone.QueryOutcome;
|
---|
| 29 | import org.expeditee.greenstone.Result;
|
---|
| 30 | import org.expeditee.greenstone.ResultDocument;
|
---|
| 31 | import org.expeditee.gui.AttributeValuePair;
|
---|
[1102] | 32 | import org.expeditee.gui.DisplayController;
|
---|
[312] | 33 | import org.expeditee.gui.Frame;
|
---|
| 34 | import org.expeditee.gui.FrameCreator;
|
---|
| 35 | import org.expeditee.gui.FrameGraphics;
|
---|
[313] | 36 | import org.expeditee.gui.MessageBay;
|
---|
[312] | 37 | import org.expeditee.items.Text;
|
---|
| 38 |
|
---|
| 39 | public class SearchGreenstone extends SearchAgent {
|
---|
| 40 |
|
---|
[313] | 41 | private static String _fullCaseSearchQuery = null;
|
---|
| 42 |
|
---|
[312] | 43 | private static boolean _doCasefolding = true;
|
---|
| 44 |
|
---|
| 45 | private static boolean _doStemming = false;
|
---|
| 46 |
|
---|
| 47 | protected static Greenstone3Connection _gsdl = null;
|
---|
| 48 |
|
---|
[376] | 49 | public static Greenstone3Connection getConnection() {
|
---|
| 50 | return _gsdl;
|
---|
| 51 | }
|
---|
| 52 |
|
---|
[312] | 53 | private static String _maxResults = "10";
|
---|
| 54 |
|
---|
[313] | 55 | private static boolean _showAbstract = false;
|
---|
[336] | 56 |
|
---|
[313] | 57 | private static boolean _showKeywords = false;
|
---|
[336] | 58 |
|
---|
[313] | 59 | private static boolean _showAuthors = false;
|
---|
[336] | 60 |
|
---|
[313] | 61 | private static boolean _showDate = false;
|
---|
| 62 |
|
---|
| 63 | private String _thisMaxResults = "10";
|
---|
| 64 |
|
---|
[312] | 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 |
|
---|
[313] | 73 | protected static Vector<Result> _currentResultSet = null;
|
---|
[312] | 74 |
|
---|
[313] | 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 |
|
---|
[312] | 101 | public SearchGreenstone(String searchText) {
|
---|
| 102 | super(searchText);
|
---|
[313] | 103 | _thisMaxResults = _maxResults;
|
---|
| 104 | _fullCaseSearchQuery = searchText;
|
---|
[312] | 105 | }
|
---|
| 106 |
|
---|
[376] | 107 | public SearchGreenstone(int resultsCount) {
|
---|
| 108 | this(null);
|
---|
| 109 | _thisMaxResults = resultsCount + "";
|
---|
| 110 | }
|
---|
| 111 |
|
---|
[313] | 112 | public SearchGreenstone() {
|
---|
| 113 | super(null);
|
---|
| 114 | _useLastSearchResults = true;
|
---|
[312] | 115 | }
|
---|
| 116 |
|
---|
| 117 | public static void init(Frame settings) {
|
---|
| 118 | if (settings == null)
|
---|
| 119 | return;
|
---|
| 120 |
|
---|
[336] | 121 | _maxResults = "10";
|
---|
| 122 | _showAbstract = false;
|
---|
| 123 | _showKeywords = false;
|
---|
| 124 | _showAuthors = false;
|
---|
| 125 | _showDate = false;
|
---|
| 126 | _locationChoice = 1;
|
---|
| 127 |
|
---|
[312] | 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;
|
---|
[313] | 139 | else if (attribute.equals("autoconnect"))
|
---|
[312] | 140 | connect();
|
---|
[313] | 141 | else if (attribute.equals("maxresults")) {
|
---|
[312] | 142 | try {
|
---|
| 143 | _maxResults = avp.getValue();
|
---|
| 144 | } catch (Exception e) {
|
---|
| 145 | }
|
---|
[376] | 146 | } else if (attribute.equals("dostemming"))
|
---|
[312] | 147 | _doStemming = true;
|
---|
[313] | 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;
|
---|
[312] | 156 | }
|
---|
| 157 | }
|
---|
| 158 |
|
---|
| 159 | public static void connect() {
|
---|
| 160 | if (_gsdl == null)
|
---|
| 161 | _gsdl = new Greenstone3Connection(_locationChoice);
|
---|
| 162 | }
|
---|
| 163 |
|
---|
[376] | 164 | protected String getResultsTitle() {
|
---|
| 165 | return this.getClass().getSimpleName() + "[" + getCursorText() + "]";
|
---|
| 166 | }
|
---|
| 167 |
|
---|
[312] | 168 | @Override
|
---|
| 169 | protected Frame process(Frame frame) {
|
---|
[376] | 170 | String resultsTitle = getResultsTitle();
|
---|
[313] | 171 | _results.setTitle(resultsTitle);
|
---|
[312] | 172 |
|
---|
[313] | 173 | if (!_useLastSearchResults) {
|
---|
| 174 | connect();
|
---|
| 175 | doQuery(_pattern);
|
---|
| 176 | } else if (_currentResultSet != null) {
|
---|
[1102] | 177 | Text newText = DisplayController.getCurrentFrame().createNewText(getCursorText());
|
---|
[313] | 178 | _clicked = newText;
|
---|
[1102] | 179 | StandardGestureActions.pickup(newText);
|
---|
[313] | 180 | }
|
---|
[312] | 181 |
|
---|
[376] | 182 | if (_currentResultSet == null || _currentResultSet.size() == 0) {
|
---|
| 183 | MessageBay.errorMessage(getNoResultsMessage());
|
---|
[313] | 184 | return null;
|
---|
| 185 | }
|
---|
[312] | 186 |
|
---|
| 187 | createResults();
|
---|
[313] | 188 |
|
---|
[312] | 189 | _results.save();
|
---|
| 190 |
|
---|
| 191 | String resultFrameName = _results.getName();
|
---|
[313] | 192 | if (_clicked != null) {
|
---|
[312] | 193 | _clicked.setLink(resultFrameName);
|
---|
[313] | 194 | _clicked.setText(resultsTitle);
|
---|
| 195 | }
|
---|
[312] | 196 |
|
---|
| 197 | return _results.getFirstFrame();
|
---|
| 198 | }
|
---|
| 199 |
|
---|
[376] | 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 |
|
---|
[312] | 211 | protected void createResults() {
|
---|
| 212 | viewByScore(_currentResultSet, _results);
|
---|
| 213 | }
|
---|
| 214 |
|
---|
[313] | 215 | /**
|
---|
| 216 | * TODO make this more efficient so the maps are loaded on demand...
|
---|
| 217 | *
|
---|
| 218 | * @param queryText
|
---|
| 219 | */
|
---|
[312] | 220 | protected void doQuery(String queryText) {
|
---|
[313] | 221 | _pageCountMap.clear();
|
---|
| 222 | _dateMap.clear();
|
---|
| 223 | _titleMap.clear();
|
---|
| 224 |
|
---|
[312] | 225 | Query query = createQuery(queryText);
|
---|
| 226 | QueryOutcome queryOutcome = _gsdl.issueQueryToServer(query);
|
---|
[376] | 227 | if (queryOutcome != null)
|
---|
| 228 | _currentResultSet = getResultSetMetadata(queryOutcome);
|
---|
[312] | 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]);
|
---|
[313] | 237 | query.setMaxDocsToReturn(_thisMaxResults);
|
---|
[312] | 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();
|
---|
[313] | 257 | for (Result result : results) {
|
---|
[312] | 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 |
|
---|
[313] | 312 | addText(rd, resultsCreator, (docRank + 1) + ". " + rd.getTitle());
|
---|
[312] | 313 | }
|
---|
| 314 | }
|
---|
| 315 |
|
---|
| 316 | protected String getDetails(ResultDocument rd) {
|
---|
| 317 | StringBuffer resultText = new StringBuffer("");
|
---|
| 318 |
|
---|
| 319 | if (rd.metadataExists("Title")) {
|
---|
[376] | 320 | resultText.append("title: " + rd.getTitle()).append('\n');
|
---|
[312] | 321 | }
|
---|
| 322 | if (rd.metadataExists("Date")) {
|
---|
[376] | 323 | resultText.append("date: " + rd.getDate()).append('\n');
|
---|
[312] | 324 | }
|
---|
| 325 | if (rd.metadataExists("Booktitle")) {
|
---|
[376] | 326 | resultText.append("booktitle: " + rd.getBooktitle()).append('\n');
|
---|
[312] | 327 | }
|
---|
| 328 | if (rd.metadataExists("Pages")) {
|
---|
[376] | 329 | resultText.append("pages: " + rd.getPages()).append('\n');
|
---|
[312] | 330 | }
|
---|
| 331 | if (rd.metadataExists("Journal")) {
|
---|
[376] | 332 | resultText.append("journal: " + rd.getJournal()).append('\n');
|
---|
[312] | 333 | }
|
---|
| 334 | if (rd.metadataExists("Volume")) {
|
---|
[376] | 335 | resultText.append("volume: " + rd.getVolume()).append('\n');
|
---|
[312] | 336 | }
|
---|
| 337 | if (rd.metadataExists("Number")) {
|
---|
[376] | 338 | resultText.append("number: " + rd.getNumber()).append('\n');
|
---|
[312] | 339 | }
|
---|
| 340 | if (rd.metadataExists("Editor")) {
|
---|
[376] | 341 | resultText.append("editor: " + rd.getEditor()).append('\n');
|
---|
[312] | 342 | }
|
---|
| 343 | if (rd.metadataExists("Publisher")) {
|
---|
[376] | 344 | resultText.append("publisher: " + rd.getPublisher()).append('\n');
|
---|
[312] | 345 | }
|
---|
| 346 | if (rd.metadataExists("Abstract")) {
|
---|
[376] | 347 | resultText.append("abstract: " + rd.getAbstract()).append('\n');
|
---|
[312] | 348 | }
|
---|
[376] | 349 | for (String author : rd.getAuthors()) {
|
---|
| 350 | resultText.append("author: " + author).append('\n');
|
---|
[312] | 351 | }
|
---|
[376] | 352 |
|
---|
| 353 | for (String keyword : rd.getKeywords()) {
|
---|
| 354 | resultText.append("keyword: " + keyword).append('\n');
|
---|
[312] | 355 | }
|
---|
[376] | 356 |
|
---|
[312] | 357 | resultText.deleteCharAt(resultText.length() - 1);
|
---|
| 358 |
|
---|
| 359 | return resultText.toString();
|
---|
| 360 | }
|
---|
| 361 |
|
---|
[313] | 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();
|
---|
[336] | 377 |
|
---|
[313] | 378 | if (_showAbstract && rd.metadataExists("Abstract"))
|
---|
| 379 | text += "\n " + rd.getAbstract();
|
---|
[336] | 380 |
|
---|
[313] | 381 | if (_showAuthors && rd.metadataExists("Creator"))
|
---|
| 382 | text += "\nAuthors" + rd.getAuthors().toString();
|
---|
[336] | 383 |
|
---|
[313] | 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 |
|
---|
[1102] | 390 | DisplayController.requestRefresh(true);
|
---|
[313] | 391 | }
|
---|
| 392 |
|
---|
[376] | 393 | public static void clearSession() {
|
---|
| 394 | getConnection().getSessionResults().clear();
|
---|
| 395 | _currentResultSet = null;
|
---|
| 396 | }
|
---|
| 397 |
|
---|
[312] | 398 | }
|
---|