Bug 518188 - [SysML1.4][Oxygen] Model migration 

 - set the SysML 1.4 architecture when a SysML 1.4 diagram or table id
found.
 - :warning: this won't go well if all DSML proceed the same way but it
remains better that the current implementation
 
Change-Id: I0e3969e5ab488f2a9811dd38cefc3f690744b830
Signed-off-by: Benoit Maggi <benoit.maggi@cea.fr>
diff --git a/gui/org.eclipse.papyrus.sysml14.architecture/src/org/eclipse/papyrus/sysml14/architecture/internal/migration/SysMLNeonToOxygenDiagramReconciler.java b/gui/org.eclipse.papyrus.sysml14.architecture/src/org/eclipse/papyrus/sysml14/architecture/internal/migration/SysMLNeonToOxygenDiagramReconciler.java
index 10b366f..08d5646 100644
--- a/gui/org.eclipse.papyrus.sysml14.architecture/src/org/eclipse/papyrus/sysml14/architecture/internal/migration/SysMLNeonToOxygenDiagramReconciler.java
+++ b/gui/org.eclipse.papyrus.sysml14.architecture/src/org/eclipse/papyrus/sysml14/architecture/internal/migration/SysMLNeonToOxygenDiagramReconciler.java
@@ -13,13 +13,23 @@
 
 import java.util.Collection;
 
+import org.eclipse.emf.common.command.Command;
 import org.eclipse.emf.common.util.URI;
+import org.eclipse.emf.ecore.resource.Resource;
+import org.eclipse.emf.ecore.resource.ResourceSet;
 import org.eclipse.emf.ecore.util.EcoreUtil;
+import org.eclipse.emf.transaction.RecordingCommand;
+import org.eclipse.gmf.runtime.common.core.command.CompositeCommand;
+import org.eclipse.gmf.runtime.common.core.command.ICommand;
 import org.eclipse.gmf.runtime.notation.Diagram;
 import org.eclipse.papyrus.infra.architecture.ArchitectureDomainManager;
+import org.eclipse.papyrus.infra.core.architecture.ArchitectureDescription;
 import org.eclipse.papyrus.infra.core.architecture.RepresentationKind;
 import org.eclipse.papyrus.infra.core.architecture.merged.MergedArchitectureContext;
 import org.eclipse.papyrus.infra.core.architecture.merged.MergedArchitectureDescriptionLanguage;
+import org.eclipse.papyrus.infra.core.resource.ModelSet;
+import org.eclipse.papyrus.infra.core.resource.sasheditor.DiModelUtils;
+import org.eclipse.papyrus.infra.emf.gmf.command.EMFtoGMFCommandWrapper;
 import org.eclipse.papyrus.infra.gmfdiag.common.reconciler.DiagramReconciler_1_3_0;
 import org.eclipse.papyrus.infra.gmfdiag.representation.PapyrusDiagram;
 import org.eclipse.papyrus.infra.viewpoints.configuration.PapyrusView;
@@ -31,7 +41,7 @@
  * PapyrusViewStyle by the new PapyrusDiagramStyle (Will move Diagram version
  * annotation from 1.2.0 to 1.3.0)
  */
-@SuppressWarnings({ "deprecation", "nls" }) // Warning are not pertinent this migration should remains on old API
+@SuppressWarnings({ "deprecation", "nls" }) // Warning are not pertinent this migration should remains on old deprecated API
 public class SysMLNeonToOxygenDiagramReconciler extends DiagramReconciler_1_3_0 {
 	
 	private static final String BLOCK_DEFINITION_DIAGRAM_URI = "platform:/plugin/org.eclipse.papyrus.sysml14.diagram.blockdefinition/resources/configuration/blockDefinitionDiagram.configuration#_ZrBf4JaBEeS8eNvSwD0lgA";
@@ -39,6 +49,8 @@
 	private static final String PARAMETRIC_DIAGRAM_URI = "platform:/plugin/org.eclipse.papyrus.sysml14.diagram.parametric/resources/configuration/parametricDiagram.configuration#_ZrBf4JaBEeS8eNvSwD0lgA";
 	private static final String REQUIREMENT_DIAGRAM_URI = "platform:/plugin/org.eclipse.papyrus.sysml14.diagram.requirement/resources/configuration/requirementDiagram.configuration#_ZrBf4JaBEeS8eNvSwD0lgA";
 
+	private static final String SYSML14_CONTEXT_ID = "org.eclipse.papyrus.sysml.architecture.SysML14";
+	
 	/**
 	 * @see org.eclipse.papyrus.infra.gmfdiag.common.reconciler.DiagramReconciler_1_3_0#getPapyrusDiagram(org.eclipse.papyrus.infra.viewpoints.style.PapyrusViewStyle)
 	 *
@@ -71,7 +83,40 @@
 		return null;
 	}
 
+	@Override
+	public ICommand getReconcileCommand(Diagram diagram) {
+		ICommand reconcileCommand = super.getReconcileCommand(diagram);
+		if (diagram != null) {
+			Resource eResource = diagram.eResource();
+			if (eResource != null) {
+				ResourceSet resourceSet = eResource.getResourceSet();
+				if (resourceSet instanceof ModelSet) {
+					CompositeCommand compositeCommand = new CompositeCommand("Update viewpoints from configuration to architecture and set SysML 1.4 as default architecture");
+					compositeCommand.add(reconcileCommand);
+					Command setContextCommand = getSetContextCommand( (ModelSet) resourceSet ,SYSML14_CONTEXT_ID);
+					compositeCommand.add(EMFtoGMFCommandWrapper.wrap(setContextCommand));
+					return compositeCommand;
+				}				
+			}
+		}
+		return reconcileCommand;
+	}
+	
+	protected Command getSetContextCommand(ModelSet modelSet, String contextId) {
+		return new RecordingCommand(modelSet.getTransactionalEditingDomain()) {
+			@Override
+			protected void doExecute() {
+				ArchitectureDescription description = DiModelUtils.getOrAddArchitectureDescription(modelSet);
+				description.setContextId(contextId);
+			}
+		};
+	}
+	
 	///////////////////////////////////////////////////////////////////////////////
+	
+	
+
+	
 	// FIXME: should be in Papyrus core API
 	protected PapyrusDiagram getPapyrusDiagramById(String id) {
 		ArchitectureDomainManager manager = ArchitectureDomainManager.getInstance();
diff --git a/gui/org.eclipse.papyrus.sysml14.architecture/src/org/eclipse/papyrus/sysml14/architecture/internal/migration/SysMLNeonToOxygenTableReconciler.java b/gui/org.eclipse.papyrus.sysml14.architecture/src/org/eclipse/papyrus/sysml14/architecture/internal/migration/SysMLNeonToOxygenTableReconciler.java
index a4db1d5..47ef23d 100644
--- a/gui/org.eclipse.papyrus.sysml14.architecture/src/org/eclipse/papyrus/sysml14/architecture/internal/migration/SysMLNeonToOxygenTableReconciler.java
+++ b/gui/org.eclipse.papyrus.sysml14.architecture/src/org/eclipse/papyrus/sysml14/architecture/internal/migration/SysMLNeonToOxygenTableReconciler.java
@@ -16,15 +16,24 @@
 import org.eclipse.core.commands.ExecutionException;
 import org.eclipse.core.runtime.IAdaptable;
 import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.emf.common.command.Command;
 import org.eclipse.emf.common.util.URI;
+import org.eclipse.emf.ecore.resource.Resource;
+import org.eclipse.emf.ecore.resource.ResourceSet;
 import org.eclipse.emf.ecore.util.EcoreUtil;
+import org.eclipse.emf.transaction.RecordingCommand;
 import org.eclipse.gmf.runtime.common.core.command.AbstractCommand;
 import org.eclipse.gmf.runtime.common.core.command.CommandResult;
+import org.eclipse.gmf.runtime.common.core.command.CompositeCommand;
 import org.eclipse.gmf.runtime.common.core.command.ICommand;
 import org.eclipse.papyrus.infra.architecture.ArchitectureDomainManager;
+import org.eclipse.papyrus.infra.core.architecture.ArchitectureDescription;
 import org.eclipse.papyrus.infra.core.architecture.RepresentationKind;
 import org.eclipse.papyrus.infra.core.architecture.merged.MergedArchitectureContext;
 import org.eclipse.papyrus.infra.core.architecture.merged.MergedArchitectureDescriptionLanguage;
+import org.eclipse.papyrus.infra.core.resource.ModelSet;
+import org.eclipse.papyrus.infra.core.resource.sasheditor.DiModelUtils;
+import org.eclipse.papyrus.infra.emf.gmf.command.EMFtoGMFCommandWrapper;
 import org.eclipse.papyrus.infra.nattable.common.reconciler.TableReconciler;
 import org.eclipse.papyrus.infra.nattable.model.nattable.Table;
 import org.eclipse.papyrus.infra.nattable.representation.PapyrusTable;
@@ -43,6 +52,8 @@
 	private static final String REQUIREMENT_TABLE_URI = "platform:/plugin/org.eclipse.papyrus.sysml14.nattable.requirement/resources/configuration/requirementTable.configuration#_j2LQIy7uEeWklMXvaoXzgQ";
 	private static final String REQUIREMENT_TREE_TABLE_URI = "platform:/plugin/org.eclipse.papyrus.sysml14.nattable.requirement/resources/configuration/requirementTable.configuration#_eXTo8CywEeaHK-6t3D9x9w";
 
+	private static final String SYSML14_CONTEXT_ID = "org.eclipse.papyrus.sysml.architecture.SysML14";	
+	
 	@Override
 	public ICommand getReconcileCommand(Table table) {
 		if (table.getPrototype() instanceof PapyrusView) {
@@ -65,7 +76,19 @@
 						break;
 					}
 					if (newTableKind != null) {
-						return new ReplaceTablePrototypeCommand(table, newTableKind);
+						ReplaceTablePrototypeCommand replaceTablePrototypeCommand = new ReplaceTablePrototypeCommand(table, newTableKind);
+						Resource eResource = table.eResource();
+						if (eResource != null) {
+							ResourceSet resourceSet = eResource.getResourceSet();
+							if (resourceSet instanceof ModelSet) {
+								CompositeCommand compositeCommand = new CompositeCommand("Update viewpoints from configuration to architecture and set SysML 1.4 as default architecture");
+								compositeCommand.add(replaceTablePrototypeCommand);
+								Command setContextCommand = getSetContextCommand( (ModelSet) resourceSet ,SYSML14_CONTEXT_ID);
+								compositeCommand.add(EMFtoGMFCommandWrapper.wrap(setContextCommand));
+								return compositeCommand;
+							}				
+						}						
+						return replaceTablePrototypeCommand;
 					}					
 				}
 
@@ -76,6 +99,17 @@
 
 	////////////////////////////////////////////////////////////////////////////////
 	// FIXME: should be in Papyrus core API
+	
+	protected Command getSetContextCommand(ModelSet modelSet, String contextId) {
+		return new RecordingCommand(modelSet.getTransactionalEditingDomain()) {
+			@Override
+			protected void doExecute() {
+				ArchitectureDescription description = DiModelUtils.getOrAddArchitectureDescription(modelSet);
+				description.setContextId(contextId);
+			}
+		};
+	}
+	
 	protected PapyrusTable getPapyrusTableById(String id) {
 		ArchitectureDomainManager manager = ArchitectureDomainManager.getInstance();
 		Collection<MergedArchitectureContext> visibleArchitectureContexts = manager.getVisibleArchitectureContexts();