Skip to contentMethod: getContexts()
1: /**
2: * Copyright (C) 2016 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 primaryKey, Class<T> cls, Descriptor descriptor) {
45: final URI pkUri = getPrimaryKeyAsUri(primaryKey);
46: return pkUri != null && mapper.containsEntity(cls, pkUri, descriptor);
47: }
48:
49: public <T> T find(LoadingParameters<T> loadingParameters) {
50: return mapper.loadEntity(loadingParameters);
51: }
52:
53: public <T> void merge(T entity, Field field, Descriptor descriptor) {
54: mapper.updateFieldValue(entity, field, descriptor);
55: }
56:
57: public <T> void persist(Object primaryKey, T entity, Descriptor descriptor) {
58: final URI pkUri = getPrimaryKeyAsUri(primaryKey);
59: mapper.persistEntity(pkUri, entity, descriptor);
60: }
61:
62: public <T> void remove(Object primaryKey, Class<T> cls, Descriptor descriptor) {
63: final URI pkUri = getPrimaryKeyAsUri(primaryKey);
64: mapper.removeEntity(pkUri, cls, descriptor);
65: }
66:
67: public <T> void loadFieldValue(T entity, Field field, Descriptor descriptor) {
68: mapper.loadFieldValue(entity, field, descriptor);
69: }
70:
71: public void commit() {
72: try {
73: mapper.checkForUnpersistedChanges();
74: connection.commit();
75: } catch (OntoDriverException e) {
76: throw new OWLPersistenceException(e);
77: }
78: }
79:
80: public void rollback() {
81: try {
82: connection.rollback();
83: } catch (OntoDriverException e) {
84: throw new OWLPersistenceException(e);
85: }
86: }
87:
88: public void close() {
89: try {
90: connection.close();
91: } catch (Exception e) {
92: throw new OWLPersistenceException(e);
93: }
94: }
95:
96: public boolean isConsistent(URI context) {
97: try {
98: return connection.isConsistent(context);
99: } catch (OntoDriverException e) {
100: throw new OWLPersistenceException(e);
101: }
102: }
103:
104: public List<URI> getContexts() {
105: try {
106: return connection.getContexts();
107: } catch (OntoDriverException e) {
108: throw new OWLPersistenceException(e);
109: }
110: }
111:
112: public Statement createStatement() {
113: try {
114: return connection.createStatement();
115: } catch (OntoDriverException e) {
116: throw new OWLPersistenceException(e);
117: }
118: }
119:
120: private URI getPrimaryKeyAsUri(Object primaryKey) {
121: return primaryKey == null ? null : EntityPropertiesUtils.getValueAsURI(primaryKey);
122: }
123:
124: @Override
125: public <T> T unwrap(Class<T> cls) {
126: try {
127: return connection.unwrap(cls);
128: } catch (OntoDriverException e) {
129: throw new OWLPersistenceException(e);
130: }
131: }
132: }