-
Notifications
You must be signed in to change notification settings - Fork 14
/
Persistence.java
141 lines (113 loc) · 4.61 KB
/
Persistence.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/**
* Copyright (C) 2016 Czech Technical University in Prague
*
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details. You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package cz.cvut.kbss.jopa;
import cz.cvut.kbss.jopa.model.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.*;
public class Persistence {
private static final Logger LOG = LoggerFactory.getLogger(Persistence.class);
private static final Set<PersistenceProvider> map = new HashSet<>();
private static final PersistenceUtil pu = new PersistenceUtilImpl();
static {
// TODO load persistence providers
}
private Persistence() {
throw new AssertionError();
}
/**
* Create and return an EntityManagerFactory for the named persistence unit.
*
* @param persistenceUnitName the name of the persistence unit
* @return the factory that creates EntityManagers configured according to the specified persistence unit.
*/
public static EntityManagerFactory createEntityManagerFactory(final String persistenceUnitName) {
return createEntityManagerFactory(persistenceUnitName, Collections.<String, String>emptyMap());
}
public static EntityManagerFactory createEntityManagerFactory(final String persistenceUnitName,
final Map<String, String> parameters) {
final Map<String, String> realParams = new HashMap<>();
realParams.putAll(parameters);
final String className = realParams.get(PersistenceProperties.JPA_PERSISTENCE_PROVIDER);
if (className == null) {
throw new IllegalArgumentException("Persistent unit provider unknown.");
}
try {
final PersistenceProvider pp = ((Class<PersistenceProvider>) Class.forName(className)).newInstance();
// TODO get at runtime
map.add(pp);
return pp.createEntityManagerFactory(persistenceUnitName, realParams);
} catch (Exception e) {
LOG.error(e.getMessage(), e);
throw new IllegalArgumentException("Problems with creating EntityManagerFactory.", e);
}
}
/**
* @return PersistenceUtil instance
*/
public static PersistenceUtil getPersistenceUtil() {
return pu;
}
private static PersistenceProvider getProvider(final Object o) {
for (final PersistenceProvider emp : map) {
if (!emp.getProviderUtil().isLoaded(o).equals(LoadState.UNKNOWN)) {
return emp;
}
}
return null;
}
}
class PersistenceUtilImpl implements PersistenceUtil {
@Override
public boolean isLoaded(Object entity, String attributeName) {
for (final PersistenceProvider pp : PersistenceProviderResolverHolder
.getPersistenceProviderResolver().getPersistenceProviders()) {
switch (pp.getProviderUtil().isLoadedWithoutReference(entity, attributeName)) {
case LOADED:
return true;
case NOT_LOADED:
return false;
default:
break;
}
}
for (final PersistenceProvider pp : PersistenceProviderResolverHolder
.getPersistenceProviderResolver().getPersistenceProviders()) {
switch (pp.getProviderUtil().isLoadedWithReference(entity, attributeName)) {
case LOADED:
return true;
case NOT_LOADED:
return false;
default:
break;
}
}
return true;
}
@Override
public boolean isLoaded(Object entity) {
for (final PersistenceProvider pp : PersistenceProviderResolverHolder
.getPersistenceProviderResolver().getPersistenceProviders()) {
switch (pp.getProviderUtil().isLoaded(entity)) {
case LOADED:
return true;
case NOT_LOADED:
return false;
default:
}
}
return true;
}
}