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