Skip to content

Method: setUnitOfWork(UnitOfWorkImpl)

1: /**
2: * Copyright (C) 2022 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 void commit() {
87: try {
88: mapper.checkForUnpersistedChanges();
89: connection.commit();
90: } catch (OntoDriverException e) {
91: throw new OWLPersistenceException(e);
92: }
93: }
94:
95: public void rollback() {
96: try {
97: connection.rollback();
98: } catch (OntoDriverException e) {
99: throw new OWLPersistenceException(e);
100: }
101: }
102:
103: public void close() {
104: try {
105: connection.close();
106: } catch (Exception e) {
107: throw new OWLPersistenceException(e);
108: }
109: }
110:
111: public boolean isConsistent(URI context) {
112: try {
113: return connection.isConsistent(context);
114: } catch (OntoDriverException e) {
115: throw new OWLPersistenceException(e);
116: }
117: }
118:
119: public boolean isInferred(Axiom<?> axiom, Set<URI> contexts) {
120: try {
121: return connection.isInferred(axiom, contexts);
122: } catch (OntoDriverException e) {
123: throw new OWLPersistenceException(e);
124: }
125: }
126:
127: public List<URI> getContexts() {
128: try {
129: return connection.getContexts();
130: } catch (OntoDriverException e) {
131: throw new OWLPersistenceException(e);
132: }
133: }
134:
135: public Statement createStatement() {
136: try {
137: return connection.createStatement();
138: } catch (OntoDriverException e) {
139: throw new OWLPersistenceException(e);
140: }
141: }
142:
143: @Override
144: public <T> T unwrap(Class<T> cls) {
145: try {
146: return connection.unwrap(cls);
147: } catch (OntoDriverException e) {
148: throw new OWLPersistenceException(e);
149: }
150: }
151: }