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.metadata;
025:
026: import java.util.List;
027: import jeeves.exceptions.BadParameterEx;
028: import jeeves.exceptions.BadServerResponseEx;
029: import jeeves.exceptions.MissingParameterEx;
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 jeeves.utils.XmlRequest;
036: import org.fao.geonet.GeonetContext;
037: import org.fao.geonet.constants.Geonet;
038: import org.fao.geonet.constants.Params;
039: import org.fao.geonet.exceptions.MetadataNotFoundEx;
040: import org.fao.geonet.kernel.DataManager;
041: import org.fao.geonet.kernel.harvest.HarvestManager;
042: import org.fao.geonet.kernel.harvest.harvester.AbstractHarvester;
043: import org.fao.geonet.kernel.harvest.harvester.geonet.GeonetHarvester;
044: import org.fao.geonet.kernel.harvest.harvester.geonet.GeonetParams;
045: import org.fao.geonet.lib.Lib;
046: import org.jdom.Element;
047:
048: //=============================================================================
049:
050: /** Allow to rate a metadata (local or harvested by the geonetwork harvesting type)
051: */
052:
053: public class Rate implements Service {
054: //--------------------------------------------------------------------------
055: //---
056: //--- Init
057: //---
058: //--------------------------------------------------------------------------
059:
060: public void init(String appPath, ServiceConfig params)
061: throws Exception {
062: }
063:
064: //--------------------------------------------------------------------------
065: //---
066: //--- Service
067: //---
068: //--------------------------------------------------------------------------
069:
070: public Element exec(Element params, ServiceContext context)
071: throws Exception {
072: Dbms dbms = (Dbms) context.getResourceManager().open(
073: Geonet.Res.MAIN_DB);
074:
075: GeonetContext gc = (GeonetContext) context
076: .getHandlerContext(Geonet.CONTEXT_NAME);
077: DataManager dm = gc.getDataManager();
078: HarvestManager hm = gc.getHarvestManager();
079:
080: String id = Util.getParam(params, Params.ID, null);
081: String uuid = Util.getParam(params, Params.UUID, null);
082: String rat = Util.getParam(params, Params.RATING);
083: String ip = context.getIpAddress();
084:
085: if (id == null && uuid == null)
086: throw new MissingParameterEx("id or uuid");
087:
088: //--- resolve id & uuid
089:
090: if (id != null)
091: uuid = dm.getMetadataUuid(dbms, id);
092: else
093: id = dm.getMetadataId(dbms, uuid);
094:
095: if (id == null || uuid == null)
096: throw new MetadataNotFoundEx("id:" + id + " or uuid:"
097: + uuid);
098:
099: if (ip == null)
100: ip = "???.???.???.???";
101:
102: if (!Lib.type.isInteger(rat))
103: throw new BadParameterEx(Params.RATING, rat);
104:
105: int rating = Integer.parseInt(rat);
106:
107: if (rating < 1 || rating > 5)
108: throw new BadParameterEx(Params.RATING, rat);
109:
110: String harvUuid = getHarvestingUuid(dbms, id);
111:
112: if (harvUuid == null)
113: //--- metadata is local, just rate it
114: rating = dm.rateMetadata(dbms, new Integer(id), ip, rating);
115: else {
116: //--- the metadata is harvested, is type=geonetwork?
117:
118: AbstractHarvester ah = hm.getHarvester(harvUuid);
119:
120: if (ah.getType().equals(GeonetHarvester.TYPE))
121: rating = setRemoteRating(context, (GeonetParams) ah
122: .getParams(), uuid, rating);
123: else
124: rating = -1;
125: }
126:
127: return new Element(Params.RATING).setText(Integer
128: .toString(rating));
129: }
130:
131: //--------------------------------------------------------------------------
132:
133: private String getHarvestingUuid(Dbms dbms, String id)
134: throws Exception {
135: String query = "SELECT harvestUuid FROM Metadata WHERE id=?";
136:
137: List list = dbms.select(query, new Integer(id)).getChildren();
138:
139: //--- if we don't have any metadata, just return
140:
141: if (list.size() == 0)
142: throw new MetadataNotFoundEx("id:" + id);
143:
144: Element rec = (Element) list.get(0);
145:
146: String harvUuid = rec.getChildText("harvestuuid");
147:
148: //--- metadata not harvested
149:
150: return (harvUuid.length() == 0) ? null : harvUuid;
151: }
152:
153: //--------------------------------------------------------------------------
154:
155: private int setRemoteRating(ServiceContext context,
156: GeonetParams params, String uuid, int rating)
157: throws Exception {
158: context.debug("Rating remote metadata with uuid:" + uuid);
159:
160: XmlRequest req = new XmlRequest(params.host, params.port);
161:
162: Lib.net.setupProxy(context, req);
163:
164: req.setAddress("/" + params.servlet + "/srv/en/"
165: + Geonet.Service.XML_METADATA_RATE);
166: req.clearParams();
167: req.addParam("uuid", uuid);
168: req.addParam("rating", rating);
169:
170: Element response = req.execute();
171:
172: if (!response.getName().equals(Params.RATING))
173: throw new BadServerResponseEx(response);
174:
175: return Integer.parseInt(response.getText());
176: }
177: }
178:
179: //=============================================================================
|