Skip to content

Method: getContexts()

1: /*
2: * JOPA
3: * Copyright (C) 2024 Czech Technical University in Prague
4: *
5: * This library is free software; you can redistribute it and/or
6: * modify it under the terms of the GNU Lesser General Public
7: * License as published by the Free Software Foundation; either
8: * version 3.0 of the License, or (at your option) any later version.
9: *
10: * This library is distributed in the hope that it will be useful,
11: * but WITHOUT ANY WARRANTY; without even the implied warranty of
12: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13: * Lesser General Public License for more details.
14: *
15: * You should have received a copy of the GNU Lesser General Public
16: * License along with this library.
17: */
18: package cz.cvut.kbss.ontodriver.jena;
19:
20: import cz.cvut.kbss.ontodriver.Connection;
21: import cz.cvut.kbss.ontodriver.PreparedStatement;
22: import cz.cvut.kbss.ontodriver.descriptor.AxiomDescriptor;
23: import cz.cvut.kbss.ontodriver.descriptor.AxiomValueDescriptor;
24: import cz.cvut.kbss.ontodriver.exception.OntoDriverException;
25: import cz.cvut.kbss.ontodriver.jena.exception.JenaDriverException;
26: import cz.cvut.kbss.ontodriver.jena.list.JenaLists;
27: import cz.cvut.kbss.ontodriver.jena.query.JenaStatement;
28: import cz.cvut.kbss.ontodriver.jena.util.ConnectionListener;
29: import cz.cvut.kbss.ontodriver.model.Axiom;
30:
31: import java.net.URI;
32: import java.util.Collection;
33: import java.util.List;
34: import java.util.Objects;
35: import java.util.Set;
36:
37: public class JenaConnection implements Connection {
38:
39: private boolean open;
40: private boolean autoCommit;
41:
42: private ConnectionListener listener;
43:
44: private final JenaAdapter adapter;
45:
46: JenaConnection(JenaAdapter adapter) {
47: this.adapter = adapter;
48: this.open = true;
49: }
50:
51: void registerListener(ConnectionListener listener) {
52: ensureOpen();
53: this.listener = listener;
54: }
55:
56: @Override
57: public boolean isOpen() {
58: return open;
59: }
60:
61: @Override
62: public void commit() throws JenaDriverException {
63: ensureOpen();
64: if (!autoCommit) {
65: adapter.commit();
66: }
67: }
68:
69: @Override
70: public void rollback() {
71: ensureOpen();
72: if (!autoCommit) {
73: adapter.rollback();
74: }
75: }
76:
77: @Override
78: public void setAutoCommit(boolean autoCommit) {
79: ensureOpen();
80: this.autoCommit = autoCommit;
81: }
82:
83: @Override
84: public boolean isAutoCommit() {
85: ensureOpen();
86: return autoCommit;
87: }
88:
89: private void commitIfAuto() throws JenaDriverException {
90: if (autoCommit) {
91: adapter.commit();
92: }
93: }
94:
95: @Override
96: public JenaStatement createStatement() {
97: ensureOpen();
98: return adapter.createStatement();
99: }
100:
101: @Override
102: public PreparedStatement prepareStatement(String sparql) {
103: ensureOpen();
104: return adapter.prepareStatement(Objects.requireNonNull(sparql));
105: }
106:
107: @Override
108: public boolean isConsistent(URI context) {
109: ensureOpen();
110: return adapter.isConsistent(context);
111: }
112:
113: @Override
114: public List<URI> getContexts() {
115: ensureOpen();
116: return adapter.getContext();
117: }
118:
119: @Override
120: public boolean contains(Axiom<?> axiom, Set<URI> contexts) throws JenaDriverException {
121: ensureOpen();
122: Objects.requireNonNull(axiom);
123: Objects.requireNonNull(contexts);
124: try {
125: return adapter.contains(axiom, contexts);
126: } catch (RuntimeException e) {
127: throw new JenaDriverException(e);
128: }
129: }
130:
131: @Override
132: public boolean isInferred(Axiom<?> axiom, Set<URI> contexts) throws JenaDriverException {
133: ensureOpen();
134: Objects.requireNonNull(axiom);
135: Objects.requireNonNull(contexts);
136: try {
137: return adapter.isInferred(axiom, contexts);
138: } catch (RuntimeException e) {
139: throw new JenaDriverException(e);
140: }
141: }
142:
143: @Override
144: public Collection<Axiom<?>> find(AxiomDescriptor descriptor) throws JenaDriverException {
145: ensureOpen();
146: Objects.requireNonNull(descriptor);
147: try {
148: return adapter.find(descriptor);
149: } catch (RuntimeException e) {
150: throw new JenaDriverException(e);
151: }
152: }
153:
154: @Override
155: public void persist(AxiomValueDescriptor descriptor) throws JenaDriverException {
156: ensureOpen();
157: Objects.requireNonNull(descriptor);
158: try {
159: adapter.persist(descriptor);
160: commitIfAuto();
161: } catch (RuntimeException e) {
162: throw new JenaDriverException(e);
163: }
164: }
165:
166: @Override
167: public URI generateIdentifier(URI classUri) {
168: ensureOpen();
169: Objects.requireNonNull(classUri);
170: return adapter.generateIdentifier(classUri);
171: }
172:
173: @Override
174: public void update(AxiomValueDescriptor descriptor) throws OntoDriverException {
175: ensureOpen();
176: Objects.requireNonNull(descriptor);
177: adapter.update(descriptor);
178: commitIfAuto();
179: }
180:
181: @Override
182: public void remove(AxiomDescriptor descriptor) throws OntoDriverException {
183: ensureOpen();
184: Objects.requireNonNull(descriptor);
185: try {
186: adapter.remove(descriptor);
187: commitIfAuto();
188: } catch (RuntimeException e) {
189: throw new JenaDriverException(e);
190: }
191: }
192:
193: @Override
194: public JenaLists lists() {
195: ensureOpen();
196: return new JenaLists(adapter, this::ensureOpen, this::commitIfAuto);
197: }
198:
199: @Override
200: public JenaTypes types() {
201: ensureOpen();
202: return new JenaTypes(adapter, this::ensureOpen, this::commitIfAuto);
203: }
204:
205: @Override
206: public JenaProperties properties() {
207: ensureOpen();
208: return new JenaProperties(adapter, this::ensureOpen, this::commitIfAuto);
209: }
210:
211: @Override
212: public <T> T unwrap(Class<T> cls) throws OntoDriverException {
213: ensureOpen();
214: Objects.requireNonNull(cls);
215: if (cls.isAssignableFrom(getClass())) {
216: return cls.cast(this);
217: }
218: return adapter.unwrap(cls);
219: }
220:
221: @Override
222: public void close() throws JenaDriverException {
223: if (!open) {
224: return;
225: }
226: adapter.close();
227: if (listener != null) {
228: listener.connectionClosed(this);
229: }
230: this.open = false;
231: }
232:
233: private void ensureOpen() {
234: if (!open) {
235: throw new IllegalStateException("This connection is closed.");
236: }
237: }
238: }