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