Skip to main content
summaryrefslogtreecommitdiffstats
blob: 2496e34b0215dc710251ca1fe8d51b881674b055 (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
/**
 * <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 - Initial API and implementation
 *
 * </copyright>
 *
 * $Id: EContainerRepairControl.java,v 1.6 2007/02/01 12:34:21 mtaal Exp $
 */

package org.eclipse.emf.teneo;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Iterator;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.eclipse.emf.common.util.EList;
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.mapping.elist.PersistableDelegateList;
import org.eclipse.emf.teneo.mapping.elist.PersistableEList;
import org.eclipse.emf.teneo.mapping.elist.PersistableFeatureMap;

/**
 * Supports the repair of the eContainer and resource setting of child objects when an object is loaded from the backing
 * store.
 * 
 * Repair of the eContainer is required in two distinct cases: 1) 1:1 relation: in this case the repair is implemented
 * in the caching mechanism. This was the correct location because in jpox an object is added to the level 1 cache just
 * after it is retrieved from the db and before it is passed on to the requesting application. 2) 1:n relation: in this
 * case the EListWrapper knows that a containment relation is being loaded and calls the equivalent methods here.
 * 
 * Note that both cases need to take into account two-way relatiofns. For two-way relations the featureid of the
 * opposing ereferencing is used. For one-way relations emf apparently works with negative featureid's.
 * 
 * This class also supports caching so that the system can quickly determine if for a certain class eContainers need to
 * be set in child objects.
 * 
 * @author <a href="mailto:mtaal@elver.org">Martin Taal</a>
 * @version $Revision: 1.6 $
 */

public class EContainerRepairControl {
	/** The logger */
	private static Log log = LogFactory.getLog(EContainerRepairControl.class);

	/** Hashmap of classes for which no repair is required */
	private static final Hashtable norepairRequired = new Hashtable();

	/** Hashmap of repair controls for a certain class */
	private static final Hashtable repair = new Hashtable();

	/**
	 * Recursively sets the resource of the object and all its referenced objects, only if the object has a resource
	 * which is not set and does not have a container.
	 */
	public static void setEResourceToAlLContent(InternalEObject start, Resource res) {
		for (Iterator it = start.eClass().getEAllStructuralFeatures().iterator(); it.hasNext();) {
			final EStructuralFeature estruct = (EStructuralFeature) it.next();
			if (estruct instanceof EReference) {
				final EReference eref = (EReference) estruct;
				if (eref.isMany()) {
					final EList list = (EList) start.eGet(eref);
					if (list == null)
						continue;
					if ((list instanceof PersistableEList) && !((PersistableEList)list).isLoaded()) {
						continue;
					}
					if ((list instanceof PersistableFeatureMap) && !((PersistableFeatureMap)list).isLoaded()) {
						continue;
					}
					for (int i = 0; i < list.size(); i++) {
						final InternalEObject child = (InternalEObject) list.get(i);
						if (child.eResource() == null) // no container
						{
							setResource(child, new ArrayList(), (Resource.Internal) res);
						}
					}
				} else {
					final InternalEObject child = (InternalEObject) start.eGet(eref);
					if (child != null && child.eResource() == null) {
						setResource(child, new ArrayList(), (Resource.Internal) res);
					}
				}
			}
		}
	}

	/** Sets the resource on an object or if it has a container on its container */
	private static void setResource(InternalEObject eobj, ArrayList objs, Resource.Internal res) {
		// been here go away
		if (objs.contains(eobj))
			return;

		// set the resource here or at the container
		if (eobj.eResource() == null) {
			if (eobj.eContainer() == null) {
				eobj.eSetResource(res, null);
			} else {
				objs.add(eobj);
				setResource((InternalEObject) eobj.eContainer(), objs, res);
			}
		}
	}

	/** Method to repair the eContainer of the child object of this object */
	public static void repair(Object owner) {
		if (log.isDebugEnabled()) {
			log.debug("Repairing container relations of children of: " + owner.getClass().getName());
		}
		
		if (!(owner instanceof InternalEObject))
			return;

		if (norepairRequired.get(owner.getClass()) != null)
			return;

		ArrayList repairList = (ArrayList) repair.get(owner.getClass());

		if (repairList == null)
			repairList = buildRepairList((InternalEObject) owner);

		if (log.isDebugEnabled() && repairList.size() > 0) {
			log.debug("Repairing container relations of children of: " + owner.getClass().getName());
		}

		for (int i = 0; i < repairList.size(); i++) {
			RepairControl repairControl = (RepairControl) repairList.get(i);
			if (log.isDebugEnabled()) {
				log.debug("Repairing reference " + repairControl.container.getName() + " to child "
						+ repairControl.childClass.getName());
			}

			repairControl.repair((InternalEObject) owner);
		}
	}

	/**
	 * Convenience method to just set the container directly for an object, this method does not cascade down. The
	 * featureid is the id of the feature of the owner which contains the child. The feature id is corrected in the
	 * method.
	 */
	public static void setContainer(InternalEObject owner, InternalEObject child, EStructuralFeature estruct) {
		final int featureID;
		if (estruct instanceof EReference && ((EReference) estruct).getEOpposite() != null) {
			featureID = ((EReference) estruct).getEOpposite().getFeatureID();
		} else {
			featureID = InternalEObject.EOPPOSITE_FEATURE_BASE - estruct.getFeatureID();
		}
		child.eBasicSetContainer(owner, featureID, null);
	}

	/**
	 * Method to repair the eContainer of the child object of this object. Note the featureid is internally translated
	 * to an econtainer id, nl. subtract from EOPPOSITE_FEATURE_BASE
	 */
	public static void repair(Object owner, Object child, EStructuralFeature estruct) {
		final int correctedFeatureID;
		if (estruct instanceof EReference && ((EReference) estruct).getEOpposite() != null) {
			correctedFeatureID = ((EReference) estruct).getEOpposite().getFeatureID();
		} else {
			correctedFeatureID = InternalEObject.EOPPOSITE_FEATURE_BASE - estruct.getFeatureID();
		}

		if (!(owner instanceof InternalEObject))
			return;
		if (!(child instanceof InternalEObject))
			return;

		if (norepairRequired.get(owner.getClass()) != null)
			return;

		ArrayList repairList = (ArrayList) repair.get(owner.getClass());

		if (repairList == null)
			repairList = buildRepairList((InternalEObject) owner);

		if (log.isDebugEnabled() && repairList.size() > 0) {
			log.debug("Repairing container relations of children of: " + owner.getClass().getName());
		}

		for (int i = 0; i < repairList.size(); i++) {
			RepairControl repairControl = (RepairControl) repairList.get(i);
			if (repairControl.getFeatureID() == correctedFeatureID
				&& repairControl.childClass.isAssignableFrom(child.getClass())) {
				repairControl.repair((InternalEObject) owner, (InternalEObject) child);
				return;
			}
		}
	}

	/** Builds a repair control list for an object */
	private static ArrayList buildRepairList(InternalEObject owner) {
		final ArrayList result = new ArrayList();
		for (Iterator it = owner.eClass().getEAllStructuralFeatures().iterator(); it.hasNext();) {
			final EStructuralFeature estruct = (EStructuralFeature) it.next();
			if (estruct instanceof EReference) {
				final EReference eref = (EReference) estruct;
				if (eref.isContainment()) {
					// now check if we are two or not
					if (eref.getEOpposite() != null) {
						result.add(new TwoWayContainer(eref, eref.getEOpposite()));
					} else {
						result.add(new OneWayContainer(eref));
					}
				}
			}
		}

		if (result.size() == 0) {
			norepairRequired.put(owner.getClass(), owner.getClass());
		} else {
			repair.put(owner.getClass(), result);
		}
		return result;
	}

	/** Abstract class for repairing containers */
	private static abstract class RepairControl {
		/** The ereference of the owner which contains the childs */
		private final EReference container;

		/** Some shortcuts for spead */
		private final Class childClass;

		/** Featureid set as container */
		private final int featureID;

		/** Constructor */
		RepairControl(EReference containerReference, int myFeatureID) {
			container = containerReference;
			childClass = container.getEType().getInstanceClass();
			featureID = myFeatureID;
		}

		/** Returns the feature id of this containment relation */
		public int getFeatureID() {
			return featureID;
		}

		/** Repairs all containers of the owner */
		void repair(InternalEObject owner) {
			// The container repair of a list is done through the repair(owner, child) method,
			// directly in the elist.doLoadFromStore method
			if (container.isMany())
				return;

			final Object containedObject = owner.eGet(container);
			if (containedObject == null) // not set
			{
				return;
			}

			// no list should be caught in the first line
			assert (!(containedObject instanceof PersistableDelegateList));

			/*
			 * if (containedObject instanceof JPOXEList) { if (((JPOXEList)containedObject).getOwner() == owner) return;
			 * 
			 * throw new StoreJPOXEmfException("Owner of containerobject is different from passed owner, " + "this
			 * should have been solved in the elist" + containedObject.getClass() + "/" + owner.getClass().getName() +
			 * "/" + container.getName()); }
			 */

			if (!(containedObject instanceof InternalEObject))
				return;

			final InternalEObject containedEObject = (InternalEObject) containedObject;
			if (containedEObject.eContainer() == owner)
				return; // already set?

			if (log.isDebugEnabled()) {
				log.debug("Set container of child " + containedObject.getClass().getName() + " containerfield "
						+ container.getName());
			}

			// and set it
			containedEObject.eBasicSetContainer(owner, featureID, null);

			// also repair the resource if applicable!
			/*
			 * if (containedObject instanceof InternalEObject) { final InternalEObject eobj =
			 * (InternalEObject)containedObject; if (eobj.eResource() != owner.eResource()) { log.debug("Set resource of
			 * eobj " + eobj.getClass().getName() + " to resource " + owner.eResource().getURI());
			 * eobj.eSetResource((Resource.Internal)owner.eResource(), null); } }
			 */

			// and also do its children
			EContainerRepairControl.repair(containedEObject);
		}

		/** Repairs all specific relation */
		void repair(InternalEObject owner, InternalEObject child) {
			if (!childClass.isAssignableFrom(child.getClass())) {
				return; // not handled by this container
			}

			if (child.eContainer() == owner)
				return; // already set?

			if (log.isDebugEnabled()) {
				log.debug("Set container of child " + child.getClass().getName() + " containerfield "
						+ container.getName());
			}

			// and set it
			child.eBasicSetContainer(owner, featureID, null);

			// also repair the resource if applicable!
			/*
			 * if (child instanceof InternalEObject) { final InternalEObject eobj = (InternalEObject)child; Object ores =
			 * owner.eResource(); Object eres = eobj.eResource(); if (eobj.eResource() != owner.eResource()) {
			 * log.debug("Set resource of eobj " + eobj.getClass().getName() + " to resource " +
			 * owner.eResource().getURI()); eobj.eSetResource((Resource.Internal)owner.eResource(), null); } }
			 */

			EContainerRepairControl.repair(child);
		}
	}

	/**
	 * Class handles a oneway container relation, in this case the eBasicSetContainer is used.
	 */
	private static class OneWayContainer extends RepairControl {
		/** Constructor */
		OneWayContainer(EReference containerReference) {
			super(containerReference, InternalEObject.EOPPOSITE_FEATURE_BASE - containerReference.getFeatureID());
		}
	}

	/**
	 * Class handles a twoway container relation, in this case the eBasicSetContainer is used.
	 */
	private static class TwoWayContainer extends RepairControl {
		/** Constructor */
		TwoWayContainer(EReference containerReference, EReference toContainer) {
			super(containerReference, toContainer.getFeatureID());
		}
	}
}

Back to the top