001: /*
002: * Copyright Aduna (http://www.aduna-software.com/) (c) 2007.
003: *
004: * Licensed under the Aduna BSD-style license.
005: */
006: package org.openrdf.repository.dataset;
007:
008: import java.net.MalformedURLException;
009: import java.net.URL;
010:
011: import org.openrdf.model.URI;
012: import org.openrdf.query.BooleanQuery;
013: import org.openrdf.query.Dataset;
014: import org.openrdf.query.GraphQuery;
015: import org.openrdf.query.MalformedQueryException;
016: import org.openrdf.query.Query;
017: import org.openrdf.query.QueryEvaluationException;
018: import org.openrdf.query.QueryLanguage;
019: import org.openrdf.query.TupleQuery;
020: import org.openrdf.repository.RepositoryException;
021: import org.openrdf.repository.base.RepositoryConnectionWrapper;
022: import org.openrdf.repository.sail.SailBooleanQuery;
023: import org.openrdf.repository.sail.SailGraphQuery;
024: import org.openrdf.repository.sail.SailQuery;
025: import org.openrdf.repository.sail.SailRepositoryConnection;
026: import org.openrdf.repository.sail.SailTupleQuery;
027:
028: public class DatasetRepositoryConnection extends
029: RepositoryConnectionWrapper {
030:
031: private DatasetRepository repository;
032:
033: private SailRepositoryConnection delegate;
034:
035: public DatasetRepositoryConnection(DatasetRepository repository,
036: SailRepositoryConnection delegate) {
037: super (repository, delegate);
038: this .repository = repository;
039: this .delegate = delegate;
040: }
041:
042: @Override
043: public BooleanQuery prepareBooleanQuery(QueryLanguage ql,
044: String query, String baseURI)
045: throws MalformedQueryException, RepositoryException {
046: return wrap(delegate.prepareBooleanQuery(ql, query, baseURI));
047: }
048:
049: @Override
050: public GraphQuery prepareGraphQuery(QueryLanguage ql, String query,
051: String baseURI) throws MalformedQueryException,
052: RepositoryException {
053: return wrap(delegate.prepareGraphQuery(ql, query, baseURI));
054: }
055:
056: @Override
057: public Query prepareQuery(QueryLanguage ql, String query,
058: String baseURI) throws MalformedQueryException,
059: RepositoryException {
060: return wrap(delegate.prepareQuery(ql, query, baseURI));
061: }
062:
063: @Override
064: public TupleQuery prepareTupleQuery(QueryLanguage ql, String query,
065: String baseURI) throws MalformedQueryException,
066: RepositoryException {
067: return wrap(delegate.prepareTupleQuery(ql, query, baseURI));
068: }
069:
070: void loadDataset(Dataset datasets) throws QueryEvaluationException {
071: try {
072: if (datasets == null) {
073: return;
074: }
075: for (URI dataset : datasets.getDefaultGraphs()) {
076: repository.loadDataset(new URL(dataset.toString()),
077: dataset);
078: }
079: for (URI dataset : datasets.getNamedGraphs()) {
080: repository.loadDataset(new URL(dataset.toString()),
081: dataset);
082: }
083: } catch (MalformedURLException e) {
084: throw new AssertionError(e);
085: } catch (RepositoryException e) {
086: throw new QueryEvaluationException(e);
087: }
088: }
089:
090: private Query wrap(SailQuery q) {
091: if (q instanceof SailBooleanQuery) {
092: return wrap((SailBooleanQuery) q);
093: }
094: if (q instanceof SailGraphQuery) {
095: return wrap((SailGraphQuery) q);
096: }
097: if (q instanceof SailTupleQuery) {
098: return wrap((SailTupleQuery) q);
099: }
100: throw new IllegalArgumentException(q.getClass().getSimpleName()
101: + " not supported on DatasetRepository");
102: }
103:
104: private BooleanQuery wrap(final SailBooleanQuery q) {
105: return new DatasetBooleanQuery(this , q);
106: }
107:
108: private GraphQuery wrap(final SailGraphQuery q) {
109: return new DatasetGraphQuery(this , q);
110: }
111:
112: private TupleQuery wrap(final SailTupleQuery q) {
113: return new DatasetTupleQuery(this, q);
114: }
115: }
|