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.lib;
025:
026: import java.io.File;
027: import java.io.FileInputStream;
028: import java.io.FileOutputStream;
029: import java.io.IOException;
030: import java.sql.SQLException;
031: import java.util.List;
032: import jeeves.interfaces.Logger;
033: import jeeves.resources.dbms.Dbms;
034: import jeeves.server.context.ServiceContext;
035: import jeeves.utils.BinaryFile;
036: import jeeves.utils.XmlRequest;
037: import org.fao.geonet.constants.Geonet;
038: import org.jdom.Element;
039:
040: //=============================================================================
041:
042: public class SourcesLib {
043: //---------------------------------------------------------------------------
044: //---
045: //--- API methods
046: //---
047: //---------------------------------------------------------------------------
048:
049: /** isLocal: means that the source is local to the node. That is:
050: * - the source is the site-id of the node
051: * - the source has been generated by CSW, WEBDAV or Z39.50 harvesting
052: */
053:
054: public void update(Dbms dbms, String uuid, String name,
055: boolean isLocal) throws SQLException {
056: String query = "SELECT isLocal FROM Sources WHERE uuid=?";
057:
058: List list = dbms.select(query, uuid).getChildren();
059:
060: if (list.size() == 0) {
061: query = "INSERT INTO Sources(uuid, name, isLocal) VALUES(?,?,?)";
062: dbms.execute(query, uuid, name, isLocal ? "y" : "n");
063: } else {
064: Element rec = (Element) list.get(0);
065:
066: if (isLocal || "n".equals(rec.getChildText("islocal"))) {
067: query = "UPDATE Sources SET name=? WHERE uuid=?";
068: dbms.execute(query, name, uuid);
069: }
070: }
071: }
072:
073: //---------------------------------------------------------------------------
074:
075: public void delete(Dbms dbms, String uuid) throws SQLException {
076: dbms.execute("DELETE FROM Sources WHERE uuid=?", uuid);
077: }
078:
079: //---------------------------------------------------------------------------
080:
081: public void copyLogo(ServiceContext context, String icon,
082: String uuid) {
083: File src = new File(context.getAppPath() + icon);
084: File des = new File(context.getAppPath() + "images/logos", uuid
085: + ".gif");
086:
087: try {
088: FileInputStream is = new FileInputStream(src);
089: FileOutputStream os = new FileOutputStream(des);
090:
091: BinaryFile.copy(is, os, true, true);
092: } catch (IOException e) {
093: //--- we ignore exceptions here, just log them
094:
095: context.warning("Cannot copy icon -> " + e.getMessage());
096: context.warning(" (C) Source : " + src);
097: context.warning(" (C) Destin : " + des);
098: }
099: }
100:
101: //---------------------------------------------------------------------------
102:
103: public void copyUnknownLogo(ServiceContext context, String uuid) {
104: copyLogo(context, "/images/unknown-logo.gif", uuid);
105: }
106:
107: //---------------------------------------------------------------------------
108:
109: public void retrieveLogo(ServiceContext context, String host,
110: int port, String servlet, String uuid) {
111: String logo = uuid + ".gif";
112:
113: XmlRequest req = new XmlRequest(host, port);
114: Lib.net.setupProxy(context, req);
115: req.setAddress("/" + servlet + Geonet.Path.LOGOS + logo);
116:
117: File logoFile = new File(context.getAppPath()
118: + Geonet.Path.LOGOS + logo);
119:
120: try {
121: req.executeLarge(logoFile);
122: } catch (IOException e) {
123: context.warning("Cannot retrieve logo file from : " + host
124: + ":" + port);
125: context.warning(" (C) Logo : " + logo);
126: context.warning(" (C) Excep : " + e.getMessage());
127:
128: logoFile.delete();
129:
130: copyUnknownLogo(context, uuid);
131: }
132: }
133: }
134:
135: //=============================================================================
|