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.IOException;
027: import java.util.Collection;
028:
029: import org.openrdf.model.BNode;
030: import org.openrdf.model.Graph;
031: import org.openrdf.model.GraphException;
032: import org.openrdf.model.Literal;
033: import org.openrdf.model.Statement;
034: import org.openrdf.model.URI;
035: import org.openrdf.model.Value;
036: import org.openrdf.model.ValueFactory;
037: import org.openrdf.sesame.config.AccessDeniedException;
038: import org.openrdf.sesame.constants.QueryLanguage;
039: import org.openrdf.sesame.query.MalformedQueryException;
040: import org.openrdf.sesame.query.QueryEvaluationException;
041: import org.openrdf.sesame.query.QueryResultsTable;
042: import org.openrdf.sesame.repository.local.LocalRepository;
043: import org.openrdf.sesame.sail.StatementIterator;
044:
045: public class Thesaurus {
046: private String fname;
047:
048: private String type;
049:
050: private String dname;
051:
052: private File thesaurusFile;
053:
054: private LocalRepository repository;
055:
056: @SuppressWarnings("unused")
057: private String name;
058:
059: @SuppressWarnings("unused")
060: private String description;
061:
062: @SuppressWarnings("unused")
063: private String source;
064:
065: @SuppressWarnings("unused")
066: private String langue;
067:
068: @SuppressWarnings("unused")
069: private String autority;
070:
071: /**
072: * @param fname
073: * file name
074: * @param type
075: * @param dname
076: */
077: public Thesaurus(String fname, String type, String dname,
078: File thesaurusFile) {
079: super ();
080: this .fname = fname;
081: this .type = type;
082: this .dname = dname;
083: this .thesaurusFile = thesaurusFile;
084:
085: }
086:
087: /**
088: *
089: * @return Thesaurus identifier
090: */
091: public String getKey() {
092: return buildThesaurusKey(fname, type, dname);
093: }
094:
095: public String getDname() {
096: return dname;
097: }
098:
099: public String getFname() {
100: return fname;
101: }
102:
103: public File getFile() {
104: return thesaurusFile;
105: }
106:
107: public String getType() {
108: return type;
109: }
110:
111: /**
112: *
113: * @param fname
114: * @param type
115: * @param dname
116: * @return
117: */
118: public static String buildThesaurusKey(String fname, String type,
119: String dname) {
120: return type + "." + dname + "."
121: + fname.substring(0, fname.indexOf(".rdf"));
122: }
123:
124: public LocalRepository getRepository() {
125: return repository;
126: }
127:
128: public void setRepository(LocalRepository repository) {
129: this .repository = repository;
130: }
131:
132: public QueryResultsTable performRequest(String query)
133: throws IOException, MalformedQueryException,
134: QueryEvaluationException, AccessDeniedException {
135: System.out.println("Query : " + query);
136: QueryResultsTable resultsTable = repository.performTableQuery(
137: QueryLanguage.SERQL, query);
138: //printResultsTable(resultsTable);
139: return resultsTable;
140: }
141:
142: /**
143: *
144: * @param resultsTable
145: */
146: private void printResultsTable(QueryResultsTable resultsTable) {
147: int rowCount = resultsTable.getRowCount();
148: int columnCount = resultsTable.getColumnCount();
149:
150: for (int row = 0; row < rowCount; row++) {
151: for (int column = 0; column < columnCount; column++) {
152: Value value = resultsTable.getValue(row, column);
153:
154: if (value != null) {
155: System.out.print(value.toString());
156: } else {
157: System.out.print("null");
158: }
159: System.out.print("\t");
160: }
161: System.out.println();
162: }
163: }
164:
165: public URI addElement(String code, String prefLab, String note,
166: String lang) throws GraphException, IOException,
167: AccessDeniedException {
168:
169: Graph myGraph = new org.openrdf.model.impl.GraphImpl();
170:
171: ValueFactory myFactory = myGraph.getValueFactory();
172: String namespaceSkos = "http://www.w3.org/2004/02/skos/core#";
173: //String namespace = "http://geosource.org/keyword#";
174: String namespace = "#";
175:
176: URI mySubject = myFactory.createURI(namespace, code);
177:
178: URI skosClass = myFactory.createURI(namespaceSkos, "Concept");
179: URI rdfType = myFactory
180: .createURI(org.openrdf.vocabulary.RDF.TYPE);
181: mySubject.addProperty(rdfType, skosClass);
182:
183: URI myPredicate1 = myFactory.createURI(namespaceSkos,
184: "prefLabel");
185: Literal myObject1 = myFactory.createLiteral(prefLab, lang);
186: myGraph.add(mySubject, myPredicate1, myObject1);
187:
188: URI myPredicate2 = myFactory.createURI(namespaceSkos,
189: "scopeNote");
190: Literal myObject2 = myFactory.createLiteral(note, lang);
191: myGraph.add(mySubject, myPredicate2, myObject2);
192:
193: repository.addGraph(myGraph);
194:
195: return mySubject;
196: }
197:
198: public void addElement(String code, String prefLab, String note,
199: String east, String west, String south, String north,
200: String lang) throws IOException, AccessDeniedException,
201: GraphException {
202: Graph myGraph = new org.openrdf.model.impl.GraphImpl();
203:
204: ValueFactory myFactory = myGraph.getValueFactory();
205:
206: // Define namespace
207: String namespaceSkos = "http://www.w3.org/2004/02/skos/core#";
208: String namespaceGml = "http://www.opengis.net/gml#";
209: String namespace = "#";
210:
211: // Create subject
212: URI mySubject = myFactory.createURI(namespace, code);
213:
214: URI skosClass = myFactory.createURI(namespaceSkos, "Concept");
215: URI rdfType = myFactory
216: .createURI(org.openrdf.vocabulary.RDF.TYPE);
217: URI predicatePrefLabel = myFactory.createURI(namespaceSkos,
218: "prefLabel");
219: URI predicateScopeNote = myFactory.createURI(namespaceSkos,
220: "scopeNote");
221:
222: URI predicateBoundedBy = myFactory.createURI(namespaceGml,
223: "BoundedBy");
224: URI predicateEnvelope = myFactory.createURI(namespaceGml,
225: "Envelope");
226: URI predicateSrsName = myFactory.createURI(namespaceGml,
227: "srsName");
228: URI srsNameURI = myFactory
229: .createURI("http://www.opengis.net/gml/srs/epsg.xml#epsg:4326");
230: BNode gmlNode = myFactory.createBNode();
231: URI predicateLowerCorner = myFactory.createURI(namespaceGml,
232: "lowerCorner");
233: URI predicateUpperCorner = myFactory.createURI(namespaceGml,
234: "upperCorner");
235:
236: Literal myObject1 = myFactory.createLiteral(prefLab, lang);
237: Literal myObject2 = myFactory.createLiteral(note, lang);
238:
239: Literal lowerCorner = myFactory.createLiteral(west + " "
240: + south);
241: Literal upperCorner = myFactory.createLiteral(east + " "
242: + north);
243:
244: mySubject.addProperty(rdfType, skosClass);
245: myGraph.add(mySubject, predicatePrefLabel, myObject1);
246: myGraph.add(mySubject, predicateScopeNote, myObject2);
247: myGraph.add(mySubject, predicateBoundedBy, gmlNode);
248:
249: gmlNode.addProperty(rdfType, predicateEnvelope);
250: myGraph.add(gmlNode, predicateLowerCorner, lowerCorner);
251: myGraph.add(gmlNode, predicateUpperCorner, upperCorner);
252: myGraph.add(gmlNode, predicateSrsName, srsNameURI);
253:
254: repository.addGraph(myGraph);
255:
256: }
257:
258: public void removeElement(KeywordBean keyword)
259: throws MalformedQueryException, QueryEvaluationException,
260: IOException, AccessDeniedException {
261: Graph myGraph = repository.getGraph();
262: ValueFactory myFactory = myGraph.getValueFactory();
263:
264: URI subject = myFactory.createURI(keyword.getNameSpaceCode(),
265: keyword.getRelativeCode());
266: StatementIterator iter = myGraph.getStatements(subject, null,
267: null);
268: while (iter.hasNext()) {
269: Statement st = (Statement) iter.next();
270: if (st.getObject() instanceof BNode) {
271: BNode node = (BNode) st.getObject();
272: repository.getGraph().remove(node, null, null);
273: }
274: System.out.println(st.getSubject().toString() + " : "
275: + st.getPredicate().getLocalName() + " : "
276: + st.getObject().toString());
277: }
278:
279: myGraph.remove(subject, null, null);
280: }
281:
282: public URI updateElement(String namespace, String id,
283: String prefLab, String note, String lang)
284: throws IOException, MalformedQueryException,
285: QueryEvaluationException, AccessDeniedException,
286: GraphException {
287: // Get thesaurus graph
288: Graph myGraph = repository.getGraph();
289:
290: // Set namespace skos and predicates
291: ValueFactory myFactory = myGraph.getValueFactory();
292: String namespaceSkos = "http://www.w3.org/2004/02/skos/core#";
293: URI predicatePrefLabel = myFactory.createURI(namespaceSkos,
294: "prefLabel");
295: URI predicateScopeNote = myFactory.createURI(namespaceSkos,
296: "scopeNote");
297:
298: // Get subject (URI)
299: URI subject = myFactory.createURI(namespace, id);
300:
301: // Remove old one
302: StatementIterator iter = myGraph.getStatements(subject,
303: predicatePrefLabel, null);
304: while (iter.hasNext()) {
305: Statement st = (Statement) iter.next();
306: if (st.getObject() instanceof Literal) {
307: Literal litt = (Literal) st.getObject();
308: if (litt.getLanguage() != null
309: && litt.getLanguage().equals(lang)) {
310: // remove
311: myGraph.remove(st);
312: break;
313: }
314: }
315: }
316: // Supp de scopeNote
317: iter = myGraph.getStatements(subject, predicateScopeNote, null);
318: while (iter.hasNext()) {
319: Statement st = (Statement) iter.next();
320: if (st.getObject() instanceof Literal) {
321: Literal litt = (Literal) st.getObject();
322: if (litt.getLanguage() != null
323: && litt.getLanguage().equals(lang)) {
324: // Remove
325: myGraph.remove(st);
326: break;
327: }
328: }
329: }
330:
331: Literal litPrefLab = myFactory.createLiteral(prefLab, lang);
332: Literal litNote = myFactory.createLiteral(note, lang);
333:
334: myGraph.add(subject, predicatePrefLabel, litPrefLab);
335: myGraph.add(subject, predicateScopeNote, litNote);
336:
337: return subject;
338: }
339:
340: public void updateElement(String namespace, String id,
341: String prefLab, String note, String east, String west,
342: String south, String north, String lang)
343: throws AccessDeniedException, IOException,
344: MalformedQueryException, QueryEvaluationException,
345: GraphException {
346:
347: // update label and definition
348: URI subject = updateElement(namespace, id, prefLab, note, lang);
349:
350: // update bbox
351:
352: Graph myGraph = repository.getGraph();
353:
354: ValueFactory myFactory = myGraph.getValueFactory();
355: String namespaceGml = "http://www.opengis.net/gml#";
356: URI predicateBoundedBy = myFactory.createURI(namespaceGml,
357: "BoundedBy");
358: URI predicateLowerCorner = myFactory.createURI(namespaceGml,
359: "lowerCorner");
360: URI predicateUpperCorner = myFactory.createURI(namespaceGml,
361: "upperCorner");
362:
363: BNode subjectGml = null;
364: StatementIterator iter = myGraph.getStatements(subject,
365: predicateBoundedBy, null);
366: while (iter.hasNext()) {
367: Statement st = (Statement) iter.next();
368: if (st.getObject() instanceof BNode) {
369: subjectGml = (BNode) st.getObject();
370: }
371: }
372: if (subjectGml != null) {
373: // lowerCorner
374: iter = myGraph.getStatements(subjectGml,
375: predicateLowerCorner, null);
376: while (iter.hasNext()) {
377: Statement st = (Statement) iter.next();
378: myGraph.remove(st);
379: break;
380: }
381: // upperCorner
382: iter = myGraph.getStatements(subjectGml,
383: predicateUpperCorner, null);
384: while (iter.hasNext()) {
385: Statement st = (Statement) iter.next();
386: myGraph.remove(st);
387: break;
388: }
389: // Preparation des nouveaux statements
390: Literal lowerCorner = myFactory.createLiteral(west + " "
391: + south);
392: Literal upperCorner = myFactory.createLiteral(east + " "
393: + north);
394:
395: // ajout des nouveaux statements
396: myGraph.add(subjectGml, predicateLowerCorner, lowerCorner);
397: myGraph.add(subjectGml, predicateUpperCorner, upperCorner);
398: }
399: }
400:
401: public boolean isFreeCode(String namespace, String code)
402: throws AccessDeniedException {
403: boolean res = true;
404: Graph myGraph = repository.getGraph();
405: ValueFactory myFactory = myGraph.getValueFactory();
406: URI obj = myFactory.createURI(namespace, code);
407: Collection statementsCollection = myGraph
408: .getStatementCollection(obj, null, null);
409: if (statementsCollection != null
410: && statementsCollection.size() > 0) {
411: res = false;
412: }
413: statementsCollection = myGraph.getStatementCollection(null,
414: null, obj);
415: if (statementsCollection != null
416: && statementsCollection.size() > 0) {
417: res = false;
418: }
419: return res;
420: }
421:
422: public void updateCode(String namespace, String oldcode,
423: String newcode) throws AccessDeniedException, IOException {
424: Graph myGraph = repository.getGraph();
425: //Graph myTmpGraph = new org.openrdf.model.impl.GraphImpl();
426:
427: ValueFactory myFactory = myGraph.getValueFactory();
428: //ValueFactory myTmpFactory = myTmpGraph.getValueFactory();
429:
430: URI oldobj = myFactory.createURI(namespace, oldcode);
431: URI newobj = myFactory.createURI(namespace, newcode);
432: StatementIterator iterStSubject = myGraph.getStatements(oldobj,
433: null, null);
434: while (iterStSubject.hasNext()) {
435: Statement st = (Statement) iterStSubject.next();
436: myGraph.add(newobj, st.getPredicate(), st.getObject());
437: }
438:
439: StatementIterator iterStObject = myGraph.getStatements(null,
440: null, oldobj);
441: while (iterStObject.hasNext()) {
442: Statement st = (Statement) iterStObject.next();
443: myGraph.add(st.getSubject(), st.getPredicate(), newobj);
444: }
445: myGraph.remove(oldobj, null, null);
446: myGraph.remove(null, null, oldobj);
447: //repository.addGraph(myTmpGraph);
448: }
449: }
|