[548631] Fix getRepresentationDescriptor() in case of migration

If org.eclipse.sirius.business.api.query.DRepresentationQuery.getRepresentationDescriptor()
is called during a migration participant, the session is not yet
initialized so we must use the resource to find the analysis.

Bug: 548631
Change-Id: I6f3440254841797492e55b7cf41bdcb747b77d68
Signed-off-by: Laurent Redor <laurent.redor@obeo.fr>
diff --git a/plugins/org.eclipse.sirius/src/org/eclipse/sirius/business/api/query/DRepresentationQuery.java b/plugins/org.eclipse.sirius/src/org/eclipse/sirius/business/api/query/DRepresentationQuery.java
index 52b60da..446171a 100644
--- a/plugins/org.eclipse.sirius/src/org/eclipse/sirius/business/api/query/DRepresentationQuery.java
+++ b/plugins/org.eclipse.sirius/src/org/eclipse/sirius/business/api/query/DRepresentationQuery.java
@@ -14,10 +14,12 @@
 
 import java.util.ArrayList;
 import java.util.Collection;
+import java.util.Iterator;
 
 import org.eclipse.emf.common.util.EList;
 import org.eclipse.emf.ecore.EObject;
 import org.eclipse.emf.ecore.EStructuralFeature;
+import org.eclipse.emf.ecore.resource.Resource;
 import org.eclipse.sirius.business.api.session.Session;
 import org.eclipse.sirius.business.api.session.SessionManager;
 import org.eclipse.sirius.business.internal.session.danalysis.DAnalysisSessionImpl;
@@ -153,6 +155,10 @@
                 if (result == null) {
                     result = findDescriptorFromAnalysis();
                 }
+            } else {
+                // There is no session (during a migration participant for example) so we search the analysis of the
+                // eResource
+                result = findDescriptorFromEResource();
             }
         }
         return result;
@@ -170,6 +176,24 @@
         return result;
     }
 
+    private DRepresentationDescriptor findDescriptorFromEResource() {
+        DRepresentationDescriptor result = null;
+        Resource eResource = representation.eResource();
+        if (eResource != null && eResource.getContents() != null) {
+            Iterator<EObject> contentsIterator = eResource.getContents().iterator();
+            while (contentsIterator.hasNext()) {
+                EObject content = contentsIterator.next();
+                if (content instanceof DAnalysis) {
+                    result = findDescriptorFromAnalysis((DAnalysis) content);
+                }
+                if (result != null) {
+                    break;
+                }
+            }
+        }
+        return result;
+    }
+
     /**
      * Find a {@link DRepresentationDescriptor} that references the {@link DRepresentation} in the given
      * {@link DAnalysis}.