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;
025:
026: import java.sql.SQLException;
027: import java.util.HashMap;
028: import jeeves.exceptions.BadInputEx;
029: import jeeves.exceptions.JeevesException;
030: import jeeves.exceptions.MissingParameterEx;
031: import jeeves.resources.dbms.Dbms;
032: import jeeves.server.context.ServiceContext;
033: import jeeves.utils.Log;
034: import jeeves.utils.Xml;
035: import org.fao.geonet.constants.Edit;
036: import org.fao.geonet.constants.Geonet;
037: import org.fao.geonet.kernel.DataManager;
038: import org.fao.geonet.kernel.harvest.Common.OperResult;
039: import org.fao.geonet.kernel.harvest.harvester.AbstractHarvester;
040: import org.fao.geonet.kernel.setting.SettingManager;
041: import org.jdom.Element;
042:
043: //=============================================================================
044:
045: public class HarvestManager {
046: //---------------------------------------------------------------------------
047: //---
048: //--- Constructor
049: //---
050: //---------------------------------------------------------------------------
051:
052: public HarvestManager(ServiceContext context, SettingManager sm,
053: DataManager dm) throws Exception {
054: this .context = context;
055:
056: xslPath = context.getAppPath() + Geonet.Path.STYLESHEETS
057: + "/xml/harvesting/";
058: settingMan = sm;
059: dataMan = dm;
060:
061: AbstractHarvester.staticInit(context);
062:
063: Element entries = settingMan.get("harvesting", -1).getChild(
064: "children");
065:
066: if (entries != null)
067: for (Object o : entries.getChildren()) {
068: Element node = transform((Element) o);
069: String type = node.getAttributeValue("type");
070:
071: AbstractHarvester ah = AbstractHarvester.create(type,
072: context, sm, dm);
073: ah.init(node);
074: hmHarvesters.put(ah.getID(), ah);
075: hmHarvestLookup.put(ah.getParams().uuid, ah);
076: }
077: }
078:
079: //---------------------------------------------------------------------------
080:
081: private Element transform(Element node) throws Exception {
082: String type = node.getChildText("value");
083:
084: node = (Element) node.clone();
085:
086: return Xml.transform(node, xslPath + type + ".xsl");
087: }
088:
089: //---------------------------------------------------------------------------
090: //---
091: //--- API methods
092: //---
093: //---------------------------------------------------------------------------
094:
095: public Element get(String id) throws Exception {
096: Element result = (id == null) ? settingMan
097: .get("harvesting", -1) : settingMan.get(
098: "harvesting/id:" + id, -1);
099:
100: if (result == null)
101: return null;
102:
103: if (id != null) {
104: result = transform(result);
105: addInfo(result);
106: }
107:
108: else {
109: Element nodes = result.getChild("children");
110:
111: result = new Element("nodes");
112:
113: if (nodes != null)
114: for (Object o : nodes.getChildren()) {
115: Element node = transform((Element) o);
116: addInfo(node);
117: result.addContent(node);
118: }
119: }
120:
121: return result;
122: }
123:
124: //---------------------------------------------------------------------------
125:
126: public String add(Dbms dbms, Element node) throws JeevesException,
127: SQLException {
128: Log.debug(Geonet.HARVEST_MAN, "Adding harvesting node : \n"
129: + Xml.getString(node));
130:
131: String type = node.getAttributeValue("type");
132:
133: AbstractHarvester ah = AbstractHarvester.create(type, context,
134: settingMan, dataMan);
135:
136: ah.add(dbms, node);
137: hmHarvesters.put(ah.getID(), ah);
138: hmHarvestLookup.put(ah.getParams().uuid, ah);
139: Log.debug(Geonet.HARVEST_MAN, "Added node with id : \n"
140: + ah.getID());
141:
142: return ah.getID();
143: }
144:
145: //---------------------------------------------------------------------------
146:
147: public synchronized boolean update(Dbms dbms, Element node)
148: throws BadInputEx, SQLException {
149: Log.debug(Geonet.HARVEST_MAN, "Updating harvesting node : \n"
150: + Xml.getString(node));
151:
152: String id = node.getAttributeValue("id");
153:
154: if (id == null)
155: throw new MissingParameterEx("attribute:id", node);
156:
157: AbstractHarvester ah = hmHarvesters.get(id);
158:
159: if (ah == null)
160: return false;
161:
162: ah.update(dbms, node);
163: return true;
164: }
165:
166: //---------------------------------------------------------------------------
167: /** This method must be synchronized because it cannot run if we are updating some entries */
168:
169: public synchronized OperResult remove(Dbms dbms, String id)
170: throws Exception {
171: Log.debug(Geonet.HARVEST_MAN, "Removing harvesting with id : "
172: + id);
173:
174: AbstractHarvester ah = hmHarvesters.get(id);
175:
176: if (ah == null)
177: return OperResult.NOT_FOUND;
178:
179: hmHarvestLookup.remove(ah.getParams().uuid);
180: ah.destroy(dbms);
181: hmHarvesters.remove(id);
182: settingMan.remove(dbms, "harvesting/id:" + id);
183:
184: return OperResult.OK;
185: }
186:
187: //---------------------------------------------------------------------------
188:
189: public OperResult start(Dbms dbms, String id) throws SQLException {
190: Log.debug(Geonet.HARVEST_MAN, "Starting harvesting with id : "
191: + id);
192:
193: AbstractHarvester ah = hmHarvesters.get(id);
194:
195: if (ah == null)
196: return OperResult.NOT_FOUND;
197:
198: return ah.start(dbms);
199: }
200:
201: //---------------------------------------------------------------------------
202:
203: public OperResult stop(Dbms dbms, String id) throws SQLException {
204: Log.debug(Geonet.HARVEST_MAN, "Stopping harvesting with id : "
205: + id);
206:
207: AbstractHarvester ah = hmHarvesters.get(id);
208:
209: if (ah == null)
210: return OperResult.NOT_FOUND;
211:
212: return ah.stop(dbms);
213: }
214:
215: //---------------------------------------------------------------------------
216:
217: public OperResult run(String id) {
218: Log.debug(Geonet.HARVEST_MAN, "Running harvesting with id : "
219: + id);
220:
221: AbstractHarvester ah = hmHarvesters.get(id);
222:
223: if (ah == null)
224: return OperResult.NOT_FOUND;
225:
226: return ah.run();
227: }
228:
229: //---------------------------------------------------------------------------
230:
231: public Element getHarvestInfo(String harvestUuid, String id,
232: String uuid) {
233: Element info = new Element(Edit.Info.Elem.HARVEST_INFO);
234:
235: AbstractHarvester ah = hmHarvestLookup.get(harvestUuid);
236:
237: if (ah != null)
238: ah.addHarvestInfo(info, id, uuid);
239:
240: return info;
241: }
242:
243: //---------------------------------------------------------------------------
244:
245: public AbstractHarvester getHarvester(String harvestUuid) {
246: return hmHarvestLookup.get(harvestUuid);
247: }
248:
249: //---------------------------------------------------------------------------
250: //---
251: //--- Private methods
252: //---
253: //---------------------------------------------------------------------------
254:
255: private void addInfo(Element node) {
256: String id = node.getAttributeValue("id");
257: hmHarvesters.get(id).addInfo(node);
258: }
259:
260: //---------------------------------------------------------------------------
261: //---
262: //--- Vars
263: //---
264: //---------------------------------------------------------------------------
265:
266: private String xslPath;
267: private SettingManager settingMan;
268: private DataManager dataMan;
269: private ServiceContext context;
270:
271: private HashMap<String, AbstractHarvester> hmHarvesters = new HashMap<String, AbstractHarvester>();
272: private HashMap<String, AbstractHarvester> hmHarvestLookup = new HashMap<String, AbstractHarvester>();
273: }
274:
275: //=============================================================================
|