Package: Persistence
Persistence
name | instruction | branch | complexity | line | method | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Persistence() |
|
|
|
|
|
||||||||||||||||||||
createEntityManagerFactory(String) |
|
|
|
|
|
||||||||||||||||||||
createEntityManagerFactory(String, Map) |
|
|
|
|
|
||||||||||||||||||||
getPersistenceUtil() |
|
|
|
|
|
||||||||||||||||||||
getProvider(Object) |
|
|
|
|
|
||||||||||||||||||||
static {...} |
|
|
|
|
|
Coverage
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;
16:
17: import cz.cvut.kbss.jopa.model.*;
18: import org.slf4j.Logger;
19: import org.slf4j.LoggerFactory;
20:
21: import java.util.*;
22:
23: public class Persistence {
24:
25: private static final Logger LOG = LoggerFactory.getLogger(Persistence.class);
26:
27: private static final Set<PersistenceProvider> map = new HashSet<>();
28:
29: private static final PersistenceUtil pu = new PersistenceUtilImpl();
30:
31: static {
32: // TODO load persistence providers
33: }
34:
35: private Persistence() {
36: throw new AssertionError();
37: }
38:
39: /**
40: * Create and return an EntityManagerFactory for the named persistence unit.
41: *
42: * @param persistenceUnitName the name of the persistence unit
43: * @return the factory that creates EntityManagers configured according to the specified persistence unit.
44: */
45: public static EntityManagerFactory createEntityManagerFactory(final String persistenceUnitName) {
46: return createEntityManagerFactory(persistenceUnitName, Collections.<String, String>emptyMap());
47: }
48:
49: public static EntityManagerFactory createEntityManagerFactory(final String persistenceUnitName,
50: final Map<String, String> parameters) {
51: final Map<String, String> realParams = new HashMap<>();
52:
53: realParams.putAll(parameters);
54:
55: final String className = realParams.get(PersistenceProperties.JPA_PERSISTENCE_PROVIDER);
56:
57:• if (className == null) {
58: throw new IllegalArgumentException("Persistent unit provider unknown.");
59: }
60:
61: try {
62: final PersistenceProvider pp = ((Class<PersistenceProvider>) Class.forName(className)).newInstance();
63:
64: // TODO get at runtime
65: map.add(pp);
66:
67: return pp.createEntityManagerFactory(persistenceUnitName, realParams);
68: } catch (Exception e) {
69: LOG.error(e.getMessage(), e);
70: throw new IllegalArgumentException("Problems with creating EntityManagerFactory.", e);
71: }
72: }
73:
74: /**
75: * @return PersistenceUtil instance
76: */
77: public static PersistenceUtil getPersistenceUtil() {
78: return pu;
79: }
80:
81: private static PersistenceProvider getProvider(final Object o) {
82:• for (final PersistenceProvider emp : map) {
83:• if (!emp.getProviderUtil().isLoaded(o).equals(LoadState.UNKNOWN)) {
84: return emp;
85: }
86: }
87: return null;
88: }
89: }
90:
91: class PersistenceUtilImpl implements PersistenceUtil {
92:
93: @Override
94: public boolean isLoaded(Object entity, String attributeName) {
95: for (final PersistenceProvider pp : PersistenceProviderResolverHolder
96: .getPersistenceProviderResolver().getPersistenceProviders()) {
97:
98: switch (pp.getProviderUtil().isLoadedWithoutReference(entity, attributeName)) {
99: case LOADED:
100: return true;
101: case NOT_LOADED:
102: return false;
103: default:
104: break;
105: }
106: }
107:
108: for (final PersistenceProvider pp : PersistenceProviderResolverHolder
109: .getPersistenceProviderResolver().getPersistenceProviders()) {
110:
111: switch (pp.getProviderUtil().isLoadedWithReference(entity, attributeName)) {
112: case LOADED:
113: return true;
114: case NOT_LOADED:
115: return false;
116: default:
117: break;
118: }
119: }
120: return true;
121: }
122:
123: @Override
124: public boolean isLoaded(Object entity) {
125: for (final PersistenceProvider pp : PersistenceProviderResolverHolder
126: .getPersistenceProviderResolver().getPersistenceProviders()) {
127:
128: switch (pp.getProviderUtil().isLoaded(entity)) {
129: case LOADED:
130: return true;
131: case NOT_LOADED:
132: return false;
133: default:
134:
135: }
136: }
137:
138: return true;
139: }
140:
141: }