1 | package org.expeditee.agents;
|
---|
2 |
|
---|
3 | import java.util.Collections;
|
---|
4 | import java.util.Enumeration;
|
---|
5 | import java.util.HashMap;
|
---|
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.Frame;
|
---|
15 | import org.expeditee.gui.FrameCreator;
|
---|
16 | import org.expeditee.gui.FrameGraphics;
|
---|
17 | import org.expeditee.items.Text;
|
---|
18 |
|
---|
19 | public class SearchGreenstone extends SearchAgent {
|
---|
20 |
|
---|
21 | private static boolean _doCasefolding = true;
|
---|
22 |
|
---|
23 | private static boolean _doStemming = false;
|
---|
24 |
|
---|
25 | protected static Greenstone3Connection _gsdl = null;
|
---|
26 |
|
---|
27 | private static String _maxResults = "10";
|
---|
28 |
|
---|
29 | private int _indexChoice = 1;
|
---|
30 |
|
---|
31 | private static boolean _getAllMetadata = true;
|
---|
32 |
|
---|
33 | private static int _locationChoice = 1;
|
---|
34 |
|
---|
35 | private static String[] _indexKeys = { "TX", "TI", "JO", "BO", "CR", "KE" };
|
---|
36 |
|
---|
37 | protected static Vector<Result> _currentResultSet;
|
---|
38 |
|
---|
39 | public SearchGreenstone(String searchText) {
|
---|
40 | super(searchText);
|
---|
41 | }
|
---|
42 |
|
---|
43 | public SearchGreenstone(int resultsCount, String searchText) {
|
---|
44 | this(searchText);
|
---|
45 | _maxResults = resultsCount + "";
|
---|
46 | }
|
---|
47 |
|
---|
48 | public static void init(Frame settings) {
|
---|
49 | if (settings == null)
|
---|
50 | return;
|
---|
51 |
|
---|
52 | // Set the settings
|
---|
53 | for (Text item : settings.getBodyTextItems(false)) {
|
---|
54 |
|
---|
55 | AttributeValuePair avp = new AttributeValuePair(item.getText());
|
---|
56 | if (avp.isAnnotation())
|
---|
57 | continue;
|
---|
58 |
|
---|
59 | String attribute = avp.getAttributeOrValue().toLowerCase();
|
---|
60 |
|
---|
61 | if (attribute.equals("campus"))
|
---|
62 | _locationChoice = 0;
|
---|
63 | if (attribute.equals("autoconnect"))
|
---|
64 | connect();
|
---|
65 |
|
---|
66 | if (attribute.equals("maxresults")) {
|
---|
67 | try {
|
---|
68 | _maxResults = avp.getValue();
|
---|
69 | } catch (Exception e) {
|
---|
70 | }
|
---|
71 | }
|
---|
72 |
|
---|
73 | if (attribute.equals("dostemming"))
|
---|
74 | _doStemming = true;
|
---|
75 | }
|
---|
76 | }
|
---|
77 |
|
---|
78 | public static void connect() {
|
---|
79 | if (_gsdl == null)
|
---|
80 | _gsdl = new Greenstone3Connection(_locationChoice);
|
---|
81 | }
|
---|
82 |
|
---|
83 | @Override
|
---|
84 | protected Frame process(Frame frame) {
|
---|
85 | _results.setTitle("SearchGreenstone [" + _pattern + "]");
|
---|
86 |
|
---|
87 | connect();
|
---|
88 |
|
---|
89 | doQuery(_pattern);
|
---|
90 |
|
---|
91 | createResults();
|
---|
92 |
|
---|
93 | _results.save();
|
---|
94 |
|
---|
95 | String resultFrameName = _results.getName();
|
---|
96 | if (_clicked != null)
|
---|
97 | _clicked.setLink(resultFrameName);
|
---|
98 |
|
---|
99 | return _results.getFirstFrame();
|
---|
100 | }
|
---|
101 |
|
---|
102 | protected void createResults() {
|
---|
103 | viewByScore(_currentResultSet, _results);
|
---|
104 | }
|
---|
105 |
|
---|
106 | protected void doQuery(String queryText) {
|
---|
107 | Query query = createQuery(queryText);
|
---|
108 | QueryOutcome queryOutcome = _gsdl.issueQueryToServer(query);
|
---|
109 | _currentResultSet = getResultSetMetadata(queryOutcome);
|
---|
110 | }
|
---|
111 |
|
---|
112 | private Query createQuery(String queryText) {
|
---|
113 |
|
---|
114 | Query query = new Query();
|
---|
115 |
|
---|
116 | // set the query options
|
---|
117 | query.setQueryText(queryText);
|
---|
118 |
|
---|
119 | query.setIndex(_indexKeys[_indexChoice]);
|
---|
120 | query.setMaxDocsToReturn(_maxResults);
|
---|
121 |
|
---|
122 | if (_doStemming) {
|
---|
123 | query.setStemming("1");
|
---|
124 | } else {
|
---|
125 | query.setStemming("0");
|
---|
126 | }
|
---|
127 |
|
---|
128 | if (_doCasefolding) {
|
---|
129 | query.setCasefolding("1");
|
---|
130 | } else {
|
---|
131 | query.setCasefolding("0");
|
---|
132 | }
|
---|
133 |
|
---|
134 | return query;
|
---|
135 | }
|
---|
136 |
|
---|
137 | public Vector<Result> getResultSetMetadata(QueryOutcome queryOutcome) {
|
---|
138 |
|
---|
139 | Vector<Result> results = queryOutcome.getResults();
|
---|
140 | for (Enumeration<Result> e = results.elements(); e.hasMoreElements();) {
|
---|
141 | Result result = e.nextElement();
|
---|
142 | getResultMetadata(result);
|
---|
143 | }
|
---|
144 | return results;
|
---|
145 | }
|
---|
146 |
|
---|
147 | private void getResultMetadata(Result result) {
|
---|
148 | String docID = result.getDocID();
|
---|
149 |
|
---|
150 | _gsdl.getDocumentMetadataFromServer(docID, "Title");
|
---|
151 | _gsdl.getDocumentMetadataFromServer(docID, "Date");
|
---|
152 |
|
---|
153 | if (_getAllMetadata) {
|
---|
154 | _gsdl.getDocumentMetadataFromServer(docID, "Date");
|
---|
155 | _gsdl.getDocumentMetadataFromServer(docID, "Booktitle");
|
---|
156 | _gsdl.getDocumentMetadataFromServer(docID, "Journal");
|
---|
157 | _gsdl.getDocumentMetadataFromServer(docID, "Creator");
|
---|
158 | _gsdl.getDocumentMetadataFromServer(docID, "Keywords");
|
---|
159 | _gsdl.getDocumentMetadataFromServer(docID, "Publisher");
|
---|
160 | _gsdl.getDocumentMetadataFromServer(docID, "Abstract");
|
---|
161 | _gsdl.getDocumentMetadataFromServer(docID, "Pages");
|
---|
162 | _gsdl.getDocumentMetadataFromServer(docID, "Number");
|
---|
163 | _gsdl.getDocumentMetadataFromServer(docID, "Volume");
|
---|
164 | }
|
---|
165 |
|
---|
166 | }
|
---|
167 |
|
---|
168 | /*
|
---|
169 | * given the Vector of result items (ordered by descending relevance to the
|
---|
170 | * query) this method iterates through them and constructs an HTML document
|
---|
171 | * and has it rendered in the result window
|
---|
172 | *
|
---|
173 | * This is the default presentation for results
|
---|
174 | *
|
---|
175 | * You can modify this method as you wish to change the HTML for the default
|
---|
176 | * presentation
|
---|
177 | *
|
---|
178 | * Some useful method calls are illustrated here - if you have the ID of a
|
---|
179 | * document you can get all data stored for it with ResultDocument rd =
|
---|
180 | * gsdl.getDocument(docID); - a ResultDocument object has a set of methods
|
---|
181 | * for getting metadata values for that document most metadata is a single
|
---|
182 | * value, but authors and keywords can have multiple values and these are
|
---|
183 | * stored in a Vector
|
---|
184 | *
|
---|
185 | * The IF condition for 'Title' shows how to construct a link that can be
|
---|
186 | * clicked on and then dealt with by the handleLinkClick() method
|
---|
187 | *
|
---|
188 | */
|
---|
189 | public void viewByScore(Vector<Result> results, FrameCreator resultsCreator) {
|
---|
190 |
|
---|
191 | for (Result result : results) {
|
---|
192 | String docID = result.getDocID();
|
---|
193 | ResultDocument rd = _gsdl.getDocument(docID);
|
---|
194 | int docRank = result.getRank();
|
---|
195 | // Put the details on a separate frame
|
---|
196 | FrameCreator details = new FrameCreator(rd.getTitle());
|
---|
197 | details.addText(getDetails(rd), null, null, null, true);
|
---|
198 |
|
---|
199 | resultsCreator.addText((docRank + 1) + ". " + rd.getTitle(), null,
|
---|
200 | details.getName(), "getTextFromChildFrame", false);
|
---|
201 |
|
---|
202 | FrameGraphics.requestRefresh(true);
|
---|
203 | }
|
---|
204 | }
|
---|
205 |
|
---|
206 | protected String getDetails(ResultDocument rd) {
|
---|
207 | StringBuffer resultText = new StringBuffer("");
|
---|
208 |
|
---|
209 | if (rd.metadataExists("Title")) {
|
---|
210 | resultText.append("title = " + rd.getTitle()).append('\n');
|
---|
211 | }
|
---|
212 | if (rd.metadataExists("Date")) {
|
---|
213 | resultText.append("date = " + rd.getDate()).append('\n');
|
---|
214 | }
|
---|
215 | if (rd.metadataExists("Booktitle")) {
|
---|
216 | resultText.append("booktitle = " + rd.getBooktitle()).append('\n');
|
---|
217 | }
|
---|
218 | if (rd.metadataExists("Pages")) {
|
---|
219 | resultText.append("pages = " + rd.getPages()).append('\n');
|
---|
220 | }
|
---|
221 | if (rd.metadataExists("Journal")) {
|
---|
222 | resultText.append("journal = " + rd.getJournal()).append('\n');
|
---|
223 | }
|
---|
224 | if (rd.metadataExists("Volume")) {
|
---|
225 | resultText.append("volume = " + rd.getVolume()).append('\n');
|
---|
226 | }
|
---|
227 | if (rd.metadataExists("Number")) {
|
---|
228 | resultText.append("number = " + rd.getNumber()).append('\n');
|
---|
229 | }
|
---|
230 | if (rd.metadataExists("Editor")) {
|
---|
231 | resultText.append("editor = " + rd.getEditor()).append('\n');
|
---|
232 | }
|
---|
233 | if (rd.metadataExists("Publisher")) {
|
---|
234 | resultText.append("publisher = " + rd.getPublisher()).append('\n');
|
---|
235 | }
|
---|
236 | if (rd.metadataExists("Abstract")) {
|
---|
237 | resultText.append("abstract = " + rd.getAbstract()).append('\n');
|
---|
238 | }
|
---|
239 | Vector<String> authors = rd.getAuthors();
|
---|
240 | for (Enumeration a = authors.elements(); a.hasMoreElements();) {
|
---|
241 | String author = (String) a.nextElement();
|
---|
242 | resultText.append("author = " + author).append('\n');
|
---|
243 | }
|
---|
244 | Vector<String> keywords = rd.getKeywords();
|
---|
245 | for (Enumeration k = keywords.elements(); k.hasMoreElements();) {
|
---|
246 | String keyword = (String) k.nextElement();
|
---|
247 | resultText.append("keyword = " + keyword).append('\n');
|
---|
248 | }
|
---|
249 | resultText.deleteCharAt(resultText.length() - 1);
|
---|
250 |
|
---|
251 | return resultText.toString();
|
---|
252 | }
|
---|
253 |
|
---|
254 | }
|
---|