Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: cbc75a6e5a41d0da14004c65791824a9b6e03d17 (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
/**
 * <copyright>
 * </copyright>
 *
 * $Id: PAnnotatedModelImpl.java,v 1.30 2010/08/18 13:17:49 mtaal Exp $
 */
package org.eclipse.emf.teneo.annotations.pamodel.impl;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.notify.NotificationChain;
import org.eclipse.emf.common.notify.impl.AdapterImpl;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.common.util.TreeIterator;
import org.eclipse.emf.ecore.EAttribute;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EClassifier;
import org.eclipse.emf.ecore.EDataType;
import org.eclipse.emf.ecore.EModelElement;
import org.eclipse.emf.ecore.ENamedElement;
import org.eclipse.emf.ecore.EPackage;
import org.eclipse.emf.ecore.EReference;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.ecore.EcorePackage;
import org.eclipse.emf.ecore.InternalEObject;
import org.eclipse.emf.ecore.impl.EObjectImpl;
import org.eclipse.emf.ecore.util.EObjectContainmentWithInverseEList;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.emf.ecore.util.InternalEList;
import org.eclipse.emf.ecore.xmi.XMLResource;
import org.eclipse.emf.ecore.xmi.impl.XMLResourceImpl;
import org.eclipse.emf.teneo.annotations.pamodel.PAnnotatedEAttribute;
import org.eclipse.emf.teneo.annotations.pamodel.PAnnotatedEClass;
import org.eclipse.emf.teneo.annotations.pamodel.PAnnotatedEDataType;
import org.eclipse.emf.teneo.annotations.pamodel.PAnnotatedEModelElement;
import org.eclipse.emf.teneo.annotations.pamodel.PAnnotatedEPackage;
import org.eclipse.emf.teneo.annotations.pamodel.PAnnotatedEReference;
import org.eclipse.emf.teneo.annotations.pamodel.PAnnotatedEStructuralFeature;
import org.eclipse.emf.teneo.annotations.pamodel.PAnnotatedModel;
import org.eclipse.emf.teneo.annotations.pamodel.PamodelPackage;
import org.eclipse.emf.teneo.annotations.pannotation.SequenceGenerator;
import org.eclipse.emf.teneo.annotations.pannotation.SequenceStyleGenerator;
import org.eclipse.emf.teneo.annotations.pannotation.TableGenerator;

/**
 * <!-- begin-user-doc --> An implementation of the model object ' <em><b>PAnnotated Model</b></em>
 * '. <!-- end-user-doc -->
 * <p>
 * The following features are implemented:
 * <ul>
 * <li>{@link org.eclipse.emf.teneo.annotations.pamodel.impl.PAnnotatedModelImpl#getPaEPackages <em>
 * Pa EPackages</em>}</li>
 * </ul>
 * </p>
 * 
 * @generated
 */
public class PAnnotatedModelImpl extends EObjectImpl implements PAnnotatedModel {

	@SuppressWarnings("unchecked")
	class MapTrackingAdapter extends AdapterImpl {
		@Override
		public void notifyChanged(Notification msg) {
			// TODO handle resolve?
			Object msgFeature = msg.getFeature();
			if (!msg.isTouch()
					&& msgFeature == PamodelPackage.eINSTANCE.getPAnnotatedEModelElement_ModelElement()) {
				switch (msg.getEventType()) {
				case Notification.SET:
				case Notification.UNSET:
					removeMapping((EModelElement) msg.getOldValue());
					addMapping((PAnnotatedEModelElement) msg.getNotifier());
					break;
				}
			} else if (msgFeature instanceof EReference && !msg.isTouch()) {
				EReference r = (EReference) msgFeature;
				if (PamodelPackage.eINSTANCE.getPAnnotatedEModelElement().isSuperTypeOf(
						r.getEReferenceType())
						&& r.isContainment()) {
					switch (msg.getEventType()) {
					case Notification.UNSET:
					case Notification.SET:
						if (msg.getOldValue() != null) {
							detach((PAnnotatedEModelElement) msg.getOldValue());
						}
						if (msg.getNewValue() != null) {
							attach((PAnnotatedEModelElement) msg.getNewValue());
						}
						break;
					case Notification.ADD:
						if (msg.getNewValue() != null) {
							attach((PAnnotatedEModelElement) msg.getNewValue());
						}
						break;
					case Notification.ADD_MANY:
						attach((List<? extends Object>) msg.getNewValue());
						break;
					case Notification.REMOVE:
						if (msg.getOldValue() != null) {
							detach((PAnnotatedEModelElement) msg.getOldValue());
						}
						break;
					case Notification.REMOVE_MANY:
						detach((List<? extends Object>) msg.getNewValue());
						break;
					}
				}
			}
			super.notifyChanged(msg);
		}
	};

	final MapTrackingAdapter itsMapTrackingAdapter;

	/**
	 * The cached value of the '{@link #getPaEPackages() <em>Pa EPackages</em>}' containment reference
	 * list. <!-- begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @see #getPaEPackages()
	 * @generated
	 * @ordered
	 */
	protected EList<PAnnotatedEPackage> paEPackages;

	/**
	 * Record the association between the EModelElement and the PAnnotatedEModelElement that are
	 * contained in this PAnnotatedModel content.
	 */
	protected final Map<EModelElement, PAnnotatedEModelElement> eElement_to_pElement;

	/** Set to true if initialized */
	private boolean initialized = false;

	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @generated NOT
	 */
	protected PAnnotatedModelImpl() {
		eElement_to_pElement = new HashMap<EModelElement, PAnnotatedEModelElement>();
		itsMapTrackingAdapter = new MapTrackingAdapter();
		this.eAdapters().add(itsMapTrackingAdapter);
	}

	/** Return the mapping from epackage elements to annotated package elements */
	public Map<EModelElement, PAnnotatedEModelElement> getModelElementMapping() {
		return eElement_to_pElement;
	}

	/** Merge the passed annotated model in this one */
	public void merge(PAnnotatedModel pModel) {
		eElement_to_pElement.putAll(pModel.getModelElementMapping());
	}

	/**
	 * Register the association among a PAnnotatedEModelElement and its annotated EModelElement. If
	 * another PAnnotatedEModelElement was associatied to the same EModelElement, such association
	 * will be broken.
	 * 
	 * <p>
	 * In order to mantain the consistency of the model, whenever an association among a
	 * PAnnotatedEModelElement <code>pa</code> and an EModelElement <code>e</code> is broken, the
	 * annotatedElement feature of <code>pa</code> is unset.
	 */
	protected void addMapping(PAnnotatedEModelElement target) {
		final EModelElement eModelElement = target.getModelElement();
		if (eModelElement != null) {
			PAnnotatedEModelElement prevAssoc = eElement_to_pElement.get(eModelElement);
			if (prevAssoc != null) {
				removeMapping(eModelElement);
				prevAssoc.setModelElement(null);
			}
			eElement_to_pElement.put(eModelElement, target);
		}
	}

	/**
	 * Unregister the association between an <code>EModelElement</code> and the
	 * <code>PAnnotatedEModelElement</code> that references it via the <code>annotatedElement</code>
	 * feature.
	 */
	protected void removeMapping(EModelElement eModelElement) {
		final Object prevAssoc = eElement_to_pElement.remove(eModelElement);
		assert prevAssoc != null;
	}

	/**
	 * Invoked whenever a new PAnnotatedEModelElement is added to the PAnnotatedModel.
	 */
	protected void attach(PAnnotatedEModelElement aElement) {
		aElement.eAdapters().add(0, itsMapTrackingAdapter);
		addMapping(aElement);
		attach(aElement.eContents());
	}

	/**
	 * Invoked whenever a set of new PAnnotatedEModelElement is added to the PAnnotatedModel.
	 */
	protected void attach(List<? extends Object> aElements) {
		for (Object x : aElements) {
			if (x instanceof PAnnotatedEModelElement) {
				attach((PAnnotatedEModelElement) x);
			}
		}
	}

	/**
	 * Invoked whenever a PAnnotatedEModelElement is removed from the PAnnotatedModel.
	 */
	protected void detach(PAnnotatedEModelElement aElement) {
		aElement.eAdapters().remove(itsMapTrackingAdapter);
		ENamedElement annotatedElement = aElement.getModelElement();
		if (annotatedElement != null) {
			removeMapping(annotatedElement);
		}
		detach(aElement.eContents());
	}

	/**
	 * Invoked whenever a set of PAnnotatedEModelElement is removed from the PAnnotatedModel.
	 */
	protected void detach(List<? extends Object> aElements) {
		for (Object x : aElements) {
			if (x instanceof PAnnotatedEModelElement) {
				detach((PAnnotatedEModelElement) x);
			}
		}
	}

	/**
	 * Invariant: the eElement_to_pElement map agree with the PAnnotatedModel content.
	 * <p>
	 * This method should be used only for debugging. TODO use in validation.
	 */
	public void invMapIsWellFormed() {
		Set<EModelElement> definedUnused = new HashSet<EModelElement>(eElement_to_pElement.keySet());
		for (TreeIterator<? extends Object> i = EcoreUtil.getAllContents(eContents()); i.hasNext();) {
			Object x = i.next();
			if (x instanceof PAnnotatedEModelElement) {
				PAnnotatedEModelElement paElement = (PAnnotatedEModelElement) x;
				ENamedElement annotatedElement = paElement.getModelElement();
				if (annotatedElement != null) {
					assert getPAnnotated(annotatedElement) == paElement;
					definedUnused.remove(annotatedElement);
				}
			} else {
				i.prune();
			}
		}
		assert definedUnused.isEmpty();
	}

	/**
	 * @return Returns the unique <code>PAnnotatedEModelElement</code> associated (via the
	 *         <code>annotatedElement</code> feature) to the given <code>EModelElement</code>.
	 */
	public PAnnotatedEModelElement getPAnnotated(EModelElement e) {
		checkAnnotatedPresent(e, eElement_to_pElement);
		return eElement_to_pElement.get(e);
	}

	/**
	 * @return Returns the unique <code>PAnnotatedEPackage</code> associated (via the
	 *         <code>annotatedElement</code> feature) to the given <code>EPackage</code>.
	 */
	public PAnnotatedEPackage getPAnnotated(EPackage e) {
		checkAnnotatedPresent(e, eElement_to_pElement);
		return (PAnnotatedEPackage) eElement_to_pElement.get(e);
	}

	/**
	 * @return Returns the unique <code>PAnnotatedEClass</code> associated (via the
	 *         <code>annotatedElement</code> feature) to the given <code>EClass</code>.
	 */
	public PAnnotatedEClass getPAnnotated(EClass e) {
		// in this case do not throw an error
		if (e.getEPackage() instanceof EcorePackage) {
			return (PAnnotatedEClass) eElement_to_pElement.get(e);
		}
		checkAnnotatedPresent(e, eElement_to_pElement);
		return (PAnnotatedEClass) eElement_to_pElement.get(e);
	}

	/**
	 * @return Returns the unique <code>PAnnotatedEStructuralFeature</code> associated (via the
	 *         <code>annotatedElement</code> feature) to the given <code>EStructuralFeature</code>.
	 */
	public PAnnotatedEStructuralFeature getPAnnotated(EStructuralFeature e) {
		checkAnnotatedPresent(e, eElement_to_pElement);
		return (PAnnotatedEStructuralFeature) eElement_to_pElement.get(e);
	}

	/**
	 * @return Returns the unique <code>PAnnotatedEDataType</code> associated (via the
	 *         <code>annotatedElement</code> feature) to the given <code>EDataType</code>.
	 */
	public PAnnotatedEDataType getPAnnotated(EDataType e) {
		// do not check because many datatypes belong to the emf package
		return (PAnnotatedEDataType) eElement_to_pElement.get(e);
	}

	/**
	 * @return Returns the unique <code>PAnnotatedEAttribute</code> associated (via the
	 *         <code>annotatedElement</code> feature) to the given <code>EAttribute</code>.
	 */
	public PAnnotatedEAttribute getPAnnotated(EAttribute e) {
		checkAnnotatedPresent(e, eElement_to_pElement);
		return (PAnnotatedEAttribute) eElement_to_pElement.get(e);
	}

	/**
	 * @return Returns the unique <code>PAnnotatedEReference</code> associated (via the
	 *         <code>annotatedElement</code> feature) to the given <code>EReference</code>.
	 */
	public PAnnotatedEReference getPAnnotated(EReference e) {
		checkAnnotatedPresent(e, eElement_to_pElement);
		return (PAnnotatedEReference) eElement_to_pElement.get(e);
	}

	/**
	 * Checks if there is a annotated model element for the passed emf model element. If not then an
	 * illegal argument is thrown
	 */
	private void checkAnnotatedPresent(EModelElement ee,
			Map<EModelElement, PAnnotatedEModelElement> map) {
		if (!isInitialized()) {
			return; // at this point the model is not yet fully initialized.
		}
		if (ee == null) {
			throw new IllegalArgumentException(
					"Trying to retrieve Annotated Type using null. "
							+ "This can occur if not all epackages have been registered with Teneo.\n"
							+ "Or this can occur if epackages which refer to eachother are placed in different ecore/xsd files "
							+ "and they are not read using one resource set. The reference from one epackage to another must be "
							+ "resolvable by EMF.");
		}
		if (map.get(ee) == null) {
			final String name = ee instanceof ENamedElement ? " for: " + ((ENamedElement) ee).getName()
					: "";
			throw new IllegalArgumentException("No annotated model element present " + name
					+ " for type " + ee.eClass().getName() + " has its epackage been registered with Teneo?");
		}
	}

	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @generated
	 */
	@Override
	protected EClass eStaticClass() {
		return PamodelPackage.Literals.PANNOTATED_MODEL;
	}

	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @generated
	 */
	public EList<PAnnotatedEPackage> getPaEPackages() {
		if (paEPackages == null) {
			paEPackages = new EObjectContainmentWithInverseEList<PAnnotatedEPackage>(
					PAnnotatedEPackage.class, this, PamodelPackage.PANNOTATED_MODEL__PA_EPACKAGES,
					PamodelPackage.PANNOTATED_EPACKAGE__PA_MODEL);
		}
		return paEPackages;
	}

	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @generated
	 */
	@SuppressWarnings("unchecked")
	@Override
	public NotificationChain eInverseAdd(InternalEObject otherEnd, int featureID,
			NotificationChain msgs) {
		switch (featureID) {
		case PamodelPackage.PANNOTATED_MODEL__PA_EPACKAGES:
			return ((InternalEList<InternalEObject>) (InternalEList<?>) getPaEPackages()).basicAdd(
					otherEnd, msgs);
		}
		return super.eInverseAdd(otherEnd, featureID, msgs);
	}

	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @generated
	 */
	@Override
	public NotificationChain eInverseRemove(InternalEObject otherEnd, int featureID,
			NotificationChain msgs) {
		switch (featureID) {
		case PamodelPackage.PANNOTATED_MODEL__PA_EPACKAGES:
			return ((InternalEList<?>) getPaEPackages()).basicRemove(otherEnd, msgs);
		}
		return super.eInverseRemove(otherEnd, featureID, msgs);
	}

	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @generated
	 */
	@Override
	public Object eGet(int featureID, boolean resolve, boolean coreType) {
		switch (featureID) {
		case PamodelPackage.PANNOTATED_MODEL__PA_EPACKAGES:
			return getPaEPackages();
		}
		return super.eGet(featureID, resolve, coreType);
	}

	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @generated
	 */
	@SuppressWarnings("unchecked")
	@Override
	public void eSet(int featureID, Object newValue) {
		switch (featureID) {
		case PamodelPackage.PANNOTATED_MODEL__PA_EPACKAGES:
			getPaEPackages().clear();
			getPaEPackages().addAll((Collection<? extends PAnnotatedEPackage>) newValue);
			return;
		}
		super.eSet(featureID, newValue);
	}

	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @generated
	 */
	@Override
	public void eUnset(int featureID) {
		switch (featureID) {
		case PamodelPackage.PANNOTATED_MODEL__PA_EPACKAGES:
			getPaEPackages().clear();
			return;
		}
		super.eUnset(featureID);
	}

	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @generated
	 */
	@Override
	public boolean eIsSet(int featureID) {
		switch (featureID) {
		case PamodelPackage.PANNOTATED_MODEL__PA_EPACKAGES:
			return paEPackages != null && !paEPackages.isEmpty();
		}
		return super.eIsSet(featureID);
	}

	/**
	 * Returns a sequence generator on the basis of its name, if not found then an exception is
	 * thrown. efeature is passed for debugging purposes.
	 */
	public SequenceGenerator getSequenceGenerator(EStructuralFeature efeature, String name) {
		// TODO: should not only the paepackage of the efeature be checked?
		for (PAnnotatedEPackage pae : getPaEPackages()) {
			for (SequenceGenerator sg : pae.getSequenceGenerators()) {
				if (sg.getName() != null && sg.getName().compareTo(name) == 0) {
					return sg;
				}
			}
			for (PAnnotatedEClass aClass : pae.getPaEClasses()) {
				for (SequenceGenerator sg : aClass.getSequenceGenerators()) {
					if (sg.getName() != null && sg.getName().compareTo(name) == 0) {
						return sg;
					}
				}

				for (PAnnotatedEStructuralFeature aFeature : aClass.getPaEStructuralFeatures()) {
					if (aFeature.getSequenceGenerator() != null
							&& aFeature.getSequenceGenerator().getName() != null
							&& aFeature.getSequenceGenerator().getName().compareTo(name) == 0) {
						return aFeature.getSequenceGenerator();
					}
				}
			}
		}
		throw new IllegalStateException("No sequence generator found with the name: " + name
				+ ", name is used in " + "annotation of element "
				+ efeature.getEContainingClass().getName() + "/" + efeature.getName());
	}

	public SequenceStyleGenerator getSequenceStyleGenerator(EStructuralFeature efeature, String name) {
		// TODO: should not only the paepackage of the efeature be checked?
		for (PAnnotatedEPackage pae : getPaEPackages()) {
			for (SequenceStyleGenerator sg : pae.getSequenceStyleGenerators()) {
				if (sg.getName() != null && sg.getName().compareTo(name) == 0) {
					return sg;
				}
			}
		}
		throw new IllegalStateException("No sequence generator found with the name: " + name
				+ ", name is used in " + "annotation of element "
				+ efeature.getEContainingClass().getName() + "/" + efeature.getName());
	}

	/**
	 * Returns a table generator on the basis of its name, if not found then an exception is thrown.
	 * efeature is passed for debugging purposes.
	 */
	public TableGenerator getTableGenerator(EStructuralFeature efeature, String name) {
		// TODO: should not only the paepackage of the efeature be checked?
		for (PAnnotatedEPackage pae : getPaEPackages()) {
			for (TableGenerator tg : pae.getTableGenerators()) {
				if (tg.getName() != null && tg.getName().compareTo(name) == 0) {
					return tg;
				}
			}
			for (PAnnotatedEClass pec : pae.getPaEClasses()) {
				if (pec.getTableGenerator() != null && pec.getTableGenerator().getName() != null
						&& pec.getTableGenerator().getName().compareTo(name) == 0) {
					return pec.getTableGenerator();
				}

				for (PAnnotatedEStructuralFeature pef : pec.getPaEStructuralFeatures()) {
					for (TableGenerator tg : pef.getTableGenerators()) {
						if (tg.getName() != null && tg.getName().compareTo(name) == 0) {
							return tg;
						}
					}
				}
			}
		}

		return null;
	}

	/**
	 * @return the initialized
	 */
	public boolean isInitialized() {
		return initialized;
	}

	/**
	 * @param initialized
	 *          the initialized to set
	 */
	public void setInitialized(boolean initialized) {
		this.initialized = initialized;
	}

	private ConcurrentHashMap<String, EClass> entityNameToEClass = new ConcurrentHashMap<String, EClass>();

	/**
	 * @return the eclass annotated with this entity name. If not found then an exception is thrown
	 */
	public EClass getEClass(String entityName) {
		EClass eClass;
		if ((eClass = entityNameToEClass.get(entityName)) != null) {
			return eClass;
		}
		for (PAnnotatedEPackage aPackage : getPaEPackages()) {
			for (PAnnotatedEClass aClass : aPackage.getPaEClasses()) {
				if (aClass.getEntity() != null && aClass.getEntity().getName() != null
						&& aClass.getEntity().getName().compareTo(entityName) == 0) {
					entityNameToEClass.put(entityName, aClass.getModelEClass());
					return aClass.getModelEClass();
				}
			}
		}
		throw new IllegalArgumentException("No Annotated EClass for entityName " + entityName);
	}

	/**
	 * @return the EClassifier for a certain name. First the eclasses are searched using the name as
	 *         the entityname then the edatatypes are searched.
	 */
	public EClassifier getEClassifier(String name) {
		if (hasEClass(name)) {
			return getEClass(name);
		}

		for (PAnnotatedEPackage aPackage : getPaEPackages()) {
			final EClassifier eClassifier = aPackage.getModelEPackage().getEClassifier(name);
			if (eClassifier instanceof EDataType) {
				return eClassifier;
			}
		}
		throw new IllegalArgumentException("No EClassifier for name " + name);
	}

	/**
	 * @return true if there is an EClass with the name as entityname or an EDataType with the name
	 *         passed as a parameter.
	 */
	public boolean hasEClassifier(String name) {
		if (hasEClass(name)) {
			return true;
		}

		for (PAnnotatedEPackage aPackage : getPaEPackages()) {
			final EClassifier eClassifier = aPackage.getModelEPackage().getEClassifier(name);
			if (eClassifier instanceof EDataType) {
				return true;
			}
		}
		return false;
	}

	/** @return true if there is annotated eclass with the passed entityname */
	public boolean hasEClass(String entityName) {
		if (entityNameToEClass.get(entityName) != null) {
			return true;
		}
		for (PAnnotatedEPackage aPackage : getPaEPackages()) {
			for (PAnnotatedEClass aClass : aPackage.getPaEClasses()) {
				if (aClass.getEntity() != null && aClass.getEntity().getName() != null
						&& aClass.getEntity().getName().compareTo(entityName) == 0) {
					entityNameToEClass.put(entityName, aClass.getModelEClass());
					return true;
				}
			}
		}
		return false;
	}

	/** Dump this model to xml */
	public String toXML() {
		try {
			final XMLResourceImpl xmlResource = new XMLResourceImpl();
			xmlResource.getContents().add(this);
			final ByteArrayOutputStream bos = new ByteArrayOutputStream();
			final HashMap<String, String> options = new HashMap<String, String>();
			options.put(XMLResource.OPTION_PROCESS_DANGLING_HREF,
					XMLResource.OPTION_PROCESS_DANGLING_HREF_RECORD);
			xmlResource.save(bos, options);
			final String res = bos.toString();
			bos.close();
			return res;
		} catch (IOException e) {
			// todo replace with teneo exception
			throw new IllegalStateException("Exception while converting pamodel to a xml string", e);
		}
	}

} // PAnnotatedModelImpl

Back to the top