package gov.nih.nci.caadapter.ui.common;

import javax.swing.tree.DefaultMutableTreeNode;

import gov.nih.nci.caadapter.common.metadata.AttributeMetadata;
import gov.nih.nci.caadapter.common.metadata.ObjectMetadata;
import gov.nih.nci.ncicb.xmiinout.domain.UMLAttribute;
import gov.nih.nci.ncicb.xmiinout.domain.UMLClass;

public class Iso21090uiUtil {
	public static boolean isCollectionDatatype(AttributeMetadata attributeMeta)
	{
		if (attributeMeta.getDatatype()==null)
			return false;
		if (attributeMeta.getDatatype().startsWith("DSET<"))
			return true;
		return false;
	}
	public static AttributeMetadata findAnnotationAttribute(DefaultMutableTreeNode localAttributeNode)
	{
		AttributeMetadata rtnMeta=(AttributeMetadata)localAttributeNode.getUserObject();
		DefaultMutableTreeNode parentNode =(DefaultMutableTreeNode)localAttributeNode.getParent();
		while (parentNode.getUserObject() instanceof AttributeMetadata)
		{
			rtnMeta=(AttributeMetadata)parentNode.getUserObject();
			parentNode=(DefaultMutableTreeNode)parentNode.getParent();
		}
		return rtnMeta;
	}
	
	public static String findAttributeRelativePath(DefaultMutableTreeNode localAttributeNode)
	{
		AttributeMetadata attrMeta=(AttributeMetadata)localAttributeNode.getUserObject();
		String rtnPath=attrMeta.getName();
		DefaultMutableTreeNode parentNode =(DefaultMutableTreeNode)localAttributeNode.getParent();
		while (parentNode.getUserObject() instanceof AttributeMetadata)
		{
			attrMeta=(AttributeMetadata)parentNode.getUserObject();
			rtnPath=attrMeta.getName()+"."+rtnPath;
			parentNode=(DefaultMutableTreeNode)parentNode.getParent();
		}
		//remove the last attribute name since it is the direct attribute
		if (rtnPath.length()>attrMeta.getName().length()+1)
			rtnPath=rtnPath.substring(attrMeta.getName().length()+1);//rtnPath.replace(attrMeta.getName()+".","");
		
		return rtnPath;
	}
	
	public static UMLAttribute findUMLAttributeFromMeta(DefaultMutableTreeNode attributeNode)
	{
		UMLAttribute rtnAttr=null;
		DefaultMutableTreeNode parentNode =(DefaultMutableTreeNode)attributeNode.getParent();
		String attrName=((AttributeMetadata)attributeNode.getUserObject()).getName();
		while (parentNode.getUserObject() instanceof AttributeMetadata)
		{
			parentNode=(DefaultMutableTreeNode)parentNode.getParent();
		}
		ObjectMetadata parentMeta=(ObjectMetadata)parentNode.getUserObject();
		UMLClass parentUmlClass=parentMeta.getUmlClass();
		if(parentUmlClass!=null)
			rtnAttr=parentUmlClass.getAttribute(attrName);
		//The UML attribute may still be null if it is an inherited attribute
		
		return rtnAttr;
	}

}

@@ -1315,24 +1315,66 @@
 getName();%0D%0A
+%09%09AttributeMetadata parentAttrMeta=null;%0D%0A
 %09%09DefaultMut
@@ -1530,17 +1530,23 @@
 %09%09%7B%0D%0A%09%09%09
-a
+parentA
 ttrMeta=
@@ -1596,33 +1596,39 @@
 ();%0D%0A%09%09%09rtnPath=
-a
+parentA
 ttrMeta.getName(
@@ -1711,16 +1711,191 @@
 ;%0D%0A%09%09%7D%0D%0A
+%09%09if (parentAttrMeta==null)%0D%0A%09%09%09//the mapped attribute is the direct attribute of object%0D%0A%09%09%09//this attribute is the location of the annotation tag %0D%0A%09%09%09rtnPath= %22%22;%0D%0A%09%09else%0D%0A
 %09%09//remo
@@ -1955,16 +1955,18 @@
 ribute%0D%0A
+//
 %09%09if (rt
@@ -2041,17 +2041,23 @@
 bstring(
-a
+parentA
 ttrMeta.
@@ -2082,56 +2082,8 @@
 +1);
-//rtnPath.replace(attrMeta.getName()+%22.%22,%22%22);%0D%0A%09
 %09%0D%0A%09

Return empty string as relative path of a simple or prime data type