Skip to content

Package: EntityManagerImpl

EntityManagerImpl

nameinstructionbranchcomplexitylinemethod
EntityManagerImpl(EntityManagerFactoryImpl, Configuration, ServerSession)
M: 0 C: 17
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 7
100%
M: 0 C: 1
100%
clear()
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
close()
M: 0 C: 8
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
contains(Object)
M: 0 C: 7
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
createNativeQuery(String)
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%
createNativeQuery(String, Class)
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%
createQuery(String)
M: 5 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
createQuery(String, Class)
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
detach(Object)
M: 0 C: 23
100%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 0 C: 7
100%
M: 0 C: 1
100%
ensureOpen()
M: 5 C: 4
44%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 1 C: 2
67%
M: 0 C: 1
100%
finalize()
M: 0 C: 8
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
find(Class, Object)
M: 0 C: 10
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
find(Class, Object, Descriptor)
M: 0 C: 51
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 7
100%
M: 0 C: 1
100%
flush()
M: 17 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 6 C: 0
0%
M: 1 C: 0
0%
getConfiguration()
M: 0 C: 3
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
getContexts()
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%
getCurrentPersistenceContext()
M: 0 C: 16
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
getDelegate()
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%
getEntityManagerFactory()
M: 0 C: 3
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
getMetamodel()
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
getState(Object)
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%
getState(Object, Descriptor)
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%
getTransaction()
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
isConsistent(URI)
M: 5 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
isLoaded(Object, String)
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%
isOpen()
M: 0 C: 3
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
merge(Object)
M: 0 C: 9
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
merge(Object, Descriptor)
M: 0 C: 15
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
mergeInternal(Object, Descriptor)
M: 12 C: 51
81%
M: 3 C: 4
57%
M: 3 C: 2
40%
M: 1 C: 13
93%
M: 0 C: 1
100%
mergeX(Attribute, Object, Descriptor)
M: 0 C: 53
100%
M: 0 C: 6
100%
M: 0 C: 4
100%
M: 0 C: 14
100%
M: 0 C: 1
100%
persist(Object)
M: 0 C: 9
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
persist(Object, Descriptor)
M: 4 C: 66
94%
M: 3 C: 4
57%
M: 3 C: 3
50%
M: 1 C: 16
94%
M: 0 C: 1
100%
refresh(Object)
M: 0 C: 20
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 6
100%
M: 0 C: 1
100%
remove(Object)
M: 0 C: 37
100%
M: 2 C: 2
50%
M: 2 C: 2
50%
M: 0 C: 8
100%
M: 0 C: 1
100%
removeCurrentPersistenceContext()
M: 0 C: 14
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
setTransactionWrapper()
M: 0 C: 7
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
setUseBackupOntologyForQueryProcessing()
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
setUseTransactionalOntologyForQueryProcessing()
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
static {...}
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
transactionFinished(EntityTransaction)
M: 0 C: 5
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
transactionStarted(EntityTransaction)
M: 0 C: 7
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
unwrap(Class)
M: 13 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
useBackupOntologyForQueryProcessing()
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%
useTransactionalOntologyForQueryProcessing()
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%

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.model;
16:
17: import cz.cvut.kbss.jopa.exceptions.OWLEntityExistsException;
18: import cz.cvut.kbss.jopa.exceptions.OWLPersistenceException;
19: import cz.cvut.kbss.jopa.exceptions.TransactionRequiredException;
20: import cz.cvut.kbss.jopa.model.annotations.CascadeType;
21: import cz.cvut.kbss.jopa.model.descriptors.Descriptor;
22: import cz.cvut.kbss.jopa.model.descriptors.EntityDescriptor;
23: import cz.cvut.kbss.jopa.model.metamodel.Attribute;
24: import cz.cvut.kbss.jopa.model.metamodel.Metamodel;
25: import cz.cvut.kbss.jopa.model.query.Query;
26: import cz.cvut.kbss.jopa.model.query.TypedQuery;
27: import cz.cvut.kbss.jopa.sessions.ServerSession;
28: import cz.cvut.kbss.jopa.sessions.UnitOfWorkImpl;
29: import cz.cvut.kbss.jopa.transactions.EntityTransaction;
30: import cz.cvut.kbss.jopa.transactions.EntityTransactionWrapper;
31: import cz.cvut.kbss.jopa.transactions.TransactionWrapper;
32: import cz.cvut.kbss.jopa.utils.CollectionFactory;
33: import cz.cvut.kbss.jopa.utils.Configuration;
34: import cz.cvut.kbss.jopa.utils.EntityPropertiesUtils;
35: import cz.cvut.kbss.jopa.utils.ErrorUtils;
36: import org.slf4j.Logger;
37: import org.slf4j.LoggerFactory;
38:
39: import java.net.URI;
40: import java.util.Collection;
41: import java.util.List;
42: import java.util.Objects;
43:
44: public class EntityManagerImpl extends AbstractEntityManager {
45:
46: private static final Logger LOG = LoggerFactory.getLogger(EntityManagerImpl.class);
47:
48: private EntityManagerFactoryImpl emf;
49:
50: private boolean open;
51:
52: private TransactionWrapper transaction;
53: private UnitOfWorkImpl persistenceContext;
54: private ServerSession serverSession;
55: private final Configuration configuration;
56:
57: public EntityManagerImpl(EntityManagerFactoryImpl emf, Configuration configuration,
58: ServerSession serverSession) {
59: this.emf = emf;
60: this.serverSession = serverSession;
61: this.configuration = configuration;
62:
63: this.setTransactionWrapper();
64:
65: this.open = true;
66: }
67:
68: public enum State {
69: MANAGED, MANAGED_NEW, NOT_MANAGED, REMOVED
70: }
71:
72: @Override
73: public void persist(final Object entity) {
74: final Descriptor d = new EntityDescriptor();
75: persist(entity, d);
76: }
77:
78: @Override
79: public void persist(final Object entity, final Descriptor descriptor) {
80: LOG.trace("Persisting {}", entity);
81: ensureOpen();
82: Objects.requireNonNull(entity, ErrorUtils.constructNPXMessage("entity"));
83: Objects.requireNonNull(descriptor, ErrorUtils.constructNPXMessage("descriptor"));
84:
85:• switch (getState(entity, descriptor)) {
86: case NOT_MANAGED:
87: try {
88: getCurrentPersistenceContext().registerNewObject(entity, descriptor);
89: } catch (RuntimeException e) {
90:• if (getTransaction().isActive()) {
91: getTransaction().setRollbackOnly();
92: }
93: throw e;
94: }
95: case MANAGED:
96: new OneLevelCascadeExplorer() {
97: @Override
98: protected void exploreCascaded(Attribute<?, ?> at, Object o) {
99: try {
100: Object ox = EntityPropertiesUtils.getAttributeValue(at, o);
101: LOG.trace("object={}, attribute={}, value={}", o, at.getName(), ox);
102: if (ox == null) {
103: return;
104: }
105: final Descriptor attDescriptor = descriptor.getAttributeDescriptor(at);
106: if (at.isCollection()) {
107: for (final Object ox2 : (Collection<?>) ox) {
108: persist(ox2, attDescriptor);
109: }
110: } else {
111: persist(ox, attDescriptor);
112: }
113: } catch (Exception e) {
114: if (getTransaction().isActive()) {
115: getTransaction().setRollbackOnly();
116: }
117: throw new OWLPersistenceException(
118: "A problem occured when persisting attribute " + at.getName()
119: + " of with value " + o + " of object " + entity, e);
120: }
121: }
122: }.start(this, entity, CascadeType.PERSIST);
123: break;
124: case MANAGED_NEW:
125: throw new OWLEntityExistsException("Entity " + entity
126: + " is already managed in this persistence context.");
127: case REMOVED:
128: getCurrentPersistenceContext().revertObject(entity);
129: break;
130: }
131: }
132:
133: @Override
134: public <T> T merge(final T entity) {
135: final Descriptor d = new EntityDescriptor();
136: return merge(entity, d);
137: }
138:
139: @Override
140: public <T> T merge(final T entity, final Descriptor descriptor) {
141: Objects.requireNonNull(entity, ErrorUtils.constructNPXMessage("entity"));
142: Objects.requireNonNull(descriptor, ErrorUtils.constructNPXMessage("descriptor"));
143:
144: return mergeInternal(entity, descriptor);
145: }
146:
147: /**
148: * Merges state of the specified entity into the current persistence context. </p>
149: *
150: * @param entity Entity instance
151: * @param descriptor Contains information about contexts into which the entity and its field should be merged
152: * @return Managed instance of the merged entity
153: */
154: private <T> T mergeInternal(final T entity, final Descriptor descriptor) {
155:• assert entity != null;
156:• assert descriptor != null;
157: LOG.trace("Merging {}", entity);
158: ensureOpen();
159:
160: Class<T> clz = (Class<T>) entity.getClass();
161:
162:• switch (getState(entity, descriptor)) {
163: case MANAGED_NEW:
164: case MANAGED:
165: new OneLevelCascadeExplorer() {
166: @Override
167: protected void exploreCascaded(Attribute<?, ?> at, Object o) {
168: mergeX(at, o, descriptor);
169: }
170: }.start(this, entity, CascadeType.MERGE);
171: return entity;
172: case NOT_MANAGED:
173: final T merged;
174: merged = getCurrentPersistenceContext().mergeDetached(entity, descriptor);
175:
176: new OneLevelCascadeExplorer() {
177: @Override
178: protected void exploreCascaded(Attribute<?, ?> at, Object o) {
179: final Descriptor attDescriptor = descriptor.getAttributeDescriptor(at);
180: mergeX(at, o, attDescriptor);
181: }
182:
183: @Override
184: protected void exploreNonCascaded(Attribute<?, ?> at, Object o) {
185: final Object attVal = EntityPropertiesUtils.getAttributeValue(at, o);
186: EntityPropertiesUtils.setFieldValue(at.getJavaField(), o, attVal);
187: }
188: }.start(this, merged, CascadeType.MERGE);
189: return merged;
190: case REMOVED:
191: default:
192: throw new IllegalArgumentException();
193: }
194: }
195:
196: private void mergeX(Attribute<?, ?> at, Object o, Descriptor descriptor) {
197: Object attVal = EntityPropertiesUtils.getAttributeValue(at, o);
198:• if (attVal == null) {
199: return;
200: }
201:• if (at.isCollection()) {
202: Collection c = (Collection) attVal;
203: Collection merged = CollectionFactory.createInstance(c);
204:• for (final Object ox2 : c) {
205: merged.add(mergeInternal(ox2, descriptor));
206: }
207: attVal = getCurrentPersistenceContext().createIndirectCollection(merged, o, at.getJavaField());
208: } else {
209: attVal = mergeInternal(attVal, descriptor);
210: }
211: EntityPropertiesUtils.setFieldValue(at.getJavaField(), o, attVal);
212: }
213:
214: public void remove(Object object) {
215: ensureOpen();
216:
217:• switch (getState(object)) {
218: case MANAGED_NEW:
219: case MANAGED:
220: getCurrentPersistenceContext().removeObject(object);
221: // Intentional fall-through
222: case REMOVED:
223: new SimpleOneLevelCascadeExplorer() {
224: @Override
225: protected void runCascadedForEach(Object ox2) {
226: remove(ox2);
227: }
228: }.start(this, object, CascadeType.REMOVE);
229: break;
230: case NOT_MANAGED:
231: throw new IllegalArgumentException("Entity " + object
232: + " is not managed and cannot be removed.");
233: }
234: }
235:
236: @Override
237: public <T> T find(Class<T> cls, Object primaryKey) {
238: final EntityDescriptor d = new EntityDescriptor();
239: return find(cls, primaryKey, d);
240: }
241:
242: @Override
243: public <T> T find(Class<T> cls, Object primaryKey, Descriptor descriptor) {
244: Objects.requireNonNull(cls, ErrorUtils.constructNPXMessage("cls"));
245: Objects.requireNonNull(primaryKey, ErrorUtils.constructNPXMessage("primaryKey"));
246: Objects.requireNonNull(descriptor, ErrorUtils.constructNPXMessage("descriptor"));
247:
248: ensureOpen();
249: LOG.trace("Finding instance of {} with identifier {} in context ", cls, primaryKey, descriptor);
250:• final URI uri = (primaryKey instanceof URI) ? (URI) primaryKey : URI.create(primaryKey.toString());
251:
252: return getCurrentPersistenceContext().readObject(cls, uri, descriptor);
253: }
254:
255: public void flush() {
256: ensureOpen();
257:
258: LOG.trace("Flushing changes...");
259:• if (!getTransaction().isActive()) {
260: throw new TransactionRequiredException();
261: }
262: this.getCurrentPersistenceContext().writeUncommittedChanges();
263: }
264:
265: @Override
266: public void refresh(Object entity) {
267: ensureOpen();
268: Objects.requireNonNull(entity, ErrorUtils.constructNPXMessage("entity"));
269:
270: this.getCurrentPersistenceContext().revertObject(entity);
271: new SimpleOneLevelCascadeExplorer() {
272: @Override
273: protected void runCascadedForEach(Object ox2) {
274: refresh(ox2);
275: }
276: }.start(this, entity, CascadeType.REFRESH);
277: }
278:
279: public void clear() {
280: getCurrentPersistenceContext().clear();
281: }
282:
283: public void detach(Object entity) {
284: ensureOpen();
285:
286:• switch (getState(entity)) {
287: case MANAGED_NEW:
288: case MANAGED:
289: getCurrentPersistenceContext().unregisterObject(entity);
290: new SimpleOneLevelCascadeExplorer() {
291: @Override
292: protected void runCascadedForEach(Object ox2) {
293: detach(ox2);
294: }
295: }.start(this, entity, CascadeType.DETACH);
296: break;
297: default:
298: break;
299: }
300: }
301:
302: public boolean contains(Object entity) {
303: ensureOpen();
304: return getCurrentPersistenceContext().contains(entity);
305: }
306:
307: public void close() {
308: ensureOpen();
309: removeCurrentPersistenceContext();
310: open = false;
311: }
312:
313: public boolean isOpen() {
314: return open;
315: }
316:
317: @Override
318: public EntityTransaction getTransaction() {
319: return transaction.getTransaction();
320: }
321:
322: public EntityManagerFactoryImpl getEntityManagerFactory() {
323: return emf;
324: }
325:
326: public Metamodel getMetamodel() {
327: return emf.getMetamodel();
328: }
329:
330: public boolean isLoaded(final Object object, final String attributeName) {
331: // TODO
332: return false;
333: }
334:
335: @Override
336: public Query createQuery(String qlString) {
337: return getCurrentPersistenceContext().createQuery(qlString);
338: }
339:
340: @Override
341: public <T> TypedQuery<T> createQuery(String query, Class<T> resultClass) {
342: return getCurrentPersistenceContext().createQuery(query, resultClass);
343: }
344:
345: @Override
346: public Query createNativeQuery(String sqlString) {
347: return getCurrentPersistenceContext().createNativeQuery(sqlString);
348: }
349:
350: @Override
351: public <T> TypedQuery<T> createNativeQuery(String sqlString, Class<T> resultClass) {
352: return getCurrentPersistenceContext().createNativeQuery(sqlString, resultClass);
353: }
354:
355: @Override
356: public boolean isConsistent(URI context) {
357: return getCurrentPersistenceContext().isConsistent(context);
358: }
359:
360: @Override
361: public List<URI> getContexts() {
362: return getCurrentPersistenceContext().getContexts();
363: }
364:
365: @Override
366: public void setUseTransactionalOntologyForQueryProcessing() {
367: getCurrentPersistenceContext().setUseTransactionalOntologyForQueryProcessing();
368: }
369:
370: @Override
371: public boolean useTransactionalOntologyForQueryProcessing() {
372: return getCurrentPersistenceContext().useTransactionalOntologyForQueryProcessing();
373: }
374:
375: @Override
376: public void setUseBackupOntologyForQueryProcessing() {
377: getCurrentPersistenceContext().setUseBackupOntologyForQueryProcessing();
378: }
379:
380: @Override
381: public boolean useBackupOntologyForQueryProcessing() {
382: return getCurrentPersistenceContext().useBackupOntologyForQueryProcessing();
383: }
384:
385: public <T> T unwrap(Class<T> cls) {
386:• if (cls.equals(this.getClass())) {
387: return cls.cast(this);
388: }
389:
390: throw new OWLPersistenceException();
391: }
392:
393: public Object getDelegate() {
394: return unwrap(EntityManagerImpl.class);
395: }
396:
397: private void ensureOpen() {
398:• if (!isOpen()) {
399: throw new OWLPersistenceException("The entity manager is closed !");
400: }
401: }
402:
403: private State getState(Object entity) {
404: return getCurrentPersistenceContext().getState(entity);
405: }
406:
407: private State getState(Object entity, Descriptor descriptor) {
408: return getCurrentPersistenceContext().getState(entity, descriptor);
409: }
410:
411: @Override
412: protected void finalize() throws Throwable {
413:• if (isOpen()) {
414: close();
415: }
416: super.finalize();
417: }
418:
419: public UnitOfWorkImpl getCurrentPersistenceContext() {
420:• if (this.persistenceContext == null) {
421: this.persistenceContext = (UnitOfWorkImpl) this.serverSession.acquireUnitOfWork();
422: persistenceContext.setEntityManager(this);
423: }
424: return this.persistenceContext;
425: }
426:
427: /**
428: * Called from EntityTransaction in case of a rollback. Releasing the UoW is up to the EntityTransaction.
429: */
430: public void removeCurrentPersistenceContext() {
431:• if (persistenceContext != null && persistenceContext.isActive()) {
432: persistenceContext.release();
433: }
434: this.persistenceContext = null;
435: }
436:
437: public void transactionStarted(EntityTransaction t) {
438: this.serverSession.transactionStarted(t, this);
439: }
440:
441: public void transactionFinished(EntityTransaction t) {
442: this.serverSession.transactionFinished(t);
443: }
444:
445: /**
446: * Since we support only EntityTransactions, we set the TransactionWrapper to EntityTransactionWrapper. In the
447: * future, if JTA transactions are supported, JTATransactionWrapper should be set instead of the
448: * EntityTransactionWrapper.
449: */
450: private void setTransactionWrapper() {
451: this.transaction = new EntityTransactionWrapper(this);
452: }
453:
454: @Override
455: public Configuration getConfiguration() {
456: return configuration;
457: }
458: }