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.z3950;
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 Z3950Harvester extends AbstractHarvester {
043: //--------------------------------------------------------------------------
044: //---
045: //--- Static init
046: //---
047: //--------------------------------------------------------------------------
048:
049: public static void init(ServiceContext context) throws Exception {
050: }
051:
052: //--------------------------------------------------------------------------
053: //---
054: //--- Harvesting type
055: //---
056: //--------------------------------------------------------------------------
057:
058: public String getType() {
059: return "z3950";
060: }
061:
062: //--------------------------------------------------------------------------
063: //---
064: //--- Init
065: //---
066: //--------------------------------------------------------------------------
067:
068: protected void doInit(Element node) throws BadInputEx {
069: params = new Z3950Params(dataMan);
070: params.create(node);
071: }
072:
073: //---------------------------------------------------------------------------
074: //---
075: //--- doDestroy
076: //---
077: //---------------------------------------------------------------------------
078:
079: protected void doDestroy(Dbms dbms) throws SQLException {
080: File icon = new File(context.getAppPath() + "images/logos",
081: params.uuid + ".gif");
082:
083: icon.delete();
084: Lib.sources.delete(dbms, params.uuid);
085: }
086:
087: //---------------------------------------------------------------------------
088: //---
089: //--- Add
090: //---
091: //---------------------------------------------------------------------------
092:
093: protected String doAdd(Dbms dbms, Element node) throws BadInputEx,
094: SQLException {
095: params = new Z3950Params(dataMan);
096:
097: //--- retrieve/initialize information
098: params.create(node);
099:
100: //--- force the creation of a new uuid
101: params.uuid = UUID.randomUUID().toString();
102:
103: String id = settingMan.add(dbms, "harvesting", "node",
104: getType());
105:
106: storeNode(dbms, params, "id:" + id);
107: Lib.sources.update(dbms, params.uuid, params.name, true);
108: Lib.sources.copyLogo(context, "/images/harvesting/"
109: + params.icon, params.uuid);
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: Z3950Params 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 CswParams
135: //--- could be half updated and so it could be in an inconsistent state
136:
137: Lib.sources.update(dbms, copy.uuid, copy.name, true);
138: Lib.sources.copyLogo(context,
139: "/images/harvesting/" + copy.icon, copy.uuid);
140:
141: params = copy;
142: }
143:
144: //---------------------------------------------------------------------------
145:
146: protected void storeNodeExtra(Dbms dbms, AbstractParams p,
147: String path, String siteId, String optionsId)
148: throws SQLException {
149: Z3950Params params = (Z3950Params) p;
150:
151: // settingMan.add(dbms, "id:"+siteId, "capabUrl", params.capabUrl);
152: settingMan.add(dbms, "id:" + siteId, "icon", params.icon);
153:
154: //--- store search nodes
155:
156: // for (Search s : params.getSearches())
157: // {
158: // String searchID = settingMan.add(dbms, path, "search", "");
159: //
160: // settingMan.add(dbms, "id:"+searchID, "freeText", s.freeText);
161: // settingMan.add(dbms, "id:"+searchID, "title", s.title);
162: // settingMan.add(dbms, "id:"+searchID, "abstract", s.abstrac);
163: // settingMan.add(dbms, "id:"+searchID, "subject", s.subject);
164: // }
165: }
166:
167: //---------------------------------------------------------------------------
168: //---
169: //--- AbstractParameters
170: //---
171: //---------------------------------------------------------------------------
172:
173: public AbstractParams getParams() {
174: return params;
175: }
176:
177: //---------------------------------------------------------------------------
178: //---
179: //--- AddInfo
180: //---
181: //---------------------------------------------------------------------------
182:
183: protected void doAddInfo(Element node) {
184: //--- if the harvesting is not started yet, we don't have any info
185:
186: if (result == null)
187: return;
188:
189: //--- ok, add proper info
190:
191: Element info = node.getChild("info");
192: Element res = new Element("result");
193:
194: //--- put here harvesting information after it has been executed
195:
196: // add(res, "total", result.totalMetadata);
197: // add(res, "added", result.addedMetadata);
198: // add(res, "updated", result.updatedMetadata);
199: // add(res, "unchanged", result.unchangedMetadata);
200: // add(res, "unknownSchema",result.unknownSchema);
201: // add(res, "removed", result.locallyRemoved);
202: // add(res, "unretrievable",result.unretrievable);
203:
204: info.addContent(res);
205: }
206:
207: //---------------------------------------------------------------------------
208: //---
209: //--- Harvest
210: //---
211: //---------------------------------------------------------------------------
212:
213: protected void doHarvest(Logger log, ResourceManager rm)
214: throws Exception {
215: Dbms dbms = (Dbms) rm.open(Geonet.Res.MAIN_DB);
216:
217: Harvester h = new Harvester(log, context, dbms, params);
218: result = h.harvest();
219: }
220:
221: //---------------------------------------------------------------------------
222: //---
223: //--- Variables
224: //---
225: //---------------------------------------------------------------------------
226:
227: private Z3950Params params;
228: private Z3950Result result;
229: }
230:
231: //=============================================================================
232:
233: class Z3950Result {
234: // public int totalMetadata;
235: // public int addedMetadata;
236: // public int updatedMetadata;
237: // public int unchangedMetadata;
238: // public int locallyRemoved;
239: // public int unknownSchema;
240: // public int unretrievable;
241: }
242:
243: //=============================================================================
|