private static void InsertIDAsChildInNodeList(XmlNodeList nodeList, String where)
        {
            MetadataDocumentManagement metaDataDocMgt = MetadataDocumentManagement.Instance;

            foreach (XmlNode node in nodeList)
            {
                string  newId       = string.Empty;
                XmlNode fixedIdNode = node.SelectSingleNode("./a:FixedID", metaDataDocMgt.XmlNamespaceMgt);
                if (fixedIdNode != null)
                {
                    newId = fixedIdNode.InnerText;
                    RemoveNodeFromParent(fixedIdNode);
                }
                else
                {
                    if (node.ParentNode.Name.Equals("Part"))
                    {
                        switch (node.SelectSingleNode("./a:PartType", metaDataDocMgt.XmlNamespaceMgt).InnerText)
                        {
                        case "Page":
                            newId = metaDataDocMgt.CalcId(GetProperty(node.ParentNode, "NewID"), node.SelectSingleNode("./a:PagePartID", metaDataDocMgt.XmlNamespaceMgt).InnerText, where).ToString(CultureInfo.InvariantCulture);
                            break;

                        case "System":
                            newId = metaDataDocMgt.CalcId(GetProperty(node.ParentNode, "NewID"), node.SelectSingleNode("./a:SystemPartID", metaDataDocMgt.XmlNamespaceMgt).InnerText, where).ToString(CultureInfo.InvariantCulture);
                            break;

                        case "Chart":
                            newId = metaDataDocMgt.CalcId(GetProperty(node.ParentNode, "NewID"), node.SelectSingleNode("./a:ChartPartID", metaDataDocMgt.XmlNamespaceMgt).InnerText, where).ToString(CultureInfo.InvariantCulture);
                            break;
                        }
                    }
                    else
                    {
                        newId = newId + 0;
                    }

                    //XmlNode partNode = node.SelectSingleNode(@"./a:Properties/a:Visible", metaDataDocMgt.XmlNamespaceMgt);
                    //newId = metaDataDocMgt.GetNewId.ToString(CultureInfo.InvariantCulture);
                }

                node.AppendChild(XmlUtility.CreateXmlElement("ID", newId));
            }
        }
        private static void ResortControlsForTrendscape(
            ref XmlNode currCntrlContainer,
            XmlNode cntrlNode,
            XmlNode currNode,
            XmlNode controlNode,
            ref bool breakWork,
            String optionCaptionML)
        {
            MetadataDocumentManagement metaDataDocMgt = MetadataDocumentManagement.Instance;

            if (metaDataDocMgt.MoveElementsDoc == null)
            {
                return;
            }

            StringBuilder queryPromoted = new StringBuilder();

            queryPromoted.Append(@"./a:MovePageElements/a:Page[@ID='");
            queryPromoted.Append(((int)MovePageElements.NotFormsId.TrendscapeControls).ToString(CultureInfo.InvariantCulture));
            queryPromoted.Append("' and @OptionCaptionML ='");
            queryPromoted.Append(optionCaptionML.Replace("'", ""));
            queryPromoted.Append("']");

            XmlNode trend = metaDataDocMgt.MoveElementsDoc.SelectSingleNode(queryPromoted.ToString(), metaDataDocMgt.XmlNamespaceMgt);

            if (trend == null)
            {
                return;
            }

            if (GetProperty(controlNode, "CaptionML") == null)
            {
                XmlNode propNode = controlNode.SelectSingleNode(
                    @"./a:Properties", metaDataDocMgt.XmlNamespaceMgt);

                string captionMlFromFile = GetAttribute(trend, "CaptionML");
                propNode.AppendChild(XmlUtility.CreateXmlElement("CaptionML", captionMlFromFile));

                propNode.AppendChild(XmlUtility.CreateXmlElement("TempProperty", "Trendscape"));

                XmlNode cntrlTypeNode = propNode.SelectSingleNode(
                    @"./a:Controltype", metaDataDocMgt.XmlNamespaceMgt);
                if ((cntrlTypeNode != null) && (cntrlTypeNode.InnerText == "RadioButton"))
                {
                    cntrlTypeNode.InnerText = "Field";
                }
            }

            if (currCntrlContainer == null)
            {
                Boolean done         = false;
                XmlNode tempCurrNode = currNode;
                if (tempCurrNode != null)
                {
                    while (!done)
                    {
                        if (((tempCurrNode.Name == "Control") && (GetProperty(tempCurrNode, "Controltype") == "Part")) ||
                            ((tempCurrNode.Name == "Group") && (GetProperty(tempCurrNode, "GroupType") == "Repeater")))
                        {
                            if ((tempCurrNode.PreviousSibling != null) &&
                                (tempCurrNode.PreviousSibling.Name == "Group") &&
                                (GetProperty(tempCurrNode.PreviousSibling, "CaptionML").Contains("ENU=Options")))
                            {
                                currCntrlContainer = tempCurrNode.PreviousSibling;
                                currCntrlContainer.AppendChild(controlNode.ParentNode.RemoveChild(controlNode));
                            }
                            else
                            {
                                tempCurrNode =
                                    cntrlNode.InsertBefore(
                                        PageControls.CreateBand(
                                            /*metaDataDocMgt.GetNewId.ToString(CultureInfo.InvariantCulture)*/
                                            metaDataDocMgt.CalcId(null, "Options", "ContentArea").ToString(CultureInfo.InvariantCulture), "ENU=Options"),
                                        tempCurrNode);
                                currCntrlContainer = tempCurrNode;
                                currCntrlContainer.AppendChild(controlNode.ParentNode.RemoveChild(controlNode));
                            }

                            done = true;
                        }

                        if (tempCurrNode != null)
                        {
                            break;
                        }
                    }

                    if (!done)
                    {
                        tempCurrNode = tempCurrNode.PreviousSibling;
                        if (tempCurrNode != null)
                        {
                            if (tempCurrNode.Name == "Properties")
                            {
                                breakWork = true;
                            }
                        }
                    }
                }

                if (done)
                {
                    breakWork = true;
                }
            }
            else if ((currCntrlContainer.Name == "Group") &&
                     (GetProperty(currCntrlContainer, "CaptionML").Contains("ENU=Options")))
            {
                currCntrlContainer.AppendChild(controlNode.ParentNode.RemoveChild(controlNode));
                breakWork = true;
            }
        }
        private static void MergeComplexStructure(XmlNode sectionNode, XmlNode destinationNode)
        {
            MetadataDocumentManagement metaDataDocMgt = MetadataDocumentManagement.Instance;
            Int32 GroupCount = 0;

            foreach (XmlNode childNode in sectionNode.ChildNodes)
            {
                switch (childNode.Name)
                {
                case "Properties":
                    XmlNode destinationPropertiesNode = destinationNode.SelectSingleNode("./a:" + childNode.Name, metaDataDocMgt.XmlNamespaceMgt);
                    if (destinationPropertiesNode == null)
                    {
                        destinationPropertiesNode = metaDataDocMgt.XmlDocument.CreateNode(XmlNodeType.Element, childNode.Name, metaDataDocMgt.XmlNamespace);
                        destinationNode.AppendChild(destinationPropertiesNode);
                    }

                    MergeSimpleSection(childNode, destinationPropertiesNode);
                    break;

                case "Action":
                {
                    MergeComplexStructure_Action(childNode, destinationNode);
                    break;
                }

                /*
                 * case "ID":
                 * {
                 *  XmlNode destinationActionNode = destinationNode.ParentNode.SelectSingleNode(".//a:Action[./a:ID = " + childNode.InnerText + "]", metaDataDocMgt.XmlNamespaceMgt);
                 *  if (destinationActionNode != null)
                 *  {
                 *    MergeSimpleSection(childNode.ParentNode, destinationActionNode);
                 *  }
                 *  else
                 *  {
                 *    string logStr = string.Format(
                 *      CultureInfo.InvariantCulture,
                 *      Resources.IDNotFound,
                 *      childNode.InnerText);
                 *    TransformationLog.GenericLogEntry(logStr, LogCategory.CheckInputFile, metaDataDocMgt.GetCurrentPageID);
                 *  }
                 *
                 *  break;
                 * }
                 */

                case "ActionGroup":
                case "Control":
                case "Group":
                case "Part":
                case "Field":
                    XmlNode complexIDNode = childNode.SelectSingleNode("./a:Properties/a:ID", metaDataDocMgt.XmlNamespaceMgt);

                    if ((complexIDNode == null) || (String.IsNullOrEmpty(complexIDNode.InnerText)))
                    {
                        complexIDNode = null;
                        XmlNode newNode = metaDataDocMgt.XmlDocument.ImportNode(childNode, true);
                        InsertIDAsChildInNodeList(newNode.SelectNodes(@".//a:Properties", metaDataDocMgt.XmlNamespaceMgt), sectionNode.Name);
                        InsertIDAsChildInNodeList(newNode.SelectNodes(@".//a:Action", metaDataDocMgt.XmlNamespaceMgt), sectionNode.Name);
                        if ((newNode.Name == "Group") || (newNode.Name == "Control") || (newNode.Name == "Field") || (newNode.Name == "Part") || (newNode.Name == "ActionGroup"))
                        {
                            if (newNode.SelectSingleNode("./a:Properties", metaDataDocMgt.XmlNamespaceMgt) == null)
                            {
                                XmlNode propertiesNode = metaDataDocMgt.XmlDocument.CreateNode(XmlNodeType.Element, "Properties", metaDataDocMgt.XmlNamespace);
                                XmlNode idNode         = metaDataDocMgt.XmlDocument.CreateNode(XmlNodeType.Element, "ID", metaDataDocMgt.XmlNamespace);
                                //idNode.InnerText = metaDataDocMgt.CalcID(null, null, sectionNode.Name);
                                //idNode.InnerText = metaDataDocMgt.GetNewId.ToString(CultureInfo.InvariantCulture); // not possible to generate id using content
                                idNode.InnerText = metaDataDocMgt.CalcId(null, (GroupCount++).ToString(CultureInfo.InvariantCulture), sectionNode.Name).ToString(CultureInfo.InvariantCulture);
                                propertiesNode.AppendChild(idNode);
                                newNode.PrependChild(propertiesNode);
                            }
                        }

                        destinationNode.AppendChild(newNode);
                    }
                    else
                    {
                        XmlNode destinationActionGroupNode = destinationNode.SelectSingleNode("./a:" + childNode.Name + "[./a:Properties/a:ID = '" + complexIDNode.InnerText + "']", metaDataDocMgt.XmlNamespaceMgt);
                        if (destinationActionGroupNode != null)
                        {
                            MergeComplexStructure(childNode, destinationActionGroupNode);
                        }
                        else
                        {
                            /* Something is wrong in the transformation input file... */
                            string logStr = string.Format(
                                CultureInfo.InvariantCulture,
                                Resources.IDNotFound,
                                complexIDNode.InnerText);
                            TransformationLog.GenericLogEntry(logStr, LogCategory.CheckInputFile, metaDataDocMgt.GetCurrentPageId);
                        }
                    }

                    break;

                case "Triggers":
                    XmlNode triggerNode = metaDataDocMgt.XmlDocument.ImportNode(childNode, true);
                    destinationNode.AppendChild(triggerNode);
                    break;
                }
            }
        }
        private static void MergeComplexStructure_Action(XmlNode childNode, XmlNode destinationNode)
        {
            if (childNode == null)
            {
                return;
            }

            MetadataDocumentManagement metaDataDocMgt = MetadataDocumentManagement.Instance;
            XmlNode actionIDNode = childNode.SelectSingleNode("./a:ID", metaDataDocMgt.XmlNamespaceMgt);

            if ((actionIDNode == null) || (String.IsNullOrEmpty(actionIDNode.InnerText)))
            {
                XmlNode captionMlNode = childNode.SelectSingleNode("./a:CaptionML", metaDataDocMgt.XmlNamespaceMgt);
                if (captionMlNode == null)
                {
                    captionMlNode = childNode.FirstChild;
                    string log = string.Format(CultureInfo.InvariantCulture, Resources.AddCaptionMl, childNode.InnerXml);
                    TransformationLog.GenericLogEntry(log, LogCategory.Warning, metaDataDocMgt.GetCurrentPageId);
                }

                String newID =
                    metaDataDocMgt.CalcId(GetProperty(captionMlNode.ParentNode, "NewID"),
                                          XmlUtility.GetCaption(captionMlNode.InnerText), captionMlNode.ParentNode.ParentNode.Name).ToString(CultureInfo.InvariantCulture);

                actionIDNode           = metaDataDocMgt.XmlDocument.CreateNode(XmlNodeType.Element, "ID", metaDataDocMgt.XmlNamespace);
                actionIDNode.InnerText = newID;

                XmlNode newNode = metaDataDocMgt.XmlDocument.ImportNode(childNode, true);
                newNode.PrependChild(actionIDNode);
                destinationNode.AppendChild(newNode);
            }
            else
            {
                XmlNode destinationActionNode = destinationNode.SelectSingleNode("./a:" + childNode.Name + "[./a:ID = " + actionIDNode.InnerText + "]", metaDataDocMgt.XmlNamespaceMgt);
                if (destinationActionNode != null)
                {
                    MergeSimpleSection(childNode, destinationActionNode);
                }
                else
                {
                    /* TODO  !!! TEMPORARY !!!  we should change TransformPages.xml instead! */
                    // Lets try to find it by ID:
                    XmlNodeList actionListById = childNode.SelectNodes(@".//a:ID", metaDataDocMgt.XmlNamespaceMgt);
                    if (actionListById.Count == 0)
                    {
                        /* Something is wrong in the transformation input file... */
                        string logStr = string.Format(
                            CultureInfo.InvariantCulture,
                            Resources.IDNotFound,
                            actionIDNode.InnerText);
                        TransformationLog.GenericLogEntry(logStr, LogCategory.CheckInputFile, metaDataDocMgt.GetCurrentPageId);

                        // break;
                        return;
                    }

                    foreach (XmlNode actionById in actionListById)
                    {
                        string  query = ".//a:Action[./a:ID = " + actionById.InnerText + "]";
                        XmlNode destinationActionNodeById = destinationNode.ParentNode.SelectSingleNode(query, metaDataDocMgt.XmlNamespaceMgt);
                        if (destinationActionNodeById != null)
                        {
                            MergeSimpleSection(actionById.ParentNode, destinationActionNodeById);
                        }
                        else
                        {
                            /* Something is wrong in the transformation input file... */
                            string logStr = string.Format(
                                CultureInfo.InvariantCulture,
                                Resources.IDNotFound,
                                actionById.InnerText);
                            string  tmpAddStr = string.Empty;
                            XmlNode tmp       = metaDataDocMgt.XmlCurrentFormNode.SelectSingleNode(".//a:ID[../a:ID = " + actionById.InnerText + "]", metaDataDocMgt.XmlNamespaceMgt);
                            if (tmp != null)
                            {
                                tmpAddStr = tmp.ParentNode.ParentNode.InnerXml;
                            }
                            TransformationLog.GenericLogEntry(logStr, LogCategory.CheckInputFile, metaDataDocMgt.GetCurrentPageId, tmpAddStr);
                        }
                    }
                }
            }
        }
        private static void MergeStructure(XmlNode sectionNode, SectionType sectionType)
        {
            MetadataDocumentManagement metaDataDocMgt = MetadataDocumentManagement.Instance;

            if (sectionType == SectionType.Actions)
            {
                foreach (XmlNode childNode in sectionNode.ChildNodes)
                {
                    /* TODO  !!! TEMPORARY !!!  we should change TransformPages.xml instead! */
                    XmlNode destinationNode = metaDataDocMgt.XmlCurrentFormNode.SelectSingleNode("./a:Actions/a:" + childNode.Name, metaDataDocMgt.XmlNamespaceMgt);
                    if (destinationNode == null)
                    {
                        XmlNode actionsNode = metaDataDocMgt.XmlCurrentFormNode.SelectSingleNode("./a:Actions", metaDataDocMgt.XmlNamespaceMgt);
                        XmlUtility.InsertNodeWithPropertyChild(actionsNode, childNode.Name, metaDataDocMgt.CalcId(null, null, childNode.Name) /*metaDataDocMgt.GetNewId */);
                        destinationNode = metaDataDocMgt.XmlCurrentFormNode.SelectSingleNode("./a:Actions/a:" + childNode.Name, metaDataDocMgt.XmlNamespaceMgt);
                    }

                    MergeComplexStructure(childNode, destinationNode);
                }
            }

            if (sectionType == SectionType.Controls)
            {
                foreach (XmlNode childNode in sectionNode.ChildNodes)
                {
                    XmlNode destinationNode = metaDataDocMgt.XmlCurrentFormNode.SelectSingleNode("./a:Controls/a:" + childNode.Name, metaDataDocMgt.XmlNamespaceMgt);
                    if (destinationNode == null)
                    {
                        XmlNode controlsNode = metaDataDocMgt.XmlCurrentFormNode.SelectSingleNode("./a:Controls", metaDataDocMgt.XmlNamespaceMgt);
                        XmlUtility.InsertNodeWithPropertyChild(controlsNode, childNode.Name, metaDataDocMgt.CalcId(null, null, childNode.Name) /* metaDataDocMgt.GetNewId */);
                        destinationNode = metaDataDocMgt.XmlCurrentFormNode.SelectSingleNode("./a:Controls/a:" + childNode.Name, metaDataDocMgt.XmlNamespaceMgt);
                    }

                    MergeComplexStructure(childNode, destinationNode);
                }
            }
        }