Skip to content

Method: persist(AxiomValueDescriptor)

1: /**
2: * Copyright (C) 2020 Czech Technical University in Prague
3: *
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: *
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.jena;
16:
17: import cz.cvut.kbss.ontodriver.Connection;
18: import cz.cvut.kbss.ontodriver.PreparedStatement;
19: import cz.cvut.kbss.ontodriver.descriptor.AxiomDescriptor;
20: import cz.cvut.kbss.ontodriver.descriptor.AxiomValueDescriptor;
21: import cz.cvut.kbss.ontodriver.exception.OntoDriverException;
22: import cz.cvut.kbss.ontodriver.jena.exception.JenaDriverException;
23: import cz.cvut.kbss.ontodriver.jena.list.JenaLists;
24: import cz.cvut.kbss.ontodriver.jena.query.JenaStatement;
25: import cz.cvut.kbss.ontodriver.jena.util.ConnectionListener;
26: import cz.cvut.kbss.ontodriver.model.Axiom;
27:
28: import java.net.URI;
29: import java.util.Collection;
30: import java.util.List;
31: import java.util.Objects;
32:
33: public class JenaConnection implements Connection {
34:
35: private boolean open;
36: private boolean autoCommit;
37:
38: private ConnectionListener listener;
39:
40: private final JenaAdapter adapter;
41:
42: JenaConnection(JenaAdapter adapter) {
43: this.adapter = adapter;
44: this.open = true;
45: }
46:
47: void registerListener(ConnectionListener listener) {
48: ensureOpen();
49: this.listener = listener;
50: }
51:
52: @Override
53: public boolean isOpen() {
54: return open;
55: }
56:
57: @Override
58: public void commit() throws JenaDriverException {
59: ensureOpen();
60: if (!autoCommit) {
61: adapter.commit();
62: }
63: }
64:
65: @Override
66: public void rollback() {
67: ensureOpen();
68: if (!autoCommit) {
69: adapter.rollback();
70: }
71: }
72:
73: @Override
74: public void setAutoCommit(boolean autoCommit) {
75: ensureOpen();
76: this.autoCommit = autoCommit;
77: }
78:
79: @Override
80: public boolean isAutoCommit() {
81: ensureOpen();
82: return autoCommit;
83: }
84:
85: private void commitIfAuto() throws JenaDriverException {
86: if (autoCommit) {
87: adapter.commit();
88: }
89: }
90:
91: @Override
92: public JenaStatement createStatement() {
93: ensureOpen();
94: return adapter.createStatement();
95: }
96:
97: @Override
98: public PreparedStatement prepareStatement(String sparql) {
99: ensureOpen();
100: return adapter.prepareStatement(Objects.requireNonNull(sparql));
101: }
102:
103: @Override
104: public boolean isConsistent(URI context) {
105: ensureOpen();
106: return adapter.isConsistent(context);
107: }
108:
109: @Override
110: public List<URI> getContexts() {
111: ensureOpen();
112: return adapter.getContext();
113: }
114:
115: @Override
116: public boolean contains(Axiom<?> axiom, URI context) throws JenaDriverException {
117: ensureOpen();
118: Objects.requireNonNull(axiom);
119: try {
120: return adapter.contains(axiom, context);
121: } catch (RuntimeException e) {
122: throw new JenaDriverException(e);
123: }
124: }
125:
126: @Override
127: public Collection<Axiom<?>> find(AxiomDescriptor descriptor) throws JenaDriverException {
128: ensureOpen();
129: Objects.requireNonNull(descriptor);
130: try {
131: return adapter.find(descriptor);
132: } catch (RuntimeException e) {
133: throw new JenaDriverException(e);
134: }
135: }
136:
137: @Override
138: public void persist(AxiomValueDescriptor descriptor) throws JenaDriverException {
139: ensureOpen();
140: Objects.requireNonNull(descriptor);
141: try {
142: adapter.persist(descriptor);
143: commitIfAuto();
144: } catch (RuntimeException e) {
145: throw new JenaDriverException(e);
146: }
147: }
148:
149: @Override
150: public URI generateIdentifier(URI classUri) {
151: ensureOpen();
152: Objects.requireNonNull(classUri);
153: return adapter.generateIdentifier(classUri);
154: }
155:
156: @Override
157: public void update(AxiomValueDescriptor descriptor) throws OntoDriverException {
158: ensureOpen();
159: Objects.requireNonNull(descriptor);
160: adapter.update(descriptor);
161: commitIfAuto();
162: }
163:
164: @Override
165: public void remove(AxiomDescriptor descriptor) throws OntoDriverException {
166: ensureOpen();
167: Objects.requireNonNull(descriptor);
168: try {
169: adapter.remove(descriptor);
170: commitIfAuto();
171: } catch (RuntimeException e) {
172: throw new JenaDriverException(e);
173: }
174: }
175:
176: @Override
177: public JenaLists lists() {
178: ensureOpen();
179: return new JenaLists(adapter, this::ensureOpen, this::commitIfAuto);
180: }
181:
182: @Override
183: public JenaTypes types() {
184: ensureOpen();
185: return new JenaTypes(adapter, this::ensureOpen, this::commitIfAuto);
186: }
187:
188: @Override
189: public JenaProperties properties() {
190: ensureOpen();
191: return new JenaProperties(adapter, this::ensureOpen, this::commitIfAuto);
192: }
193:
194: @Override
195: public <T> T unwrap(Class<T> cls) throws OntoDriverException {
196: ensureOpen();
197: Objects.requireNonNull(cls);
198: if (cls.isAssignableFrom(getClass())) {
199: return cls.cast(this);
200: }
201: return adapter.unwrap(cls);
202: }
203:
204: @Override
205: public void close() throws JenaDriverException {
206: if (!open) {
207: return;
208: }
209: adapter.close();
210: if (listener != null) {
211: listener.connectionClosed(this);
212: }
213: this.open = false;
214: }
215:
216: private void ensureOpen() {
217: if (!open) {
218: throw new IllegalStateException("This connection is closed.");
219: }
220: }
221: }