Skip to content

Method: getProviderUtil()

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.model;
16:
17: import cz.cvut.kbss.jopa.exceptions.OWLInferredAttributeModifiedException;
18: import cz.cvut.kbss.jopa.sessions.CloneBuilderImpl;
19: import cz.cvut.kbss.jopa.sessions.ServerSession;
20: import cz.cvut.kbss.jopa.sessions.UnitOfWorkImpl;
21: import cz.cvut.kbss.jopa.utils.EntityPropertiesUtils;
22:
23: import java.lang.reflect.Field;
24: import java.util.HashSet;
25: import java.util.Map;
26: import java.util.Set;
27:
28: public class JOPAPersistenceProvider implements PersistenceProvider, ProviderUtil {
29:
30: private static Set<EntityManagerFactoryImpl> emfs = new HashSet<>();
31:
32: public JOPAPersistenceProvider() {
33: }
34:
35: public EntityManagerFactoryImpl createEntityManagerFactory(String emName, Map<String, String> properties) {
36: final EntityManagerFactoryImpl emf = new EntityManagerFactoryImpl(properties);
37: emfs.add(emf);
38: return emf;
39: }
40:
41: public ProviderUtil getProviderUtil() {
42: return this;
43: }
44:
45: public LoadState isLoaded(Object entity) {
46: return LoadState.UNKNOWN;
47: }
48:
49: public LoadState isLoadedWithReference(Object entity, String attributeName) {
50: return LoadState.UNKNOWN;
51: }
52:
53: public LoadState isLoadedWithoutReference(Object entity, String attributeName) {
54: return LoadState.UNKNOWN;
55: }
56:
57: private static UnitOfWorkImpl getPersistenceContext(Object entity) {
58: if (entity == null) {
59: return null;
60: }
61: for (EntityManagerFactoryImpl emf : emfs) {
62: final ServerSession session = emf.getServerSession();
63: if (session == null) {
64: continue;
65: }
66: final UnitOfWorkImpl uow = session.getPersistenceContext(entity);
67: if (uow != null) {
68: return uow;
69: }
70: }
71: return null;
72: }
73:
74: static void loadReference(Object o, Field f) throws IllegalArgumentException,
75: IllegalAccessException {
76: final UnitOfWorkImpl uow = getPersistenceContext(o);
77:
78: if (uow != null) {
79: Object managedOrig = uow.getOriginal(o);
80: if (managedOrig == null) {
81: return;
82: }
83: Object val = EntityPropertiesUtils.getFieldValue(f, managedOrig);
84: if (val != null) {
85: return;
86: }
87: uow.loadEntityField(o, f);
88: }
89: }
90:
91: /**
92: * Write changes to the specified entity to the transaction ontology.
93: *
94: * @param entity Entity to persist
95: */
96: static void persistEntityChanges(Object entity, Field f) {
97: final UnitOfWorkImpl uow = getPersistenceContext(entity);
98: if (uow != null && uow.isInTransaction()) {
99: uow.attributeChanged(entity, f);
100: }
101: }
102:
103: static void verifyInferredAttributeNotModified(Object entity, Field field) {
104: if (getPersistenceContext(entity) == null) {
105: return;
106: }
107: if (CloneBuilderImpl.isFieldInferred(field)) {
108: throw new OWLInferredAttributeModifiedException(
109: "Modifying inferred attributes is forbidden.");
110: }
111: }
112: }