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.relations;
025:
026: import java.sql.SQLException;
027: import java.util.HashSet;
028: import java.util.List;
029: import java.util.Set;
030: import jeeves.interfaces.Service;
031: import jeeves.resources.dbms.Dbms;
032: import jeeves.server.ServiceConfig;
033: import jeeves.server.context.ServiceContext;
034: import jeeves.utils.Util;
035: import org.fao.geonet.GeonetContext;
036: import org.fao.geonet.constants.Geonet;
037: import org.fao.geonet.kernel.DataManager;
038: import org.jdom.Element;
039:
040: //=============================================================================
041:
042: public class Get implements Service {
043: public void init(String appPath, ServiceConfig params)
044: throws Exception {
045: }
046:
047: //--------------------------------------------------------------------------
048: //---
049: //--- Service
050: //---
051: //--------------------------------------------------------------------------
052:
053: public Element exec(Element params, ServiceContext context)
054: throws Exception {
055: GeonetContext gc = (GeonetContext) context
056: .getHandlerContext(Geonet.CONTEXT_NAME);
057: DataManager dm = gc.getDataManager();
058:
059: Dbms dbms = (Dbms) context.getResourceManager().open(
060: Geonet.Res.MAIN_DB);
061:
062: int id = Util.getParamAsInt(params, "id");
063:
064: String relation = Util.getParam(params, "relation", "normal");
065:
066: Set<String> result = new HashSet<String>();
067:
068: //--- perform proper queries to retrieve the id set
069:
070: if (relation.equals("normal") || relation.equals("full")) {
071: String query = "SELECT relatedId FROM Relations WHERE id=?";
072: result.addAll(retrieveIds(dbms, query, "relatedid", id));
073: }
074:
075: if (relation.equals("reverse") || relation.equals("full")) {
076: String query = "SELECT id FROM Relations WHERE relatedId=?";
077: result.addAll(retrieveIds(dbms, query, "id", id));
078: }
079:
080: //--- retrieve metadata and return result
081:
082: Element response = new Element("response");
083:
084: for (String mdId : result) {
085: Element md = dm.getMetadata(context, mdId, false);
086:
087: //--- we could have a race condition so, just perform a simple check
088:
089: if (md != null)
090: response.addContent(md);
091: }
092:
093: return response;
094: }
095:
096: //--------------------------------------------------------------------------
097:
098: private Set<String> retrieveIds(Dbms dbms, String query,
099: String field, int id) throws SQLException {
100: List records = dbms.select(query, id).getChildren();
101:
102: Set<String> results = new HashSet<String>();
103:
104: for (Object o : records) {
105: Element rec = (Element) o;
106: String val = rec.getChildText(field);
107:
108: results.add(val);
109: }
110:
111: return results;
112: }
113: }
114:
115: //=============================================================================
|