Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 410c8c93d5585e1ec16171f3be54255d0638318a (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
/*****************************************************************************
 * Copyright (c) 2009 Atos Origin.
 *
 *    
 * 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:
 *  Tristan Faure (Atos Origin) tristan.faure@atosorigin.com - Initial API and implementation
 *
 *****************************************************************************/
package org.eclipse.papyrus.resource.migration.controller;

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

import org.eclipse.emf.codegen.ecore.genmodel.GenModel;
import org.eclipse.emf.codegen.ecore.genmodel.GenPackage;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EClassifier;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EPackage;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.papyrus.resource.ResourcePackage;
import org.eclipse.papyrus.resource.migration.controller.exceptions.URINotValidException;

/**
 * This class makes comaptible a genmodel with Resource meta model
 * 
 * @author tristan.faure@atosorigin.com
 * 
 */
public class GenModelUpdater {

	/**
	 * The URI for Resource Genmodel
	 */
	private static final URI uriForResourceGenModel = URI
			.createURI("platform:/plugin/org.eclipse.papyrus.resource/model/resource.genmodel");

	/**
	 * The URI for Resource Ecore meta model
	 */
	private static final URI uriForEclassResourceEObject = URI
			.createURI("platform:/plugin/org.eclipse.papyrus.resource/model/resource.ecore#//ResourceEObject");

	/**
	 * instance of resource package used to reference usedGenpackage in the original genmodel
	 */
	private ResourcePackage resourcePackage = ResourcePackage.eINSTANCE;

	/**
	 * Eclass for resource Eobject
	 */
	private EClass resourceEObjectEClass = null;

	/**
	 * current Epackage for the meta model corresponding to the current genmodel
	 */
	private EPackage currentEPackage = null;

	/**
	 * Transform the genmodel sotred at the given URI to link it with Resource Meta model
	 * 
	 * @param uri
	 * @throws URINotValidException
	 * @throws IOException
	 */
	public void transform(URI uri) throws URINotValidException, IOException {
		if(uri == null) {
			throw new URINotValidException("uri is null");
		}
		ResourceSet set = new ResourceSetImpl();
		try {
			Resource resource = set.getResource(uri, true);
			if(resource != null) {
				EObject e = resource.getContents().get(0);
				if(e instanceof GenModel) {
					GenModel genmodel = (GenModel)e;
					Resource toSave = update(genmodel);
					if(toSave != null) {
						resource.save(Collections.EMPTY_MAP);
						toSave.save(Collections.EMPTY_MAP);
					}
				} else {
					throw new URINotValidException("uri " + uri.toString() + "doesn't reference a genmodel");
				}
			}
			// we have to unload the resource to free the memory
			for(Resource r : set.getResources()) {
				if(r.isLoaded()) {
					try {
						r.unload();
					} catch (Exception e) {
						// to prevent unload exception
					}
				}
			}
		} catch (RuntimeException e) {
			throw new URINotValidException("uri " + uri.toString() + "not valid");
		}
	}

	/**
	 * update the genmodel to reference Resource Meta model
	 * 
	 * @param genmodel
	 * @return true if the resource has to be saved
	 */
	private Resource update(GenModel genmodel) {
		// does the genmodel already contain the resource ?
		Resource result = null;
		boolean found = false;
		for(GenPackage p : genmodel.getUsedGenPackages()) {
			found |= resourcePackage.equals(p.getEcorePackage());
		}
		if(!found) {
			// we load the Resource GenModel
			Resource resourceForResourceGenmodel = genmodel.eResource().getResourceSet().getResource(
					uriForResourceGenModel, true);
			if(resourceForResourceGenmodel != null) {
				EObject root = resourceForResourceGenmodel.getContents().get(0);
				if(root instanceof GenModel) {
					GenModel model = (GenModel)root;
					// Resource Genmodel is a used gen package for the current genmodel
					genmodel.getUsedGenPackages().add(model.getGenPackages().get(0));
					// load the eclass resource eobject
					Resource resourceResource = genmodel.eResource().getResourceSet().getResource(
							uriForEclassResourceEObject.trimFragment(), true);
					if(resourceResource != null) {
						// we get the ResourceEObject EClass
						EObject resourceEObjectEobject = resourceResource.getEObject(uriForEclassResourceEObject
								.fragment());
						if(resourceEObjectEobject instanceof EClass) {
							resourceEObjectEClass = (EClass)resourceEObjectEobject;
							result = addResourceEobjectInheritance(genmodel);
						}
					}
				}
			}
		}
		// if not found we have to save
		return result;
	}

	/**
	 * add the inheritance to each EObject
	 * 
	 * @param genmodel
	 */
	private Resource addResourceEobjectInheritance(GenModel genmodel) {
		// we resolve to have access to all the eobjects
		EcoreUtil.resolveAll(genmodel.eResource().getResourceSet());
		Resource result = null;
		List<GenPackage> packages = genmodel.getGenPackages();
		if(packages.size() > 0) {
			GenPackage genPackage = packages.get(0);
			currentEPackage = genPackage.getEcorePackage();
			result = currentEPackage.eResource();
			manageEPackage(currentEPackage);
		}
		return result;
	}

	/**
	 * Recursive method to browse EPackages
	 * 
	 * @param currentEPackage2
	 */
	private void manageEPackage(EPackage currentEPackage2) {
		for(EClassifier eclassifier : currentEPackage2.getEClassifiers()) {
			if(eclassifier instanceof EClass) {
				EClass eClass = (EClass)eclassifier;
				manageEClass(eClass);
			}
		}
		for(EPackage ePackage : currentEPackage2.getESubpackages()) {
			manageEPackage(ePackage);
		}

	}

	/**
	 * Recursive method to manage hierarchy tree
	 * 
	 * @param eClass
	 */
	private void manageEClass(EClass eClass) {
		if(eClass != resourceEObjectEClass) {
			if(eClass.getESuperTypes().size() == 0) {
				if(isACorrectEPackage(eClass.getEPackage())) {
					eClass.getESuperTypes().add(resourceEObjectEClass);
				}
			} else {
				boolean toAdd = true;
				for(EClass eClass2 : eClass.getESuperTypes()) {
					toAdd &= !isACorrectEPackage(eClass2.getEPackage());
					manageEClass(eClass2);
				}
				if(toAdd) {
					eClass.getESuperTypes().add(resourceEObjectEClass);
				}
			}
		}
	}

	/**
	 * determine if the ePackage given in parameter is a children of the current Epackage
	 * 
	 * @param ePackage
	 * @return
	 */
	private boolean isACorrectEPackage(EPackage ePackage) {
		EPackage current = ePackage;
		while(current != null && current != currentEPackage) {
			current = current.getESuperPackage();
		}
		return current == currentEPackage;
	}
}

Back to the top