001: //=== Copyright (C) 2001-2005 Food and Agriculture Organization of the
002: //=== United Nations (FAO-UN), United Nations World Food Programme (WFP)
003: //=== and United Nations Environment Programme (UNEP)
004: //===
005: //=== This program is free software; you can redistribute it and/or modify
006: //=== it under the terms of the GNU General Public License as published by
007: //=== the Free Software Foundation; either version 2 of the License, or (at
008: //=== your option) any later version.
009: //===
010: //=== This program is distributed in the hope that it will be useful, but
011: //=== WITHOUT ANY WARRANTY; without even the implied warranty of
012: //=== MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: //=== General Public License for more details.
014: //===
015: //=== You should have received a copy of the GNU General Public License
016: //=== along with this program; if not, write to the Free Software
017: //=== Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: //===
019: //=== Contact: Jeroen Ticheler - FAO - Viale delle Terme di Caracalla 2,
020: //=== Rome - Italy. email: GeoNetwork@fao.org
021: //==============================================================================
022:
023: package org.fao.geonet.kernel;
024:
025: import java.io.File;
026: import java.io.FilenameFilter;
027: import java.util.Hashtable;
028:
029: import jeeves.utils.Log;
030:
031: import org.fao.geonet.constants.Geonet;
032:
033: import org.openrdf.sesame.Sesame;
034: import org.openrdf.sesame.config.ConfigurationException;
035: import org.openrdf.sesame.config.RepositoryConfig;
036: import org.openrdf.sesame.config.SailConfig;
037: import org.openrdf.sesame.constants.RDFFormat;
038: import org.openrdf.sesame.repository.local.LocalRepository;
039: import org.openrdf.sesame.repository.local.LocalService;
040:
041: //=============================================================================
042:
043: public class ThesaurusManager {
044:
045: private Hashtable<String, Thesaurus> thesauriTable = null;
046:
047: private LocalService service = null;
048:
049: private String thesauriDirectory = null;
050:
051: /**
052: *
053: * @param appPath
054: * @param thesauriRepository
055: * @throws Exception
056: */
057: public ThesaurusManager(String appPath, String thesauriRepository)
058: throws Exception {
059: // Get Sesame interface
060: service = Sesame.getService();
061:
062: File thesauriDir = new File(thesauriRepository);
063:
064: if (!thesauriDir.isAbsolute())
065: thesauriDir = new File(appPath + thesauriDir);
066:
067: thesauriDirectory = thesauriDir.getAbsolutePath();
068:
069: initThesauriTable(thesauriDir);
070: }
071:
072: /**
073: * @param fname
074: * @param type
075: * @param dname
076: * @return
077: */
078: public String buildThesaurusFilePath(String fname, String type,
079: String dname) {
080: return thesauriDirectory + File.separator + type
081: + File.separator + Geonet.CodeList.THESAURUS
082: + File.separator + dname + File.separator + fname;
083: }
084:
085: /**
086: *
087: * @param thesauriDirectory
088: */
089: private void initThesauriTable(File thesauriDirectory) {
090:
091: //repositoryTable = new Hashtable<String, LocalRepository>();
092: thesauriTable = new Hashtable<String, Thesaurus>();
093:
094: if (thesauriDirectory.isDirectory()) {
095: // init of external repositories
096: File externalThesauriDirectory = new File(
097: thesauriDirectory, Geonet.CodeList.EXTERNAL
098: + File.separator
099: + Geonet.CodeList.THESAURUS);
100: if (externalThesauriDirectory.isDirectory()) {
101: File[] rdfDataDirectory = externalThesauriDirectory
102: .listFiles();
103: for (int i = 0; i < rdfDataDirectory.length; i++) {
104: if (rdfDataDirectory[i].isDirectory()) {
105: loadRepositories(rdfDataDirectory[i],
106: Geonet.CodeList.EXTERNAL);
107: }
108: }
109: }
110:
111: // init of local repositories
112: File localThesauriDirectory = new File(thesauriDirectory,
113: Geonet.CodeList.LOCAL + File.separator
114: + Geonet.CodeList.THESAURUS);
115: if (localThesauriDirectory.isDirectory()) {
116: File[] rdfDataDirectory = localThesauriDirectory
117: .listFiles();
118: for (int i = 0; i < rdfDataDirectory.length; i++) {
119: if (rdfDataDirectory[i].isDirectory()) {
120: loadRepositories(rdfDataDirectory[i],
121: Geonet.CodeList.LOCAL);
122: }
123: }
124: }
125: }
126: }
127:
128: /**
129: *
130: * @param thesauriDirectory
131: */
132: private void loadRepositories(File thesauriDirectory, String root) {
133:
134: FilenameFilter filter = new FilenameFilter() {
135: public boolean accept(File dir, String name) {
136: return name.endsWith(".rdf");
137: }
138: };
139:
140: String[] rdfDataFile = thesauriDirectory.list(filter);
141:
142: for (int i = 0; i < rdfDataFile.length; i++) {
143:
144: Thesaurus gst = new Thesaurus(rdfDataFile[i], root,
145: thesauriDirectory.getName(), new File(
146: thesauriDirectory, rdfDataFile[i]));
147: try {
148: addThesaurus(gst);
149: } catch (Exception e) {
150: e.printStackTrace();
151: // continue loading
152: }
153: }
154: }
155:
156: /**
157: *
158: * @param gst
159: */
160: public void addThesaurus(Thesaurus gst) throws Exception {
161:
162: String thesaurusName = gst.getKey();
163:
164: Log.debug(Geonet.THESAURUS_MAN, "Adding thesaurus : "
165: + thesaurusName);
166:
167: if (existsThesaurus(thesaurusName)) {
168: throw new Exception("A thesaurus exists with code "
169: + thesaurusName);
170: }
171:
172: LocalRepository thesaurusRepository;
173: try {
174: RepositoryConfig repConfig = new RepositoryConfig(gst
175: .getKey());
176:
177: SailConfig syncSail = new SailConfig(
178: "org.openrdf.sesame.sailimpl.sync.SyncRdfSchemaRepository");
179: SailConfig memSail = new org.openrdf.sesame.sailimpl.memory.RdfSchemaRepositoryConfig(
180: gst.getFile().getAbsolutePath(), RDFFormat.RDFXML);
181: repConfig.addSail(syncSail);
182: repConfig.addSail(memSail);
183: repConfig.setWorldReadable(true);
184: repConfig.setWorldWriteable(true);
185:
186: thesaurusRepository = service.createRepository(repConfig);
187:
188: gst.setRepository(thesaurusRepository);
189:
190: thesauriTable.put(thesaurusName, gst);
191:
192: } catch (ConfigurationException e) {
193: e.printStackTrace();
194: throw e;
195: }
196: }
197:
198: /**
199: *
200: * @param name
201: */
202: public void remove(String name) {
203: service.removeRepository(name);
204: thesauriTable.remove(name);
205: }
206:
207: // =============================================================================
208: // PUBLIC SERVICES
209:
210: public String getThesauriDirectory() {
211: return thesauriDirectory;
212: }
213:
214: public Hashtable<String, Thesaurus> getThesauriTable() {
215: return thesauriTable;
216: }
217:
218: public LocalRepository getRepositoryByName(String thesaurusName) {
219: return thesauriTable.get(thesaurusName).getRepository();
220: }
221:
222: public Thesaurus getThesaurusByName(String thesaurusName) {
223: return thesauriTable.get(thesaurusName);
224: }
225:
226: /**
227: * @param name
228: * @return
229: */
230: public boolean existsThesaurus(String name) {
231: return (thesauriTable.get(name) != null);
232: }
233:
234: // =============================================================================
235:
236: public static void main(String[] args) throws Exception {
237: /* ThesaurusManager tm = new ThesaurusManager("",
238: "E:\\workspace3.2\\TestSesame\\res\\codelist\\");*/
239: // tm.getAllPrefLabel("adminstrativeAreaFrv0.1");
240: }
241:
242: }
|