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.FilenameFilter;
028: import java.util.Iterator;
029:
030: import jeeves.constants.Jeeves;
031: import jeeves.interfaces.Service;
032: import jeeves.server.ServiceConfig;
033: import jeeves.server.context.ServiceContext;
034: import jeeves.utils.Util;
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.ThesaurusManager;
040: import org.jdom.Element;
041:
042: //=============================================================================
043:
044: /** Given a metadata id returns all associated categories. Called by the
045: * metadata.category service
046: */
047:
048: public class List implements Service {
049: //--------------------------------------------------------------------------
050: //---
051: //--- Init
052: //---
053: //--------------------------------------------------------------------------
054:
055: private static String LOCAL_DIR;
056: private static String EXTERNAL_DIR;
057: private String init_type;
058:
059: public void init(String appPath, ServiceConfig params)
060: throws Exception {
061:
062: LOCAL_DIR = File.separator + Geonet.CodeList.LOCAL
063: + File.separator + Geonet.CodeList.THESAURUS
064: + File.separator;
065: EXTERNAL_DIR = File.separator + Geonet.CodeList.EXTERNAL
066: + File.separator + Geonet.CodeList.THESAURUS
067: + File.separator;
068:
069: init_type = params.getValue(Params.TYPE, "_none_");
070: }
071:
072: /** Filtre sur les répertoires */
073: private FilenameFilter directoryFilter = new FilenameFilter() {
074: public boolean accept(File dir, String name) {
075: if (dir.isDirectory() && !name.startsWith("."))
076: return true;
077: else
078: return false;
079: }
080: };
081:
082: /** Filtre sur les répertoires */
083: private FilenameFilter thesauriFilter = new FilenameFilter() {
084: public boolean accept(File dir, String name) {
085: if (dir.isDirectory() && !name.startsWith("."))
086: return true;
087: else if (!dir.isDirectory() && name.endsWith(".xml")
088: || name.endsWith(".rdf"))
089: return true;
090: else
091: return false;
092: }
093: };
094:
095: //--------------------------------------------------------------------------
096: //---
097: //--- Service
098: //---
099: //--------------------------------------------------------------------------
100:
101: public Element exec(Element params, ServiceContext context)
102: throws Exception {
103: GeonetContext gc = (GeonetContext) context
104: .getHandlerContext(Geonet.CONTEXT_NAME);
105: ThesaurusManager thesaurusMan = gc.getThesaurusManager();
106: String THESAURUS_DIR = thesaurusMan.getThesauriDirectory();
107:
108: Element thesauriList = new Element("thesaurusList");
109:
110: String type = Util.getParam(params, Params.TYPE, init_type);
111:
112: if (type.equals("all-directories")) {
113: listThesauri(thesauriList, THESAURUS_DIR + EXTERNAL_DIR, 1,
114: directoryFilter, Geonet.CodeList.EXTERNAL);
115: listThesauri(thesauriList, THESAURUS_DIR + LOCAL_DIR, 1,
116: directoryFilter, Geonet.CodeList.LOCAL);
117: } else if (type.equals("upload-directories")) {
118: listThesauri(thesauriList, THESAURUS_DIR + EXTERNAL_DIR, 1,
119: directoryFilter, Geonet.CodeList.EXTERNAL);
120: } else if (type.equals("all-thesauri")) {
121: listThesauri(thesauriList, THESAURUS_DIR + EXTERNAL_DIR, 2,
122: thesauriFilter, Geonet.CodeList.EXTERNAL);
123: listThesauri(thesauriList, THESAURUS_DIR + LOCAL_DIR, 2,
124: thesauriFilter, Geonet.CodeList.LOCAL);
125: } else if (type.equals("update-thesauri")) {
126: listThesauri(thesauriList, THESAURUS_DIR + LOCAL_DIR, 3,
127: thesauriFilter, Geonet.CodeList.LOCAL);
128: } else {
129: listThesauri(thesauriList, THESAURUS_DIR + EXTERNAL_DIR, 3,
130: thesauriFilter, Geonet.CodeList.EXTERNAL);
131: listThesauri(thesauriList, THESAURUS_DIR + LOCAL_DIR, 3,
132: thesauriFilter, Geonet.CodeList.LOCAL);
133: }
134:
135: //-----------------------------------------------------------------------
136:
137: Element elRes = new Element(Jeeves.Elem.RESPONSE)
138: .addContent(thesauriList);
139:
140: //-----------------------------------------------------------------------
141:
142: String selected = Util
143: .getParam(params, Params.SELECTED, "none");
144: if (!selected.equals("none")) {
145: Element elSelected = new Element("selectedThesaurus");
146: elSelected.addContent(selected);
147: elRes.addContent(elSelected);
148: }
149: String mode = Util.getParam(params, "mode", "none");
150: if (!mode.equals("none")) {
151: Element elMode = new Element("mode");
152: elMode.addContent(mode);
153: elRes.addContent(elMode);
154: }
155: //-----------------------------------------------------------------------
156:
157: return elRes;
158: }
159:
160: /**
161: * Parcours l'arborescence des thesaurus et retourne les éléments en fonction du filtre et du mode
162: * @param params
163: * @param context
164: * @return
165: * @throws Exception
166: */
167: private void listThesauri(Element list, String dir, int mode,
168: FilenameFilter filter, String rootName) throws Exception {
169: File thesauriDirectory = new File(dir);
170: if (thesauriDirectory.isDirectory()) {
171:
172: File[] rdfDataDirectory = thesauriDirectory
173: .listFiles(filter);
174: for (int i = 0; i < rdfDataDirectory.length; i++) {
175:
176: if (rdfDataDirectory[i].isDirectory()) { // Répertoire
177:
178: if (mode == 1) { // ARBORESCENCE SIMPLE
179: Element dirE = new Element("directory")
180: .setAttribute("label",
181: rdfDataDirectory[i].getName());
182: dirE.setAttribute("type", rootName);
183: list.addContent(dirE);
184: listThesauri(dirE, rdfDataDirectory[i]
185: .getAbsolutePath(), mode, filter,
186: rootName);
187: }
188:
189: else if (mode == 2) { // ARBORESCENCE FUSIONNEE LOCAL + EXTERNAL
190: Element dirE = new Element("directory")
191: .setAttribute("label",
192: rdfDataDirectory[i].getName());
193: dirE.setAttribute("type", rootName);
194:
195: // Recherche d'un élément directory de même nom
196: java.util.List children = list
197: .getChildren("directory");
198: Element element = null;
199: for (Iterator iter = children.iterator(); iter
200: .hasNext();) {
201: element = (Element) iter.next();
202: if (element.getAttribute("label")
203: .getValue().equals(
204: rdfDataDirectory[i]
205: .getName()))
206: break;
207: element = null;
208: }
209:
210: if (element != null) {
211: listThesauri(element, rdfDataDirectory[i]
212: .getAbsolutePath(), mode, filter,
213: rootName);
214: } else {
215: list.addContent(dirE);
216: listThesauri(dirE, rdfDataDirectory[i]
217: .getAbsolutePath(), mode, filter,
218: rootName);
219: }
220: }
221:
222: else
223: // TOUT A PLAT : PAS D'ARBORESCENCE
224: listThesauri(list, rdfDataDirectory[i]
225: .getAbsolutePath(), mode, filter,
226: rootName);
227:
228: } else if (mode != 1) { // Fichier
229: String thesaurusName = rootName
230: + '.'
231: + thesauriDirectory.getName()
232: + '.'
233: + rdfDataDirectory[i].getName().substring(
234: 0,
235: rdfDataDirectory[i].getName()
236: .indexOf(".rdf"));
237:
238: Element thesaurusE = new Element("thesaurus")
239: .setAttribute("value", thesaurusName);
240: thesaurusE.setAttribute("type", rootName);
241: Element dirE = new Element("dname")
242: .setText(thesauriDirectory.getName());
243: Element fnameE = new Element("fname")
244: .setText(rdfDataDirectory[i].getName());
245: // Element dirNE = new Element("dir").setText(thesauriDirectory.getAbsolutePath());
246: thesaurusE.addContent(fnameE);
247: thesaurusE.addContent(dirE);
248: // thesaurusE.addContent(dirNE);
249: list.addContent(thesaurusE);
250: }
251: }
252: }
253: }
254:
255: // =====================================================================================
256:
257: }
|