package com.temenos.interaction.rimdsl.scoping;

import org.eclipse.emf.ecore.EObject;
import org.eclipse.xtext.naming.IQualifiedNameProvider.AbstractImpl;
import org.eclipse.xtext.naming.QualifiedName;

import com.temenos.interaction.rimdsl.rim.Command;
import com.temenos.interaction.rimdsl.rim.DomainDeclaration;
import com.temenos.interaction.rimdsl.rim.Event;
import com.temenos.interaction.rimdsl.rim.Relation;
import com.temenos.interaction.rimdsl.rim.ResourceInteractionModel;
import com.temenos.interaction.rimdsl.rim.State;

/**
 * IRIS QualifiedNameProvider.
 * 
 * Don't use the DefaultDeclarativeQualifiedNameProvider, as its polymorphic
 * thing seems to be a slower (measured), and its cache seems to have memory
 * impacts; simply coding this out explicit/clear seems to be better.
 * 
 * @see http://www.eclipse.org/Xtext/documentation.html#scoping
 * 
 * @author Michael Vorburger
 */
public class RIMDslQualifiedNameProvider extends AbstractImpl {

	public QualifiedName getFullyQualifiedName(EObject obj) {
		QualifiedName partialQualifiedName = null;
		if (obj instanceof DomainDeclaration) {
			DomainDeclaration d = (DomainDeclaration) obj;
			return QualifiedName.create(d.getName());
		} else if (obj instanceof ResourceInteractionModel) {
			ResourceInteractionModel r = (ResourceInteractionModel) obj;
			partialQualifiedName = QualifiedName.create(r.getName());
		} else if (obj instanceof State) {
			State s = (State) obj;
			partialQualifiedName = QualifiedName.create(s.getName());
		} else if (obj instanceof Event) {
			Event e = (Event) obj;
			partialQualifiedName = QualifiedName.create(e.getName());
		} else if (obj instanceof Command) {
			Command c = (Command) obj;
			partialQualifiedName = QualifiedName.create(c.getName());
		} else if (obj instanceof Relation) {
			Relation r = (Relation) obj;
			partialQualifiedName = QualifiedName.create(r.getName());
		}
		
		if (partialQualifiedName != null) {
			EObject temp = obj;
			while (temp.eContainer() != null) {
				temp = temp.eContainer();
				QualifiedName parentsQualifiedName = getFullyQualifiedName(temp);
				if (parentsQualifiedName != null)
					return parentsQualifiedName.append(partialQualifiedName);
				else
					return partialQualifiedName;
			}
		}
		
		return null;
	}

}

@@ -80,16 +80,73 @@
 Object;%0A
+import org.eclipse.xtext.naming.IQualifiedNameConverter;%0A
 import o
@@ -254,16 +254,49 @@
 dName;%0A%0A
+import com.google.inject.Inject;%0A
 import c
@@ -1051,16 +1051,114 @@
 Impl %7B%0A%0A
+%09@Inject%0A%09private IQualifiedNameConverter converter = new IQualifiedNameConverter.DefaultImpl();%0A%0A
 %09public 
@@ -1348,23 +1348,23 @@
 obj;%0A%09%09%09
-return 
+partial
 Qualifie
@@ -1368,22 +1368,43 @@
 fiedName
-.creat
+ = converter.toQualifiedNam
 e(d.getN
@@ -1549,32 +1549,44 @@
 QualifiedName = 
+converter.to
 QualifiedName.cr
@@ -1574,39 +1574,32 @@
 .toQualifiedName
-.create
 (r.getName());%0A%09
@@ -1678,32 +1678,44 @@
 QualifiedName = 
+converter.to
 QualifiedName.cr
@@ -1711,23 +1711,16 @@
 fiedName
-.create
 (s.getNa
@@ -1807,32 +1807,44 @@
 QualifiedName = 
+converter.to
 QualifiedName.cr
@@ -1840,23 +1840,16 @@
 fiedName
-.create
 (e.getNa
@@ -1942,32 +1942,44 @@
 QualifiedName = 
+converter.to
 QualifiedName.cr
@@ -1975,23 +1975,16 @@
 fiedName
-.create
 (c.getNa
@@ -2088,16 +2088,28 @@
 dName = 
+converter.to
 Qualifie
@@ -2117,15 +2117,8 @@
 Name
-.create
 (r.g
@@ -2128,16 +2128,43 @@
 ame());%0A
+%09%09%7D else %7B%0A%09%09%09return null;%0A
 %09%09%7D%0A%09%09%0A%09
@@ -2215,74 +2215,20 @@
 ect 
-temp = obj;%0A%09%09%09while (temp.eContainer() != null) %7B%0A%09%09%09%09temp = temp
+parent = obj
 .eCo
@@ -2238,17 +2238,16 @@
 iner();%0A
-%09
 %09%09%09Quali
@@ -2304,16 +2304,17 @@
 ame(
-temp
+parent
 );%0A
-%09
 %09%09%09i
@@ -2346,31 +2346,46 @@
 = null)%0A%09%09%09%09
-%09return
+partialQualifiedName =
  parentsQual
@@ -2430,18 +2430,13 @@
 ;%0A%09%09
-%09%09else%0A%09%09%09
+%7D%0A%09%09%0A
 %09%09re
@@ -2466,35 +2466,8 @@
 me;%0A
-%09%09%09%7D%0A%09%09%7D%0A%09%09%0A%09%09return null;%0A
 %09%7D%0A%0A

Use the xtext name converter to resolve fully qualified references correctly.