Skip to contentMethod: createStatement()
1: /**
2: * Copyright (C) 2023 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.jopa.sessions;
16:
17: import cz.cvut.kbss.jopa.exceptions.OWLPersistenceException;
18: import cz.cvut.kbss.jopa.model.descriptors.Descriptor;
19: import cz.cvut.kbss.jopa.model.metamodel.FieldSpecification;
20: import cz.cvut.kbss.jopa.oom.ObjectOntologyMapper;
21: import cz.cvut.kbss.jopa.oom.ObjectOntologyMapperImpl;
22: import cz.cvut.kbss.jopa.utils.EntityPropertiesUtils;
23: import cz.cvut.kbss.jopa.utils.Wrapper;
24: import cz.cvut.kbss.ontodriver.Connection;
25: import cz.cvut.kbss.ontodriver.Statement;
26: import cz.cvut.kbss.ontodriver.exception.OntoDriverException;
27: import cz.cvut.kbss.ontodriver.model.Axiom;
28:
29: import java.net.URI;
30: import java.util.List;
31: import java.util.Set;
32:
33: public class ConnectionWrapper implements Wrapper {
34:
35: private final Connection connection;
36: private ObjectOntologyMapper mapper;
37:
38: public ConnectionWrapper(Connection connection) {
39: this.connection = connection;
40: }
41:
42: void setUnitOfWork(UnitOfWorkImpl uow) {
43: this.mapper = new ObjectOntologyMapperImpl(uow, connection);
44: }
45:
46: public <T> boolean contains(Object identifier, Class<T> cls, Descriptor descriptor) {
47: final URI idUri = getIdentifierAsUri(identifier);
48: return idUri != null && mapper.containsEntity(cls, idUri, descriptor);
49: }
50:
51: private static URI getIdentifierAsUri(Object identifier) {
52: return identifier == null ? null : EntityPropertiesUtils.getValueAsURI(identifier);
53: }
54:
55: public <T> T find(LoadingParameters<T> loadingParameters) {
56: return mapper.loadEntity(loadingParameters);
57: }
58:
59: public <T> T getReference(LoadingParameters<T> loadingParameters) {
60: return mapper.loadReference(loadingParameters);
61: }
62:
63: public <T> void merge(T entity, FieldSpecification<? super T, ?> fieldSpec, Descriptor descriptor) {
64: mapper.updateFieldValue(entity, fieldSpec, descriptor);
65: }
66:
67: public <T> void persist(Object identifier, T entity, Descriptor descriptor) {
68: final URI idUri = getIdentifierAsUri(identifier);
69: mapper.persistEntity(idUri, entity, descriptor);
70: }
71:
72: public <T> void remove(Object identifier, Class<T> cls, Descriptor descriptor) {
73: final URI idUri = getIdentifierAsUri(identifier);
74: mapper.removeEntity(idUri, cls, descriptor);
75: }
76:
77: public <T> void loadFieldValue(T entity, FieldSpecification<? super T, ?> fieldSpec, Descriptor descriptor) {
78: mapper.loadFieldValue(entity, fieldSpec, descriptor);
79: }
80:
81: public <T> Set<Axiom<?>> getAttributeAxioms(T entity, FieldSpecification<? super T, ?> fieldSpec,
82: Descriptor entityDescriptor) {
83: return mapper.getAttributeAxioms(entity, fieldSpec, entityDescriptor);
84: }
85:
86: public <T> boolean isInferred(T entity, FieldSpecification<? super T, ?> fieldSpec, Object value,
87: Descriptor entityDescriptor) {
88: return mapper.isInferred(entity, fieldSpec, value, entityDescriptor);
89: }
90:
91: public void commit() {
92: try {
93: mapper.checkForUnpersistedChanges();
94: connection.commit();
95: } catch (OntoDriverException e) {
96: throw new OWLPersistenceException(e);
97: }
98: }
99:
100: public void rollback() {
101: try {
102: connection.rollback();
103: } catch (OntoDriverException e) {
104: throw new OWLPersistenceException(e);
105: }
106: }
107:
108: public void close() {
109: try {
110: connection.close();
111: } catch (Exception e) {
112: throw new OWLPersistenceException(e);
113: }
114: }
115:
116: public boolean isConsistent(URI context) {
117: try {
118: return connection.isConsistent(context);
119: } catch (OntoDriverException e) {
120: throw new OWLPersistenceException(e);
121: }
122: }
123:
124: public boolean isInferred(Axiom<?> axiom, Set<URI> contexts) {
125: try {
126: return connection.isInferred(axiom, contexts);
127: } catch (OntoDriverException e) {
128: throw new OWLPersistenceException(e);
129: }
130: }
131:
132: public List<URI> getContexts() {
133: try {
134: return connection.getContexts();
135: } catch (OntoDriverException e) {
136: throw new OWLPersistenceException(e);
137: }
138: }
139:
140: public Statement createStatement() {
141: try {
142: return connection.createStatement();
143: } catch (OntoDriverException e) {
144: throw new OWLPersistenceException(e);
145: }
146: }
147:
148: @Override
149: public <T> T unwrap(Class<T> cls) {
150: try {
151: return connection.unwrap(cls);
152: } catch (OntoDriverException e) {
153: throw new OWLPersistenceException(e);
154: }
155: }
156: }