001: //=============================================================================
002: //=== Copyright (C) 2001-2005 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: //===
020: //=== Contact: Jeroen Ticheler - FAO - Viale delle Terme di Caracalla 2,
021: //=== Rome - Italy. email: GeoNetwork@fao.org
022: //==============================================================================
023:
024: package org.fao.geonet.services.thesaurus;
025:
026: import java.io.File;
027: import java.io.FileOutputStream;
028:
029: import jeeves.interfaces.Service;
030: import jeeves.server.ServiceConfig;
031: import jeeves.server.context.ServiceContext;
032: import jeeves.utils.Util;
033: import jeeves.utils.Xml;
034: import jeeves.utils.Log;
035:
036: import org.fao.geonet.GeonetContext;
037: import org.fao.geonet.constants.Geonet;
038: import org.fao.geonet.constants.Params;
039: import org.fao.geonet.kernel.Thesaurus;
040: import org.fao.geonet.kernel.ThesaurusManager;
041: import org.jdom.Document;
042: import org.jdom.Element;
043:
044: //=============================================================================
045:
046: /**
047: *
048: * Upload one thesaurus file
049: */
050:
051: public class Upload implements Service {
052: static String FS = System.getProperty("file.separator", "/");
053: static int inc = 0;
054:
055: // XSL - IMPORT NOMENCLATURE - FR
056: public static final String STYLESHEET_SKOS_FR = "lang-fr.xsl";
057: private String stylePath;
058:
059: //--------------------------------------------------------------------------
060: //---
061: //--- Init
062: //---
063: //--------------------------------------------------------------------------
064:
065: public void init(String appPath, ServiceConfig params)
066: throws Exception {
067: this .stylePath = appPath + FS + Geonet.Path.STYLESHEETS + FS;
068: }
069:
070: //--------------------------------------------------------------------------
071: //---
072: //--- API
073: //---
074: //--------------------------------------------------------------------------
075:
076: public Element exec(Element params, ServiceContext context)
077: throws Exception {
078: long start = System.currentTimeMillis();
079: Element uploadResult;
080:
081: uploadResult = upload(params, context);
082:
083: long end = System.currentTimeMillis();
084: long duration = (end - start) / 1000;
085:
086: Log.debug("Thesaurus", "Uploaded in " + duration + " s.");
087:
088: Element response = new Element("response");
089: if (uploadResult != null)
090: response.addContent(uploadResult);
091: return response;
092:
093: // chaîner sur le service d'update/view de la nomenclature ?
094: // return update.exec(params, context);
095: }
096:
097: /**
098: *
099: * @param params
100: * @param context
101: * @return
102: * @throws Exception
103: */
104: private Element upload(Element params, ServiceContext context)
105: throws Exception {
106: String uploadDir = context.getUploadDir();
107: Element uploadResult = null;
108:
109: // Le fichier particulier format rdf
110: String fname = null;
111: Element param = params.getChild(Params.FNAME);
112: if (!(param == null || param.getTextTrim().length() == 0)) {
113: fname = param.getTextTrim();
114: }
115:
116: // Type de thesaurus (local, external)
117: String type = Util.getParam(params, Params.TYPE, "external");
118:
119: // Répertoire de thesaurus (Discipline, Place, Stratum, Temporal, Theme)
120: String dir = Util.getParam(params, Params.DIR);
121:
122: // Le schéma cible par rapport auquel effectuer la validation éventuelle
123: String style = Util.getParam(params, Params.STYLESHEET,
124: "_none_");
125:
126: // ?
127: // String siteId = Util.getParam(params, Params.SITE_ID, gc.getSiteId());
128:
129: // Valider par rapport au schéma cible ?
130: boolean validate = Util
131: .getParam(params, Params.VALIDATE, "off").equals("on");
132:
133: if ((fname != null) && !fname.equals("")) {
134:
135: Element eTSResult;
136:
137: File oldFile = new File(uploadDir, fname);
138: String extension = fname.substring(fname.lastIndexOf('.'))
139: .toLowerCase();
140:
141: // -> UN FICHIER XML
142: if (extension.equals(".rdf")) {
143:
144: Log.debug("Thesaurus", "Uploading thesaurus: " + fname);
145: eTSResult = UploadThesaurus(oldFile, style, context,
146: validate, fname, type, dir);
147: } else {
148: Log.debug("Thesaurus",
149: "Incorrect extension for thesaurus file name : "
150: + fname);
151: throw new Exception(
152: "Incorrect extension for thesaurus file name : "
153: + fname);
154: }
155:
156: uploadResult = new Element("record")
157: .setText("Thesaurus uploaded");
158: uploadResult.addContent(eTSResult);
159: }
160:
161: return uploadResult;
162: }
163:
164: /**
165: * Upload one Thesaurus
166: * @param oldFile
167: * @param style
168: * @param context
169: * @param validate
170: * @param siteId
171: * @param fname
172: * @param type
173: * @param dir
174: * @return Element thesaurus uploaded
175: * @throws Exception
176: */
177: private Element UploadThesaurus(File oldFile, String style,
178: ServiceContext context, boolean validate, String fname,
179: String type, String dir) throws Exception {
180:
181: Element TS_xml = null;
182: Element xml = Xml.loadFile(oldFile);
183: xml.detach();
184:
185: if (!style.equals("_none_")) {
186: TS_xml = Xml.transform(xml, stylePath + "/" + style);
187: TS_xml.detach();
188: } else
189: TS_xml = xml;
190:
191: // Analyse du fichier
192: if (TS_xml.getNamespacePrefix().equals("rdf")
193: && TS_xml.getName().equals("RDF")) {
194:
195: GeonetContext gc = (GeonetContext) context
196: .getHandlerContext(Geonet.CONTEXT_NAME);
197: ThesaurusManager thesaurusMan = gc.getThesaurusManager();
198:
199: // Copie du fichier dans l'espace adéquat
200: // Position dans l'arborescence déterminée par la categorie
201: String path = thesaurusMan.buildThesaurusFilePath(fname,
202: type, dir);
203: File newFile = new File(path);
204: Xml.writeResponse(new Document(TS_xml),
205: new FileOutputStream(newFile));
206:
207: Thesaurus gst = new Thesaurus(fname, type, dir, newFile);
208: thesaurusMan.addThesaurus(gst);
209: } else {
210: oldFile.delete();
211:
212: // CE N'EST PAS UN FICHIER SKOS !
213: throw new Exception("Format de fichier inconnu");
214:
215: }
216:
217: return new Element("Thesaurus").setText(oldFile.getName());
218: }
219:
220: }
|