Skip to contentMethod: rollback()
1: /**
2: * Copyright (C) 2011 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.ontodriver.Connection;
23: import cz.cvut.kbss.ontodriver.Statement;
24: import cz.cvut.kbss.ontodriver.exception.OntoDriverException;
25:
26: import java.lang.reflect.Field;
27: import java.net.URI;
28: import java.util.List;
29:
30: public class ConnectionWrapper {
31:
32: private final Connection connection;
33: private ObjectOntologyMapper mapper;
34:
35: public ConnectionWrapper(Connection connection) {
36: this.connection = connection;
37: }
38:
39: void setUnitOfWork(UnitOfWorkImpl uow) {
40: this.mapper = new ObjectOntologyMapperImpl(uow, connection);
41: }
42:
43: <T> boolean contains(Object primaryKey, Class<T> cls, Descriptor descriptor) {
44: final URI pkUri = getPrimaryKeyAsUri(primaryKey);
45: return pkUri != null && mapper.containsEntity(cls, pkUri, descriptor);
46: }
47:
48: <T> T find(LoadingParameters<T> loadingParameters) {
49: return mapper.loadEntity(loadingParameters);
50: }
51:
52: <T> void merge(T entity, Field field, Descriptor descriptor) {
53: mapper.updateFieldValue(entity, field, descriptor);
54: }
55:
56: <T> void persist(Object primaryKey, T entity, Descriptor descriptor) {
57: final URI pkUri = getPrimaryKeyAsUri(primaryKey);
58: mapper.persistEntity(pkUri, entity, descriptor);
59: }
60:
61: <T> void remove(Object primaryKey, Class<T> cls, Descriptor descriptor) {
62: final URI pkUri = getPrimaryKeyAsUri(primaryKey);
63: mapper.removeEntity(pkUri, cls, descriptor);
64: }
65:
66: <T> void loadFieldValue(T entity, Field field, Descriptor descriptor) {
67: mapper.loadFieldValue(entity, field, descriptor);
68: }
69:
70: void commit() {
71: try {
72: mapper.checkForUnpersistedChanges();
73: connection.commit();
74: } catch (OntoDriverException e) {
75: throw new OWLPersistenceException(e);
76: }
77: }
78:
79: void rollback() {
80: try {
81: connection.rollback();
82: } catch (OntoDriverException e) {
83: throw new OWLPersistenceException(e);
84: }
85: }
86:
87: void close() {
88: try {
89: connection.close();
90: } catch (Exception e) {
91: throw new OWLPersistenceException(e);
92: }
93: }
94:
95: boolean isConsistent(URI context) {
96: try {
97: return connection.isConsistent(context);
98: } catch (OntoDriverException e) {
99: throw new OWLPersistenceException(e);
100: }
101: }
102:
103: List<URI> getContexts() {
104: try {
105: return connection.getContexts();
106: } catch (OntoDriverException e) {
107: throw new OWLPersistenceException(e);
108: }
109: }
110:
111: public Statement createStatement() {
112: try {
113: return connection.createStatement();
114: } catch (OntoDriverException e) {
115: throw new OWLPersistenceException(e);
116: }
117: }
118:
119: private URI getPrimaryKeyAsUri(Object primaryKey) {
120: return primaryKey == null ? null : EntityPropertiesUtils.getValueAsURI(primaryKey);
121: }
122: }