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.guiservices.metadata;
025:
026: import org.jdom.*;
027:
028: import jeeves.interfaces.*;
029: import jeeves.server.*;
030: import jeeves.server.context.*;
031:
032: import org.fao.geonet.constants.*;
033: import org.fao.geonet.kernel.search.*;
034: import org.fao.geonet.kernel.DataManager;
035: import org.fao.geonet.GeonetContext;
036:
037: import java.util.*;
038:
039: //=============================================================================
040:
041: /** main.search service. Perform a search
042: */
043:
044: public class GetRandom implements Service {
045: private int _maxItems;
046: private long _timeBetweenUpdates;
047:
048: private Element _response;
049: private long _lastUpdateTime;
050:
051: private ServiceConfig _config;
052:
053: //--------------------------------------------------------------------------
054: //---
055: //--- Init
056: //---
057: //--------------------------------------------------------------------------
058:
059: public void init(String appPath, ServiceConfig config)
060: throws Exception {
061: _config = config;
062:
063: String sMaxItems = config.getValue("maxItems", "10");
064: _maxItems = Integer.parseInt(sMaxItems);
065: String sTimeBetweenUpdates = config.getValue(
066: "timeBetweenUpdates", "60");
067: _timeBetweenUpdates = Integer.parseInt(sTimeBetweenUpdates) * 1000;
068:
069: }
070:
071: //--------------------------------------------------------------------------
072: //---
073: //--- Service
074: //---
075: //--------------------------------------------------------------------------
076:
077: public Element exec(Element params, ServiceContext context)
078: throws Exception {
079: if (System.currentTimeMillis() > _lastUpdateTime
080: + _timeBetweenUpdates) {
081: GeonetContext gc = (GeonetContext) context
082: .getHandlerContext(Geonet.CONTEXT_NAME);
083: DataManager dataMan = gc.getDataManager();
084: SearchManager searchMan = gc.getSearchmanager();
085: MetaSearcher searcher = searchMan.newSearcher(
086: SearchManager.LUCENE, Geonet.File.SEARCH_LUCENE);
087:
088: // FIXME: featured should be at metadata level, not at group level
089: Element searchRequest = new Element("request");
090: searchRequest.addContent(new Element("featured")
091: .setText("true"));
092: searcher.search(context, searchRequest, _config);
093:
094: Element presentRequest = new Element("request");
095: presentRequest.addContent(new Element("fast")
096: .setText("true"));
097: presentRequest.addContent(new Element("from").setText("1"));
098: presentRequest.addContent(new Element("to")
099: .setText(searcher.getSize() + ""));
100: List results = searcher.present(context, presentRequest,
101: _config).getChildren();
102:
103: _response = new Element("response");
104: for (int i = 0; i < _maxItems && results.size() > 1; i++) {
105: Random rnd = new Random();
106: int r = rnd.nextInt(results.size() - 1) + 1; // skip summary
107:
108: Element mdInfo = (Element) results.remove(r);
109: mdInfo.detach();
110:
111: Element info = mdInfo.getChild("info", Edit.NAMESPACE);
112: String id = info.getChildText("id");
113: Element md = dataMan.getMetadata(context, id, false);
114:
115: _response.addContent(md);
116: }
117: _lastUpdateTime = System.currentTimeMillis();
118: }
119: return (Element) _response.clone();
120: }
121: }
122:
123: //=============================================================================
|