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.kernel.harvest.harvester.geonet20;
025:
026: import java.io.File;
027: import java.sql.SQLException;
028: import java.util.ArrayList;
029: import java.util.UUID;
030: import jeeves.exceptions.BadInputEx;
031: import jeeves.exceptions.UserNotFoundEx;
032: import jeeves.interfaces.Logger;
033: import jeeves.resources.dbms.Dbms;
034: import jeeves.server.context.ServiceContext;
035: import jeeves.server.resources.ResourceManager;
036: import jeeves.utils.Xml;
037: import jeeves.utils.XmlRequest;
038: import org.fao.geonet.constants.Geonet;
039: import org.fao.geonet.kernel.harvest.harvester.AbstractHarvester;
040: import org.fao.geonet.kernel.harvest.harvester.AbstractParams;
041: import org.fao.geonet.kernel.harvest.harvester.CategoryMapper;
042: import org.fao.geonet.kernel.harvest.harvester.GroupMapper;
043: import org.fao.geonet.lib.Lib;
044: import org.jdom.Element;
045:
046: //=============================================================================
047:
048: public class Geonet20Harvester extends AbstractHarvester {
049: //--------------------------------------------------------------------------
050: //---
051: //--- Static init
052: //---
053: //--------------------------------------------------------------------------
054:
055: public static void init(ServiceContext context) throws Exception {
056: }
057:
058: //--------------------------------------------------------------------------
059: //---
060: //--- Harvesting type
061: //---
062: //--------------------------------------------------------------------------
063:
064: public String getType() {
065: return "geonetwork20";
066: }
067:
068: //--------------------------------------------------------------------------
069: //---
070: //--- Init
071: //---
072: //--------------------------------------------------------------------------
073:
074: protected void doInit(Element node) throws BadInputEx {
075: params = new GeonetParams(dataMan);
076: params.create(node);
077: }
078:
079: //---------------------------------------------------------------------------
080: //---
081: //--- doDestroy
082: //---
083: //---------------------------------------------------------------------------
084:
085: protected void doDestroy(Dbms dbms) throws SQLException {
086: File icon = new File(context.getAppPath() + "images/logos",
087: params.uuid + ".gif");
088:
089: icon.delete();
090: Lib.sources.delete(dbms, params.uuid);
091: }
092:
093: //---------------------------------------------------------------------------
094: //---
095: //--- Add
096: //---
097: //---------------------------------------------------------------------------
098:
099: protected String doAdd(Dbms dbms, Element node) throws BadInputEx,
100: SQLException {
101: params = new GeonetParams(dataMan);
102:
103: //--- retrieve/initialize information
104: params.create(node);
105:
106: //--- force the creation of a new uuid
107: params.uuid = UUID.randomUUID().toString();
108:
109: String id = settingMan.add(dbms, "harvesting", "node",
110: getType());
111:
112: storeNode(dbms, params, "id:" + id);
113: Lib.sources.update(dbms, params.uuid, params.name, true);
114: Lib.sources.copyLogo(context, "/images/harvesting/gn20.gif",
115: params.uuid);
116:
117: return id;
118: }
119:
120: //---------------------------------------------------------------------------
121: //---
122: //--- Update
123: //---
124: //---------------------------------------------------------------------------
125:
126: protected void doUpdate(Dbms dbms, String id, Element node)
127: throws BadInputEx, SQLException {
128: //--- update variables
129:
130: GeonetParams copy = params.copy();
131:
132: //--- update variables
133: copy.update(node);
134:
135: String path = "harvesting/id:" + id;
136:
137: settingMan.removeChildren(dbms, path);
138:
139: //--- update database
140: storeNode(dbms, copy, path);
141:
142: //--- we update a copy first because if there is an exception GeonetParams
143: //--- could be half updated and so it could be in an inconsistent state
144:
145: Lib.sources.update(dbms, copy.uuid, copy.name, true);
146:
147: params = copy;
148: }
149:
150: //---------------------------------------------------------------------------
151:
152: protected void storeNodeExtra(Dbms dbms, AbstractParams p,
153: String path, String siteId, String optionsId)
154: throws SQLException {
155: GeonetParams params = (GeonetParams) p;
156:
157: settingMan.add(dbms, "id:" + siteId, "host", params.host);
158: settingMan.add(dbms, "id:" + siteId, "port", params.port);
159: settingMan.add(dbms, "id:" + siteId, "servlet", params.servlet);
160:
161: //--- store search nodes
162:
163: for (Search s : params.getSearches()) {
164: String searchID = settingMan.add(dbms, path, "search", "");
165:
166: settingMan.add(dbms, "id:" + searchID, "freeText",
167: s.freeText);
168: settingMan.add(dbms, "id:" + searchID, "title", s.title);
169: settingMan.add(dbms, "id:" + searchID, "abstract",
170: s.abstrac);
171: settingMan.add(dbms, "id:" + searchID, "keywords",
172: s.keywords);
173: settingMan
174: .add(dbms, "id:" + searchID, "digital", s.digital);
175: settingMan.add(dbms, "id:" + searchID, "hardcopy",
176: s.hardcopy);
177: settingMan.add(dbms, "id:" + searchID, "siteId", s.siteId);
178: }
179: }
180:
181: //---------------------------------------------------------------------------
182: //---
183: //--- addHarvestInfo
184: //---
185: //---------------------------------------------------------------------------
186:
187: public void addHarvestInfo(Element info, String id, String uuid) {
188: super .addHarvestInfo(info, id, uuid);
189:
190: String small = "http://" + params.host + ":" + params.port
191: + "/" + params.servlet
192: + "/srv/en/resources.get2?access=public&uuid=" + uuid
193: + "&fname=";
194:
195: info.addContent(new Element("smallThumbnail").setText(small));
196: }
197:
198: //---------------------------------------------------------------------------
199: //---
200: //--- AbstractParameters
201: //---
202: //---------------------------------------------------------------------------
203:
204: public AbstractParams getParams() {
205: return params;
206: }
207:
208: //---------------------------------------------------------------------------
209: //---
210: //--- AddInfo
211: //---
212: //---------------------------------------------------------------------------
213:
214: protected void doAddInfo(Element node) {
215: //--- if the harvesting is not started yet, we don't have any info
216:
217: if (result == null)
218: return;
219:
220: //--- ok, add proper info
221:
222: Element info = node.getChild("info");
223:
224: for (AlignerResult ar : result.alResult) {
225: Element site = new Element("search");
226: site.setAttribute("siteId", ar.siteId);
227:
228: add(site, "total", ar.totalMetadata);
229: add(site, "added", ar.addedMetadata);
230: add(site, "updated", ar.updatedMetadata);
231: add(site, "unchanged", ar.unchangedMetadata);
232: add(site, "skipped", ar.schemaSkipped + ar.uuidSkipped);
233: add(site, "removed", ar.locallyRemoved);
234:
235: info.addContent(site);
236: }
237: }
238:
239: //---------------------------------------------------------------------------
240: //---
241: //--- Harvest
242: //---
243: //---------------------------------------------------------------------------
244:
245: protected void doHarvest(Logger log, ResourceManager rm)
246: throws Exception {
247: Dbms dbms = (Dbms) rm.open(Geonet.Res.MAIN_DB);
248:
249: CategoryMapper localCateg = new CategoryMapper(dbms);
250: GroupMapper localGroups = new GroupMapper(dbms);
251:
252: XmlRequest req = new XmlRequest(params.host, params.port);
253:
254: Lib.net.setupProxy(context, req);
255:
256: String name = getParams().name;
257:
258: //--- login
259:
260: if (params.useAccount) {
261: log.info("Login into : " + name);
262:
263: req.setAddress("/" + params.servlet + "/srv/en/"
264: + Geonet.Service.XML_LOGIN);
265: req.addParam("username", params.username);
266: req.addParam("password", params.password);
267:
268: Element response = req.execute();
269:
270: if (!response.getName().equals("ok"))
271: throw new UserNotFoundEx(params.username);
272: }
273:
274: //--- search
275:
276: result = new GeonetResult();
277:
278: Aligner aligner = new Aligner(log, req, params, dataMan, dbms,
279: context, localCateg, localGroups);
280:
281: for (Search s : params.getSearches()) {
282: log.info("Searching on : " + name + "/" + s.siteId);
283:
284: req.setAddress("/" + params.servlet + "/srv/en/"
285: + Geonet.Service.XML_SEARCH);
286:
287: Element searchResult = req.execute(s.createRequest());
288:
289: log.debug("Obtained:\n" + Xml.getString(searchResult));
290:
291: //--- site alignment
292: AlignerResult ar = aligner.align(searchResult, s.siteId);
293:
294: //--- collect some stats
295: result.alResult.add(ar);
296: }
297:
298: //--- logout
299:
300: if (params.useAccount) {
301: log.info("Logout from : " + name);
302:
303: req.clearParams();
304: req.setAddress("/" + params.servlet + "/srv/en/"
305: + Geonet.Service.XML_LOGOUT);
306: }
307:
308: dbms.commit();
309: }
310:
311: //---------------------------------------------------------------------------
312: //---
313: //--- Variables
314: //---
315: //---------------------------------------------------------------------------
316:
317: private GeonetParams params;
318: private GeonetResult result;
319: }
320:
321: //=============================================================================
322:
323: class GeonetResult {
324: public ArrayList<AlignerResult> alResult = new ArrayList<AlignerResult>();
325: }
326:
327: //=============================================================================
|