⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 thesaurusmanagersesame.java

📁 联合国农粮署牵头开发的geonetwork源代码最新版
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
//===	Copyright (C) 2001-2005 Food and Agriculture Organization of the
//===	United Nations (FAO-UN), United Nations World Food Programme (WFP)
//===	and United Nations Environment Programme (UNEP)
//===
//===	This program is free software; you can redistribute it and/or modify
//===	it under the terms of the GNU General Public License as published by
//===	the Free Software Foundation; either version 2 of the License, or (at
//===	your option) any later version.
//===
//===	This program is distributed in the hope that it will be useful, but
//===	WITHOUT ANY WARRANTY; without even the implied warranty of
//===	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
//===	General Public License for more details.
//===
//===	You should have received a copy of the GNU General Public License
//===	along with this program; if not, write to the Free Software
//===	Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//===
//===	Contact: Jeroen Ticheler - FAO - Viale delle Terme di Caracalla 2,
//===	Rome - Italy. email: GeoNetwork@fao.org
//==============================================================================

package org.fao.geonet.kernel;

import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.Date;
import java.util.Hashtable;

import org.fao.geonet.constants.Geonet;
import org.jdom.Element;
import org.openrdf.model.BNode;
import org.openrdf.model.Graph;
import org.openrdf.model.GraphException;
import org.openrdf.model.Literal;
import org.openrdf.model.Statement;
import org.openrdf.model.URI;
import org.openrdf.model.Value;
import org.openrdf.model.ValueFactory;
import org.openrdf.sesame.Sesame;
import org.openrdf.sesame.config.AccessDeniedException;
import org.openrdf.sesame.config.ConfigurationException;
import org.openrdf.sesame.config.RepositoryConfig;
import org.openrdf.sesame.config.SailConfig;
import org.openrdf.sesame.constants.QueryLanguage;
import org.openrdf.sesame.constants.RDFFormat;
import org.openrdf.sesame.query.MalformedQueryException;
import org.openrdf.sesame.query.QueryEvaluationException;
import org.openrdf.sesame.query.QueryResultsTable;
import org.openrdf.sesame.repository.local.LocalRepository;
import org.openrdf.sesame.repository.local.LocalService;
import org.openrdf.sesame.sail.StatementIterator;

//=============================================================================

public class ThesaurusManagerSesame {

	private Hashtable<String, LocalRepository> repositoryTable = null;

	private Hashtable<String, Thesaurus> thesauriTable = null;

	private LocalService service = null;

	private String thesauriDirectory = null;

	/**
	 * 
	 * @param appPath
	 * @param thesauriRepository
	 * @throws Exception
	 */
	public ThesaurusManagerSesame(String appPath, String thesauriRepository)
			throws Exception {
		// Reccuperation de l'interface Sesame
		service = Sesame.getService();

		File thesauriDir = new File(thesauriRepository);

		if (!thesauriDir.isAbsolute())
			thesauriDir = new File(appPath + thesauriDir);

		thesauriDirectory = thesauriDir.getAbsolutePath();

		initThesauriTable(thesauriDir);
	}

	/**
	 * @param fname
	 * @param type
	 * @param dname
	 * @return
	 */
	public String buildThesaurusFilePath(String fname, String type, String dname) {
		return thesauriDirectory + File.separator + type + File.separator
				+ Geonet.CodeList.THESAURUS + File.separator + dname
				+ File.separator + fname;
	}

	/**
	 * 
	 * @param thesauriDirectory
	 */
	private void initThesauriTable(File thesauriDirectory) {

		repositoryTable = new Hashtable<String, LocalRepository>();
		thesauriTable = new Hashtable<String, Thesaurus>();

		if (thesauriDirectory.isDirectory()) {
			// init of external repositories
			File externalThesauriDirectory = new File(thesauriDirectory,
					Geonet.CodeList.EXTERNAL + File.separator
							+ Geonet.CodeList.THESAURUS);
			if (externalThesauriDirectory.isDirectory()) {
				File[] rdfDataDirectory = externalThesauriDirectory.listFiles();
				for (int i = 0; i < rdfDataDirectory.length; i++) {
					if (rdfDataDirectory[i].isDirectory()) {
						loadRepositories(rdfDataDirectory[i],
								Geonet.CodeList.EXTERNAL);
					}
				}
			}

			// init of local repositoris
			File localThesauriDirectory = new File(thesauriDirectory,
					Geonet.CodeList.LOCAL + File.separator
							+ Geonet.CodeList.THESAURUS);
			if (localThesauriDirectory.isDirectory()) {
				File[] rdfDataDirectory = localThesauriDirectory.listFiles();
				for (int i = 0; i < rdfDataDirectory.length; i++) {
					if (rdfDataDirectory[i].isDirectory()) {
						loadRepositories(rdfDataDirectory[i],
								Geonet.CodeList.LOCAL);
					}
				}
			}
		}
	}

	/**
	 * 
	 * @param thesauriDirectory
	 */
	private void loadRepositories(File thesauriDirectory, String root) {
		FilenameFilter filter = new FilenameFilter() {
			public boolean accept(File dir, String name) {
				return name.endsWith(".rdf");
			}
		};

		String[] rdfDataFile = thesauriDirectory.list(filter);

		for (int i = 0; i < rdfDataFile.length; i++) {

			Thesaurus gst = new Thesaurus(rdfDataFile[i], root,
					thesauriDirectory.getName(), new File(thesauriDirectory,
							rdfDataFile[i]));
			try {
				addThesaurus(gst);
			} catch (Exception e) {
				e.printStackTrace();
				// continue loading
			}
		}
	}

	/**
	 * 
	 * @param gst
	 */
	public void addThesaurus(Thesaurus gst) throws Exception {

		String thesaurusName = gst.getKey();
		if (existsThesaurus(thesaurusName)) {
			throw new Exception("A thesaurus exists with code " + thesaurusName);
		}

		addConfiguredThesaurus(gst);
	}

	/**
	 * TODO TEST
	 * 
	 * @param gst
	 */
	private void addConfiguredThesaurus(Thesaurus gst) throws Exception {

		String thesaurusName = gst.getKey();
		if (existsThesaurus(thesaurusName)) {
			throw new Exception("A thesaurus exists with code " + thesaurusName);
		}

		// boolean inferencing = true;
		// boolean inferencing = false;

		LocalRepository thesaurusRepository;
		try {
			RepositoryConfig repConfig = new RepositoryConfig(gst.getKey());

			SailConfig syncSail = new SailConfig(
					"org.openrdf.sesame.sailimpl.sync.SyncRdfSchemaRepository");
			SailConfig memSail = new org.openrdf.sesame.sailimpl.memory.RdfSchemaRepositoryConfig(
					gst.getFile().getAbsolutePath(), RDFFormat.RDFXML);
			repConfig.addSail(syncSail);
			repConfig.addSail(memSail);
			repConfig.setWorldReadable(true);
			repConfig.setWorldWriteable(true);

			thesaurusRepository = service.createRepository(repConfig);

			// create thesaurus repository
			// thesaurusRepository = service.createRepository(thesaurusName,
			// inferencing);

			// populate thesaurus repository
			// String baseURI = "http://www.w3.org/2004/02/skos/core#";
			// boolean verifyData = false;
			// AdminListener myListener = new StdOutAdminListener();
			// thesaurusRepository.addData(gst.getFile(), baseURI,
			// RDFFormat.RDFXML, verifyData,
			// myListener);

			// put thesaurus in hashtable
			repositoryTable.put(thesaurusName, thesaurusRepository);
			thesauriTable.put(thesaurusName, gst);

		} catch (ConfigurationException e) {
			e.printStackTrace();
			throw e;
		}
		// catch (IOException e) {
		// e.printStackTrace();
		// throw e;
		// } catch (AccessDeniedException e) {
		// e.printStackTrace();
		// throw e;
		// }
	}

	/**
	 * TODO IMPACT SUR LA SUPPRESSION DU REPOSITORY SESAME ?
	 * 
	 * @param name
	 */
	public void remove(String name) {
		thesauriTable.remove(name);
		repositoryTable.remove(name);
	}

	// =============================================================================
	// PUBLIC SERVICES

	public String getThesauriDirectory() {
		return thesauriDirectory;
	}

	public Hashtable<String, LocalRepository> getRepositoryTable() {
		return repositoryTable;
	}

	public Hashtable<String, Thesaurus> getThesauriTable() {
		return thesauriTable;
	}

	public LocalRepository getRepositoryByName(String thesaurusName) {
		return repositoryTable.get(thesaurusName);
	}

	public Thesaurus getThesaurusByName(String thesaurusName) {
		return thesauriTable.get(thesaurusName);
	}

	/**
	 * 
	 * @param query
	 * @param thesaurusRepository
	 * @return
	 * @throws IOException
	 * @throws MalformedQueryException
	 * @throws QueryEvaluationException
	 * @throws AccessDeniedException
	 */
	public QueryResultsTable performRequest(String query,
			LocalRepository thesaurusRepository) throws IOException,
			MalformedQueryException, QueryEvaluationException,
			AccessDeniedException {

		System.out.println("Requete : " + query);

		QueryResultsTable resultsTable = thesaurusRepository.performTableQuery(
				QueryLanguage.SERQL, query);

		printResultsTable(resultsTable);

		return resultsTable;
	}

	/**
	 * 
	 * @param thesaurusRepository
	 * @return
	 * @throws IOException
	 * @throws MalformedQueryException
	 * @throws QueryEvaluationException
	 * @throws AccessDeniedException
	 */
	public Element getAllPrefLabel(String thesaurusRepository)
			throws IOException, MalformedQueryException,
			QueryEvaluationException, AccessDeniedException {

		String query = "SELECT prefLab, note "
				+ " from {} rdf:type {skos:Concept}; "
				+ " skos:prefLabel {prefLab} [skos:scopeNote {note}] "
				+ " where lang(prefLab) like \"fr\""
				+ " USING NAMESPACE skos=<http://www.w3.org/2004/02/skos/core#>";

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -