示例#1
0
        public static void annotateWithCDE(EA.Repository m_Repository, QueryServiceControl.QueryServiceManager.dataelement de)
        {
            object item;
            const string CDEREF = "CDERef";
            const string PREFNAME = "preferred name";

            try
            {
                string id = de.names.id;
                string name = de.names.preferred;

                EA.ObjectType type = m_Repository.GetTreeSelectedItem(out item);
                if (type != EA.ObjectType.otAttribute)
                {
                    MessageBox.Show("No class attribute has been selected in the Project Browser", "Error",
                        MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                EA.Attribute attr = (EA.Attribute)item;

                //string txt = "Container["+attr.Container;
                //txt += "]\nContainment["+attr.Containment;
                //txt += "]\nDefault["+attr.Default;
                //txt += "]\nParentID["+attr.ParentID;
                //txt += "]\nPos["+attr.Pos;
                //txt += "]\nStereotype["+attr.Stereotype;
                //txt += "]\nStereotypeEx["+attr.StereotypeEx;
                //txt += "]\nStyle["+attr.Style;
                //txt += "]\nStyleEx["+attr.StyleEx;
                //txt += "]\n";
                //MessageBox.Show(txt);

                EA.Collection tvs = attr.TaggedValues;

                if (tagExists(attr.TaggedValues, CDEREF))
                {
                    //DialogResult dg = MessageBox.Show(
                    //    el.Name + " already has a " + CDEREF + " annotation. Replace?",
                    //    "Confirm", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                    //if (dg == DialogResult.Cancel)
                    //    return;
                    MessageBox.Show(attr.Name + " already has a " + CDEREF + " annotation. Please remove and try again.");
                    return;
                }

                if (tagExists(attr.TaggedValues, PREFNAME))
                {
                    MessageBox.Show(attr.Name + " already has a " + PREFNAME + " annotation. Please remove and try again.");
                    return;
                }

                EAUtil.addTaggedValue(CDEREF, id, attr);
                EAUtil.addTaggedValue(PREFNAME, name, attr);

                //
                // All done, just display a successful message window
                //
                string attrName = "";
                EA.Element pel = m_Repository.GetElementByID(attr.ParentID);
                if (pel != null && pel.Name != null && pel.Name.Length > 0)
                    attrName = pel.Name + ".";
                attrName += attr.Name;

                MessageBox.Show("Attribute [" + attrName + "] successfully annotated with:\n\n"
                    + CDEREF + " =>   " + id + "\n"
                    + PREFNAME + " =>   " + name + "\n",
                    "Success",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Information);
            }
            catch(Exception exp)
            {
                MessageBox.Show("Error annotating with CDE. " + exp.Message);
            }
        }
示例#2
0
        public static void insertConceptRef(EA.Repository m_Repository, QueryServiceControl.QueryServiceManager.concept concept)
        {
            try
            {
                string id = concept.names.id;
                if (id.StartsWith("http:"))
                {
                    id = id.Substring(id.IndexOf("#") + 1);
                }
                else
                {
                    id = id.Substring(id.LastIndexOf("-") + 1);
                }

                object item;
                EA.ObjectType t = m_Repository.GetTreeSelectedItem(out item);

                if (t.ToString() == "otDiagram")
                {
                    MessageBox.Show("Please select an package or element in the Project Browser.");
                }
                else if (t.ToString() == "otElement")
                {
                    EA.Element el = (EA.Element)item;
                    string note = el.Notes;
                    if (note.Contains("ConceptRefs: ["))
                    {
                        if (!note.Substring(note.IndexOf("ConceptRefs: [") + 14).Replace("])", "").Contains(id))
                        {
                            el.Notes = note.Substring(0, note.IndexOf("ConceptRefs: [") + 14) + note.Substring(note.IndexOf("ConceptRefs: [") + 14).Replace("])", ", " + id + "])");
                        }
                    }
                    else
                    {
                        el.Notes += " (ConceptRefs: [" + id + "])";
                    }
                    el.Update();
                    el.Refresh();
                    MessageBox.Show("Concept reference, " + id + ", inserted to selected element.");
                }
                else if (t.ToString() == "otPackage")
                {
                    EA.Package pkg = (EA.Package)item;
                    string note = pkg.Notes;
                    if (note.Contains("ConceptRefs: ["))
                    {
                        if (!note.Substring(note.IndexOf("ConceptRefs: [") + 14).Replace("])", "").Contains(id))
                        {
                            pkg.Notes = note.Substring(0, note.IndexOf("ConceptRefs: [") + 14) + note.Substring(note.IndexOf("ConceptRefs: [") + 14).Replace("])", ", " + id + "])");
                        }
                    }
                    else
                    {
                        pkg.Notes += " (ConceptRefs: [" + id + "])";
                    }
                    pkg.Update();
                    MessageBox.Show("Concept reference, " + id + ", inserted to selected package.");
                }
                else
                {
                    MessageBox.Show("Please select a valid element (class, package) for the concept reference to be inserted into.");
                }
            }
            catch (Exception exp)
            {
                MessageBox.Show("Error inserting concept reference. " + exp.Message);
            }
        }
示例#3
0
        public static void insertCDE(EA.Repository m_Repository, QueryServiceControl.QueryServiceManager.dataelement de, EAUtil.INSERT_XSD_TYPE xsdType)
        {
            try
            {
                string id = de.names.id;
                string name = de.names.preferred;
                string definition = QueryServiceControl.QueryServiceControl.getDefinition(de.definition);

                EA.Package package = m_Repository.GetPackageByID(m_Repository.GetCurrentDiagram().PackageID);
                EA.Element o = (EA.Element)package.Elements.AddNew(name.Replace(" ", "_"), "Class");
                o.Notes = definition + " (ID: " + id + ")";

                EAUtil.addTaggedValue("CDERef", id, o);
                EAUtil.addTaggedValue("preferred name", name, o);

                if (package.StereotypeEx == "XSDschema")
                {
                    if (xsdType == EAUtil.INSERT_XSD_TYPE.TOP_LEVEL_ELEMENT)
                    {
                        o.Stereotype = "XSDtopLevelElement";
                    }
                    else if (xsdType == EAUtil.INSERT_XSD_TYPE.TOP_LEVEL_ATTRIBUTE)
                    {
                        o.Stereotype = "XSDtopLevelAttribute";
                    }
                }

                if (de.values != null && de.values.Item is QueryServiceControl.QueryServiceManager.nonenumerated)
                {
                    string dataType = (de.values.Item as QueryServiceControl.QueryServiceManager.nonenumerated).datatype;
                    if (dataType.StartsWith("xs:"))// || dataType.StartsWith("xsd:"))
                    {
                        //o.Genlinks = "Parent=" + dataType.Substring(dataType.IndexOf(":") + 1) + ";";
                        string simpleTypeClassName = name.Replace(" ", "_") + "_Type";
                        EA.Element simpleTypeClass = (EA.Element)package.Elements.AddNew(simpleTypeClassName, "Class");
                        simpleTypeClass.Stereotype = "XSDsimpleType";
                        simpleTypeClass.Genlinks = "Parent=" + dataType.Substring(dataType.IndexOf(":") + 1) + ";";
                        simpleTypeClass.Update();
                        simpleTypeClass.Refresh();

                        EA.DiagramObject simpleTypeDiag = (EA.DiagramObject)(m_Repository.GetCurrentDiagram().DiagramObjects.AddNew("", ""));
                        simpleTypeDiag.ElementID = simpleTypeClass.ElementID;
                        simpleTypeDiag.Update();

                        EA.Connector simpleTypeConnector = (EA.Connector)o.Connectors.AddNew("", "Generalization");
                        simpleTypeConnector.SupplierID = simpleTypeDiag.ElementID;
                        simpleTypeConnector.Update();
                        o.Connectors.Refresh();
                    }
                    else
                    {
                        o.Genlinks = "Parent=" + dataType + ";";
                    }
                }
                else if (de.values != null && de.values.Item is QueryServiceControl.QueryServiceManager.enumerated)
                {
                    string enumClassName = "Enum_" + name.Replace(" ", "_");
                    EA.Element enumClass = (EA.Element)package.Elements.AddNew(enumClassName, "Class");
                    enumClass.Stereotype = "enumeration";
                    enumClass.Genlinks = "Parent=string;";

                    QueryServiceControl.QueryServiceManager.enumerated en =
                        de.values.Item as QueryServiceControl.QueryServiceManager.enumerated;

                    foreach (QueryServiceControl.QueryServiceManager.validvalue vv in en.validvalue)
                    {
                        EA.Attribute at = (EA.Attribute)enumClass.Attributes.AddNew("", "");
                        at.Stereotype = "enum";
                        at.Name = vv.code;
                        at.Notes = QueryServiceControl.QueryServiceControl.getMeaning(vv.meaning);
                        at.Update();
                    }
                    enumClass.Attributes.Refresh();

                    EA.DiagramObject enumDiag = (EA.DiagramObject)(m_Repository.GetCurrentDiagram().DiagramObjects.AddNew("", ""));
                    enumDiag.ElementID = enumClass.ElementID;
                    enumDiag.Update();

                    EA.Connector enumConnector = (EA.Connector)o.Connectors.AddNew("", "Generalization");
                    enumConnector.SupplierID = enumDiag.ElementID;
                    enumConnector.Update();
                    o.Connectors.Refresh();

                    enumClass.Update();
                    enumClass.Refresh();
                }
                //attr.Update();

                //o.Attributes.Refresh();

                o.Update();
                o.Refresh();

                EA.DiagramObject diagram = (EA.DiagramObject)(m_Repository.GetCurrentDiagram().DiagramObjects.AddNew("", ""));
                diagram.ElementID = o.ElementID;
                diagram.Update();

                m_Repository.ReloadDiagram(diagram.DiagramID);

                //this.Close();
            }
            catch (Exception exp)
            {
                MessageBox.Show("Please make sure you have an active diagram openned." + "\n" + exp.Message + "\n" + exp.StackTrace, "No active diagram currently selected", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }
        protected void handleProp(QueryServiceControl.QueryServiceManager.property property)
        {
            string id = property.names.id;

            //Removing the institution identifying prefix from CADSR elements on client request
            if (id.Contains("-CADSR-"))
            {
                string[] idarr = id.Split('-');
                id = idarr[idarr.Length - 2] + " v." + idarr[idarr.Length - 1];
            }

            string preferredName = property.names.preferred;
            string definition = QueryServiceControl.QueryServiceControl.getDefinition(property.definition);
            string label = preferredName;

            //Get selected range
            Excel.Range selected = (Excel.Range)application.Selection;
            if (selected.Value2 == null || selected.Value2.ToString().Length == 0)
            {
                selected.Value2 = label;
            }
            else
            {
                //Refuse to add?  Do you have to remove first?
            }

            //Create concept list if not exists

            propList = useList(propList, "prop_list");

            //Add new concept entry to concept_list
            propList.Unprotect(dummyPass);
            Excel.Range c = (Excel.Range)propList.Cells[2, 1];

            Excel.Range found = propList.Cells.Find(id, Type.Missing, Excel.XlFindLookIn.xlValues, Excel.XlLookAt.xlPart, Excel.XlSearchOrder.xlByRows, Excel.XlSearchDirection.xlNext, false, Type.Missing, Type.Missing);
            if (found == null) //if (existingIndex == 0)
            {
                for (int i = 3; c.Value2 != null; i++)
                {
                    c = (Excel.Range)ocList.Cells[i, 1];

                }

                c.Value2 = id;
                c.Next.Value2 = preferredName;
                c.Next.Next.Value2 = definition.Trim().Replace("&gt;", ">").Replace("&lt;", "<").Replace("&amp;", "&");
                //c.Next.Next.Next.Value2 = attr.Trim().Replace(",", "\n\n").Replace("&#44;", ", ").Replace("&gt;", ">").Replace("&lt;", "<").Replace("&amp;", "&");

                //Cells mapped counter
                c.Next.Next.Next.Next.Value2 = 1;
            }
            else
            {
                found.Next.Next.Next.Next.Value2 = 1 + Convert.ToInt16(found.Next.Next.Next.Next.Value2.ToString());
            }

            propList.Protect(dummyPass, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);

            /* Remove hyperlinks for now until a suitable two way link method is found */
            selected.Hyperlinks.Add(selected, "", getSelectedRangeAddress(c), Type.Missing, label);

            selected.Font.Bold = true;
            selected.Font.Underline = false;
            selected.Font.ColorIndex = 1;
            selected.Interior.ThemeColor = Excel.XlThemeColor.xlThemeColorAccent3;
            selected.Interior.TintAndShade = 0.2;
        }
        /// <summary>
        /// Handles insertion of concept element into worksheet.
        /// 
        /// TODO: Refactor code
        /// TODO: Is two way hyperlink possible?
        /// </summary>
        /// <param name="selectedNode">Concept element to use</param>
        protected void handleConcept(QueryServiceControl.QueryServiceManager.concept concept)
        {
            string id = concept.names.id;
            string preferredName = concept.names.preferred;
            string definition = QueryServiceControl.QueryServiceControl.getDefinition(concept.definition);

            string code = null;
            if (id.StartsWith("http"))
            {
                code = id.Substring(id.LastIndexOf('#') + 1);
            }
            else
            {
                code = id.Substring(id.LastIndexOf('-') + 1);
            }
            string label = code + ": " + preferredName;

            //Get selected range
            Excel.Range selected = (Excel.Range)application.Selection;
            if (selected.Value2 == null || selected.Value2.ToString().Length == 0)
            {
                selected.Value2 = label;
            }
            else
            {
                selected.Value2 += ";"+label;
            }

            //Check for existing concept (May need to remove this to allow duplicates, so that two way link can be established)
            /*
            int existingIndex = 0;
            for (int i = 1; i <= application.Names.Count; i++)
            {
                if (application.Names.Item(i, Type.Missing, Type.Missing).Name == "_" + code)
                {
                    existingIndex = i;
                    break;
                }
            }

            if (existingIndex > 0)
            {
                application.Names.Item(existingIndex, Type.Missing, Type.Missing).RefersTo = application.Names.Item(existingIndex, Type.Missing, Type.Missing).RefersTo + "," + getSelectedRangeAddress(selected);

            }
            else
            {
                selected.Name = "_" + code;
            }
            */

            //Create concept list if not exists
            if (!isConceptListExists())
            {
                createConceptList();
            }

            //Add new concept entry to concept_list
            conceptList.Unprotect(dummyPass);
            Excel.Range c = (Excel.Range)conceptList.Cells[2, 1];

            Excel.Range found = conceptList.Cells.Find(code, Type.Missing, Excel.XlFindLookIn.xlValues, Excel.XlLookAt.xlPart, Excel.XlSearchOrder.xlByRows, Excel.XlSearchDirection.xlNext, false, Type.Missing, Type.Missing);
            if (found == null) //if (existingIndex == 0)
            {
                for (int i = 3; c.Value2 != null; i++)
                {
                    c = (Excel.Range)conceptList.Cells[i, 1];

                }

                c.Value2 = id;
                //c.Hyperlinks.Add(c, "", "_" + code, Type.Missing, id);
                c.Next.Value2 = preferredName;
                c.Next.Next.Value2 = definition.Trim().Replace("&gt;", ">").Replace("&lt;", "<").Replace("&amp;", "&");
                //c.Next.Next.Next.Value2 = attr.Trim().Replace(",", "\n\n").Replace("&#44;", ", ").Replace("&gt;", ">").Replace("&lt;", "<").Replace("&amp;", "&");

                //Cells mapped counter
                c.Next.Next.Next.Next.Value2 = 1;
            }
            else
            {
                found.Next.Next.Next.Next.Value2 = 1 + Convert.ToInt16(found.Next.Next.Next.Next.Value2.ToString());
                /*
                //Limit the loop to 10000 to prevent infinite search.
                for (int i = 2; i < 10000; i++)
                {
                    c = (Excel.Range)conceptList.Cells[i, 1];

                    //look in cell to see if you need to update it.
                    if (c.Text.ToString().Contains(code))
                    {
                        c.Next.Next.Next.Next.Value2 = 1 + Convert.ToInt16(c.Next.Next.Next.Next.Text);
                        //We found the correct concept in previous cell.  Break out of the loop.
                        break;
                    }
                }
                */
            }

            conceptList.Protect(dummyPass, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);

            /* Remove hyperlinks for now until a suitable two way link method is found */
            //selected.Hyperlinks.Add(selected, "", getSelectedRangeAddress(c), Type.Missing, label);

            selected.Font.Bold = true;
            selected.Font.Underline = false;
            selected.Font.ColorIndex = 1;
            selected.Interior.ThemeColor = Excel.XlThemeColor.xlThemeColorAccent3;
            selected.Interior.TintAndShade = 0.6;
        }
        /// <summary>
        /// Applying CDE to selected cell/range with validation based on value domain of data element, and XmlMap for exporting XML data.
        /// 
        /// TODO: Refactor code
        /// </summary>
        /// <param name="selectedNode">Data element to use</param>
        protected void handleCDE(QueryServiceControl.QueryServiceManager.dataelement de)
        {
            string id = de.names.id;

            //Removing the institution identifying prefix from CADSR elements on client request
            if (id.Contains("-CADSR-")){
                string[] idarr = id.Split('-');
                id = idarr[idarr.Length - 2] + " v." + idarr[idarr.Length - 1];
            }
            string preferredName = de.names.preferred;
            string preferredNameTag = preferredName.Replace(" ", "_");
            string definition = QueryServiceControl.QueryServiceControl.getDefinition(de.definition);

            Excel.Workbook workbook = (Excel.Workbook)application.ActiveWorkbook;
            Excel.Range selected = (Excel.Range)application.Selection;

            Excel.XmlMap xmlMap = null;

            string attr = "";
            string attrWithDef = "";
            string attrWithDefWithConc = "";

            if (de.values != null && de.values.Item is QueryServiceControl.QueryServiceManager.enumerated)
            {
                QueryServiceControl.QueryServiceManager.enumerated en =
                    de.values.Item as QueryServiceControl.QueryServiceManager.enumerated;

                XNamespace xs = "http://www.w3.org/2001/XMLSchema";
                XDocument x =
                    new XDocument(
                        new XDeclaration("1.0", "utf-8", "yes"),
                        new XElement(xs + "schema",
                            new XAttribute(XNamespace.Xmlns + "xs", "http://www.w3.org/2001/XMLSchema"),
                            new XAttribute("xmlns", "http://cagrid.org/schema/result-set"),
                            new XAttribute("targetNamespace", "http://cagrid.org/schema/result-set"),
                            new XElement(xs + "element",
                                new XAttribute("name", preferredNameTag + "List"),
                                new XAttribute("type", preferredNameTag + "List")
                                ),
                            new XElement(xs + "complexType",
                                new XAttribute("name", preferredNameTag + "List"),
                                new XElement(xs + "sequence",
                                    new XElement(xs + "element",
                                        new XAttribute("ref", preferredNameTag),
                                        new XAttribute("minOccurs", "0"),
                                        new XAttribute("maxOccurs", "unbounded")
                                        )
                                    )
                                ),
                            new XElement(xs + "element",
                                new XAttribute("name", preferredNameTag),
                                new XAttribute("type", preferredNameTag)
                            ),
                            new XElement(xs + "simpleType",
                                new XAttribute("name", preferredNameTag),
                                new XElement(xs + "restriction",
                                    new XAttribute("base", "xs:string")
                                    )
                                )
                            )
                        );

                XElement restrictionNode = x.Element(xs + "schema").Element(xs + "simpleType").Element(xs + "restriction");

                foreach(QueryServiceControl.QueryServiceManager.validvalue vv in en.validvalue)
                {
                    XElement enumNode = new XElement(xs + "enumeration",
                        new XAttribute("value", vv.code),
                        new XElement(xs + "annotation", vv.meaning.Value)
                        );
                    restrictionNode.Add(enumNode);

                    string evscode = "";

                    if (vv.conceptCollection != null)
                    {
                        foreach (QueryServiceControl.QueryServiceManager.conceptRef concept in vv.conceptCollection)
                        {
                            evscode += ":" + concept.name;
                        }

                        evscode = evscode.Substring(1);
                        evscode = " | " + evscode;
                    }

                    attr += vv.code + ",";
                    attrWithDef += vv.code + " : (" + vv.meaning.Value + "),";
                    attrWithDefWithConc += vv.code + " : ("+ vv.meaning.Value + " " + evscode + "),";
                }

                if (attr != null && attr.Contains(","))
                {
                    attr.Remove(attr.LastIndexOf(','));
                    attrWithDef.Remove(attrWithDef.LastIndexOf(','));
                    attrWithDefWithConc.Remove(attrWithDefWithConc.LastIndexOf(','));
                    attr.Trim();
                }
                try
                {
                    xmlMap = workbook.XmlMaps.Add(x.ToString(), (selected.Count > 1) ? preferredNameTag + "List" : preferredNameTag);
                    xmlMap.ShowImportExportValidationErrors = true;
                    xmlMap.AdjustColumnWidth = true;
                    selected.XPath.SetValue(xmlMap, (selected.Count > 1) ? "/rs:" + preferredNameTag + "List/rs:" + preferredNameTag : "/rs:" + preferredNameTag, "xmlns:rs=\"" + rs.NamespaceName + "\"", selected.Count > 1);
                }
                catch (ArgumentException ex)
                {
                    MessageBox.Show(ex.Message, "Range Selection Error");
                    xmlMap.Delete();
                    return;
                }

                selected.Validation.Delete();

                //xs:enumeration
                selected.Validation.Add(Excel.XlDVType.xlValidateList, Excel.XlDVAlertStyle.xlValidAlertStop, Excel.XlFormatConditionOperator.xlBetween, attr, attr);
                selected.Validation.InputTitle = "Select a value from the list";
                //WORKAROUND: TODO: Fix this one way or another
                try
                {
                    selected.Validation.InputMessage = attrWithDef.Replace(",", "\n");
                }
                catch (Exception)
                {
                    //If the exception was thrown setting Validation Input Message, set the message in comment)
                    selected.AddComment(attrWithDef.Replace(",", "\n"));
                    selected.Comment.Shape.TextFrame.AutoSize = true;
                }

                selected.Validation.ErrorMessage = "Please select a value from the enumeration list.";
            }
            else if (de.values != null && de.values.Item is QueryServiceControl.QueryServiceManager.nonenumerated)
            {
                QueryServiceControl.QueryServiceManager.nonenumerated nen =
                    de.values.Item as QueryServiceControl.QueryServiceManager.nonenumerated;

                attr = nen.datatype;
                attrWithDef = "data-type: " + nen.datatype + "\nUnits: " + nen.units;

                XNamespace xs = "http://www.w3.org/2001/XMLSchema";
                XDocument x =
                    new XDocument(
                        new XDeclaration("1.0", "utf-8", "yes"),
                        new XElement(xs + "schema",
                            new XAttribute(XNamespace.Xmlns + "xs", "http://www.w3.org/2001/XMLSchema"),
                            new XAttribute("xmlns", "http://cagrid.org/schema/result-set"),
                            new XAttribute("targetNamespace", "http://cagrid.org/schema/result-set"),
                            new XElement(xs + "element",
                                new XAttribute("name", preferredNameTag + "List"),
                                new XAttribute("type", preferredNameTag + "List")
                                ),
                            new XElement(xs + "complexType",
                                new XAttribute("name", preferredNameTag + "List"),
                                new XElement(xs + "sequence",
                                    new XElement(xs + "element",
                                        new XAttribute("ref", preferredNameTag),
                                        new XAttribute("minOccurs", "0"),
                                        new XAttribute("maxOccurs", "unbounded")
                                        )
                                    )
                                ),
                            new XElement(xs + "element",
                                new XAttribute("name", preferredNameTag),
                                new XAttribute("type", preferredNameTag)
                            ),
                            new XElement(xs + "simpleType",
                                new XAttribute("name", preferredNameTag),
                                new XElement(xs + "annotation", definition),
                                new XElement(xs + "restriction",
                                    new XAttribute("base", attr)
                                    )
                                )
                            )
                        );
                try
                {
                    xmlMap = workbook.XmlMaps.Add(x.ToString(), (selected.Count > 1) ? preferredNameTag + "List" : preferredNameTag);
                    xmlMap.ShowImportExportValidationErrors = true;
                    xmlMap.AdjustColumnWidth = true;
                    selected.XPath.SetValue(xmlMap, (selected.Count > 1) ? "/rs:" + preferredNameTag + "List/rs:" + preferredNameTag : "/rs:" + preferredNameTag, "xmlns:rs=\"" + rs.NamespaceName + "\"", selected.Count > 1);
                }
                catch (ArgumentException ex)
                {
                    MessageBox.Show(ex.Message, "Range Selection Error");
                    xmlMap.Delete();
                    return;
                }

                selected.Validation.Delete();

                if (attr == "xs:nonNegativeInteger")
                {
                    selected.Validation.Add(Excel.XlDVType.xlValidateWholeNumber, Excel.XlDVAlertStyle.xlValidAlertStop, Excel.XlFormatConditionOperator.xlGreaterEqual, 0, 0);
                    selected.Validation.InputTitle = "Enter a positive integer";
                    selected.Validation.InputMessage = " ";
                    selected.Validation.ErrorMessage = "Please enter a positive number";
                }
                else if (attr == "xs:nonPositiveInteger")
                {
                    selected.Validation.Add(Excel.XlDVType.xlValidateWholeNumber, Excel.XlDVAlertStyle.xlValidAlertStop, Excel.XlFormatConditionOperator.xlLessEqual, 0, 0);
                    selected.Validation.InputTitle = "Enter a negative integer";
                    selected.Validation.InputMessage = " ";
                    selected.Validation.ErrorMessage = "Please enter a negative number";
                }
                else if (attr == "xs:positiveInteger")
                {
                    selected.Validation.Add(Excel.XlDVType.xlValidateWholeNumber, Excel.XlDVAlertStyle.xlValidAlertStop, Excel.XlFormatConditionOperator.xlGreater, 1, 1);
                    selected.Validation.InputTitle = "Enter an integer number greater than 0(zero)";
                    selected.Validation.InputMessage = " ";
                    selected.Validation.ErrorMessage = "Please enter an integer number 0(zero)";
                }
                else if (attr == "xs:integer")
                {
                    selected.Validation.Add(Excel.XlDVType.xlValidateWholeNumber, Excel.XlDVAlertStyle.xlValidAlertStop, Excel.XlFormatConditionOperator.xlBetween, Decimal.MinValue, Decimal.MaxValue);
                    selected.Validation.InputTitle = "Enter an integer number";
                    selected.Validation.InputMessage = " ";
                    selected.Validation.ErrorMessage = "Please enter an integer number";
                }
                else if (attr == "xs:byte")
                {
                    selected.Validation.Add(Excel.XlDVType.xlValidateWholeNumber, Excel.XlDVAlertStyle.xlValidAlertStop, Excel.XlFormatConditionOperator.xlBetween, SByte.MinValue, SByte.MaxValue);
                    selected.Validation.InputTitle = "Enter a byte number";
                    selected.Validation.InputMessage = " ";
                    selected.Validation.ErrorMessage = "Please enter a byte number";
                }
                else if (attr == "xs:unsignedByte")
                {
                    selected.Validation.Add(Excel.XlDVType.xlValidateWholeNumber, Excel.XlDVAlertStyle.xlValidAlertStop, Excel.XlFormatConditionOperator.xlBetween, Byte.MinValue, Byte.MaxValue);
                    selected.Validation.InputTitle = "Enter a byte number";
                    selected.Validation.InputMessage = " ";
                    selected.Validation.ErrorMessage = "Please enter a byte number";
                }
                else if (attr == "xs:short")
                {
                    selected.Validation.Add(Excel.XlDVType.xlValidateWholeNumber, Excel.XlDVAlertStyle.xlValidAlertStop, Excel.XlFormatConditionOperator.xlBetween, Int16.MinValue, Int16.MaxValue);
                    selected.Validation.InputTitle = "Enter an integer number";
                    selected.Validation.InputMessage = " ";
                    selected.Validation.ErrorMessage = "Please enter an integer number";
                }
                else if (attr == "xs:unsignedShort")
                {
                    selected.Validation.Add(Excel.XlDVType.xlValidateWholeNumber, Excel.XlDVAlertStyle.xlValidAlertStop, Excel.XlFormatConditionOperator.xlBetween, UInt16.MinValue, UInt16.MaxValue);
                    selected.Validation.InputTitle = "Enter an integer number";
                    selected.Validation.InputMessage = " ";
                    selected.Validation.ErrorMessage = "Please enter an integer number";
                }
                else if (attr == "xs:int")
                {
                    selected.Validation.Add(Excel.XlDVType.xlValidateWholeNumber, Excel.XlDVAlertStyle.xlValidAlertStop, Excel.XlFormatConditionOperator.xlBetween, Int32.MinValue, Int32.MaxValue);
                    selected.Validation.InputTitle = "Enter an integer number";
                    selected.Validation.InputMessage = " ";
                    selected.Validation.ErrorMessage = "Please enter an integer number";
                }
                else if (attr == "xs:unsignedInt")
                {
                    selected.Validation.Add(Excel.XlDVType.xlValidateWholeNumber, Excel.XlDVAlertStyle.xlValidAlertStop, Excel.XlFormatConditionOperator.xlBetween, UInt32.MinValue, UInt32.MaxValue);
                    selected.Validation.InputTitle = "Enter a positive integer number";
                    selected.Validation.InputMessage = " ";
                    selected.Validation.ErrorMessage = "Please enter a positive integer number";
                }
                else if (attr == "xs:long")
                {
                    selected.Validation.Add(Excel.XlDVType.xlValidateWholeNumber, Excel.XlDVAlertStyle.xlValidAlertStop, Excel.XlFormatConditionOperator.xlBetween, Int64.MinValue, Int64.MaxValue);
                    selected.Validation.InputTitle = "Enter an integer number";
                    selected.Validation.InputMessage = " ";
                    selected.Validation.ErrorMessage = "Please enter an integer number";
                }
                else if (attr == "xs:unsignedLong")
                {
                    selected.Validation.Add(Excel.XlDVType.xlValidateWholeNumber, Excel.XlDVAlertStyle.xlValidAlertStop, Excel.XlFormatConditionOperator.xlBetween, UInt64.MinValue, UInt64.MaxValue);
                    selected.Validation.InputTitle = "Enter a positive integer number";
                    selected.Validation.InputMessage = " ";
                    selected.Validation.ErrorMessage = "Please enter a positive integer number";
                }
                else if (attr == "xs:float")
                {
                    selected.Validation.Add(Excel.XlDVType.xlValidateDecimal, Excel.XlDVAlertStyle.xlValidAlertStop, Excel.XlFormatConditionOperator.xlBetween, Single.MinValue, Single.MaxValue);
                    selected.Validation.InputTitle = "Enter a decimal number";
                    selected.Validation.InputMessage = " ";
                    selected.Validation.ErrorMessage = "Please enter a decimal number";
                }
                else if (attr == "xs:double")
                {
                    selected.Validation.Add(Excel.XlDVType.xlValidateDecimal, Excel.XlDVAlertStyle.xlValidAlertStop, Excel.XlFormatConditionOperator.xlBetween, Double.MinValue, Double.MaxValue);
                    selected.Validation.InputTitle = "Enter a decimal number";
                    selected.Validation.InputMessage = " ";
                    selected.Validation.ErrorMessage = "Please enter a decimal number";
                }
                else if (attr == "xs:decimal")
                {
                    selected.Validation.Add(Excel.XlDVType.xlValidateDecimal, Excel.XlDVAlertStyle.xlValidAlertStop, Excel.XlFormatConditionOperator.xlBetween, Decimal.MinValue, Decimal.MaxValue);
                    selected.Validation.InputTitle = "Enter a decimal number";
                    selected.Validation.InputMessage = " ";
                    selected.Validation.ErrorMessage = "Please enter a decimal number";
                }
                else if (attr == "xs:date")
                {
                    selected.Validation.Add(Excel.XlDVType.xlValidateDate, Excel.XlDVAlertStyle.xlValidAlertStop, Excel.XlFormatConditionOperator.xlGreaterEqual, "=DATE(1900,1,1)", "=DATE(1900,1,1)");
                    selected.Validation.InputTitle = "Enter a date";
                    selected.Validation.InputMessage = "Date format: =DATE(year,month,day)";
                    selected.Validation.ErrorMessage = "Please enter correct date format: =DATE(year,month,day)";
                }
                else if (attr == "xs:boolean")
                {
                    selected.Validation.Add(Excel.XlDVType.xlValidateList, Excel.XlDVAlertStyle.xlValidAlertStop, Excel.XlFormatConditionOperator.xlBetween, "true,false", "true,false");
                    selected.Validation.InputTitle = "Select True or False";
                    selected.Validation.InputMessage = " ";
                    selected.Validation.ErrorMessage = "Please select a boolean value from the list";
                }
                /*
                else if (attr == "xs:duration")
                {
                    //xs:duration
                    selected.Validation.Add(Excel.XlDVType.xlValidateDecimal, Excel.XlDVAlertStyle.xlValidAlertStop, Excel.XlFormatConditionOperator.xlBetween, Decimal.MinValue, Decimal.MaxValue);
                    selected.Validation.InputTitle = "Enter a decimal number";
                    selected.Validation.InputMessage = " ";
                    selected.Validation.ErrorMessage = "Please select a decimal number";
                }
                else if (attr == "xs:dateTime")
                {
                    //xs:dateTime
                    selected.Validation.Add(Excel.XlDVType.xlValidateDecimal, Excel.XlDVAlertStyle.xlValidAlertStop, Excel.XlFormatConditionOperator.xlBetween, Decimal.MinValue, Decimal.MaxValue);
                    selected.Validation.InputTitle = "Enter a decimal number";
                    selected.Validation.InputMessage = " ";
                    selected.Validation.ErrorMessage = "Please select a decimal number";
                }
                else if (attr == "xs:time")
                {
                    //xs:time
                    selected.Validation.Add(Excel.XlDVType.xlValidateDecimal, Excel.XlDVAlertStyle.xlValidAlertStop, Excel.XlFormatConditionOperator.xlBetween, Decimal.MinValue, Decimal.MaxValue);
                    selected.Validation.InputTitle = "Enter a decimal number";
                    selected.Validation.InputMessage = " ";
                    selected.Validation.ErrorMessage = "Please select a decimal number";
                }
                else if (attr == "xs:gYearMonth")
                {
                    //xs:gYearMonth
                    selected.Validation.Add(Excel.XlDVType.xlValidateDecimal, Excel.XlDVAlertStyle.xlValidAlertStop, Excel.XlFormatConditionOperator.xlBetween, Decimal.MinValue, Decimal.MaxValue);
                    selected.Validation.InputTitle = "Enter a decimal number";
                    selected.Validation.InputMessage = " ";
                    selected.Validation.ErrorMessage = "Please select a decimal number";
                }
                else if (attr == "xs:gYear")
                {
                    //xs:gYear
                    selected.Validation.Add(Excel.XlDVType.xlValidateDecimal, Excel.XlDVAlertStyle.xlValidAlertStop, Excel.XlFormatConditionOperator.xlBetween, Decimal.MinValue, Decimal.MaxValue);
                    selected.Validation.InputTitle = "Enter a decimal number";
                    selected.Validation.InputMessage = " ";
                    selected.Validation.ErrorMessage = "Please select a decimal number";
                }
                else if (attr == "xs:gMonthDay")
                {
                    //xs:gMonthDay
                    selected.Validation.Add(Excel.XlDVType.xlValidateDecimal, Excel.XlDVAlertStyle.xlValidAlertStop, Excel.XlFormatConditionOperator.xlBetween, Decimal.MinValue, Decimal.MaxValue);
                    selected.Validation.InputTitle = "Enter a decimal number";
                    selected.Validation.InputMessage = " ";
                    selected.Validation.ErrorMessage = "Please select a decimal number";
                }
                else if (attr == "xs:gDay")
                {
                    //xs:gDay
                    selected.Validation.Add(Excel.XlDVType.xlValidateDecimal, Excel.XlDVAlertStyle.xlValidAlertStop, Excel.XlFormatConditionOperator.xlBetween, Decimal.MinValue, Decimal.MaxValue);
                    selected.Validation.InputTitle = "Enter a decimal number";
                    selected.Validation.InputMessage = " ";
                    selected.Validation.ErrorMessage = "Please select a decimal number";
                }
                else if (attr == "xs:gMonth")
                {
                    //xs:gMonth
                    selected.Validation.Add(Excel.XlDVType.xlValidateDecimal, Excel.XlDVAlertStyle.xlValidAlertStop, Excel.XlFormatConditionOperator.xlBetween, Decimal.MinValue, Decimal.MaxValue);
                    selected.Validation.InputTitle = "Enter a decimal number";
                    selected.Validation.InputMessage = " ";
                    selected.Validation.ErrorMessage = "Please select a decimal number";
                }
                else if (attr == "xs:anyURI")
                {
                    //xs:anyURI
                    selected.Validation.Add(Excel.XlDVType.xlValidateDecimal, Excel.XlDVAlertStyle.xlValidAlertStop, Excel.XlFormatConditionOperator.xlBetween, Decimal.MinValue, Decimal.MaxValue);
                    selected.Validation.InputTitle = "Enter a decimal number";
                    selected.Validation.InputMessage = " ";
                    selected.Validation.ErrorMessage = "Please select a decimal number";
                }
                */
            }

            //TODO: Fix the issue that requires this to be commented out.
            //selected.Name = xmlMap.Name;

            //Create CDE list is not exist
            if (!isCDEListExists())
            {
                createCDEList();
            }

            //Add new CDE entry to cde_list
            cdeList.Unprotect(dummyPass);
            Excel.Range c = (Excel.Range)cdeList.Cells[2, 1];

            for (int i = 3; c.Value2 != null; i++)
            {
                c = (Excel.Range)cdeList.Cells[i, 1];
            }

            string instanceNum = xmlMap.Name.Substring(xmlMap.Name.LastIndexOf("_Map") + 4);

            //Since the XML map can be deleted if an exception is thrown, just set address manually.
            string selectedAddress = selected.get_Address(Type.Missing, Type.Missing, Excel.XlReferenceStyle.xlA1, Type.Missing, Type.Missing);
            selectedAddress = selected.Worksheet.Name + "!" + selectedAddress;

            //c.Hyperlinks.Add(c, "", xmlMap.Name, Type.Missing, id + ((selected.Count > 1) ? "(List)" : "(Single)") + ((instanceNum != null && instanceNum.Length > 0) ? "(" + instanceNum + ")" : "") + "\n\nRange: " + getSelectedRangeAddress(selected));
            c.Hyperlinks.Add(c, "", selectedAddress, Type.Missing, id);
            c.Next.Value2 = preferredName;
            c.Next.Next.Value2 = definition.Trim().Replace("&gt;", ">").Replace("&lt;", "<").Replace("&amp;", "&");
            c.Next.Next.Next.Value2 = attrWithDefWithConc.Trim().Replace(",", "\n\n").Replace("&#44;", ", ").Replace("&gt;", ">").Replace("&lt;", "<").Replace("&amp;", "&");
            cdeList.Protect(dummyPass, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);

            // Create/Format Header
            if (selected.Count > 1)
            {
                Excel.Range firstCell = (Excel.Range)selected.Cells[1, 1];

                firstCell.Validation.Delete();
                firstCell.Value2 = ((string)firstCell.Value2).Substring(((string)firstCell.Value2).IndexOf(":") + 1).Replace("_", " ");
                firstCell.Hyperlinks.Add(firstCell, "", getSelectedRangeAddress(c), Type.Missing, firstCell.Value2);
                firstCell.Font.ColorIndex = 2;
                firstCell.Font.Underline = false;
            }
            else if (selected.Count == 1)
            {
                selected.Interior.ThemeColor = Excel.XlThemeColor.xlThemeColorAccent1;
                selected.Interior.TintAndShade = 0.6;

                if (selected.Column != 1)
                {
                    try
                    {
                        Excel.Range leftCell = selected.get_Offset(0, -1);
                        if (leftCell != null && (leftCell.Value2 == null || ((string)leftCell.Value2).Length == 0))
                        {
                            leftCell.Value2 = id + ":" + preferredName;
                            leftCell.Hyperlinks.Add(leftCell, "", getSelectedRangeAddress(c), Type.Missing, leftCell.Value2);
                            leftCell.Font.Bold = true;
                            leftCell.Font.Underline = false;
                            leftCell.Font.ColorIndex = 1;
                            return;
                        }

                        Excel.Range upCell = selected.get_Offset(-1, 0);
                        if (upCell != null && (upCell.Value2 == null || ((string)upCell.Value2).Length == 0))
                        {
                            upCell.Value2 = id+":"+preferredName;
                            upCell.Hyperlinks.Add(upCell, "", getSelectedRangeAddress(c), Type.Missing, upCell.Value2);
                            upCell.Font.Bold = true;
                            upCell.Font.Underline = false;
                            upCell.Font.ColorIndex = 1;
                            return;
                        }
                    }
                    catch (Exception)
                    {
                        //Unable to get valid cell
                        MessageBox.Show("Unable to find suitable location to insert header/label", "Header/Label not created");
                    }
                }
                else
                {
                    try
                    {
                        Excel.Range upCell = selected.get_Offset(-1, 0);
                        if (upCell != null && (upCell.Value2 == null || ((string)upCell.Value2).Length == 0))
                        {
                            upCell.Value2 = preferredName;
                            upCell.Hyperlinks.Add(upCell, "", getSelectedRangeAddress(c), Type.Missing, upCell.Value2);
                            upCell.Font.Bold = true;
                            upCell.Font.Underline = false;
                            upCell.Font.ColorIndex = 1;
                            return;
                        }
                    }
                    catch (Exception)
                    {
                        MessageBox.Show("Unable to find suitable location to insert header/label", "Header/Label not created");
                    }
                }
            }
        }