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.geonet;
025:
026: import java.io.File;
027: import java.sql.SQLException;
028: import java.util.UUID;
029: import jeeves.exceptions.BadInputEx;
030: import jeeves.interfaces.Logger;
031: import jeeves.resources.dbms.Dbms;
032: import jeeves.server.context.ServiceContext;
033: import jeeves.server.resources.ResourceManager;
034: import org.fao.geonet.constants.Geonet;
035: import org.fao.geonet.kernel.harvest.harvester.AbstractHarvester;
036: import org.fao.geonet.kernel.harvest.harvester.AbstractParams;
037: import org.fao.geonet.lib.Lib;
038: import org.jdom.Element;
039:
040: //=============================================================================
041:
042: public class GeonetHarvester extends AbstractHarvester {
043: public static final String TYPE = "geonetwork";
044:
045: //--------------------------------------------------------------------------
046: //---
047: //--- Static init
048: //---
049: //--------------------------------------------------------------------------
050:
051: public static void init(ServiceContext context) throws Exception {
052: }
053:
054: //--------------------------------------------------------------------------
055: //---
056: //--- Harvesting type
057: //---
058: //--------------------------------------------------------------------------
059:
060: public String getType() {
061: return TYPE;
062: }
063:
064: //--------------------------------------------------------------------------
065: //---
066: //--- Init
067: //---
068: //--------------------------------------------------------------------------
069:
070: protected void doInit(Element node) throws BadInputEx {
071: params = new GeonetParams(dataMan);
072: params.create(node);
073: }
074:
075: //---------------------------------------------------------------------------
076: //---
077: //--- doDestroy
078: //---
079: //---------------------------------------------------------------------------
080:
081: protected void doDestroy(Dbms dbms) throws SQLException {
082: File icon = new File(context.getAppPath() + "images/logos",
083: params.uuid + ".gif");
084:
085: icon.delete();
086: Lib.sources.delete(dbms, params.uuid);
087: }
088:
089: //---------------------------------------------------------------------------
090: //---
091: //--- Add
092: //---
093: //---------------------------------------------------------------------------
094:
095: protected String doAdd(Dbms dbms, Element node) throws BadInputEx,
096: SQLException {
097: params = new GeonetParams(dataMan);
098:
099: //--- retrieve/initialize information
100: params.create(node);
101:
102: //--- force the creation of a new uuid
103: params.uuid = UUID.randomUUID().toString();
104:
105: String id = settingMan.add(dbms, "harvesting", "node",
106: getType());
107:
108: storeNode(dbms, params, "id:" + id);
109: Lib.sources.update(dbms, params.uuid, params.name, false);
110:
111: return id;
112: }
113:
114: //---------------------------------------------------------------------------
115: //---
116: //--- Update
117: //---
118: //---------------------------------------------------------------------------
119:
120: protected void doUpdate(Dbms dbms, String id, Element node)
121: throws BadInputEx, SQLException {
122: GeonetParams copy = params.copy();
123:
124: //--- update variables
125: copy.update(node);
126:
127: String path = "harvesting/id:" + id;
128:
129: settingMan.removeChildren(dbms, path);
130:
131: //--- update database
132: storeNode(dbms, copy, path);
133:
134: //--- we update a copy first because if there is an exception GeonetParams
135: //--- could be half updated and so it could be in an inconsistent state
136:
137: Lib.sources.update(dbms, copy.uuid, copy.name, false);
138:
139: params = copy;
140: }
141:
142: //---------------------------------------------------------------------------
143:
144: protected void storeNodeExtra(Dbms dbms, AbstractParams p,
145: String path, String siteId, String optionsId)
146: throws SQLException {
147: GeonetParams params = (GeonetParams) p;
148:
149: settingMan.add(dbms, "id:" + siteId, "host", params.host);
150: settingMan.add(dbms, "id:" + siteId, "port", params.port);
151: settingMan.add(dbms, "id:" + siteId, "servlet", params.servlet);
152:
153: //--- store search nodes
154:
155: for (Search s : params.getSearches()) {
156: String searchID = settingMan.add(dbms, path, "search", "");
157:
158: settingMan.add(dbms, "id:" + searchID, "freeText",
159: s.freeText);
160: settingMan.add(dbms, "id:" + searchID, "title", s.title);
161: settingMan.add(dbms, "id:" + searchID, "abstract",
162: s.abstrac);
163: settingMan.add(dbms, "id:" + searchID, "keywords",
164: s.keywords);
165: settingMan
166: .add(dbms, "id:" + searchID, "digital", s.digital);
167: settingMan.add(dbms, "id:" + searchID, "hardcopy",
168: s.hardcopy);
169: settingMan.add(dbms, "id:" + searchID, "sourceUuid",
170: s.sourceUuid);
171: settingMan.add(dbms, "id:" + searchID, "sourceName",
172: s.sourceName);
173: }
174:
175: //--- store group mapping
176:
177: for (Group g : params.getGroupCopyPolicy()) {
178: String groupID = settingMan.add(dbms, path,
179: "groupCopyPolicy", "");
180:
181: settingMan.add(dbms, "id:" + groupID, "name", g.name);
182: settingMan.add(dbms, "id:" + groupID, "policy", g.policy);
183: }
184: }
185:
186: //---------------------------------------------------------------------------
187: //---
188: //--- addHarvestInfo
189: //---
190: //---------------------------------------------------------------------------
191:
192: public void addHarvestInfo(Element info, String id, String uuid) {
193: super .addHarvestInfo(info, id, uuid);
194:
195: String small = context.getBaseUrl()
196: + "/srv/en/resources.get?access=public&id=" + id
197: + "&fname=";
198:
199: String large = context.getBaseUrl()
200: + "/srv/en/graphover.show?access=public&id=" + id
201: + "&fname=";
202:
203: info.addContent(new Element("smallThumbnail").setText(small));
204: info.addContent(new Element("largeThumbnail").setText(large));
205: }
206:
207: //---------------------------------------------------------------------------
208: //---
209: //--- AbstractParameters
210: //---
211: //---------------------------------------------------------------------------
212:
213: public AbstractParams getParams() {
214: return params;
215: }
216:
217: //---------------------------------------------------------------------------
218: //---
219: //--- AddInfo
220: //---
221: //---------------------------------------------------------------------------
222:
223: protected void doAddInfo(Element node) {
224: //--- if the harvesting is not started yet, we don't have any info
225:
226: if (result == null)
227: return;
228:
229: //--- ok, add proper info
230:
231: Element info = node.getChild("info");
232: Element res = new Element("result");
233:
234: add(res, "total", result.totalMetadata);
235: add(res, "added", result.addedMetadata);
236: add(res, "updated", result.updatedMetadata);
237: add(res, "unchanged", result.unchangedMetadata);
238: add(res, "unknownSchema", result.unknownSchema);
239: add(res, "removed", result.locallyRemoved);
240: add(res, "unretrievable", result.unretrievable);
241:
242: info.addContent(res);
243: }
244:
245: //---------------------------------------------------------------------------
246: //---
247: //--- Harvest
248: //---
249: //---------------------------------------------------------------------------
250:
251: protected void doHarvest(Logger log, ResourceManager rm)
252: throws Exception {
253: Dbms dbms = (Dbms) rm.open(Geonet.Res.MAIN_DB);
254:
255: Harvester h = new Harvester(log, context, dbms, params);
256: result = h.harvest();
257: }
258:
259: //---------------------------------------------------------------------------
260: //---
261: //--- Variables
262: //---
263: //---------------------------------------------------------------------------
264:
265: private GeonetParams params;
266: private GeonetResult result;
267: }
268:
269: //=============================================================================
270:
271: class GeonetResult {
272: public int totalMetadata;
273: public int addedMetadata;
274: public int updatedMetadata;
275: public int unchangedMetadata;
276: public int locallyRemoved;
277: public int unknownSchema;
278: public int unretrievable;
279: }
280:
281: //=============================================================================
|