Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: d66e8731f3e2cf8b33e7e533600edd2fb3a12288 (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
/*****************************************************************************
 * Copyright (c) 2013, 2017 CEA LIST 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:
 *   CEA LIST - Initial API and implementation
 *   Christian W. Damus (CEA) - bug 429242
 *   Christian W. Damus (CEA) - bug 422257
 *   Eike Stepper (CEA) - bug 466520
 *
 *****************************************************************************/
package org.eclipse.papyrus.cdo.internal.core.importer;

import java.util.Collection;
import java.util.Set;

import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.SubMonitor;
import org.eclipse.emf.cdo.explorer.CDOExplorerUtil;
import org.eclipse.emf.cdo.explorer.checkouts.CDOCheckout;
import org.eclipse.emf.cdo.transaction.CDOTransaction;
import org.eclipse.emf.cdo.util.CommitException;
import org.eclipse.emf.common.util.BasicDiagnostic;
import org.eclipse.emf.common.util.Diagnostic;
import org.eclipse.emf.common.util.DiagnosticChain;
import org.eclipse.emf.common.util.URI;
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.cdo.core.importer.IModelImportMapping;
import org.eclipse.papyrus.cdo.core.importer.IModelImporter;
import org.eclipse.papyrus.cdo.core.importer.IModelTransferConfiguration;
import org.eclipse.papyrus.cdo.core.importer.IModelTransferNode;
import org.eclipse.papyrus.cdo.core.importer.IModelTransferOperation;
import org.eclipse.papyrus.cdo.internal.core.Activator;
import org.eclipse.papyrus.cdo.internal.core.CDOUtils;
import org.eclipse.papyrus.cdo.internal.core.controlmode.CDOControlModeParticipant;
import org.eclipse.papyrus.cdo.internal.core.l10n.Messages;
import org.eclipse.papyrus.cdo.internal.core.resource.CDOSashModelProvider;
import org.eclipse.papyrus.infra.core.sashwindows.di.DiPackage;
import org.eclipse.papyrus.infra.core.sashwindows.di.PageList;
import org.eclipse.papyrus.infra.core.sashwindows.di.SashModel;
import org.eclipse.papyrus.infra.core.sashwindows.di.SashWindowsMngr;
import org.eclipse.papyrus.infra.core.sashwindows.di.util.DiUtils;
import org.eclipse.papyrus.infra.emf.utils.EMFHelper;

import com.google.common.collect.Sets;

/**
 * This is the ModelImporter type. Enjoy.
 */
public class ModelImporter implements IModelImporter {

	protected static final ContentType DI_CONTENT = new ContentType("DI"); //$NON-NLS-1$

	protected static final ContentType UML_CONTENT = new ContentType("UML"); //$NON-NLS-1$

	protected static final ContentType NOTATION_CONTENT = new ContentType("Notation"); //$NON-NLS-1$

	protected static final ContentType UNKNOWN_CONTENT = new ContentType("unknown"); //$NON-NLS-1$

	public ModelImporter() {
		super();
	}

	@Override
	public Diagnostic importModels(final IModelImportMapping mapping) {
		BasicDiagnostic result = new BasicDiagnostic();

		add(result, mapping.getConfiguration().validate());
		add(result, mapping.validate());

		if (result.getSeverity() < Diagnostic.ERROR) {
			add(result, mapping.getConfiguration().getOperationContext().run(new IModelTransferOperation() {

				@Override
				public Diagnostic run(IProgressMonitor monitor) {
					return doImport(mapping, monitor);
				}
			}));
		}

		return result;
	}

	protected Diagnostic doImport(IModelImportMapping mapping, IProgressMonitor monitor) {
		BasicDiagnostic result = new BasicDiagnostic();
		IModelTransferConfiguration configuration = mapping.getConfiguration();

		// by the time the configuration has analyzed every model to be
		// imported, all proxies have been resolved that can be. So,
		// there's no need for a further EcoreUtil.resolveAll() or such

		// 1/resource for import and 1/resource for sub-unit proxies
		// 1 for each transaction commit, 1 for saving affected non-imported models, and 1 for clean-up
		SubMonitor sub = SubMonitor.convert(monitor, Messages.ModelImporter_4, configuration.getModelsToTransfer().size() + 4);

		CDOCheckout checkout = mapping.getCheckout();
		CDOTransaction transaction = checkout.openTransaction(new ResourceSetImpl());
		ResourceSet destination = transaction.getResourceSet();

		try {
			for (IModelTransferNode model : configuration.getModelsToTransfer()) {
				add(result, importModel(model, configuration.getResourceSet(), mapping.getMapping(model), transaction, sub.newChild(1)));
			}

			try {
				transaction.commit(sub.newChild(1));

				// save sash resources (if any)
				for (Resource next : destination.getResources()) {
					// sash resource would have been saved by commit if it were a CDO URI
					if (DependencyAdapter.isDIResource(next) && !CDOUtils.isCDOURI(next.getURI())) {
						next.save(null);
					}
				}
			} catch (Exception e) {
				result.add(new BasicDiagnostic(IStatus.ERROR, Activator.PLUGIN_ID, 0, Messages.ModelImporter_5, new Object[] { e }));
			}

			// can't create CDO-style proxies until the resources have been committed, because only then
			// will the objects be persisted and have OIDs to reference
			boolean hasSubUnits = false;
			for (IModelTransferNode model : configuration.getModelsToTransfer()) {
				if (createSubUnitProxies(model, mapping.getMapping(model), transaction, sub.newChild(1))) {
					hasSubUnits = true;
				}
			}
			if (hasSubUnits) {
				try {
					transaction.commit(sub.newChild(1));
				} catch (CommitException e) {
					result.add(new BasicDiagnostic(IStatus.ERROR, Activator.PLUGIN_ID, 0, Messages.ModelImporter_5, new Object[] { e }));
				}
			} else {
				sub.worked(1); // nothing to commit but still count progress
			}

			try {
				saveNonimportedModels(mapping, transaction, sub.newChild(1));
			} catch (Exception e) {
				result.add(new BasicDiagnostic(IStatus.ERROR, Activator.PLUGIN_ID, 0, Messages.ModelImporter_6, new Object[] { e }));
			}
		} finally {
			EMFHelper.unload(configuration.getResourceSet());
			CDOUtils.unload(transaction);
			transaction.close();
			EMFHelper.unload(destination);
			sub.worked(1);
		}

		sub.done();

		return result;
	}

	protected Diagnostic importModel(IModelTransferNode model, ResourceSet rset, IPath toPath, CDOTransaction transaction, IProgressMonitor monitor) {
		BasicDiagnostic result = new BasicDiagnostic();

		IPath basePath = toPath.removeFileExtension();

		SubMonitor sub = SubMonitor.convert(monitor, model.getName(), model.getResourceURIs().size());

		for (URI next : model.getResourceURIs()) {
			Resource destination = transaction.getOrCreateResource(basePath.addFileExtension(next.fileExtension()).toString());
			Resource source = rset.getResource(next, true);

			if (model.getConfiguration().isStripSashModelContent() && DependencyAdapter.isDIResource(source)) {
				// import *.di content into the *.sash
				CDOCheckout checkout = CDOExplorerUtil.getCheckout(transaction);
				URI sashURI = new CDOSashModelProvider().initialize(checkout).getSashModelURI(destination.getURI());
				ResourceSet dset = destination.getResourceSet();
				Resource sashResource = dset.getURIConverter().exists(sashURI, null) ? dset.getResource(sashURI, true) : null;
				if (sashResource == null) {
					sashResource = dset.createResource(sashURI);
				}
				destination = sashResource;
			}

			add(result, importResource(source, destination));
			sub.worked(1);
		}

		sub.done();

		return result;
	}

	protected boolean createSubUnitProxies(IModelTransferNode model, IPath toPath, CDOTransaction transaction, IProgressMonitor monitor) {
		boolean result;

		IPath basePath = toPath.removeFileExtension();
		URI uri = model.getPrimaryResourceURI();

		SubMonitor sub = SubMonitor.convert(monitor, model.getName(), 1);

		Resource destination = transaction.getResource(basePath.addFileExtension(uri.fileExtension()).toString());
		CDOControlModeParticipant.IUpdate update = new CDOControlModeParticipant().getProxyCrossReferencesUpdate(destination);
		result = !update.isEmpty();
		update.apply(); // no harm in applying an empty update
		sub.worked(1);

		sub.done();

		return result;
	}

	protected Diagnostic importResource(Resource source, Resource destination) {
		if (!destination.getContents().isEmpty()) {
			ContentType contentType = getContentType(source);
			if (contentType == DI_CONTENT) {
				mergeDIContent(source, destination);
			} else {
				// just append the additional content
				destination.getContents().addAll(source.getContents());
			}
		} else {
			destination.getContents().addAll(source.getContents());
		}

		return Diagnostic.OK_INSTANCE;
	}

	/**
	 * Determines the content-type of a resource for the purpose of combining
	 * content.
	 *
	 * @param resource
	 *            a resource to be combined with existing content
	 *
	 * @return the content type
	 */
	protected ContentType getContentType(Resource resource) {
		ContentType result = UNKNOWN_CONTENT;

		for (EObject next : resource.getContents()) {
			EPackage ePackage = next.eClass().getEPackage();
			if (ePackage == DiPackage.eINSTANCE) {
				result = DI_CONTENT;
				break;
			}
			if (ePackage.getName().equalsIgnoreCase("uml")) { //$NON-NLS-1$
				result = UML_CONTENT;
				break;
			}
			if (ePackage.getName().equalsIgnoreCase("notation")) { //$NON-NLS-1$
				result = NOTATION_CONTENT;
				break;
			}
		}

		return result;
	}

	protected void mergeDIContent(Resource source, Resource destination) {
		// snip out the source window manager and get its counterpart
		SashWindowsMngr srcMngr = DiUtils.lookupSashWindowsMngr(source);
		EcoreUtil.remove(srcMngr);
		SashWindowsMngr dstMngr = DiUtils.lookupSashWindowsMngr(destination);

		// merge the window manager contents
		if (dstMngr == null) {
			destination.getContents().add(0, srcMngr);
		} else {
			SashModel dstModel = dstMngr.getSashModel();
			SashModel srcModel = srcMngr.getSashModel();

			if (dstModel == null) {
				dstMngr.setSashModel(srcModel);
			} else {
				dstModel.getWindows().addAll(srcModel.getWindows());
				if (dstModel.getCurrentSelection() == null) {
					dstModel.setCurrentSelection(srcModel.getCurrentSelection());
				}
			}

			PageList dstPages = dstMngr.getPageList();
			PageList srcPages = srcMngr.getPageList();

			if (dstPages == null) {
				dstMngr.setPageList(srcPages);
			} else {
				dstPages.getAvailablePage().addAll(srcPages.getAvailablePage());
			}
		}

		// and add all of the tables and other content
		destination.getContents().addAll(source.getContents());
	}

	protected Diagnostic saveNonimportedModels(IModelImportMapping mapping, CDOTransaction transaction, IProgressMonitor monitor) {
		IModelTransferConfiguration configuration = mapping.getConfiguration();

		BasicDiagnostic result = new BasicDiagnostic();

		Collection<IModelTransferNode> imported = configuration.getModelsToTransfer();
		Set<IModelTransferNode> nonImported = Sets.newHashSet();

		for (IModelTransferNode next : configuration.getModelsToTransfer()) {
			for (IModelTransferNode dependent : next.getDependents()) {
				if (!imported.contains(dependent)) {
					nonImported.add(dependent);
				}
			}
		}

		if (!nonImported.isEmpty()) {
			SubMonitor sub = SubMonitor.convert(monitor, Messages.ModelImporter_9, nonImported.size());

			ResourceSet rset = configuration.getResourceSet();

			try {
				for (IModelTransferNode next : nonImported) {
					for (URI uri : next.getResourceURIs()) {
						Resource resource = rset.getResource(uri, false);

						// if the resource is modified, then we imported it, so
						// don't save
						if ((resource != null) && !resource.isModified()) {
							try {
								resource.save(null);
							} catch (Exception e) {
								add(result, new BasicDiagnostic(IStatus.ERROR, Activator.PLUGIN_ID, 0, Messages.ModelImporter_10, new Object[] { e }));
							}
						}
					}

					sub.worked(1);
				}
			} finally {
				sub.done();
			}
		}

		return result;
	}

	private static void add(DiagnosticChain diagnostics, Diagnostic diagnostic) {
		if (diagnostic.getSeverity() > Diagnostic.OK) {
			diagnostics.merge(diagnostic);
		}
	}

	//
	// Nested types
	//

	protected static class ContentType {

		private final String name;

		protected ContentType(String name) {
			this.name = name;
		}

		public final String getName() {
			return name;
		}

		@Override
		public int hashCode() {
			return getName().hashCode();
		}

		@Override
		public boolean equals(Object obj) {
			return (obj instanceof ContentType) && ((ContentType) obj).getName().equals(getName());
		}

		@Override
		public String toString() {
			return getName() + " content"; //$NON-NLS-1$
		}
	}
}

Back to the top