Package: ConnectionWrapper
ConnectionWrapper
name | instruction | branch | complexity | line | method | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
ConnectionWrapper(Connection) |
|
|
|
|
|
||||||||||||||||||||
close() |
|
|
|
|
|
||||||||||||||||||||
commit() |
|
|
|
|
|
||||||||||||||||||||
contains(Object, Class, Descriptor) |
|
|
|
|
|
||||||||||||||||||||
createStatement() |
|
|
|
|
|
||||||||||||||||||||
find(LoadingParameters) |
|
|
|
|
|
||||||||||||||||||||
getContexts() |
|
|
|
|
|
||||||||||||||||||||
getIdentifierAsUri(Object) |
|
|
|
|
|
||||||||||||||||||||
getReference(LoadingParameters) |
|
|
|
|
|
||||||||||||||||||||
isConsistent(URI) |
|
|
|
|
|
||||||||||||||||||||
loadFieldValue(Object, Field, Descriptor) |
|
|
|
|
|
||||||||||||||||||||
merge(Object, Field, Descriptor) |
|
|
|
|
|
||||||||||||||||||||
persist(Object, Object, Descriptor) |
|
|
|
|
|
||||||||||||||||||||
remove(Object, Class, Descriptor) |
|
|
|
|
|
||||||||||||||||||||
rollback() |
|
|
|
|
|
||||||||||||||||||||
setUnitOfWork(UnitOfWorkImpl) |
|
|
|
|
|
||||||||||||||||||||
unwrap(Class) |
|
|
|
|
|
Coverage
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.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.oom.ObjectOntologyMapper;
20: import cz.cvut.kbss.jopa.oom.ObjectOntologyMapperImpl;
21: import cz.cvut.kbss.jopa.utils.EntityPropertiesUtils;
22: import cz.cvut.kbss.jopa.utils.Wrapper;
23: import cz.cvut.kbss.ontodriver.Connection;
24: import cz.cvut.kbss.ontodriver.Statement;
25: import cz.cvut.kbss.ontodriver.exception.OntoDriverException;
26:
27: import java.lang.reflect.Field;
28: import java.net.URI;
29: import java.util.List;
30:
31: public class ConnectionWrapper implements Wrapper {
32:
33: private final Connection connection;
34: private ObjectOntologyMapper mapper;
35:
36: public ConnectionWrapper(Connection connection) {
37: this.connection = connection;
38: }
39:
40: void setUnitOfWork(UnitOfWorkImpl uow) {
41: this.mapper = new ObjectOntologyMapperImpl(uow, connection);
42: }
43:
44: public <T> boolean contains(Object identifier, Class<T> cls, Descriptor descriptor) {
45: final URI idUri = getIdentifierAsUri(identifier);
46:• return idUri != null && mapper.containsEntity(cls, idUri, descriptor);
47: }
48:
49: private static URI getIdentifierAsUri(Object identifier) {
50:• return identifier == null ? null : EntityPropertiesUtils.getValueAsURI(identifier);
51: }
52:
53: public <T> T find(LoadingParameters<T> loadingParameters) {
54: return mapper.loadEntity(loadingParameters);
55: }
56:
57: public <T> T getReference(LoadingParameters<T> loadingParameters) {
58: return mapper.loadReference(loadingParameters);
59: }
60:
61: public <T> void merge(T entity, Field field, Descriptor descriptor) {
62: mapper.updateFieldValue(entity, field, descriptor);
63: }
64:
65: public <T> void persist(Object identifier, T entity, Descriptor descriptor) {
66: final URI idUri = getIdentifierAsUri(identifier);
67: mapper.persistEntity(idUri, entity, descriptor);
68: }
69:
70: public <T> void remove(Object identifier, Class<T> cls, Descriptor descriptor) {
71: final URI idUri = getIdentifierAsUri(identifier);
72: mapper.removeEntity(idUri, cls, descriptor);
73: }
74:
75: public <T> void loadFieldValue(T entity, Field field, Descriptor descriptor) {
76: mapper.loadFieldValue(entity, field, descriptor);
77: }
78:
79: public void commit() {
80: try {
81: mapper.checkForUnpersistedChanges();
82: connection.commit();
83: } catch (OntoDriverException e) {
84: throw new OWLPersistenceException(e);
85: }
86: }
87:
88: public void rollback() {
89: try {
90: connection.rollback();
91: } catch (OntoDriverException e) {
92: throw new OWLPersistenceException(e);
93: }
94: }
95:
96: public void close() {
97: try {
98: connection.close();
99: } catch (Exception e) {
100: throw new OWLPersistenceException(e);
101: }
102: }
103:
104: public boolean isConsistent(URI context) {
105: try {
106: return connection.isConsistent(context);
107: } catch (OntoDriverException e) {
108: throw new OWLPersistenceException(e);
109: }
110: }
111:
112: public List<URI> getContexts() {
113: try {
114: return connection.getContexts();
115: } catch (OntoDriverException e) {
116: throw new OWLPersistenceException(e);
117: }
118: }
119:
120: public Statement createStatement() {
121: try {
122: return connection.createStatement();
123: } catch (OntoDriverException e) {
124: throw new OWLPersistenceException(e);
125: }
126: }
127:
128: @Override
129: public <T> T unwrap(Class<T> cls) {
130: try {
131: return connection.unwrap(cls);
132: } catch (OntoDriverException e) {
133: throw new OWLPersistenceException(e);
134: }
135: }
136: }