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