Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: c91c580b83285da60c7f21652358164a882bb832 (plain) (blame)
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
/**
 * <copyright>
 *
 * Copyright (c) 2005, 2006, 2007, 2008 Springsite BV (The Netherlands) and others
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *   Martin Taal
 * </copyright>
 *
 * $Id: HibernatePersistableEList.java,v 1.35 2010/03/25 01:05:53 mtaal Exp $
 */

package org.eclipse.emf.teneo.hibernate.mapping.elist;

import java.io.Serializable;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EReference;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.ecore.InternalEObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.teneo.EContainerRepairControl;
import org.eclipse.emf.teneo.extension.ExtensionPoint;
import org.eclipse.emf.teneo.hibernate.HbMapperException;
import org.eclipse.emf.teneo.hibernate.SessionWrapper;
import org.eclipse.emf.teneo.hibernate.resource.HbResource;
import org.eclipse.emf.teneo.mapping.elist.PersistableEList;
import org.eclipse.emf.teneo.resource.StoreResource;
import org.eclipse.emf.teneo.util.AssertUtil;
import org.hibernate.HibernateException;
import org.hibernate.collection.internal.AbstractPersistentCollection;
import org.hibernate.collection.internal.PersistentBag;
import org.hibernate.collection.internal.PersistentIdentifierBag;
import org.hibernate.collection.internal.PersistentList;
import org.hibernate.collection.spi.PersistentCollection;
import org.hibernate.engine.spi.SessionImplementor;
import org.hibernate.loader.CollectionAliases;
import org.hibernate.persister.collection.CollectionPersister;
import org.hibernate.type.Type;

/**
 * Implements the hibernate persistable elist.
 * 
 * @author <a href="mailto:mtaal@elver.org">Martin Taal</a>
 * @version $Revision: 1.35 $
 */

public class HibernatePersistableEList<E> extends PersistableEList<E> implements ExtensionPoint,
		PersistentCollection {
	/**
	 * Serial Version ID
	 */
	private static final long serialVersionUID = -4553160393592497834L;
	/** The logger */
	private static Log log = LogFactory.getLog(HibernatePersistableEList.class);

	/** Constructor */
	public HibernatePersistableEList(InternalEObject owner, EStructuralFeature feature, List<E> list) {
		super(owner, feature, list);
	}

	/** If the delegate has been initialized */
	public boolean isInitialized() {
		return ((PersistentCollection) delegate).wasInitialized();
	}

	/**
	 * Override isLoaded to check if the delegate lists was not already loaded by hibernate behind the
	 * scenes, this happens with eagerly loaded lists.
	 */
	@Override
	public boolean isLoaded() {
		// if the delegated list was loaded under the hood and this
		// HibernatePersistableEList did
		// not yet notice it then do the local load behavior.
		// delegate is loaded in case of subselect or eager loading
		final boolean isDelegateLoaded = delegate instanceof AbstractPersistentCollection
				&& ((AbstractPersistentCollection) delegate).wasInitialized();
		if (!super.isLoaded() && !isLoading() && isDelegateLoaded) {
			if (log.isDebugEnabled()) {
				log.debug("Persistentlist already initialized, probably eagerly loaded: " + getLogString());
			}
			try {
				setIsLoading(true);
				// do load to load the resource
				doLoad();
				setIsLoaded(true);
			} finally {
				setIsLoading(false);
			}
		}
		return super.isLoaded();
	}

	/** Do the actual load can be overridden */
	@Override
	protected synchronized void doLoad() {
		// TODO, read the following link and reconsider transaction usage
		// http://community.jboss.org/wiki/Non-transactionaldataaccessandtheauto-commitmode

		AssertUtil.assertTrue("EList " + logString, !isLoaded());

		if (log.isDebugEnabled()) {
			log.debug("Started loading elist " + logString);
		}

		SessionWrapper sessionWrapper = null;
		boolean controlsTransaction = false;
		boolean err = true;
		Resource res = null;
		try {
			res = owner.eResource();
			if (res != null && res instanceof HbResource) {
				sessionWrapper = ((HbResource) res).getSessionWrapper();
				if (res.isLoaded()) // resource is loaded reopen transaction
				{
					// if the delegate is already loaded then no transaction is
					// required
					final boolean isDelegateLoaded = delegate instanceof AbstractPersistentCollection
							&& ((AbstractPersistentCollection) delegate).wasInitialized();
					if (!isDelegateLoaded && !sessionWrapper.isTransactionActive()) {
						log.debug("Reconnecting session to read a lazy collection, elist: " + logString);
						controlsTransaction = true;
						sessionWrapper.beginTransaction();
						sessionWrapper.setFlushModeManual();
					} else {
						log.debug("Delegate loaded or resource session is still active, using it");
					}
				} else if (log.isDebugEnabled()) {
					log.debug("Elist uses session from resource, " + logString);
				}
			} else if (log.isDebugEnabled()) {
				log.debug("EList is not loaded in session context");
			}

			if (controlsTransaction) {
				assert (res instanceof HbResource);
				((StoreResource) res).setIsLoading(true);
			}

			try {
				Object[] objs = delegate.toArray(); // this forces the load

				// disabled for now as containers are persisted by hibernate
				// anyway
				if (isContainment()) {
					for (Object element : objs) {
						if (element instanceof InternalEObject) {
							EContainerRepairControl.setContainer(owner, (InternalEObject) element,
									getEStructuralFeature());
						}
					}
				}

				// add the new objects to the resource so they are tracked
				if (res != null && res instanceof StoreResource) {
					// attach the new contained objects so that they are adapted
					// when required
					for (Object element : objs) {
						if (element instanceof EObject) {
							((StoreResource) res).addToContentOrAttach((InternalEObject) element,
									(EReference) getEStructuralFeature());
						}
					}
				}

				if (log.isDebugEnabled()) {
					log.debug("Loaded " + objs.length + " from backend store for " + logString);
				}
			} finally {
				if (controlsTransaction) {
					((StoreResource) res).setIsLoading(false);
				}
			}
			err = false;
		} finally {
			if (controlsTransaction) {
				if (err) {
					sessionWrapper.rollbackTransaction();
					sessionWrapper.restorePreviousFlushMode();
				} else {
					// a bit rough but delete from the persitence context
					// otherwise
					// hibernate will think that this collection is not attached
					// to anything and
					// will delete me
					// getSession().getPersistenceContext().getCollectionEntries().remove(this);
					sessionWrapper.commitTransaction();
					sessionWrapper.restorePreviousFlushMode();
				}
			}
			if (sessionWrapper != null) {
				((HbResource) res).returnSessionWrapper(sessionWrapper);
			}
		}
		if (log.isDebugEnabled()) {
			log.debug("Finished loading elist " + logString);
		}
	}

	/** Overridden because general list type is not supported as a replacement */
	@Override
	public void replaceDelegate(List<E> newDelegate) {
		if (newDelegate instanceof PersistentList) {
			// disabled this assertion because in case of a session refresh it
			// is possible
			// that the list is replaced by a persistent list
			// AssertUtil.assertTrue("This elist " + logString + " contains a
			// different list than the " +
			// " passed list",
			// ((PersistentList)newDelegate).isWrapper(delegate));
			super.replaceDelegate(newDelegate);
		} else if (newDelegate instanceof PersistentBag) {
			// disabled this assertion because in case of a session refresh it
			// is possible
			// that the list is replaced by a persistent list
			// AssertUtil.assertTrue("This elist " + logString + " contains a
			// different list than the " +
			// " passed list",
			// ((PersistentBag)newDelegate).isWrapper(delegate));
			super.replaceDelegate(newDelegate);
		} else if (newDelegate instanceof PersistentIdentifierBag) {
			// Added to support <idbag>
			super.replaceDelegate(newDelegate);
		} else if (newDelegate == delegate) // this can occur and is okay, do
		// nothing in this case
		{

		} else {
			throw new HbMapperException("Type " + newDelegate.getClass().getName() + " can not be "
					+ " used as a replacement for elist " + logString);
		}
	}

	/** Returns true if the wrapped list is a persistency layer specific list */
	public boolean isPersistencyWrapped() {
		return delegate instanceof PersistentCollection;
	}

	public boolean afterInitialize() {
		if (isPersistencyWrapped()) {
			return ((PersistentCollection) delegate).afterInitialize();
		}
		return false;
	}

	public void afterRowInsert(CollectionPersister persister, Object entry, int i)
			throws HibernateException {
		if (isPersistencyWrapped()) {
			((PersistentCollection) delegate).afterRowInsert(persister, entry, i);
		}
	}

	public void beforeInitialize(CollectionPersister persister, int anticipatedSize) {
		if (isPersistencyWrapped()) {
			((PersistentCollection) delegate).beforeInitialize(persister, anticipatedSize);
		}
	}

	public void beginRead() {
		if (isPersistencyWrapped()) {
			((PersistentCollection) delegate).beginRead();
		}
	}

	public void clearDirty() {
		if (isPersistencyWrapped()) {
			((PersistentCollection) delegate).clearDirty();
		}
	}

	public void dirty() {
		if (isPersistencyWrapped()) {
			((PersistentCollection) delegate).dirty();
		}
	}

	public Serializable disassemble(CollectionPersister persister) throws HibernateException {
		if (isPersistencyWrapped()) {
			return ((PersistentCollection) delegate).disassemble(persister);
		}
		return null;
	}

	public boolean empty() {
		if (isPersistencyWrapped()) {
			return ((PersistentCollection) delegate).empty();
		}
		return false;
	}

	public boolean endRead() {
		if (isPersistencyWrapped()) {
			return ((PersistentCollection) delegate).endRead();
		}
		return false;
	}

	public Iterator<?> entries(CollectionPersister persister) {
		if (isPersistencyWrapped()) {
			return ((PersistentCollection) delegate).entries(persister);
		}
		return null;
	}

	public boolean entryExists(Object entry, int i) {
		if (isPersistencyWrapped()) {
			return ((PersistentCollection) delegate).entryExists(entry, i);
		}
		return false;
	}

	public boolean equalsSnapshot(CollectionPersister persister) throws HibernateException {
		if (isPersistencyWrapped()) {
			return ((PersistentCollection) delegate).equalsSnapshot(persister);
		}
		return false;
	}

	public void forceInitialization() throws HibernateException {
		if (isPersistencyWrapped()) {
			((PersistentCollection) delegate).forceInitialization();
		}
	}

	public Iterator<?> getDeletes(CollectionPersister persister, boolean indexIsFormula)
			throws HibernateException {
		if (isPersistencyWrapped()) {
			return ((PersistentCollection) delegate).getDeletes(persister, indexIsFormula);
		}
		return null;
	}

	public Object getElement(Object entry) {
		if (isPersistencyWrapped()) {
			return ((PersistentCollection) delegate).getElement(entry);
		}
		return null;
	}

	public Object getIdentifier(Object entry, int i) {
		if (isPersistencyWrapped()) {
			return ((PersistentCollection) delegate).getIdentifier(entry, i);
		}
		return null;
	}

	public Object getIndex(Object entry, int i, CollectionPersister persister) {
		if (isPersistencyWrapped()) {
			return ((PersistentCollection) delegate).getIndex(entry, i, persister);
		}
		return null;
	}

	public Serializable getKey() {
		if (isPersistencyWrapped()) {
			return ((PersistentCollection) delegate).getKey();
		}
		return null;
	}

	public Collection<?> getOrphans(Serializable snapshot, String entityName)
			throws HibernateException {
		if (isPersistencyWrapped()) {
			return ((PersistentCollection) delegate).getOrphans(snapshot, entityName);
		}
		return null;
	}

	public Object getOwner() {
		if (isPersistencyWrapped()) {
			return ((PersistentCollection) delegate).getOwner();
		}
		return getEObject();
	}

	public Collection<?> getQueuedOrphans(String entityName) {
		if (isPersistencyWrapped()) {
			return ((PersistentCollection) delegate).getQueuedOrphans(entityName);
		}
		return null;
	}

	public String getRole() {
		if (isPersistencyWrapped()) {
			return ((PersistentCollection) delegate).getRole();
		}
		return null;
	}

	public Serializable getSnapshot(CollectionPersister persister) throws HibernateException {
		if (isPersistencyWrapped()) {
			return ((PersistentCollection) delegate).getSnapshot(persister);
		}
		return null;
	}

	public Object getSnapshotElement(Object entry, int i) {
		if (isPersistencyWrapped()) {
			return ((PersistentCollection) delegate).getSnapshotElement(entry, i);
		}
		return null;
	}

	public Serializable getStoredSnapshot() {
		if (isPersistencyWrapped()) {
			return ((PersistentCollection) delegate).getStoredSnapshot();
		}
		return null;
	}

	public Object getValue() {
		if (isPersistencyWrapped()) {
			return ((PersistentCollection) delegate).getValue();
		}
		return null;
	}

	public boolean hasQueuedOperations() {
		if (isPersistencyWrapped()) {
			return ((PersistentCollection) delegate).hasQueuedOperations();
		}
		return false;
	}

	public void initializeFromCache(CollectionPersister persister, Serializable disassembled,
			Object owner) throws HibernateException {
		if (isPersistencyWrapped()) {
			((PersistentCollection) delegate).initializeFromCache(persister, disassembled, owner);
		}
	}

	public boolean isDirectlyAccessible() {
		if (isPersistencyWrapped()) {
			return ((PersistentCollection) delegate).isDirectlyAccessible();
		}
		return false;
	}

	public boolean isDirty() {
		if (isPersistencyWrapped()) {
			return ((PersistentCollection) delegate).isDirty();
		}
		return false;
	}

	public boolean isRowUpdatePossible() {
		if (isPersistencyWrapped()) {
			return ((PersistentCollection) delegate).isRowUpdatePossible();
		}
		return false;
	}

	public boolean isSnapshotEmpty(Serializable snapshot) {
		if (isPersistencyWrapped()) {
			return ((PersistentCollection) delegate).isSnapshotEmpty(snapshot);
		}
		return false;
	}

	public boolean isUnreferenced() {
		if (isPersistencyWrapped()) {
			return ((PersistentCollection) delegate).isUnreferenced();
		}
		return false;
	}

	public boolean isWrapper(Object collection) {
		if (isPersistencyWrapped()) {
			return ((PersistentCollection) delegate).isWrapper(collection);
		}
		return false;
	}

	public boolean needsInserting(Object entry, int i, Type elemType) throws HibernateException {
		if (isPersistencyWrapped()) {
			return ((PersistentCollection) delegate).needsInserting(entry, i, elemType);
		}
		return false;
	}

	public boolean needsRecreate(CollectionPersister persister) {
		if (isPersistencyWrapped()) {
			return ((PersistentCollection) delegate).needsRecreate(persister);
		}
		return false;
	}

	public boolean needsUpdating(Object entry, int i, Type elemType) throws HibernateException {
		if (isPersistencyWrapped()) {
			return ((PersistentCollection) delegate).needsUpdating(entry, i, elemType);
		}
		return false;
	}

	public void postAction() {
		if (isPersistencyWrapped()) {
			((PersistentCollection) delegate).postAction();
		}
	}

	@Override
	protected int delegateSize() {
		if (delegate instanceof AbstractPersistentCollection && !isInitialized() && !isLoaded()) {
			return delegate.size();
		}
		return delegateList().size();
	}

	protected final boolean isConnectedToSession() {
		if (!(getDelegate() instanceof AbstractPersistentCollection)) {
			return false;
		}
		final AbstractPersistentCollection persistentCollection = (AbstractPersistentCollection) getDelegate();
		final SessionImplementor session = ((AbstractPersistentCollection) persistentCollection)
				.getSession();
		return isConnectedToSession(session);
	}

	private final boolean isConnectedToSession(SessionImplementor session) {
		final PersistentCollection persistentCollection = (PersistentCollection) getDelegate();
		return session != null && session.isOpen()
				&& session.getPersistenceContext().containsCollection(persistentCollection);
	}

	@Override
	protected boolean delegateIsEmpty() {
		return delegateSize() == 0;
	}

	public void preInsert(CollectionPersister persister) throws HibernateException {
		if (isPersistencyWrapped()) {
			((PersistentCollection) delegate).preInsert(persister);
		}
	}

	public Iterator<?> queuedAdditionIterator() {
		if (isPersistencyWrapped()) {
			return ((PersistentCollection) delegate).queuedAdditionIterator();
		}
		return null;
	}

	public Object readFrom(ResultSet rs, CollectionPersister role, CollectionAliases descriptor,
			Object owner) throws HibernateException, SQLException {
		if (isPersistencyWrapped()) {
			return ((PersistentCollection) delegate).readFrom(rs, role, descriptor, owner);
		}
		return null;
	}

	public boolean setCurrentSession(SessionImplementor session) throws HibernateException {
		if (isPersistencyWrapped()) {
			return ((PersistentCollection) delegate).setCurrentSession(session);
		}
		return false;
	}

	public void setOwner(Object entity) {
		if (isPersistencyWrapped()) {
			((PersistentCollection) delegate).setOwner(entity);
		}
	}

	public void setSnapshot(Serializable key, String role, Serializable snapshot) {
		if (isPersistencyWrapped()) {
			((PersistentCollection) delegate).setSnapshot(key, role, snapshot);
		}
	}

	public boolean unsetSession(SessionImplementor currentSession) {
		if (isPersistencyWrapped()) {
			return ((PersistentCollection) delegate).unsetSession(currentSession);
		}
		return false;
	}

	public boolean wasInitialized() {
		if (isPersistencyWrapped()) {
			return ((PersistentCollection) delegate).wasInitialized();
		}
		return false;
	}

}

Back to the top