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.webdav;
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 WebDavHarvester 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 "webdav";
060: }
061:
062: //--------------------------------------------------------------------------
063: //---
064: //--- Init
065: //---
066: //--------------------------------------------------------------------------
067:
068: protected void doInit(Element node) throws BadInputEx {
069: params = new WebDavParams(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 WebDavParams(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: WebDavParams 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: WebDavParams params = (WebDavParams) 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: settingMan.add(dbms, "id:" + optionsId, "recurse",
157: params.recurse);
158: }
159:
160: //---------------------------------------------------------------------------
161: //---
162: //--- AbstractParameters
163: //---
164: //---------------------------------------------------------------------------
165:
166: public AbstractParams getParams() {
167: return params;
168: }
169:
170: //---------------------------------------------------------------------------
171: //---
172: //--- AddInfo
173: //---
174: //---------------------------------------------------------------------------
175:
176: protected void doAddInfo(Element node) {
177: //--- if the harvesting is not started yet, we don't have any info
178:
179: if (result == null)
180: return;
181:
182: //--- ok, add proper info
183:
184: Element info = node.getChild("info");
185: Element res = new Element("result");
186:
187: add(res, "total", result.total);
188: add(res, "added", result.added);
189: add(res, "updated", result.updated);
190: add(res, "unchanged", result.unchanged);
191: add(res, "unknownSchema", result.unknownSchema);
192: add(res, "removed", result.locallyRemoved);
193: add(res, "unretrievable", result.unretrievable);
194: add(res, "badFormat", result.badFormat);
195: add(res, "doesNotValidate", result.doesNotValidate);
196:
197: info.addContent(res);
198: }
199:
200: //---------------------------------------------------------------------------
201: //---
202: //--- Harvest
203: //---
204: //---------------------------------------------------------------------------
205:
206: protected void doHarvest(Logger log, ResourceManager rm)
207: throws Exception {
208: Dbms dbms = (Dbms) rm.open(Geonet.Res.MAIN_DB);
209:
210: Harvester h = new Harvester(log, context, dbms, params);
211: result = h.harvest();
212: }
213:
214: //---------------------------------------------------------------------------
215: //---
216: //--- Variables
217: //---
218: //---------------------------------------------------------------------------
219:
220: private WebDavParams params;
221: private WebDavResult result;
222: }
223:
224: //=============================================================================
225:
226: class WebDavResult {
227: public int total;
228: public int added;
229: public int updated;
230: public int unchanged;
231: public int locallyRemoved;
232: public int unknownSchema;
233: public int unretrievable;
234: public int badFormat;
235: public int doesNotValidate;
236: }
237:
238: //=============================================================================
|