Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 325858

Summary: [Wizard template] Incorrect creation of model template when stereotyped elements are present
Product: [Modeling] Papyrus Reporter: David Servat <David.Servat>
Component: CoreAssignee: Patrick Tessier <Patrick.Tessier>
Status: RESOLVED FIXED QA Contact: David Servat <David.Servat>
Severity: normal    
Priority: P3    
Version: unspecified   
Target Milestone: ---   
Hardware: Macintosh   
OS: Mac OS X   
Whiteboard:

Description David Servat CLA 2010-09-21 10:03:32 EDT
When a template model features stereotyped elements, these are not correctly created in the model. A simple copy from the template model seems to be performed, see the following method in InitFromTemplateCommand from org.eclipse.papyrus.wizards.template:
	private void initializeFromTemplate() {
		Resource templateResource = loadTemplateResource();
		List<EObject> eObjectsToAdd = new ArrayList<EObject>();
		for(EObject eObject : templateResource.getContents()) {
			eObjectsToAdd.add(EcoreUtil.copy(eObject));
		}
		for(EObject eObject : eObjectsToAdd) {
			myModelResource.getContents().add(eObject);
		}
	}

This simple copy produces a model file where references inside the stereotype properties (both baseClass and link to other model elements) are made to the template model and not the model created!

For instance, template model:
  <SystemModeling:SystemModel xmi:id="_emKFUMVlEd-CvZir4IWNcw" implementationLevel="_iz8UYMVlEd-CvZir4IWNcw" analysisLevel="_iIbSMMVlEd-CvZir4IWNcw" designLevel="_igX4IMVlEd-CvZir4IWNcw" base_Class="_cX-WgMVlEd-CvZir4IWNcw"/>

Created model (check out the platform:/ links)
 <SystemModeling:SystemModel xmi:id="_fD4bwMVoEd-li4xgZl8l7Q">
    <implementationLevel href="platform:/plugin/org.eclipse.papyrus.eastadl/resources/templates/model.uml#_iz8UYMVlEd-CvZir4IWNcw"/>
    <analysisLevel href="platform:/plugin/org.eclipse.papyrus.eastadl/resources/templates/model.uml#_iIbSMMVlEd-CvZir4IWNcw"/>
    <designLevel href="platform:/plugin/org.eclipse.papyrus.eastadl/resources/templates/model.uml#_igX4IMVlEd-CvZir4IWNcw"/>
    <base_Class href="platform:/plugin/org.eclipse.papyrus.eastadl/resources/templates/model.uml#_cX-WgMVlEd-CvZir4IWNcw"/>
  </SystemModeling:SystemModel>
Comment 1 David Servat CLA 2011-01-26 06:47:35 EST
Bug is fixed with the use of 
		eObjectsToAdd = (List<EObject>)EcoreUtil.copyAll(templateResource.getContents());
in InitializeTemplateCommand instead of
		for(EObject eObject : templateResource.getContents()) {
			eObjectsToAdd.add(EcoreUtil.copy(eObject));
		}

What happens is that for profiled UML models, the resource contents are made of several eObjects, e.g. a Model and extra eObjects representing stereotypes, for instance SystemModel in an EASTADL model. The code used previously would deal with each eObject in turn, not building a complete HashMap of the keys/values for the whole file, thus references contained in the stereotypes were simply copied from the original file (default behavior when references are not found during the copy). The use of copyAll enables to trea the whole set of eObjects as a whole and keep track of all references. Which does the trick.