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 java.util.Iterator;
027: import java.util.Set;
028: import jeeves.interfaces.Service;
029: import jeeves.resources.dbms.Dbms;
030: import jeeves.server.ServiceConfig;
031: import jeeves.server.context.ServiceContext;
032: import org.fao.geonet.GeonetContext;
033: import org.fao.geonet.constants.Geonet;
034: import org.fao.geonet.kernel.AccessManager;
035: import org.jdom.Element;
036:
037: //=============================================================================
038:
039: /** Service used to return all categories in the system
040: */
041:
042: public class GetLatestUpdated implements Service {
043: private int _maxItems;
044: private long _timeBetweenUpdates;
045:
046: private Element _response;
047: private long _lastUpdateTime;
048:
049: //--------------------------------------------------------------------------
050: //---
051: //--- Init
052: //---
053: //--------------------------------------------------------------------------
054:
055: public void init(String appPath, ServiceConfig config)
056: throws Exception {
057: String sMaxItems = config.getValue("maxItems", "10");
058: String sTimeBetweenUpdates = config.getValue(
059: "timeBetweenUpdates", "60");
060:
061: _maxItems = Integer.parseInt(sMaxItems);
062: _timeBetweenUpdates = Integer.parseInt(sTimeBetweenUpdates) * 1000;
063: }
064:
065: //--------------------------------------------------------------------------
066: //---
067: //--- Service
068: //---
069: //--------------------------------------------------------------------------
070:
071: public Element exec(Element params, ServiceContext context)
072: throws Exception {
073: if (System.currentTimeMillis() > _lastUpdateTime
074: + _timeBetweenUpdates) {
075: GeonetContext gc = (GeonetContext) context
076: .getHandlerContext(Geonet.CONTEXT_NAME);
077: AccessManager am = gc.getAccessManager();
078:
079: Dbms dbms = (Dbms) context.getResourceManager().open(
080: Geonet.Res.MAIN_DB);
081:
082: Set<String> groups = am.getUserGroups(dbms, context
083: .getUserSession(), context.getIpAddress());
084:
085: // only get public metadata (group 1: internet) viewable (operation O: view)
086:
087: String query = "SELECT DISTINCT id, changeDate FROM Metadata, OperationAllowed "
088: + "WHERE id=metadataId AND isTemplate='n' AND operationId=0 AND (";
089:
090: String aux = "";
091:
092: for (String grpId : groups)
093: aux += " OR groupId=" + grpId;
094:
095: query += aux.substring(4);
096: query += ") ORDER BY changeDate DESC";
097:
098: Element result = dbms.select(query);
099:
100: _response = new Element("response");
101: int numItems = 0;
102:
103: for (Iterator iter = result.getChildren().iterator(); iter
104: .hasNext()
105: && numItems++ < _maxItems;) {
106: Element rec = (Element) iter.next();
107: String id = rec.getChildText("id");
108:
109: Element md = gc.getDataManager().getMetadata(context,
110: id, false);
111: _response.addContent(md);
112: }
113: _lastUpdateTime = System.currentTimeMillis();
114: }
115: return (Element) _response.clone();
116: }
117: }
118:
119: //=============================================================================
|