001: //=============================================================================
002: //=== Copyright (C) 2001-2007 Food and Agriculture Organization of the
003: //=== United Nations (FAO-UN), United Nations World Food Programme (WFP)
004: //=== and United Nations Environment Programme (UNEP)
005: //===
006: //=== This program is free software; you can redistribute it and/or modify
007: //=== it under the terms of the GNU General Public License as published by
008: //=== the Free Software Foundation; either version 2 of the License, or (at
009: //=== your option) any later version.
010: //===
011: //=== This program is distributed in the hope that it will be useful, but
012: //=== WITHOUT ANY WARRANTY; without even the implied warranty of
013: //=== MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: //=== General Public License for more details.
015: //===
016: //=== You should have received a copy of the GNU General Public License
017: //=== along with this program; if not, write to the Free Software
018: //=== Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
019: //===
020: //=== Contact: Jeroen Ticheler - FAO - Viale delle Terme di Caracalla 2,
021: //=== Rome - Italy. email: geonetwork@osgeo.org
022: //==============================================================================
023:
024: package org.fao.geonet.services.util;
025:
026: import java.io.IOException;
027: import java.io.StringReader;
028: import java.util.Iterator;
029: import jeeves.constants.Jeeves;
030: import jeeves.server.UserSession;
031: import jeeves.server.context.ServiceContext;
032: import org.apache.lucene.analysis.Analyzer;
033: import org.apache.lucene.analysis.Token;
034: import org.apache.lucene.analysis.TokenStream;
035: import org.apache.lucene.analysis.standard.StandardAnalyzer;
036: import org.fao.geonet.constants.Geonet;
037: import org.jdom.Element;
038:
039: //=============================================================================
040:
041: public class MainUtil {
042: //--------------------------------------------------------------------------
043: //---
044: //--- API methods
045: //---
046: //--------------------------------------------------------------------------
047:
048: /**
049: * Returns default values for the search parameters. If parameters have
050: * changed in the user session, they are read out here.
051: * @param srvContext
052: * @param request
053: * @return
054: */
055: public static Element getDefaultSearch(ServiceContext srvContext,
056: Element request) {
057: UserSession session = srvContext.getUserSession();
058: Element elData = (Element) session
059: .getProperty(Geonet.Session.MAIN_SEARCH);
060:
061: if (elData == null) {
062: srvContext.info("Creating default search data");
063:
064: elData = new Element(Jeeves.Elem.RESPONSE)
065: .addContent(
066: new Element(Geonet.SearchResult.TITLE)
067: .setText(""))
068: .addContent(
069: new Element(Geonet.SearchResult.ABSTRACT)
070: .setText(""))
071: .addContent(
072: new Element(Geonet.SearchResult.ANY)
073: .setText(""))
074: .addContent(
075: new Element(Geonet.SearchResult.REGION)
076: .setText(""))
077: .addContent(
078: new Element(Geonet.SearchResult.PHRASE)
079: .setText(""))
080: .addContent(
081: new Element(Geonet.SearchResult.OR)
082: .setText(""))
083: .addContent(
084: new Element(Geonet.SearchResult.WITHOUT)
085: .setText(""))
086: .addContent(
087: new Element(Geonet.SearchResult.SOUTH_BL)
088: .setText("-90"))
089: .addContent(
090: new Element(Geonet.SearchResult.NORTH_BL)
091: .setText("90"))
092: .addContent(
093: new Element(Geonet.SearchResult.EAST_BL)
094: .setText("180"))
095: .addContent(
096: new Element(Geonet.SearchResult.WEST_BL)
097: .setText("-180"))
098: .addContent(
099: new Element(Geonet.SearchResult.RELATION)
100: .setText(Geonet.SearchResult.Relation.OVERLAPS))
101: .addContent(
102: new Element(Geonet.SearchResult.DATE_FROM)
103: .setText(""))
104: .addContent(
105: new Element(Geonet.SearchResult.DATE_TO)
106: .setText(""))
107: .addContent(
108: new Element(Geonet.SearchResult.KEYWORDS))
109: .addContent(
110: new Element(Geonet.SearchResult.THEME_KEY)
111: .setText(""))
112: .addContent(
113: new Element(Geonet.SearchResult.CATEGORY)
114: .setText(""))
115: .addContent(
116: new Element(Geonet.SearchResult.TOPIC_CAT)
117: .setText(""))
118: .addContent(
119: new Element(Geonet.SearchResult.PROTOCOL)
120: .setText(""))
121: .addContent(
122: new Element(Geonet.SearchResult.DOWNLOAD)
123: .setText(Geonet.Text.OFF))
124: .addContent(
125: new Element(Geonet.SearchResult.DYNAMIC)
126: .setText(Geonet.Text.OFF))
127: .addContent(
128: new Element(Geonet.SearchResult.DIGITAL)
129: .setText(Geonet.Text.OFF))
130: .addContent(
131: new Element(Geonet.SearchResult.PAPER)
132: .setText(Geonet.Text.OFF))
133: .addContent(
134: new Element(Geonet.SearchResult.SITE_ID)
135: .setText(""))
136: .addContent(
137: new Element(Geonet.SearchResult.GROUP)
138: .setText(""))
139: .addContent(
140: new Element(Geonet.SearchResult.PROFILE)
141: .setText(""))
142: .addContent(
143: new Element(Geonet.SearchResult.SERVERS))
144: .addContent(
145: new Element(Geonet.SearchResult.TEMPLATE)
146: .setText("n"))
147: .addContent(
148: new Element(Geonet.SearchResult.EXTENDED)
149: .setText(Geonet.Text.OFF))
150: .addContent(
151: new Element(Geonet.SearchResult.REMOTE)
152: .setText(Geonet.Text.OFF))
153: .addContent(
154: new Element(Geonet.SearchResult.TIMEOUT)
155: .setText("20"))
156: .addContent(
157: new Element(
158: Geonet.SearchResult.HITS_PER_PAGE)
159: .setText("10"))
160: .addContent(
161: new Element(Geonet.SearchResult.SIMILARITY)
162: .setText(".8"))
163: .addContent(
164: new Element(Geonet.SearchResult.OUTPUT)
165: .setText(Geonet.SearchResult.Output.FULL))
166: .addContent(
167: new Element(Geonet.SearchResult.SORT_BY)
168: .setText(Geonet.SearchResult.SortBy.RELEVANCE))
169: .addContent(
170: new Element(Geonet.SearchResult.INTERMAP)
171: .setText(Geonet.Text.ON));
172: session.setProperty(Geonet.Session.MAIN_SEARCH, elData);
173: }
174:
175: Element elTitle = elData.getChild(Geonet.SearchResult.TITLE);
176: Element elAbstract = elData
177: .getChild(Geonet.SearchResult.ABSTRACT);
178: Element elAny = elData.getChild(Geonet.SearchResult.ANY);
179: Element elRegion = elData.getChild(Geonet.SearchResult.REGION);
180: Element elPhrase = elData.getChild(Geonet.SearchResult.PHRASE);
181: Element elOr = elData.getChild(Geonet.SearchResult.OR);
182: Element elWithout = elData
183: .getChild(Geonet.SearchResult.WITHOUT);
184: Element elSouthBL = elData
185: .getChild(Geonet.SearchResult.SOUTH_BL);
186: Element elNorthBL = elData
187: .getChild(Geonet.SearchResult.NORTH_BL);
188: Element elEastBL = elData.getChild(Geonet.SearchResult.EAST_BL);
189: Element elWestBL = elData.getChild(Geonet.SearchResult.WEST_BL);
190: Element elRelation = elData
191: .getChild(Geonet.SearchResult.RELATION);
192: Element elDateFrom = elData
193: .getChild(Geonet.SearchResult.DATE_FROM);
194: Element elDateTo = elData.getChild(Geonet.SearchResult.DATE_TO);
195: Element elThemeKey = elData
196: .getChild(Geonet.SearchResult.THEME_KEY);
197: Element elCategory = elData
198: .getChild(Geonet.SearchResult.CATEGORY);
199: Element elTopicCat = elData
200: .getChild(Geonet.SearchResult.TOPIC_CAT);
201: Element elProtocol = elData
202: .getChild(Geonet.SearchResult.PROTOCOL);
203: Element elDownload = elData
204: .getChild(Geonet.SearchResult.DOWNLOAD);
205: Element elDynamic = elData
206: .getChild(Geonet.SearchResult.DYNAMIC);
207: Element elDigital = elData
208: .getChild(Geonet.SearchResult.DIGITAL);
209: Element elPaper = elData.getChild(Geonet.SearchResult.PAPER);
210: Element elSource = elData.getChild(Geonet.SearchResult.SITE_ID);
211: Element elGroup = elData.getChild(Geonet.SearchResult.GROUP);
212: Element elProfile = elData
213: .getChild(Geonet.SearchResult.PROFILE);
214: Element elServer = elData.getChild(Geonet.SearchResult.SERVERS);
215: Element elTemplate = elData
216: .getChild(Geonet.SearchResult.TEMPLATE);
217: Element elExtended = elData
218: .getChild(Geonet.SearchResult.EXTENDED);
219: Element elRemote = elData.getChild(Geonet.SearchResult.REMOTE);
220: Element elTimeout = elData
221: .getChild(Geonet.SearchResult.TIMEOUT);
222: Element elHitsPerPage = elData
223: .getChild(Geonet.SearchResult.HITS_PER_PAGE);
224: Element elSimilarity = elData
225: .getChild(Geonet.SearchResult.SIMILARITY);
226: Element elOutput = elData.getChild(Geonet.SearchResult.OUTPUT);
227: Element elSortBy = elData.getChild(Geonet.SearchResult.SORT_BY);
228: Element elIntermap = elData
229: .getChild(Geonet.SearchResult.INTERMAP);
230:
231: // get params from request
232: if (request != null) {
233: String sTitle = request
234: .getChildText(Geonet.SearchResult.TITLE);
235: String sAbstract = request
236: .getChildText(Geonet.SearchResult.ABSTRACT);
237: String sAny = request.getChildText(Geonet.SearchResult.ANY);
238: String sRegion = request
239: .getChildText(Geonet.SearchResult.REGION);
240: String sPhrase = request
241: .getChildText(Geonet.SearchResult.PHRASE);
242: String sOr = request.getChildText(Geonet.SearchResult.OR);
243: String sWithout = request
244: .getChildText(Geonet.SearchResult.WITHOUT);
245: String sSouthBL = request
246: .getChildText(Geonet.SearchResult.SOUTH_BL);
247: String sNorthBL = request
248: .getChildText(Geonet.SearchResult.NORTH_BL);
249: String sEastBL = request
250: .getChildText(Geonet.SearchResult.EAST_BL);
251: String sWestBL = request
252: .getChildText(Geonet.SearchResult.WEST_BL);
253: String sRelation = request
254: .getChildText(Geonet.SearchResult.RELATION);
255: String sDateFrom = request
256: .getChildText(Geonet.SearchResult.DATE_FROM);
257: String sDateTo = request
258: .getChildText(Geonet.SearchResult.DATE_TO);
259: String sThemeKey = request
260: .getChildText(Geonet.SearchResult.THEME_KEY);
261: String sCategory = request
262: .getChildText(Geonet.SearchResult.CATEGORY);
263: String sTopicCat = request
264: .getChildText(Geonet.SearchResult.TOPIC_CAT);
265: String sProtocol = request
266: .getChildText(Geonet.SearchResult.PROTOCOL);
267: String sDownload = request
268: .getChildText(Geonet.SearchResult.DOWNLOAD);
269: String sDynamic = request
270: .getChildText(Geonet.SearchResult.DYNAMIC);
271: String sDigital = request
272: .getChildText(Geonet.SearchResult.DIGITAL);
273: String sPaper = request
274: .getChildText(Geonet.SearchResult.PAPER);
275: String sSource = request
276: .getChildText(Geonet.SearchResult.SITE_ID);
277: String sGroup = request
278: .getChildText(Geonet.SearchResult.GROUP);
279: String sProfile = request
280: .getChildText(Geonet.SearchResult.PROFILE);
281: Iterator iServer = request.getChildren(
282: Geonet.SearchResult.SERVERS).iterator();
283: String sTemplate = request
284: .getChildText(Geonet.SearchResult.TEMPLATE);
285: String sExtended = request
286: .getChildText(Geonet.SearchResult.EXTENDED);
287: String sRemote = request
288: .getChildText(Geonet.SearchResult.REMOTE);
289: String sTimeout = request
290: .getChildText(Geonet.SearchResult.TIMEOUT);
291: String sHitsPerPage = request
292: .getChildText(Geonet.SearchResult.HITS_PER_PAGE);
293: String sSimilarity = request
294: .getChildText(Geonet.SearchResult.SIMILARITY);
295: String sOutput = request
296: .getChildText(Geonet.SearchResult.OUTPUT);
297: String sSortBy = request
298: .getChildText(Geonet.SearchResult.SORT_BY);
299: String sIntermap = request
300: .getChildText(Geonet.SearchResult.INTERMAP);
301:
302: if (sTitle != null)
303: elTitle.setText(sTitle);
304: if (sAbstract != null)
305: elAbstract.setText(sAbstract);
306: if (sAny != null)
307: elAny.setText(sAny);
308: if (sRegion != null)
309: elRegion.setText(sRegion);
310: if (sPhrase != null)
311: elPhrase.setText(sPhrase);
312: if (sOr != null)
313: elOr.setText(sOr);
314: if (sWithout != null)
315: elWithout.setText(sWithout);
316: if (sSouthBL != null)
317: elSouthBL.setText(sSouthBL);
318: if (sNorthBL != null)
319: elNorthBL.setText(sNorthBL);
320: if (sEastBL != null)
321: elEastBL.setText(sEastBL);
322: if (sWestBL != null)
323: elWestBL.setText(sWestBL);
324: if (sRelation != null)
325: elRelation.setText(sRelation);
326: if (sDateFrom != null)
327: elDateFrom.setText(sDateFrom);
328: if (sDateTo != null)
329: elDateTo.setText(sDateTo);
330: if (sThemeKey != null)
331: elThemeKey.setText(sThemeKey);
332: if (sCategory != null)
333: elCategory.setText(sCategory);
334: if (sTopicCat != null)
335: elTopicCat.setText(sTopicCat);
336: if (sProtocol != null)
337: elProtocol.setText(sProtocol);
338: elDownload.setText(sDownload != null ? sDownload
339: : Geonet.Text.OFF);
340: elDynamic.setText(sDynamic != null ? sDynamic
341: : Geonet.Text.OFF);
342: elDigital.setText(sDigital != null ? sDigital
343: : Geonet.Text.OFF);
344: elPaper.setText(sPaper != null ? sPaper : Geonet.Text.OFF);
345: if (sSource != null)
346: elSource.setText(sSource);
347: if (sGroup != null)
348: elGroup.setText(sGroup);
349: if (sProfile != null)
350: elProfile.setText(sProfile);
351:
352: elServer.removeContent();
353: while (iServer.hasNext()) {
354: Element elSrv = (Element) iServer.next();
355: elServer.addContent(new Element(
356: Geonet.SearchResult.SERVER).addContent(elSrv
357: .getText()));
358: }
359:
360: if (sTemplate != null)
361: elTemplate.setText(sTemplate);
362: if (sExtended != null)
363: elExtended.setText(sExtended); // now you can change sExtended
364: if (sRemote != null)
365: elRemote.setText(sRemote);
366: if (sTimeout != null)
367: elTimeout.setText(sTimeout);
368: if (sHitsPerPage != null)
369: elHitsPerPage.setText(sHitsPerPage);
370: if (sSimilarity != null)
371: elSimilarity.setText(sSimilarity);
372:
373: if (sOutput != null) {
374: if (sOutput.equals(Geonet.SearchResult.Output.TEXT))
375: elOutput.setText(sOutput);
376: else
377: elOutput.setText(Geonet.SearchResult.Output.FULL);
378: }
379:
380: if (sSortBy != null) {
381: if (sSortBy.equals(Geonet.SearchResult.SortBy.DATE))
382: elSortBy.setText(sSortBy);
383:
384: else if (sSortBy
385: .equals(Geonet.SearchResult.SortBy.POPULARITY))
386: elSortBy.setText(sSortBy);
387:
388: else if (sSortBy
389: .equals(Geonet.SearchResult.SortBy.RATING))
390: elSortBy.setText(sSortBy);
391: else
392: elSortBy
393: .setText(Geonet.SearchResult.SortBy.RELEVANCE);
394: }
395:
396: if (sIntermap != null)
397: elIntermap.setText(sIntermap);
398:
399: }
400:
401: srvContext.info("Returning search data");
402:
403: return elData;
404: }
405:
406: //--------------------------------------------------------------------------
407:
408: /**
409: * Splits a text in tokens.
410: * @param requestStr
411: * @return
412: */
413: public static String splitWord(String requestStr) {
414: Analyzer a = new StandardAnalyzer();
415: // Analyzer a = new CJKAnalyzer();
416:
417: StringReader sr = new StringReader(requestStr);
418: TokenStream ts = a.tokenStream(null, sr);
419:
420: String result = new String("");
421:
422: try {
423: Token t = ts.next();
424:
425: while (t != null) {
426: result += (" " + t.termText());
427: t = ts.next();
428: }
429: } catch (IOException e) {
430: e.printStackTrace();
431: }
432:
433: return result;
434: }
435: }
436:
437: //=============================================================================
|