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.oaipmh;
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 OaiPmhHarvester 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 "oaipmh";
060: }
061:
062: //--------------------------------------------------------------------------
063: //---
064: //--- Init
065: //---
066: //--------------------------------------------------------------------------
067:
068: protected void doInit(Element node) throws BadInputEx {
069: params = new OaiPmhParams(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 OaiPmhParams(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: OaiPmhParams 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: OaiPmhParams params = (OaiPmhParams) p;
150:
151: settingMan.add(dbms, "id:" + siteId, "url", params.url);
152: settingMan.add(dbms, "id:" + siteId, "icon", params.icon);
153:
154: settingMan.add(dbms, "id:" + optionsId, "validate",
155: params.validate);
156:
157: //--- store search nodes
158:
159: for (Search s : params.getSearches()) {
160: String searchID = settingMan.add(dbms, path, "search", "");
161:
162: settingMan.add(dbms, "id:" + searchID, "from", s.from);
163: settingMan.add(dbms, "id:" + searchID, "until", s.until);
164: settingMan.add(dbms, "id:" + searchID, "set", s.set);
165: settingMan.add(dbms, "id:" + searchID, "prefix", s.prefix);
166: settingMan.add(dbms, "id:" + searchID, "stylesheet",
167: s.stylesheet);
168: }
169: }
170:
171: //---------------------------------------------------------------------------
172: //---
173: //--- AbstractParameters
174: //---
175: //---------------------------------------------------------------------------
176:
177: public AbstractParams getParams() {
178: return params;
179: }
180:
181: //---------------------------------------------------------------------------
182: //---
183: //--- AddInfo
184: //---
185: //---------------------------------------------------------------------------
186:
187: protected void doAddInfo(Element node) {
188: //--- if the harvesting is not started yet, we don't have any info
189:
190: if (result == null)
191: return;
192:
193: //--- ok, add proper info
194:
195: Element info = node.getChild("info");
196: Element res = new Element("result");
197:
198: add(res, "total", result.total);
199: add(res, "added", result.added);
200: add(res, "updated", result.updated);
201: add(res, "unchanged", result.unchanged);
202: add(res, "unknownSchema", result.unknownSchema);
203: add(res, "removed", result.locallyRemoved);
204: add(res, "unretrievable", result.unretrievable);
205: add(res, "badFormat", result.badFormat);
206: add(res, "doesNotValidate", result.doesNotValidate);
207:
208: info.addContent(res);
209: }
210:
211: //---------------------------------------------------------------------------
212: //---
213: //--- Harvest
214: //---
215: //---------------------------------------------------------------------------
216:
217: protected void doHarvest(Logger log, ResourceManager rm)
218: throws Exception {
219: Dbms dbms = (Dbms) rm.open(Geonet.Res.MAIN_DB);
220:
221: Harvester h = new Harvester(log, context, dbms, params);
222: result = h.harvest();
223: }
224:
225: //---------------------------------------------------------------------------
226: //---
227: //--- Variables
228: //---
229: //---------------------------------------------------------------------------
230:
231: private OaiPmhParams params;
232: private OaiPmhResult result;
233: }
234:
235: //=============================================================================
236:
237: class OaiPmhResult {
238: public int total;
239: public int added;
240: public int updated;
241: public int unchanged;
242: public int locallyRemoved;
243: public int unknownSchema;
244: public int unretrievable;
245: public int badFormat;
246: public int doesNotValidate;
247: }
248:
249: //=============================================================================
|