Skip to content

Method: prepareStatement(String)

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