Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: c4f2f23e575558ba077fee90cb4bd00432958e70 (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
/*******************************************************************************
 * Copyright (c) 2011 Mia-Software
 * 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:
 * 	   Emmanuelle Rouillé (Mia-Software) - Bug 352618 - To be able to use non derived facet structural features and save them values.
 *     Nicolas Bros (Mia-Software) - Bug 361612 - New core for new version of the Facet metamodel
 *     Grégoire Dupé (Mia-Software) - Bug 361612 - [Restructuring] New core for new version of the Facet metamodel
 *******************************************************************************/
package org.eclipse.papyrus.emf.facet.efacet.core.internal.serialization;

import java.io.IOException;
import java.util.Collections;
import java.util.List;

import org.eclipse.emf.common.notify.Adapter;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EAttribute;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EReference;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.papyrus.emf.facet.efacet.core.internal.exception.SaveStructuralFeatureInstanceModelException;
import org.eclipse.papyrus.emf.facet.efacet.metamodel.v0_2_0.efacet.serialization.AbstractAttributeInstance;
import org.eclipse.papyrus.emf.facet.efacet.metamodel.v0_2_0.efacet.serialization.AbstractReferenceInstance;
import org.eclipse.papyrus.emf.facet.efacet.metamodel.v0_2_0.efacet.serialization.ExtendedEObjectReference;
import org.eclipse.papyrus.emf.facet.efacet.metamodel.v0_2_0.efacet.serialization.MultiValuedAttributeInstance;
import org.eclipse.papyrus.emf.facet.efacet.metamodel.v0_2_0.efacet.serialization.MultiValuedContainmentReferenceInstance;
import org.eclipse.papyrus.emf.facet.efacet.metamodel.v0_2_0.efacet.serialization.MultiValuedReferenceInstance;
import org.eclipse.papyrus.emf.facet.efacet.metamodel.v0_2_0.efacet.serialization.SerializationFactory;
import org.eclipse.papyrus.emf.facet.efacet.metamodel.v0_2_0.efacet.serialization.SingleValuedAttributeInstance;
import org.eclipse.papyrus.emf.facet.efacet.metamodel.v0_2_0.efacet.serialization.SingleValuedContainmentReferenceInstance;
import org.eclipse.papyrus.emf.facet.efacet.metamodel.v0_2_0.efacet.serialization.SingleValuedReferenceInstance;

public class SerializationManager {

	private final Resource structuralFeatureInstancesResource;

	public SerializationManager(final Resource structuralFeatureInstancesResource) {
		this.structuralFeatureInstancesResource = structuralFeatureInstancesResource;
		if (structuralFeatureInstancesResource != null) {
			final EList<EObject> contents = structuralFeatureInstancesResource.getContents();
			for (EObject eObject : contents) {
				if (eObject instanceof ExtendedEObjectReference) {
					final ExtendedEObjectReference extendedEObjectReference = (ExtendedEObjectReference) eObject;
					final LinkToExtendedEObjectReferenceAdapter adapter = (LinkToExtendedEObjectReferenceAdapter) ILinkToExtendedEObjectReferenceAdapterFactory.INSTANCE
							.adapt(extendedEObjectReference.getExtendedEObject(),
									ILinkToExtendedEObjectReference.class);
					adapter.setExtendedEObjectReference(extendedEObjectReference);
				}
			}
		}
	}

	/**
	 * This method creates a ReferenceInstance in the given {@link ExtendedEObjectReference} with
	 * the reference {@link EStructuralFeature} and the value {@link Object}.
	 */
	private static MultiValuedContainmentReferenceInstance createMultiValuedContainmentReferenceInstance(final EReference reference,
			final List<EObject> values,
			final ExtendedEObjectReference extendedEObjectReference) {
		final MultiValuedContainmentReferenceInstance containmentReferenceInstance = SerializationFactory.eINSTANCE
				.createMultiValuedContainmentReferenceInstance();
		containmentReferenceInstance.setEReference(reference);
		if (values != null) {
			containmentReferenceInstance.getOwnedElements().addAll(values);
		}
		extendedEObjectReference.getReferenceInstances().add(containmentReferenceInstance);
		return containmentReferenceInstance;
	}

	/**
	 * This method returns the {@link ExtendedEObjectReference} associated with the given
	 * {@link EObject} if it exists or <code>null</code> if it does not exist.
	 */
	private static ExtendedEObjectReference getExtendedEObjectReference(final EObject eObject) {
		ExtendedEObjectReference result = null;
		for (final Adapter adapter : eObject.eAdapters()) {
			if (adapter.isAdapterForType(ILinkToExtendedEObjectReference.class)) {
				final LinkToExtendedEObjectReferenceAdapter linkToExtendedEObjectReferenceAdapter = (LinkToExtendedEObjectReferenceAdapter) adapter;
				result = linkToExtendedEObjectReferenceAdapter.getExtendedEObjectReference();
				break;
			}
		}
		return result;
	}

	/**
	 * This method creates an AttributeInstance in the given {@link ExtendedEObjectReference} with
	 * the attribute {@link EStructuralFeature} and the value {@link Object}.
	 */
	private static MultiValuedAttributeInstance createMultiValuedAttributeInstance(final EAttribute attribute, final List<Object> values,
			final ExtendedEObjectReference extendedEObjectReference) {
		final MultiValuedAttributeInstance attributeInstance = SerializationFactory.eINSTANCE.createMultiValuedAttributeInstance();
		attributeInstance.setEAttribute(attribute);
		if (values != null) {
			attributeInstance.getValues().addAll(values);
		}
		extendedEObjectReference.getAttributeInstances().add(attributeInstance);
		return attributeInstance;
	}

	/**
	 * This method creates a ReferenceInstance in the given {@link ExtendedEObjectReference} with
	 * the reference {@link EStructuralFeature} and the value {@link Object}.
	 */
	private static MultiValuedReferenceInstance createMultiValuedReferenceInstance(final EReference reference, final List<EObject> values,
			final ExtendedEObjectReference extendedEObjectReference) {
		final MultiValuedReferenceInstance referenceInstance = SerializationFactory.eINSTANCE
				.createMultiValuedReferenceInstance();
		referenceInstance.setEReference(reference);
		if (values != null) {
			referenceInstance.getReferencedElements().addAll(values);
		}
		extendedEObjectReference.getReferenceInstances().add(referenceInstance);
		return referenceInstance;
	}

	private static SingleValuedAttributeInstance getSingleValuedAttributeInstance(final ExtendedEObjectReference extendedEObjectReference,
			final EAttribute eAttribute) {
		SingleValuedAttributeInstance result = null;
		final EList<AbstractAttributeInstance> attributeInstances = extendedEObjectReference.getAttributeInstances();
		for (final AbstractAttributeInstance attributeInstance : attributeInstances) {
			if (eAttribute.equals(attributeInstance.getEAttribute())) {
				if (attributeInstance instanceof SingleValuedAttributeInstance) {
					result = (SingleValuedAttributeInstance) attributeInstance;
					break;
				}
				throw new IllegalStateException("single valued attribute should be associated to an instance of SingleValuedAttributeInstance"); //$NON-NLS-1$
			}
		}
		return result;
	}

	private static MultiValuedAttributeInstance getMultiValuedAttributeInstance(final ExtendedEObjectReference extendedEObjectReference,
			final EAttribute eAttribute) {
		MultiValuedAttributeInstance result = null;
		final EList<AbstractAttributeInstance> attributeInstances = extendedEObjectReference.getAttributeInstances();
		for (final AbstractAttributeInstance attributeInstance : attributeInstances) {
			if (eAttribute.equals(attributeInstance.getEAttribute())) {
				if (attributeInstance instanceof MultiValuedAttributeInstance) {
					result = (MultiValuedAttributeInstance) attributeInstance;
					break;
				}
				throw new IllegalStateException("multi valued attribute should be associated to an instance of MultiValuedAttributeInstance"); //$NON-NLS-1$

			}
		}
		return result;
	}

	private static SingleValuedReferenceInstance getSingleValuedReferenceInstance(final ExtendedEObjectReference extendedEObjectReference,
			final EReference eReference) {
		SingleValuedReferenceInstance result = null;
		final EList<AbstractReferenceInstance> referenceInstances = extendedEObjectReference.getReferenceInstances();
		for (final AbstractReferenceInstance referenceInstance : referenceInstances) {
			if (eReference.equals(referenceInstance.getEReference())) {
				if (referenceInstance instanceof SingleValuedReferenceInstance) {
					result = (SingleValuedReferenceInstance) referenceInstance;
					break;
				}
				throw new IllegalStateException(
						"a single valued non-containment reference should be associated to an instance of SingleValuedReferenceInstance"); //$NON-NLS-1$
			}
		}
		return result;
	}

	private static SingleValuedContainmentReferenceInstance getSingleValuedContainmentReferenceInstance(
			final ExtendedEObjectReference extendedEObjectReference,
			final EReference eReference) {
		SingleValuedContainmentReferenceInstance result = null;
		final EList<AbstractReferenceInstance> referenceInstances = extendedEObjectReference.getReferenceInstances();
		for (final AbstractReferenceInstance referenceInstance : referenceInstances) {
			if (eReference.equals(referenceInstance.getEReference())) {
				if (referenceInstance instanceof SingleValuedContainmentReferenceInstance) {
					result = (SingleValuedContainmentReferenceInstance) referenceInstance;
					break;
				}
				throw new IllegalStateException(
						"a single valued containment reference should be associated to an instance of SingleValuedContainmentReferenceInstance"); //$NON-NLS-1$
			}
		}
		return result;
	}

	private static MultiValuedReferenceInstance getMultiValuedReferenceInstance(final ExtendedEObjectReference extendedEObjectReference,
			final EReference eReference) {
		MultiValuedReferenceInstance result = null;
		final EList<AbstractReferenceInstance> referenceInstances = extendedEObjectReference.getReferenceInstances();
		for (final AbstractReferenceInstance referenceInstance : referenceInstances) {
			if (eReference.equals(referenceInstance.getEReference())) {
				if (referenceInstance instanceof MultiValuedReferenceInstance) {
					result = (MultiValuedReferenceInstance) referenceInstance;
					break;
				}
				throw new IllegalStateException(
						"a multi valued non-containment reference should be associated to an instance of MultiValuedReferenceInstance"); //$NON-NLS-1$
			}
		}
		return result;
	}

	private static MultiValuedContainmentReferenceInstance getMultiValuedContainmentReferenceInstance(final ExtendedEObjectReference extendedEObjectReference,
			final EReference eReference) {
		MultiValuedContainmentReferenceInstance result = null;
		final EList<AbstractReferenceInstance> referenceInstances = extendedEObjectReference.getReferenceInstances();
		for (final AbstractReferenceInstance referenceInstance : referenceInstances) {
			if (eReference.equals(referenceInstance.getEReference())) {
				if (referenceInstance instanceof MultiValuedContainmentReferenceInstance) {
					result = (MultiValuedContainmentReferenceInstance) referenceInstance;
					break;
				}
				throw new IllegalStateException(
						"a multi valued containment reference should be associated to an instance of MultiValuedContainmentReferenceInstance"); //$NON-NLS-1$
			}
		}
		return result;
	}

	public void saveStructuralFeatureInstanceModel()
			throws SaveStructuralFeatureInstanceModelException {
		if (this.structuralFeatureInstancesResource == null) {
			throw new SaveStructuralFeatureInstanceModelException("This facet manager has been initailized with a null structural feature instances resource"); //$NON-NLS-1$
		}
		try {
			this.structuralFeatureInstancesResource.save(Collections.EMPTY_MAP);
		} catch (final IOException e) {
			throw new SaveStructuralFeatureInstanceModelException(e);
		}
	}

	/**
	 * This method returns the ExtendedEObjectReference associated with the given {@link EObject} if
	 * it exists or creates it if it does not exist.
	 */
	private ExtendedEObjectReference getOrCreateExtendedEObjectReference(final EObject eObject) {
		ExtendedEObjectReference extendedEObjectReference = SerializationManager.getExtendedEObjectReference(eObject);
		if (extendedEObjectReference == null) {
			extendedEObjectReference = SerializationFactory.eINSTANCE.createExtendedEObjectReference();
			extendedEObjectReference.setExtendedEObject(eObject);
			if (this.structuralFeatureInstancesResource != null) {
				this.structuralFeatureInstancesResource.getContents().add(extendedEObjectReference);
			}
			final LinkToExtendedEObjectReferenceAdapter adapter = (LinkToExtendedEObjectReferenceAdapter) ILinkToExtendedEObjectReferenceAdapterFactory.INSTANCE
					.adapt(eObject, ILinkToExtendedEObjectReference.class);
			adapter.setExtendedEObjectReference(extendedEObjectReference);
		}
		return extendedEObjectReference;
	}

	public void setAttribute(final EObject eObject, final EAttribute attribute, final Object newValue) {
		final ExtendedEObjectReference extendedEObjectReference = this.getOrCreateExtendedEObjectReference(eObject);
		if (attribute.isMany()) {
			if (newValue instanceof List) {
				// safe to cast
				@SuppressWarnings("unchecked")
				final List<Object> list = (List<Object>) newValue;
				final MultiValuedAttributeInstance attributeInstance =
						SerializationManager.getMultiValuedAttributeInstance(extendedEObjectReference, attribute);
				if (attributeInstance == null) {
					SerializationManager.createMultiValuedAttributeInstance(attribute, list, extendedEObjectReference);
				} else {
					attributeInstance.getValues().clear();
					attributeInstance.getValues().addAll(list);
				}
			} else {
				throw new IllegalArgumentException("The given FacetAttribute is multiplicity-many, so the value must be a List"); //$NON-NLS-1$
			}
		} else {
			final SingleValuedAttributeInstance attributeInstance =
					SerializationManager.getSingleValuedAttributeInstance(extendedEObjectReference, attribute);
			if (attributeInstance == null) {
				createSingleValuedAttributeInstance(attribute, newValue, extendedEObjectReference);
			} else {
				attributeInstance.setValue(newValue);
			}
		}

	}


	/**
	 * This method creates an AttributeInstance in the given {@link ExtendedEObjectReference} with
	 * the attribute {@link EStructuralFeature} and the value {@link Object}.
	 */
	private static SingleValuedAttributeInstance createSingleValuedAttributeInstance(final EAttribute attribute, final Object value,
			final ExtendedEObjectReference extendedEObjectReference) {
		final SingleValuedAttributeInstance attributeInstance = SerializationFactory.eINSTANCE.createSingleValuedAttributeInstance();
		attributeInstance.setEAttribute(attribute);
		attributeInstance.setValue(value);
		extendedEObjectReference.getAttributeInstances().add(attributeInstance);
		return attributeInstance;
	}

	/**
	 * This method creates a ReferenceInstance in the given {@link ExtendedEObjectReference} with
	 * the reference {@link EStructuralFeature} and the value {@link Object}.
	 */
	private static SingleValuedReferenceInstance createSingleValuedReferenceInstance(final EReference reference, final EObject value,
			final ExtendedEObjectReference extendedEObjectReference) {
		final SingleValuedReferenceInstance referenceInstance = SerializationFactory.eINSTANCE
				.createSingleValuedReferenceInstance();
		referenceInstance.setEReference(reference);
		referenceInstance.setReferencedElement(value);
		extendedEObjectReference.getReferenceInstances().add(referenceInstance);
		return referenceInstance;
	}

	public void setReference(final EObject eObject, final EReference reference, final Object newValue) {
		final ExtendedEObjectReference extendedEObjectReference = this.getOrCreateExtendedEObjectReference(eObject);
		if (reference.isMany()) {
			throw new UnsupportedOperationException(
					"The given FacetReference is multi-valued : add elements to the list returned by getMultiValuedReference() instead"); //$NON-NLS-1$
		}
		if (!(newValue instanceof EObject)) {
			throw new IllegalArgumentException("newValue should be an EObject because eStructuralFeature is an EReference"); //$NON-NLS-1$	
		}
		final EObject newEObjectValue = (EObject) newValue;
		if (reference.isContainment()) {
			final SingleValuedContainmentReferenceInstance referenceInstance =
					SerializationManager.getSingleValuedContainmentReferenceInstance(extendedEObjectReference, reference);
			if (referenceInstance == null) {
				createSingleValuedContainmentReferenceInstance(reference, newEObjectValue, extendedEObjectReference);
			} else {
				referenceInstance.setOwnedElement(newEObjectValue);
			}
		} else {
			final SingleValuedReferenceInstance referenceInstance =
					SerializationManager.getSingleValuedReferenceInstance(extendedEObjectReference, reference);
			if (referenceInstance == null) {
				createSingleValuedReferenceInstance(reference, newEObjectValue, extendedEObjectReference);
			} else {
				referenceInstance.setReferencedElement(newEObjectValue);
			}
		}

	}
	
	/**
	 * This method creates a ReferenceInstance in the given {@link ExtendedEObjectReference} with
	 * the reference {@link EStructuralFeature} and the value {@link Object}.
	 */
	private static SingleValuedContainmentReferenceInstance createSingleValuedContainmentReferenceInstance(final EReference reference,
			final EObject value,
			final ExtendedEObjectReference extendedEObjectReference) {
		final SingleValuedContainmentReferenceInstance containmentReferenceInstance = SerializationFactory.eINSTANCE
				.createSingleValuedContainmentReferenceInstance();
		containmentReferenceInstance.setEReference(reference);
		containmentReferenceInstance.setOwnedElement(value);
		extendedEObjectReference.getReferenceInstances().add(containmentReferenceInstance);
		return containmentReferenceInstance;
	}

	private Object getMultiValuedStructuralFeature(final EObject eObject, final EStructuralFeature structuralFeature) {
		Object result = null;
		final ExtendedEObjectReference extendedEObjectReference = this.getOrCreateExtendedEObjectReference(eObject);
		if (structuralFeature instanceof EAttribute) {
			final EAttribute eAttribute = (EAttribute) structuralFeature;
			final MultiValuedAttributeInstance attributeInstance =
					SerializationManager.getMultiValuedAttributeInstance(extendedEObjectReference, eAttribute);
			if (attributeInstance != null) {
				result = attributeInstance.getValues();
			}
		} else if (structuralFeature instanceof EReference) {
			final EReference eReference = (EReference) structuralFeature;
			if (eReference.isContainment()) {
				MultiValuedContainmentReferenceInstance referenceInstance =
						SerializationManager.getMultiValuedContainmentReferenceInstance(extendedEObjectReference, eReference);
				if (referenceInstance == null) {
					referenceInstance = SerializationManager.createMultiValuedContainmentReferenceInstance(eReference, null, extendedEObjectReference);
					result = referenceInstance.getOwnedElements();					
				} else {
					result =  referenceInstance.getOwnedElements();
				}
			} else {
				MultiValuedReferenceInstance referenceInstance =
						SerializationManager.getMultiValuedReferenceInstance(extendedEObjectReference, eReference);
				if (referenceInstance == null) {
					referenceInstance = SerializationManager.createMultiValuedReferenceInstance(eReference, null, extendedEObjectReference);
					result =  referenceInstance.getReferencedElements();
				} else {
					result = referenceInstance.getReferencedElements();
				}
			}
		} else {
			throw new UnsupportedOperationException("Getting a structural feature of type '" + structuralFeature.getClass().getName() + "' is not implemented"); //$NON-NLS-1$ //$NON-NLS-2$
		}
		return result;
	}
	
	private static Object getSingleValuedStructuralFeature(final EObject eObject, final EStructuralFeature structuralFeature) {
		Object result = null;
		final ExtendedEObjectReference extendedEObjectReference = SerializationManager.getExtendedEObjectReference(eObject);
		if (extendedEObjectReference != null) {
			if (structuralFeature instanceof EAttribute) {
				final EAttribute eAttribute = (EAttribute) structuralFeature;
				final SingleValuedAttributeInstance attributeInstance = SerializationManager.getSingleValuedAttributeInstance(extendedEObjectReference, eAttribute);
				if (attributeInstance != null) {
					result = attributeInstance.getValue();
				}
			} else if (structuralFeature instanceof EReference) {
				final EReference eReference = (EReference) structuralFeature;
				if (eReference.isContainment()) {
					final SingleValuedContainmentReferenceInstance referenceInstance =
							SerializationManager.getSingleValuedContainmentReferenceInstance(extendedEObjectReference, eReference);
					result = referenceInstance.getOwnedElement();
				} else {
					final SingleValuedReferenceInstance referenceInstance =
							SerializationManager.getSingleValuedReferenceInstance(extendedEObjectReference, eReference);
					result = referenceInstance.getReferencedElement();
				}
			} else {
				throw new IllegalArgumentException("Unexpected structuralFeature kind"); //$NON-NLS-1$
			}
		}
		return result;
	}
	
	public Object getNotDerivedValue(final EObject eObject, final EStructuralFeature structuralFeature) {
		Object result;
		if (structuralFeature.isMany()) {
			result = this.getMultiValuedStructuralFeature(eObject, structuralFeature);
		} else {
			result = SerializationManager.getSingleValuedStructuralFeature(eObject, structuralFeature);
		}
		return result;
	}
}

Back to the top