[548631] Fix pb about copying name and documentation

For a recent version, Sirius 6.1.3 for example, ie containing all other
migration aspects of this migration participant. The name and
documentation was not copied from DRepresentation to
DRepresentationDescriptor.

Bug: 548631
Change-Id: I74eaeee5ec1438e1f6bef766bfa82ab1b4721892
Signed-off-by: Laurent Redor <laurent.redor@obeo.fr>
diff --git a/plugins/org.eclipse.sirius/src/org/eclipse/sirius/business/internal/migration/DRepInDViewToRootObjectsAndWithDRepDescRepPathMigrationParticipant.java b/plugins/org.eclipse.sirius/src/org/eclipse/sirius/business/internal/migration/DRepInDViewToRootObjectsAndWithDRepDescRepPathMigrationParticipant.java
index 0e42dfb..adb9254 100644
--- a/plugins/org.eclipse.sirius/src/org/eclipse/sirius/business/internal/migration/DRepInDViewToRootObjectsAndWithDRepDescRepPathMigrationParticipant.java
+++ b/plugins/org.eclipse.sirius/src/org/eclipse/sirius/business/internal/migration/DRepInDViewToRootObjectsAndWithDRepDescRepPathMigrationParticipant.java
@@ -15,6 +15,7 @@
 import java.text.MessageFormat;
 import java.util.HashMap;
 import java.util.HashSet;
+import java.util.Iterator;
 import java.util.Map;
 import java.util.Optional;
 import java.util.Set;
@@ -27,6 +28,7 @@
 import org.eclipse.emf.ecore.resource.Resource;
 import org.eclipse.emf.ecore.util.EcoreUtil;
 import org.eclipse.sirius.business.api.migration.AbstractRepresentationsFileMigrationParticipant;
+import org.eclipse.sirius.business.api.query.DRepresentationQuery;
 import org.eclipse.sirius.business.api.resource.ResourceDescriptor;
 import org.eclipse.sirius.business.internal.query.DRepresentationDescriptorInternalHelper;
 import org.eclipse.sirius.viewpoint.DAnalysis;
@@ -121,8 +123,8 @@
                 // Create DRepresentationDescriptor
                 // The method will call DRepresentationDescriptor.setRepresentation that will, in fact, get the
                 // DRepresentation URI and set it on DRepresentationDescriptor.repPath
-                String representationName = representationToNameMap.get(representation);
-                String representationDocumentation = representationToDocumentationMap.get(representation);
+                String representationName = representationToNameMap.remove(representation);
+                String representationDocumentation = representationToDocumentationMap.remove(representation);
                 DRepresentationDescriptor newDescriptor = DRepresentationDescriptorInternalHelper.createDescriptor(representation, representationName, representationDocumentation);
                 ((DView) owner).getOwnedRepresentationDescriptors().add(newDescriptor);
                 if (representationName == null || representationDocumentation == null) {
@@ -207,6 +209,36 @@
             super.postLoad(dAnalysis, loadedVersion);
         }
 
+        // Transfer all stored representation names during the loading, to the corresponding representation descriptor.
+        for (Iterator<DRepresentation> iterator = representationToNameMap.keySet().iterator(); iterator.hasNext(); /* */) {
+            DRepresentation dRepresentation = iterator.next();
+            String name = representationToNameMap.get(dRepresentation);
+            iterator.remove();
+            DRepresentationDescriptor dRepresentationDescriptor = new DRepresentationQuery(dRepresentation).getRepresentationDescriptor();
+            if (dRepresentationDescriptor != null && !name.equals(dRepresentationDescriptor.getName())) {
+                dRepresentationDescriptor.setName(name);
+                migratedDescriptorsforNameOrDocumentation.add(dRepresentationDescriptor);
+            }
+            // Check if there is also a documentation stored for this representation (as we already have the
+            // corresponding representationDescriptor).
+            String documentation = representationToDocumentationMap.remove(dRepresentation);
+            if (documentation != null && dRepresentationDescriptor != null && !documentation.equals(dRepresentationDescriptor.getDocumentation())) {
+                dRepresentationDescriptor.setDocumentation(documentation);
+                migratedDescriptorsforNameOrDocumentation.add(dRepresentationDescriptor);
+            }
+        }
+        // Transfer all stored representation documentations during the loading, to the corresponding representation
+        // descriptor.
+        for (Iterator<DRepresentation> iterator = representationToDocumentationMap.keySet().iterator(); iterator.hasNext(); /* */) {
+            DRepresentation dRepresentation = iterator.next();
+            String documentation = representationToDocumentationMap.get(dRepresentation);
+            iterator.remove();
+            DRepresentationDescriptor dRepresentationDescriptor = new DRepresentationQuery(dRepresentation).getRepresentationDescriptor();
+            if (dRepresentationDescriptor != null && !documentation.equals(dRepresentationDescriptor.getDocumentation())) {
+                dRepresentationDescriptor.setDocumentation(documentation);
+                migratedDescriptorsforNameOrDocumentation.add(dRepresentationDescriptor);
+            }
+        }
         if (!migratedDescriptorsforNameOrDocumentation.isEmpty()) {
             String migrationMessage = MessageFormat.format(Messages.DRepInDViewToRootObjectsAndWithDRepDescRepPathMigrationParticipant_nameMigrationMessage,
                     migratedDescriptorsforNameOrDocumentation.size());