Skip to main content
summaryrefslogtreecommitdiffstats
blob: 5d42fd71ca847673a0bd12f5b6f87535a409fb1f (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
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
/*****************************************************************************
 * Copyright (c) 2014 CEA LIST.
 *
 * 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:
 *  Camille Letavernier (CEA LIST) camille.letavernier@cea.fr - Initial API and implementation
 *****************************************************************************/
package org.eclipse.papyrus.migration.rsa.transformation;

import java.io.IOException;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.atomic.AtomicReference;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.MultiStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.IJobChangeEvent;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.core.runtime.jobs.JobChangeAdapter;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.transaction.RollbackException;
import org.eclipse.emf.transaction.Transaction;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.emf.transaction.impl.InternalTransactionalEditingDomain;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.papyrus.infra.core.resource.IEMFModel;
import org.eclipse.papyrus.infra.core.resource.IModel;
import org.eclipse.papyrus.infra.core.resource.ModelMultiException;
import org.eclipse.papyrus.infra.core.resource.ModelSet;
import org.eclipse.papyrus.infra.core.utils.DiResourceSet;
import org.eclipse.papyrus.infra.emf.resource.DependencyManagementHelper;
import org.eclipse.papyrus.infra.emf.utils.EMFHelper;
import org.eclipse.papyrus.infra.gmfdiag.common.utils.GMFUnsafe;
import org.eclipse.papyrus.migration.rsa.Activator;
import org.eclipse.papyrus.migration.rsa.RSAToPapyrusParameters.Config;
import org.eclipse.papyrus.migration.rsa.RSAToPapyrusParameters.MappingParameters;
import org.eclipse.papyrus.migration.rsa.RSAToPapyrusParameters.URIMapping;
import org.eclipse.papyrus.migration.rsa.internal.schedule.JobWrapper;
import org.eclipse.papyrus.migration.rsa.internal.schedule.Schedulable;
import org.eclipse.papyrus.migration.rsa.internal.schedule.Scheduler;
import org.eclipse.papyrus.migration.rsa.internal.schedule.TransformationWrapper;
import org.eclipse.papyrus.migration.rsa.transformation.ui.URIMappingDialog;
import org.eclipse.papyrus.uml.tools.model.UmlModel;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.console.ConsolePlugin;
import org.eclipse.ui.console.IConsole;
import org.eclipse.ui.console.IConsoleManager;
import org.eclipse.ui.console.MessageConsole;
import org.eclipse.ui.console.MessageConsoleStream;
import org.eclipse.ui.dialogs.SelectionDialog;
import org.eclipse.ui.statushandlers.StatusManager;

/**
 * Executes a batch of {@link ImportTransformation}s, then restores the dependencies (References)
 * between each other
 *
 * @author Camille Letavernier
 *
 */
public class ImportTransformationLauncher {

	// Nano to Second
	protected final static long SECOND = 1000 * 1000 * 1000;

	// Nano to Milliseconds
	protected final static long MILLIS = 1000 * 1000;

	protected final Config config;

	protected final Control baseControl;

	/**
	 * Own execution time, in nano-seconds. Doesn't take individual transformation's exec
	 * time into account. Also ignores the time when Blocking user Dialogs are opened
	 */
	protected long ownExecutionTime;

	/**
	 * Own loading time (For initial model loading)
	 */
	protected long ownLoadingTime;

	/**
	 * Own cumulated execution time for repairing stereotypes
	 */
	protected long ownRepairStereotypesTime;

	/**
	 * Own cumulated execution time for repairing libraries
	 */
	protected long ownRepairLibrariesTime;

	/**
	 * Own execution time for resolving all matches for broken profiles/libraries
	 */
	protected long resolveAllDependencies;

	/**
	 * The top-level job for this transformation
	 */
	protected Job importDependenciesJob;

	/**
	 * Total time for all invidivual transformations to complete. Since they are executed in parallel,
	 * this may be different from their cumulated execution time (Unless a single thread is used)
	 */
	protected long transformationsExecutionTime = 0L;

	protected DependencyAnalysisHelper analysisHelper;

	public ImportTransformationLauncher(Config config) {
		this(config, null);
	}

	public ImportTransformationLauncher(Config config, Control baseControl) {
		this.config = config;
		this.baseControl = baseControl;
	}

	/**
	 * Executes the transformation (Asynchronous)
	 *
	 * @param urisToImport
	 */
	public void run(List<URI> urisToImport) {
		List<ImportTransformation> transformations = new LinkedList<ImportTransformation>();

		analysisHelper = new DependencyAnalysisHelper(config);

		for (URI uri : urisToImport) {
			ImportTransformation transformation = new ImportTransformation(uri, config, analysisHelper);
			transformations.add(transformation);
		}

		// Always use the batch launcher, even if there is only 1 transformation (Bug 455012)
		importModels(transformations);
	}

	/**
	 * Start a Job and delegate to {@link #importModels(IProgressMonitor, List)}
	 *
	 * @param transformations
	 */
	protected void importModels(final List<ImportTransformation> transformations) {
		importDependenciesJob = new Job("Import Models") {

			@Override
			protected IStatus run(IProgressMonitor monitor) {
				IStatus result = ImportTransformationLauncher.this.importModels(monitor, transformations);

				if (monitor.isCanceled()) {
					return new Status(IStatus.CANCEL, Activator.PLUGIN_ID, "Operation Canceled");
				}

				long cumulatedLoadingTime = 0L;
				long cumulatedTransformationTime = 0L;
				long cumulatedHandleDanglingTime = 0L;
				long cumulatedImportRTTime = 0L;
				for (ImportTransformation transformation : transformations) {
					cumulatedLoadingTime += transformation.getLoadingTime();
					cumulatedImportRTTime += transformation.getImportRTTime();
					cumulatedHandleDanglingTime += transformation.getHandleDanglingRefTime();

					cumulatedTransformationTime += transformation.getExecutionTime();

					log("Import " + transformation.getModelName());
					log("First phase (0-50%):");
					log("\tTotal loading time: " + timeFormat(transformation.getLoadingTime()));
					log("\tTotal Import UML-RT time: " + timeFormat(transformation.getImportRTTime()));
					log("\tTotal Handle Dangling References time: " + timeFormat(transformation.getHandleDanglingRefTime()));
					log("\tTotal execution time: " + timeFormat(transformation.getExecutionTime()));

					Long loadingTime = loadingTimeV2.get(transformation);
					Long repairProxiesTime = proxiesTime.get(transformation);
					Long repairStereoTime = stereoTime.get(transformation);
					Long totalPhase2 = totalTimeV2.get(transformation);

					log("Second phase (50-100%):");
					log("\tTotal loading time: " + timeFormat(loadingTime));
					log("\tTotal fix proxies time: " + timeFormat(repairProxiesTime));
					log("\tTotal fix stereotypes time: " + timeFormat(repairStereoTime));
					log("\tTotal execution time: " + timeFormat(totalPhase2));

					log("Total");
					log("\tTotal execution time: " + timeFormat(transformation.getExecutionTime() + totalPhase2));
					log("\n");
				}

				int nbThreads = Math.max(1, config.getMaxThreads());
				log("First phase (0-50%) / " + nbThreads + " Threads");
				log("\tCumulated Transformation Time: " + timeFormat(cumulatedTransformationTime));
				log("\tCumulated Loading Time: " + timeFormat(cumulatedLoadingTime));
				log("\tCumulated Handle Dangling Refs Time: " + timeFormat(cumulatedHandleDanglingTime));
				log("\tCumulated Import RT Time: " + timeFormat(cumulatedImportRTTime));
				log("\tTotal Transformation Time: " + timeFormat(transformationsExecutionTime));

				log("Second phase (50-100%) / " + nbThreads + " Threads");
				log("\tTotal Handle all Dangling References: " + timeFormat(resolveAllDependencies));
				log("\tCumulated Loading Time: " + timeFormat(ownLoadingTime));
				log("\tCumulated Fix Libraries Time: " + timeFormat(ownRepairLibrariesTime));
				log("\tCumulated Fix Stereotypes Time: " + timeFormat(ownRepairStereotypesTime));
				log("\tTotal Fix Dependencies Time: " + timeFormat(ownExecutionTime));

				log("Total");
				log("\tCumulated Total time: " + timeFormat(ownExecutionTime + cumulatedTransformationTime));
				log("\tTotal time: " + timeFormat(ownExecutionTime + transformationsExecutionTime));

				log("Import Complete");
				log("");

				return result;
			}

		};

		importDependenciesJob.addJobChangeListener(new JobChangeAdapter() {
			@Override
			public void done(IJobChangeEvent event) {

				MultiStatus multiStatus = new MultiStatus(Activator.PLUGIN_ID, IStatus.OK, "", null);

				multiStatus.merge(event.getResult());

				for (ImportTransformation transformation : transformations) {
					multiStatus.merge(transformation.getStatus());
				}

				int severity = multiStatus.getSeverity();
				String message;

				switch (severity) {
				case IStatus.OK:
					message = "The selected models have been successfully imported";
					break;
				case IStatus.CANCEL:
					message = "Operation canceled";
					break;
				case IStatus.WARNING:
					message = "The selected models have been imported; some warnings have been reported";
					break;
				default:
					message = "Some errors occurred during model import";
					break;
				}


				handle(new MultiStatus(Activator.PLUGIN_ID, severity, multiStatus.getChildren(), message, null));
			}

			protected void handle(final IStatus status) {
				if (baseControl == null) {
					int severity = status.getSeverity();
					if (severity == IStatus.OK || severity == IStatus.CANCEL) {
						return;
					}

					StatusManager.getManager().handle(status, StatusManager.LOG);
					return;
				}

				Display display = baseControl.getDisplay();

				if (status.getSeverity() == IStatus.OK) {
					display.asyncExec(new Runnable() {

						@Override
						public void run() {
							MessageDialog.openInformation(baseControl.getShell(), "Import models", status.getMessage());
						}
					});

				} else if (status.getSeverity() == IStatus.CANCEL) {
					display.asyncExec(new Runnable() {

						@Override
						public void run() {
							MessageDialog.openInformation(baseControl.getShell(), "Import models", status.getMessage());
						}
					});
				} else {
					StatusManager.getManager().handle(status, StatusManager.BLOCK);
				}
			}
		});

		importDependenciesJob.setUser(true);
		importDependenciesJob.schedule();
	}

	protected void log(String message) {
		System.out.println(message);

		MessageConsole console = getConsole();
		MessageConsoleStream out = console.newMessageStream();
		out.println(message);
	}

	protected static final String CONSOLE_NAME = "Model Import Results"; // The name is both the ID and the Label

	protected MessageConsole getConsole() {
		ConsolePlugin plugin = ConsolePlugin.getDefault();
		IConsoleManager consoleManager = plugin.getConsoleManager();
		IConsole[] existing = consoleManager.getConsoles();
		for (int i = 0; i < existing.length; i++) {
			if (CONSOLE_NAME.equals(existing[i].getName())) {
				return (MessageConsole) existing[i];
			}
		}
		// no console found, so create a new one
		MessageConsole rsaConsole = new MessageConsole(CONSOLE_NAME, null);
		consoleManager.addConsoles(new IConsole[] { rsaConsole });
		return rsaConsole;
	}

	protected String timeFormat(Long nano) {
		if (nano == null) {
			return "?"; // FIXME: crash?
		}
		long seconds = nano / SECOND;
		if (seconds < 1) {
			long millis = nano / MILLIS;
			return String.format("%s ms", millis);
		}
		return String.format("%d:%02d:%02d", seconds / 3600, (seconds % 3600) / 60, (seconds % 60));
	}

	/**
	 * Schedules all the individual transformations, wait for completion, then
	 * call {@link #handleModelDependencies(List, IProgressMonitor)}
	 *
	 * @param monitor
	 * @param transformations
	 * @return
	 */
	protected IStatus importModels(IProgressMonitor monitor, List<ImportTransformation> transformations) {

		long begin = System.nanoTime();

		monitor.setTaskName("Waiting for import tasks to complete...");
		int numTasks = transformations.size() * 2; // For each transformation: wait for completion, then handle dependencies
		monitor.beginTask("Importing Models...", numTasks);

		List<Schedulable> tasks = new LinkedList<Schedulable>();

		for (ImportTransformation transformation : transformations) {
			tasks.add(new TransformationWrapper(transformation));
		}

		Scheduler scheduler = new Scheduler(config.getMaxThreads());
		scheduler.schedule(monitor, tasks);

		long end = System.nanoTime();
		transformationsExecutionTime = end - begin;

		if (monitor.isCanceled()) {
			return new Status(IStatus.CANCEL, Activator.PLUGIN_ID, "Operation canceled");
		}

		handleModelDependencies(transformations, monitor);

		return Status.OK_STATUS;
	}

	/**
	 * Convert all model dependencies (For "imported model -> emx library" to "imported model -> imported library")
	 * Also repairs profile and stereotype applications
	 *
	 * @param transformations
	 * @param monitor
	 */
	protected void handleModelDependencies(List<ImportTransformation> transformations, IProgressMonitor monitor) {

		long begin = System.nanoTime();

		long timeToIgnore = 0L;


		final Map<URI, URI> urisToReplace = new HashMap<URI, URI>();
		final Map<URI, URI> profileUrisToReplace = new HashMap<URI, URI>();

		for (ImportTransformation transformation : transformations) {
			urisToReplace.putAll(transformation.getURIMappings());
			profileUrisToReplace.putAll(transformation.getProfileURIMappings());
		}

		monitor.subTask("Analysing unresolved references...");
		long startResolveAll = System.nanoTime();
		analysisHelper.resolveAllMappings(urisToReplace, profileUrisToReplace);
		long endResolveAll = System.nanoTime();
		resolveAllDependencies = endResolveAll - startResolveAll;

		if (!config.getMappingParameters().getUriMappings().isEmpty() || !config.getMappingParameters().getProfileUriMappings().isEmpty()) {

			long beginDialog = System.nanoTime();
			MappingParameters parameters = confirmURIMappings(config.getMappingParameters());
			long endDialog = System.nanoTime();
			timeToIgnore = endDialog - beginDialog;

			config.setMappingParameters(parameters);

			// Include the user-defined URI mappings
			populateURIMap(parameters.getUriMappings(), urisToReplace);
			populateURIMap(parameters.getUriMappings(), profileUrisToReplace);
			populateURIMap(parameters.getProfileUriMappings(), profileUrisToReplace);
		}


		List<Schedulable> tasks = new LinkedList<Schedulable>();
		for (final ImportTransformation transformation : transformations) {
			Job transformationJob = new Job("Importing dependencies for " + transformation.getModelName()) {
				@Override
				protected IStatus run(IProgressMonitor monitor) {
					long startFix = System.nanoTime();
					fixDependencies(transformation, monitor, urisToReplace, profileUrisToReplace);
					long endFix = System.nanoTime();
					synchronized (ImportTransformationLauncher.this) {
						totalTimeV2.put(transformation, endFix - startFix);
					}

					return Status.OK_STATUS;
				}
			};

			tasks.add(new JobWrapper(transformationJob));
		}

		Scheduler scheduler = new Scheduler(config.getMaxThreads());
		scheduler.schedule(monitor, tasks);

		long end = System.nanoTime();

		ownExecutionTime = end - begin - timeToIgnore;
	}

	final protected Map<ImportTransformation, Long> loadingTimeV2 = new HashMap<ImportTransformation, Long>();

	final protected Map<ImportTransformation, Long> proxiesTime = new HashMap<ImportTransformation, Long>();

	final protected Map<ImportTransformation, Long> stereoTime = new HashMap<ImportTransformation, Long>();

	final protected Map<ImportTransformation, Long> totalTimeV2 = new HashMap<ImportTransformation, Long>();

	protected IStatus fixDependencies(ImportTransformation transformation, IProgressMonitor monitor, Map<URI, URI> urisToReplace, Map<URI, URI> profileUrisToReplace) {
		monitor.subTask("Importing dependencies for " + transformation.getModelName());
		final ModelSet modelSet = new DiResourceSet();

		final Collection<Resource> resourcesToRepair;
		try {
			URI targetURI = transformation.getTargetURI();
			if (targetURI == null) {
				// The transformation didn't complete properly
				monitor.worked(1);
				return Status.OK_STATUS;
			}

			long startLoading = System.nanoTime();
			modelSet.loadModels(transformation.getTargetURI());
			resourcesToRepair = resolveOwnResources(modelSet);
			long endLoading = System.nanoTime();
			synchronized (ImportTransformationLauncher.this) {
				ownLoadingTime += endLoading - startLoading;
				loadingTimeV2.put(transformation, endLoading - startLoading);
			}
		} catch (ModelMultiException e) {
			Activator.log.error(e);
			monitor.worked(1);
			return Status.OK_STATUS;
		}

		try {
			long startProxies = System.nanoTime();
			repairProxies(modelSet, resourcesToRepair, urisToReplace, monitor); // Repairing proxies first will change the Applied Profiles. This helps repairing stereotypes
			long endProxies = System.nanoTime();
			synchronized (ImportTransformationLauncher.this) {
				ownRepairLibrariesTime += endProxies - startProxies;
				proxiesTime.put(transformation, endProxies - startProxies);
			}
		} catch (Exception ex) {
			Activator.log.error(ex);
			return new Status(IStatus.ERROR, Activator.PLUGIN_ID, "An exception occurred when repairing library dependencies", ex);
		}

		RepairStereotypes repairStereotypesAction = new RepairStereotypes(modelSet, resourcesToRepair, profileUrisToReplace);
		try {
			long startStereotypes = System.nanoTime();
			repairStereotypesAction.execute();
			long endStereotypes = System.nanoTime();
			synchronized (ImportTransformationLauncher.this) {
				ownRepairStereotypesTime += endStereotypes - startStereotypes;
				stereoTime.put(transformation, endStereotypes - startStereotypes);
			}
		} catch (Exception ex) {
			Activator.log.error(ex);
			return new Status(IStatus.ERROR, Activator.PLUGIN_ID, "An exception occurred when repairing profiles/stereotypes", ex);
		}

		try {

			for (Resource resource : resourcesToRepair) {
				resource.save(null);
			}
			monitor.worked(1);

			final TransactionalEditingDomain domain = modelSet.getTransactionalEditingDomain();

			GMFUnsafe.write(domain, new Runnable() {
				@Override
				public void run() {
					EMFHelper.unload(modelSet);
				}
			});

			domain.dispose();

		} catch (IOException ex) {
			Activator.log.error(ex);
		} catch (RollbackException ex) {
			Activator.log.error(ex);
		} catch (InterruptedException ex) {
			Activator.log.error(ex);
		}

		return Status.OK_STATUS;
	}

	protected Collection<Resource> resolveOwnResources(ModelSet modelSet) {
		UmlModel umlModel = (UmlModel) modelSet.getModel(UmlModel.MODEL_ID);
		if (umlModel == null) {
			return Collections.emptySet();
		}

		// Iterate on the main resource's contents (Including fragments).
		// The ModelSet will take care of loading any associated resource (notation, di)

		Resource mainResource = umlModel.getResource();
		if (mainResource == null) {
			return Collections.emptySet();
		}

		Iterator<EObject> contents = mainResource.getAllContents();
		while (contents.hasNext()) {
			contents.next();
		}

		Set<Resource> resourcesToRepair = new HashSet<Resource>();
		for (Resource resource : modelSet.getResources()) {
			if (isMainModelResource(modelSet, resource)) {
				resourcesToRepair.add(resource);
			}
		}
		return resourcesToRepair;
	}

	/**
	 * A resource belongs to the main model if it is one of the 4-files model (Di, Notation, Uml, Sash), or if it is
	 * a resource associated to a Sub-model of the main model (i.e. child of the main UML resource,
	 * or resource associated to a child of the main UML resource)
	 *
	 * @param modelSet
	 * @param resource
	 * @return
	 */
	protected boolean isMainModelResource(ModelSet modelSet, Resource resource) {
		IModel model = modelSet.getModelFor(resource);
		if (model instanceof IEMFModel) {
			IEMFModel logicalModel = (IEMFModel) model;
			if (logicalModel.getResource() == resource || logicalModel.isControlled(resource)) {
				return true;
			}
		}

		return false;
	}

	protected void repairProxies(final ModelSet modelSet, final Collection<Resource> resourcesToRepair, Map<URI, URI> urisToReplace, IProgressMonitor monitor) throws InterruptedException, RollbackException {

		final TransactionalEditingDomain domain = modelSet.getTransactionalEditingDomain();

		for (final Map.Entry<URI, URI> entry : urisToReplace.entrySet()) {
			if (monitor.isCanceled()) {
				return;
			}

			if (entry.getKey().equals(entry.getValue())) {
				continue;
			}

			InternalTransactionalEditingDomain internalDomain = (InternalTransactionalEditingDomain) domain;

			Map<String, Object> options = new HashMap<String, Object>();
			options.put(Transaction.OPTION_NO_UNDO, true);
			options.put(Transaction.OPTION_NO_VALIDATION, true);
			options.put(Transaction.OPTION_NO_TRIGGERS, true);

			// We're in a batch environment, with no undo/redo support. Run a vanilla transaction to improve performances
			Transaction fastTransaction = internalDomain.startTransaction(false, options);
			try {
				DependencyManagementHelper.updateDependencies(entry.getKey(), entry.getValue(), resourcesToRepair, domain);
			} finally {
				fastTransaction.commit();
			}
		}
	}

	/**
	 * Convert and add all the URIMappings into the URI Map
	 *
	 * @param mappings
	 * @param uriMap
	 */
	protected static void populateURIMap(List<URIMapping> mappings, Map<URI, URI> uriMap) {
		for (URIMapping mapping : mappings) {
			if (mapping == null) {
				continue;
			}
			String source = mapping.getSourceURI();
			String target = mapping.getTargetURI();

			if (source != null && target != null && !source.trim().isEmpty() && !target.trim().isEmpty()) {

				URI sourceURI = URI.createURI(mapping.getSourceURI());
				URI targetURI = URI.createURI(mapping.getTargetURI());

				if (uriMap.containsKey(sourceURI)) {
					continue;
				}

				uriMap.put(sourceURI, targetURI);
			}
		}
	}

	protected MappingParameters confirmURIMappings(final MappingParameters mappingParameters) {
		if (config.isAlwaysAcceptSuggestedMappings()) {
			return mappingParameters;
		}

		final AtomicReference<MappingParameters> newParameters = new AtomicReference<MappingParameters>(mappingParameters);

		if (baseControl != null && !baseControl.isDisposed()) {
			baseControl.getDisplay().syncExec(new Runnable() {
				@Override
				public void run() {
					newParameters.set(openMappingsDialog(mappingParameters));
				}
			});
		}

		return newParameters.get();
	}

	protected MappingParameters openMappingsDialog(final MappingParameters mappingParameters) {
		final Shell shell = baseControl.getShell();

		SelectionDialog dialog = new URIMappingDialog(shell, mappingParameters);

		dialog.open();
		return (MappingParameters) dialog.getResult()[0];
	}

	/** Mainly for test purpose */
	public void waitForCompletion() throws Exception {
		importDependenciesJob.join();
	}

	/** Mainly for test purpose */
	public IStatus getResult() {
		return importDependenciesJob.getResult();
	}
}

Back to the top