Skip to content

Package: Persistence

Persistence

nameinstructionbranchcomplexitylinemethod
Persistence()
M: 7 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
createEntityManagerFactory(String)
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
createEntityManagerFactory(String, Map)
M: 0 C: 36
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 9
100%
M: 0 C: 1
100%
getPersistenceUtil()
M: 2 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
lambda$createEntityManagerFactory$0(String, PersistenceProvider)
M: 0 C: 6
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
lambda$createEntityManagerFactory$1(String)
M: 0 C: 14
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
static {...}
M: 0 C: 5
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%

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:
19: import java.util.*;
20:
21: /**
22: * Bootstrap class that is used to obtain an {@link EntityManagerFactory}.
23: * <p>
24: * The {@code Persistence} class is also used to obtain a {@link PersistenceUtil} instance.
25: */
26: public class Persistence {
27:
28: private static final PersistenceUtil pu = new PersistenceUtilImpl();
29:
30: private Persistence() {
31: throw new AssertionError("No Persistence instances can be created.");
32: }
33:
34: /**
35: * Create and return an EntityManagerFactory for the named persistence unit.
36: *
37: * @param persistenceUnitName the name of the persistence unit
38: * @return the factory that creates EntityManagers configured according to the specified persistence unit
39: */
40: public static EntityManagerFactory createEntityManagerFactory(final String persistenceUnitName) {
41: return createEntityManagerFactory(persistenceUnitName, Collections.emptyMap());
42: }
43:
44: /**
45: * Create and return an EntityManagerFactory for the named persistence unit using the given properties.
46: *
47: * @param persistenceUnitName the name of the persistence unit
48: * @param properties Additional properties to use when creating the factory. The values of these properties
49: * override any values that may have been configured elsewhere
50: * @return the factory that creates EntityManagers configured according to the specified persistence unit
51: */
52: public static EntityManagerFactory createEntityManagerFactory(final String persistenceUnitName,
53: final Map<String, String> properties) {
54: final Map<String, String> realParams = new HashMap<>(properties);
55:
56: final String className = realParams.get(PersistenceProperties.JPA_PERSISTENCE_PROVIDER);
57:
58:• if (className == null) {
59: throw new IllegalArgumentException("Missing persistence unit provider.");
60: }
61: final List<PersistenceProvider> providers = PersistenceProviderResolverHolder
62: .getPersistenceProviderResolver().getPersistenceProviders();
63:
64: final Optional<PersistenceProvider> provider = providers.stream().filter(pp -> pp.getClass().getName()
65: .equals(className))
66: .findFirst();
67:
68: return provider.orElseThrow(() -> new IllegalArgumentException(
69: "Type " + className + " is not a PersistenceProvider implementation."))
70: .createEntityManagerFactory(persistenceUnitName, realParams);
71: }
72:
73: /**
74: * Return the PersistenceUtil instance
75: *
76: * @return PersistenceUtil instance
77: */
78: public static PersistenceUtil getPersistenceUtil() {
79: return pu;
80: }
81: }
82:
83: class PersistenceUtilImpl implements PersistenceUtil {
84:
85: @Override
86: public boolean isLoaded(Object entity, String attributeName) {
87: for (final PersistenceProvider pp : PersistenceProviderResolverHolder.getPersistenceProviderResolver()
88: .getPersistenceProviders()) {
89:
90: switch (pp.getProviderUtil().isLoadedWithoutReference(entity, attributeName)) {
91: case LOADED:
92: return true;
93: case NOT_LOADED:
94: return false;
95: default:
96: break;
97: }
98: }
99:
100: for (final PersistenceProvider pp : PersistenceProviderResolverHolder.getPersistenceProviderResolver()
101: .getPersistenceProviders()) {
102:
103: switch (pp.getProviderUtil().isLoadedWithReference(entity, attributeName)) {
104: case LOADED:
105: return true;
106: case NOT_LOADED:
107: return false;
108: default:
109: break;
110: }
111: }
112: return false;
113: }
114:
115: @Override
116: public boolean isLoaded(Object entity) {
117: for (final PersistenceProvider pp : PersistenceProviderResolverHolder.getPersistenceProviderResolver()
118: .getPersistenceProviders()) {
119:
120: switch (pp.getProviderUtil().isLoaded(entity)) {
121: case LOADED:
122: return true;
123: case NOT_LOADED:
124: return false;
125: default:
126:
127: }
128: }
129:
130: return false;
131: }
132: }