Skip to content

Method: getReference(LoadingParameters)

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