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