Skip to main content
summaryrefslogtreecommitdiffstats
blob: bc173483f39fe6a34a147521460dc2333cd73a96 (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
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
/**
 * <copyright>
 *
 * Copyright (c) 2005, 2006, 2007 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: FeatureMapWrapper.java,v 1.4 2007/02/01 12:36:35 mtaal Exp $
 */

package org.eclipse.emf.teneo.jpox.elist;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import javax.jdo.JDOHelper;
import javax.jdo.spi.PersistenceCapable;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
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.ecore.util.FeatureMap;
import org.eclipse.emf.teneo.jpox.JpoxStoreException;
import org.eclipse.emf.teneo.jpox.JpoxUtil;
import org.eclipse.emf.teneo.jpox.resource.JPOXResource;
import org.eclipse.emf.teneo.mapping.elist.PersistableEList;
import org.eclipse.emf.teneo.mapping.elist.PersistableFeatureMap;
import org.eclipse.emf.teneo.resource.StoreResource;
import org.eclipse.emf.teneo.type.FeatureMapEntry;
import org.eclipse.emf.teneo.util.AssertUtil;
import org.eclipse.emf.teneo.util.StoreUtil;
import org.jpox.StateManager;
import org.jpox.sco.SCO;
import org.jpox.sco.SCOList;
import org.jpox.sco.exceptions.QueryUnownedSCOException;
import org.jpox.state.FetchPlanState;
import org.jpox.store.expression.QueryExpression;
import org.jpox.store.query.Queryable;
import org.jpox.store.query.ResultObjectFactory;
import org.jpox.util.ClassUtils;

/**
 * This class works as a wrapper around the EMF FeatureMap. This is required because the EMF - JPOX implementation can
 * not work with the standard FeatureMap.Entry it requires specific implementations which are generated by the EMF OR
 * Mapper.
 * 
 * A special featuremap entry has been developed: AnyFeatureMapEntry. This special feature map entry is used to handle
 * the xsd:any schema construction.
 * 
 * For the backing store a jpox arraylist is used. The delegate list is actually a normal arraylist, it was not possible
 * to use the backingstore as the delegate because the list can be detached.
 * 
 * @author <a href="mailto:mtaal@elver.org">Martin Taal</a>
 * @version $Revision: 1.4 $ $Date: 2007/02/01 12:36:35 $
 */

public class FeatureMapWrapper extends PersistableFeatureMap implements SCO, Queryable, SCOList {
	/** The logger */
	private static Log log = LogFactory.getLog(FeatureMapWrapper.class);

	/** The statemanager used to get to all the jdo information */
	private StateManager stateManager = null;

	/** The fieldname for which this feature map works */
	private String fieldName = null;

	/** The backing store of this elist */
	private JPOXArrayList jdoDelegate;

	/**
	 * An ArrayList of deletedobjects, is set by the didRemove method and used by the remove methods at the end
	 */
	private final ArrayList deletedObjects = new ArrayList();

	private final ArrayList deletedObjectsIndex = new ArrayList();

	/**
	 * Constructor, using the StateManager of the "owner" and the field name.
	 * 
	 * @param ownerSM
	 *            The owner StateManager
	 * @param fieldName
	 *            The name of the field of the SCO.
	 */
	public FeatureMapWrapper(StateManager ownerSM, String fieldName) {
		this(ownerSM, fieldName, new ArrayList());
	}

	/**
	 * Constructor, using the StateManager of the "owner" and the field name.
	 * 
	 * @param ownerSM
	 *            The owner StateManager
	 * @param fieldName
	 *            The name of the field of the SCO.
	 */
	public FeatureMapWrapper(StateManager ownerSM, String featureFieldName, List list) {
		super((InternalEObject) ownerSM.getObject(), StoreUtil.getEStructuralFeature((InternalEObject) ownerSM
				.getObject(), featureFieldName), list);

		stateManager = ownerSM;
		fieldName = featureFieldName;
		jdoDelegate = new JPOXArrayList(ownerSM, featureFieldName);

		log.debug("Creating feature map wrapper for " + getElementType().getName());

		// if already loaded then load the content of the jdo delegate to here
		if (jdoDelegate.isLoaded()) {
			load();
		}
	}

	/** Returns the element type to be used */
	protected Class determineElementType() {
		// determines which class should be used as the element type
		if (StoreUtil.isWildCard(getEStructuralFeature())) {
			return AnyFeatureMapEntry.class;
		} else {
			return GenericFeatureMapEntry.class; // FeatureMapEntry.getEntryClass(owner.getClass(),
			// getEStructuralFeature().getName());
		}
	}

	/** Nullify the delegate and stateManager before serializing */
	private void writeObject(java.io.ObjectOutputStream out) throws IOException {
		jdoDelegate = null;
		stateManager = null;
		out.defaultWriteObject();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.emf.teneo.mapping.elist.PersistableFeatureMap#isPersistencyWrapped()
	 */
	public boolean isPersistencyWrapped() {
		return true;
	}

	/** Is the owner detached */
	private boolean isOwnerDetached() {
		final PersistenceCapable pc = (PersistenceCapable) getEObject();
		return pc.jdoIsDetached();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.emf.teneo.mapping.elist.PersistableFeatureMap#isPersistencyWrapped()
	 */
	public Object clone() {
		throw new UnsupportedOperationException("Not yet supported for featuremap");
	}

	// ----------------------- Extra methods
	// ----------------------------------------

	/**
	 * Checks if a certain feature is containment and if so removes the object from the store
	 */
	protected void deleteDependent(EStructuralFeature feature, Object value) {
		if (isOwnerDetached())
			return; // do not do this when detached, this done at attach

		if (feature instanceof EReference) {
			final EReference eref = (EReference) feature;
			if (eref.isContainment() && value instanceof PersistenceCapable) {
				PersistenceCapable pc = (PersistenceCapable) owner;
				pc.jdoGetPersistenceManager().deletePersistent(value);
			}
		}
	}

	// ----------------------- Implementation of JPOXEList methods
	// -------------------

	/** Detach self, means nullify all references to jdo */
	public void detachSelf() {
		// and detach ourselves also
		jdoDelegate = null;
		stateManager = null;
	}

	// ----------------------- Implementation of SCO methods -------------------

	/**
	 * Utility to mark the object as dirty
	 */
	public void makeDirty() {
		if (jdoDelegate != null)
			jdoDelegate.makeDirty();
	}

	/** Does the actual loading from the store in a synchronized manner */
	protected synchronized void doLoad() {
		AssertUtil.assertTrue("EList " + getLogString() + " is already loaded", !isLoaded());

		final Resource res = getEObject().eResource();
		final boolean setLoading = res != null && res instanceof JPOXResource && !((StoreResource) res).isLoading();
		if (setLoading) {
			((StoreResource) res).setIsLoading(true);
		}

		// note add directly to the delegate to prevent infinite looping and
		// notifications
		final List list = getDelegate();
		Iterator iter = jdoDelegate.iterator();
		while (iter.hasNext()) {
			final Object child = iter.next();
			assert (getElementType().isInstance(child));
			list.add(child);

			// also set the container
			// TODO check if the content of the featuremap entry should also
			// not be
			// added to the resource explicitly, is maybe already done
			// correctly
			// in the setContainer method
			((FeatureMapEntry) child).setContainer((InternalEObject) owner);
		}

		log.debug("Loaded " + list.size() + " objects from the backing store for elist " + getLogString());
		if (setLoading) {
			((JPOXResource) res).setIsLoading(false);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jpox.sco.SCO#attachCopy(java.lang.Object, boolean)
	 */
	public void attachCopy(Object value) {
		if (!((PersistableEList) value).isLoaded())
			return;

		load();

		java.util.Collection c = (java.util.Collection) value;

		// Attach all of the elements in the new list
		ArrayList attachedElements = new ArrayList(c.size());
		Iterator iter = c.iterator();
		while (iter.hasNext()) {
			Object detachedElement = iter.next();
			if (ClassUtils.isPersistenceCapable(detachedElement)) {
				attachedElements.add(stateManager.getPersistenceManager().attachCopy(detachedElement, true));
			} else {
				// should this ever happen?
				throw new JpoxStoreException("A FeatureMap may only contain persistable objects, this object "
						+ detachedElement.getClass().getName() + " is not persistable.");
				// attachedElements.add(detachedElement);
			}
		}

		log.debug("Attaching " + attachedElements.size() + " objects to " + getLogString());
		// NOTE: needed to pass the jdodelegate because the update actions
		// should go directly to the
		// underlying datastore and not through this list. Tests showed that if
		// the updates where
		// done directly in this list that in case of bidirectional reatlions
		// elements which had
		// been moved from one list to another ended up with empty relations
		// because the remove
		// action in the updateListWithListElements cleared the one side of the
		// bidirectional case.
		JpoxUtil.updateListWithListElements(jdoDelegate, attachedElements);
	}

	/**
	 * Method to detach the elements of this list.
	 */
	public void detach(FetchPlanState state) {
		load();
		jdoDelegate.detach(state);

		final Object[] values = toArray();
		for (int i = 0; i < values.length; i++) {
			if (values[i] != null && values[i] instanceof PersistenceCapable) {
				stateManager.getPersistenceManager().detachInternal(values[i], state);
			}
		}
		stateManager = null;
		jdoDelegate = null;
	}

	/**
	 * Method to make transient all elements of this list
	 */
	public void makeTransient(FetchPlanState state) {
		final Object[] values = toArray();
		for (int i = 0; i < values.length; i++) {
			if (values[i] != null && values[i] instanceof PersistenceCapable) {
				stateManager.getPersistenceManager().findStateManager((PersistenceCapable)values[i]).makeTransient(state);
			}
		}
		stateManager = null;
		jdoDelegate = null;
	}

	/**
	 * Method to return a detached copy of the list
	 */
	public Object detachCopy(FetchPlanState state) {
		load();
		jdoDelegate.detachCopy(state);

		final ArrayList detached = new ArrayList();
		final Object[] values = toArray();
		for (int i = 0; i < values.length; i++) {
			if (values[i] == null) {
				detached.add(null);
			} else {
				Object object = values[i];
				if (object instanceof PersistenceCapable) {
					detached.add(stateManager.getPersistenceManager().detachCopyInternal(object, state));
				} else {
					detached.add(object);
				}
			}
		}
		return detached;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jpox.sco.SCO#getFieldName()
	 */
	public String getFieldName() {
		return fieldName;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jpox.sco.SCO#getOwner()
	 */
	public Object getOwner() {
		return getEObject();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jpox.sco.SCO#setValueFrom(java.lang.Object)
	 */
	public void setValueFrom(Object obj) throws ClassCastException {
		Collection c = (Collection) obj;
		clear();
		addAll(c);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jpox.sco.SCO#setValueFrom(java.lang.Object, boolean)
	 */
	public void setValueFrom(Object arg0, boolean arg1) throws ClassCastException {
		final Collection c = (Collection) arg0;
		if (arg1) {
			clear();
			addAll(c);
		} else {
			getDelegate().clear();
			getDelegate().addAll(c);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jpox.sco.SCO#unsetOwner()
	 */
	public void unsetOwner() {
		log.debug("Unsetting owner of " + getLogString());

		if (jdoDelegate != null) {
			jdoDelegate = null;
			stateManager = null;
			fieldName = null;
		}
	}

	/**
	 * Method to run reachability on this SCO.
	 * 
	 * @param reachables
	 *            List of StateManagers reachable so far
	 */
	public void runReachability(java.util.Set reachables) {
		if (jdoDelegate != null) {
			jdoDelegate.runReachability(reachables);
		} else {
			Object[] values = toArray();
			for (int i = 0; i < values.length; i++) {
				if (values[i] != null && values[i] instanceof PersistenceCapable) {
					stateManager.getPersistenceManager().findStateManager((PersistenceCapable) values[i])
							.runReachability(reachables);
				}
			}
		}
	}

	/** Set method for SCO.set */
	public Object set(int arg0, Object arg1, boolean arg2) {
		return set(arg0, arg1);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jpox.sco.SCOCollection#updateEmbeddedElement(java.lang.Object, int, java.lang.Object)
	 */
	public void updateEmbeddedElement(Object element, int fieldNumber, Object value) {
		if (jdoDelegate == null) {
			throw new JpoxStoreException("List is detached can not update embedded element, " + getLogString());
		}

		jdoDelegate.updateEmbeddedElement(element, fieldNumber, value);
	}

	// ------------------------ Query Statement methods ------------------------

	/**
	 * Method to generate a QueryStatement for the SCO.
	 * 
	 * @return The QueryStatement
	 */
	public synchronized QueryExpression newQueryStatement() {
		if (jdoDelegate == null) {
			throw new QueryUnownedSCOException();
		}

		return jdoDelegate.newQueryStatement();
	}

	/**
	 * Method to return a QueryStatement, using the specified candidate class.
	 * 
	 * @param candidate_class
	 *            The candidate class
	 * @return The QueryStatement
	 */
	public synchronized QueryExpression newQueryStatement(Class candidate_class) {
		if (jdoDelegate == null) {
			throw new QueryUnownedSCOException();
		}

		return jdoDelegate.newQueryStatement(candidate_class);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jpox.store.query.Queryable#newResultObjectFactory(org.jpox.store.expression.QueryExpression, boolean,
	 *      java.lang.Class, boolean)
	 */
    public synchronized ResultObjectFactory newResultObjectFactory(
            QueryExpression stmt, boolean ignoreCache, Class resultClass, boolean useFetchPlan) {
		if (jdoDelegate == null) {
			throw new QueryUnownedSCOException();
		}

		return jdoDelegate.newResultObjectFactory(stmt, ignoreCache, resultClass, useFetchPlan);
	}

	// -------------------------- did Methods which are called after add,
	// remove, move, set and change ----------
	// The did... method check some invariants and update the backing store

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.emf.common.util.BasicEList#didChange()
	 */
	protected void didChange() {
		if (!isLoaded())
			return; // in load state so no change
		makeDirty();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.emf.common.util.BasicEList#didAdd(int, java.lang.Object)
	 */
	protected void didAdd(int index, Object newObject) {
		assert (newObject instanceof FeatureMapEntry);

		if (!isLoaded())
			return; // this can only happen when we are loading so do not add to
		// the store

		if (jdoDelegate != null) {
			// now there is an id!
			if (newObject instanceof AnyFeatureMapEntry) {
				final Object value = ((FeatureMapEntry) newObject).getValue();
				if (value instanceof PersistenceCapable) {
					if (((PersistenceCapable) value).jdoGetObjectId() == null) // to
					// persist,
					// do
					// it
					// now
					{
						stateManager.getPersistenceManager().makePersistent(value);
						// set field again!
						((AnyFeatureMapEntry) newObject).initializeSpecificImplementation();
					}
				}
			}

			jdoDelegate.add(index, newObject);
		}

		super.didAdd(index, newObject);
	}

	/**
	 * Updates the backing store and deletes dependent objects if required
	 * 
	 * @see org.eclipse.emf.common.util.BasicEList#didClear(int, java.lang.Object[])
	 */
	protected void didClear(int size, Object[] oldObjects) {
		if (oldObjects == null)
			return;

		if (jdoDelegate != null) {
			jdoDelegate.clear();

			/*
			 * for (int i = 0; i < oldObjects.length; i++) { if (oldObjects[i] == null) continue; assert(oldObjects[i]
			 * instanceof FeatureMapEntry); // get the values before the actual remove final FeatureMapEntry entry =
			 * (FeatureMapEntry)oldObjects[i]; final EStructuralFeature feature = entry.getEStructuralFeature(); final
			 * Object value = entry.getValue();
			 * 
			 * if (ret && value instanceof PersistenceCapable) { deleteDependent(feature, value); } }
			 */
		}
		super.didClear(size, oldObjects);
	}

	/**
	 * Updates the backing store.
	 * 
	 * @see org.eclipse.emf.common.util.BasicEList#didMove(int, java.lang.Object, int)
	 */
	protected void didMove(int index, Object movedObject, int oldIndex) {
		assert (movedObject instanceof FeatureMapEntry);
		if (jdoDelegate != null) {
			final Object newObject = createEntry(((FeatureMapEntry) movedObject).getEStructuralFeature(),
					((FeatureMapEntry) movedObject).getValue());

			jdoDelegate.remove(oldIndex);

			jdoDelegate.add(index, newObject);
			assert (JDOHelper.isPersistent(newObject));
		}
		super.didMove(index, movedObject, oldIndex);
	}

	/**
	 * Adds the oldObject to the deletedObjects arraylist. The objects are actually deleted from the backing store at
	 * the end of the remove, removeAll methods. The delete from the store can not be done here because after the
	 * didRemove method the objects are still accessed to do inverseremove.
	 * 
	 * @see org.eclipse.emf.common.util.BasicEList#didRemove(int, java.lang.Object)
	 */
	protected void didRemove(int index, Object oldObject) {
		assert (oldObject instanceof FeatureMapEntry);
		assert (!deletedObjects.contains(oldObject));
		deletedObjects.add(oldObject);
		deletedObjectsIndex.add(new Integer(index));
		super.didRemove(index, oldObject);
	}

	/**
	 * Performs the actual remove is called at the end of each remove method below
	 */
	private synchronized void doRemove() {
		if (deletedObjects.size() == 0)
			return;

		if (jdoDelegate == null) {
			deletedObjects.clear();
			deletedObjectsIndex.clear();
			return;
		}

		try {
			for (int i = 0; i < deletedObjects.size(); i++) {
				final int index = ((Integer) deletedObjectsIndex.get(i)).intValue();
				final Object oldObject = deletedObjects.get(i);

				// get the values before the actual remove
				final FeatureMapEntry entry = (FeatureMapEntry) oldObject;
				final EStructuralFeature feature = entry.getEStructuralFeature();
				final Object value = entry.getValue();

				jdoDelegate.remove(index);

				deleteDependent(feature, value);
			}
		} finally {
			deletedObjects.clear();
			deletedObjectsIndex.clear();
		}
	}

	/*
	 * Updates the backing store
	 * 
	 * @see org.eclipse.emf.common.util.BasicEList#didSet(int, java.lang.Object, java.lang.Object)
	 */
	protected void didSet(int index, Object newObject, Object oldObject) {
		assert (newObject instanceof FeatureMapEntry);
		assert (oldObject instanceof FeatureMapEntry);

		if (jdoDelegate != null) {
			final Object obj = jdoDelegate.set(index, newObject);
			assert (obj == oldObject);
			if (oldObject != newObject) {
				final FeatureMapEntry entry = (FeatureMapEntry) oldObject;
				final EStructuralFeature feature = entry.getEStructuralFeature();
				final Object value = entry.getValue();

				// first delete the entry itself
				// PersistenceCapable pc = (PersistenceCapable) owner;
				// pc.jdoGetPersistenceManager().deletePersistent(entry);
				jdoDelegate.remove(entry);

				// and the dependent entry
				deleteDependent(feature, value);
			}
		}

		super.didSet(index, newObject, oldObject);
	}

	/** Creates an entry with the correct type */
	public FeatureMap.Entry createEntry(EStructuralFeature feature, Object value) {
		try {
			FeatureMapEntry entry = (FeatureMapEntry) getElementType().newInstance();
			entry.setFields(feature, value);
			return entry;
		} catch (Exception e) {
			throw createException("Exception while instantiating for elementClass " + getElementType().getName(), e);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.emf.ecore.util.BasicFeatureMap#clear(org.eclipse.emf.ecore.EStructuralFeature)
	 */
	public void clear(EStructuralFeature feature) {
		load();
		try {
			assert (deletedObjects.size() == 0);
			super.clear(feature);
		} finally {
			doRemove();
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.emf.ecore.util.BasicFeatureMap#remove(org.eclipse.emf.ecore.EStructuralFeature, int)
	 */
	public synchronized Object remove(EStructuralFeature feature, int index) {
		load();
		try {
			assert (deletedObjects.size() == 0);
			return super.remove(feature, index);
		} finally {
			doRemove();
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.emf.ecore.util.BasicFeatureMap#remove(org.eclipse.emf.ecore.EStructuralFeature,
	 *      java.lang.Object)
	 */
	public synchronized boolean remove(EStructuralFeature feature, Object object) {
		load();
		try {
			assert (deletedObjects.size() == 0);
			return super.remove(feature, object);
		} finally {
			doRemove();
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.emf.ecore.util.BasicFeatureMap#removeAll(org.eclipse.emf.ecore.EStructuralFeature,
	 *      java.util.Collection)
	 */
	public synchronized boolean removeAll(EStructuralFeature feature, Collection collection) {
		load();
		try {
			assert (deletedObjects.size() == 0);
			return super.removeAll(feature, collection);
		} finally {
			doRemove();
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.emf.common.notify.impl.NotifyingListImpl#clear()
	 */
	public void clear() {
		load();
		// doRemove does not need to be called because the jdoDelegate is cleared in didClear
		super.clear();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.emf.common.notify.impl.NotifyingListImpl#remove(int)
	 */
	public synchronized Object remove(int index) {
		load();
		try {
			assert (deletedObjects.size() == 0);
			return super.remove(index);
		} finally {
			doRemove();
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.emf.common.notify.impl.NotifyingListImpl#removeAll(java.util.Collection)
	 */
	public synchronized boolean removeAll(Collection collection) {
		load();
		try {
			assert (deletedObjects.size() == 0);
			return super.removeAll(collection);
		} finally {
			doRemove();
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.emf.common.util.BasicEList#remove(java.lang.Object)
	 */
	public synchronized boolean remove(Object object) {
		load();
		try {
			assert (deletedObjects.size() == 0);
			return super.remove(object);
		} finally {
			doRemove();
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.util.AbstractList#removeRange(int, int)
	 */
	protected synchronized void removeRange(int arg0, int arg1) {
		load();
		try {
			assert (deletedObjects.size() == 0);
			super.removeRange(arg0, arg1);
		} finally {
			doRemove();
		}
	}
	
	/**
	 * Overridden to make public
	 */
	public void load() {
		// TODO Auto-generated method stub
		super.load();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jpox.sco.SCOContainer#loadFieldsInFetchPlan(org.jpox.state.FetchPlanState)
	 */
	public void loadFieldsInFetchPlan(FetchPlanState arg0) {
		Object[] values = toArray();
		for (int i = 0; i < values.length; i++) {
			if (values[i] != null && values[i] instanceof PersistenceCapable) {
				stateManager.getPersistenceManager().findStateManager((PersistenceCapable) values[i])
						.loadFieldsInFetchPlan(arg0);
			}
		}
	}
}

Back to the top