private void WriteGuiFile(string eventsFileDirectory, int guiNumber, BulkMacroTreeNode selectedNode)
 {
     if (!string.IsNullOrEmpty(selectedNode.filepathGui)) {
         File.Move(selectedNode.filepathGui,eventsFileDirectory + @"\step" + guiNumber + "_gui.xml");
         //  string readGui = CommonUtils.ReadFile(selectedNode.filepathGui);
       //  CommonUtils.WriteFile(eventsFileDirectory + @"\step" + guiNumber + "_gui.xml",readGui);
     } else {
         CommonUtils.WriteFile(eventsFileDirectory + @"\step" + guiNumber + "_gui.xml","<GuiRoot MXRequest=\"GetFullTree\"></GuiRoot>");
     }
     if (!string.IsNullOrEmpty(selectedNode.filepathGuiGim)) {
         File.Move(selectedNode.filepathGuiGim,eventsFileDirectory + @"\step" + guiNumber + "_gui_gim.xml");
        // string readGuiGim = CommonUtils.ReadFile(selectedNode.filepathGuiGim);
       //  CommonUtils.WriteFile(eventsFileDirectory + @"\step" + guiNumber + "_gui_gim.xml",readGuiGim);
     }
 }
 private void WriteStepEventFile(string eventsFileDirectory, int stepNumber, BulkMacroTreeNode bulkMacroTreeNode)
 {
     StreamWriter writer = new StreamWriter(eventsFileDirectory + @"\step" + stepNumber + "_events.xml");
     try {
         writer.Write(bulkMacroTreeNode.stepEvents);
     } finally {
         if (writer != null) {
             writer.Flush();
             writer.Close();
             writer.Dispose();
         }
     }
 }
 private void UpdateEventsFileWithNewName(BulkMacroTreeNode bulkMacroTreeNode)
 {
     string eventsFilePath = Directory.GetParent(bulkMacroTreeNode.filepathEvents) + @"\eventsfiles.xml";
     string renameStepRegex = "(<Step.*?label=\")(.*?)(\" events=\"" + bulkMacroTreeNode.stepName + "\".*?></Step>)";
     string eventsFile = CommonUtils.ReadFile(eventsFilePath);
     eventsFile = Regex.Replace(eventsFile, renameStepRegex, "$1" + bulkMacroTreeNode.stepTitle + "$3");
     CommonUtils.WriteFile(eventsFilePath, eventsFile);
 }
 private void WriteCustomsFile(string eventsFileDirectory, int customsNumber, BulkMacroTreeNode selectedNode)
 {
     if (!string.IsNullOrEmpty(selectedNode.filepathCustoms)) {
         File.Move(selectedNode.filepathCustoms,eventsFileDirectory + @"\step" + customsNumber + "_customs.xml");
        // string readCustoms = CommonUtils.ReadFile(selectedNode.filepathCustoms);
        // CommonUtils.WriteFile(eventsFileDirectory + @"\step" + customsNumber + "_customs.xml",readCustoms);
     } else {
         CommonUtils.WriteFile(eventsFileDirectory + @"\step" + customsNumber + "_customs.xml",@"<Customs></Customs>");
     }
 }
 private void StartSplittingFile()
 {
     if (!Directory.Exists(txtOutputDir.Text)) {
         Directory.CreateDirectory(txtOutputDir.Text);
     }
     string sessionKey = string.Empty;
     sessionKey = txtSessionKey.Text.Trim();
     string fileRead = string.Empty;
     StreamReader reader = new StreamReader(txtDirectory.Text);
     string eventsFile = "<Steps label=\"Automatically Generated\">";
     try {
         fileRead = reader.ReadToEnd();
         MatchCollection collection = Regex.Matches(fileRead, "<Comment.*>(.*)</Comment>");
         List<string> matches = new List<string>();
         for (int i = 0; i < collection.Count; i++) {
             matches.Add(collection[i].Groups[1].ToString());
         }
         string newFileRead = Regex.Replace(fileRead, "<Comment.*</Comment>", "</Events></MXClientScript><!-- --><MXClientScript><Events>");
         string[] test = new string[] { "<!-- -->" };
         string[] splitArray = newFileRead.Split(test, StringSplitOptions.RemoveEmptyEntries);
         for (int j = 0; j < splitArray.Length; j++) {
             if (j < collection.Count) {
                 StreamWriter writer = new StreamWriter(txtOutputDir.Text + @"\step" + (j + 1) + "_events.xml");
                 StreamWriter guiWriter = new StreamWriter(txtOutputDir.Text + @"\step" + (j + 1) + "_gui.xml");
                 StreamWriter customsWriter = new StreamWriter(txtOutputDir.Text + @"\step" + (j + 1) + "_customs.xml");
                 try {
                     guiWriter.Write(" ");
                     customsWriter.Write(@"<Customs></Customs>");
                     writer.Write(splitArray[j]);
                     if (string.IsNullOrEmpty(sessionKey)) {
                         eventsFile = eventsFile + "\r\n <Step id=\"" + (j + 1) + "\" label=\"" + collection[j].Groups[1].ToString() + "\" events=\"step" + (j + 1) + "_events.xml\" gui=\"step" + (j + 1) + "_gui.xml\" customs=\"step" + (j + 1) + "_customs.xml\"></Step>";
                     } else {
                         eventsFile = eventsFile + "\r\n <Step id=\"" + (j + 1) + "\" sessionKey=\"" + sessionKey + "\" label=\"" + collection[j].Groups[1].ToString() + "\" events=\"step" + (j + 1) + "_events.xml\" gui=\"step" + (j + 1) + "_gui.xml\" customs=\"step" + (j + 1) + "_customs.xml\"></Step>";
                     }
                     BulkMacroTreeNode node = new BulkMacroTreeNode();
                     node.filepathEvents = txtOutputDir.Text + @"\step" + (j + 1) + "_events.xml";
                     node.stepName = "step" + (j + 1) + "_events.xml";
                     node.stepTitle = collection[j].Groups[1].ToString();
                     node.stepEvents = splitArray[j];
                     node.Text = node.stepName + " | " + node.stepTitle;
                     tvResults.Nodes.Add(node);
                 } finally {
                     if (writer != null) {
                         writer.Close();
                     }
                     if (guiWriter != null) {
                         guiWriter.Close();
                     }
                     if (customsWriter != null) {
                         customsWriter.Close();
                     }
                 }
             } else {
                 StreamWriter writer = new StreamWriter(txtOutputDir.Text + @"\step" + (j + 1) + "_events.xml");
                 try {
                     writer.Write(splitArray[j]);
                     if (string.IsNullOrEmpty(sessionKey)) {
                         eventsFile = eventsFile + "\r\n <Step id=\"" + (j + 1) + "\" label=\"Exit\" events=\"step" + (j + 1) + "_events.xml\"></Step>";
                     } else {
                         eventsFile = eventsFile + "\r\n <Step id=\"" + (j + 1) + "\" sessionKey=\"" + sessionKey + "\" label=\"Exit\" events=\"step" + (j + 1) + "_events.xml\"></Step>";
                     }
                     //remove customs and gui for exit step
                     File.Delete(txtOutputDir.Text + @"\step" + (j + 1) + "_gui.xml");
                     File.Delete(txtOutputDir.Text + @"\step" + (j + 1) + "_gui.xml");
                     BulkMacroTreeNode node = new BulkMacroTreeNode();
                     node.filepathEvents = txtOutputDir.Text + @"\step" + (j + 1) + "_events.xml";
                     node.stepName = "step" + (j + 1) + "_events.xml";
                     node.stepTitle = "Exit";
                     node.stepEvents = splitArray[j];
                     node.Text = node.stepName + " | " + "Exit";
                     tvResults.Nodes.Add(node);
                 } finally {
                     if (writer != null) {
                         writer.Close();
                     }
                 }
             }
         }
     } finally {
         if (reader != null) {
             reader.Close();
         }
     }
     eventsFile = WriteEventsFile(eventsFile);
     buildFileText = buildMacroStep.Replace("${OutputFolderName}", Path.GetFileName(txtOutputDir.Text));
 }
        private void UpdateEventsFileWithCompareException(BulkMacroTreeNode bulkMacroTreeNode)
        {
            string eventsFilePath = Directory.GetParent(bulkMacroTreeNode.filepathEvents) + @"\eventsfiles.xml";
            string renameStepRegex = "(<Step.*?(id=\"\\d+\"))(.*?label=\".*?\" events=\"" + bulkMacroTreeNode.stepName + "\".*?></Step>)";
            string eventsFile = CommonUtils.ReadFile(eventsFilePath);
            eventsFile = Regex.Replace(eventsFile, renameStepRegex, "$1" +
                                       " compareExceptions=\"true\" exception=\"" +
                                       bulkMacroTreeNode.stepName.Replace("events", "exception") +
                                       "\" exceptionCustoms=\"" +
                                       bulkMacroTreeNode.stepName.Replace("events.xml", "exception") +
                                       "_customs.xml\" " +
                                       "$3");

            //write step1_exception.xml
            CommonUtils.WriteFile(Directory.GetParent(bulkMacroTreeNode.filepathEvents) +
                                  @"\" + bulkMacroTreeNode.stepName.Replace("events", "exception"), "");
            //write step1_exception_customs.xml
            CommonUtils.WriteFile(Directory.GetParent(bulkMacroTreeNode.filepathEvents) +
                                  @"\" + bulkMacroTreeNode.stepName.Replace("events.xml", "exception") + "_customs.xml", "");

            CommonUtils.WriteFile(eventsFilePath, eventsFile);
        }
        private void JoinSplitMacro()
        {
            XDocument xdoc = new XDocument();
            string[] availableFiles = Directory.GetFiles(txtDirectory.Text,"eventsfiles.xml");
            xdoc = XDocument.Parse(CommonUtils.ReadFile(availableFiles[0]));
            var item = from c1 in xdoc.Descendants("Steps").Elements()
                       select new { elements = c1.DescendantNodesAndSelf() };
            string completeFile = "";

            for (int i = 0; i < item.Count(); i++) {
                BulkMacroTreeNode node = new BulkMacroTreeNode();
                XElement eement =  item.ElementAt(i).elements.ElementAt(0) as XElement;
                node.startStepId=Convert.ToInt32(eement.Attribute("id").Value);
                eement.Attribute("sessionKey");
                node.stepTitle = eement.Attribute("label").Value.ToString();
                node.filepathEvents = txtDirectory.Text + "/"+eement.Attribute("events").Value;

                node.filepathCustoms = eement.Attribute("customs")!=null? (txtDirectory.Text + "/"+eement.Attribute("customs").Value):"";
                node.filepathGui  = eement.Attribute("gui") !=null? (txtDirectory.Text + "/"+eement.Attribute("gui").Value):"";
                node.stepEvents = CommonUtils.ReadFile(node.filepathEvents);
                node.stepName = eement.Attribute("events").Value;
                node.Text = eement.Attribute("events").Value + " | " + node.stepTitle;

                tvResults.Nodes.Add(node);

            //                string readFile = CommonUtils.ReadFile(node.filepathEvents).Replace("</Events>","").Replace("</MXClientScript>","");
            //                if (!readFile.Contains("LoginInfo")) {
            //                    completeFile = completeFile + CommonUtils.ReadFile(node.filepathEvents).Replace("</Events>","").Replace("</MXClientScript>","").Replace("<Events>","").Replace("<MXClientScript>","")+"<Comment EventID=\"18\">"+node.stepTitle+"</Comment>";
            //                } else {
            //                    completeFile = completeFile + CommonUtils.ReadFile(node.filepathEvents).Replace("</Events>","").Replace("</MXClientScript>","");
            //                    if (!string.Equals(node.stepTitle,"Exit") ) {
            //
            //                        completeFile = completeFile +"<Comment EventID=\"18\">"+node.stepTitle+"</Comment>";
            //                    }
            //                }
            }
              //      completeFile = completeFile +"</Events></MXClientScript>";
              //      string tempPath = Path.GetTempFileName();
              //      CommonUtils.WriteFile(tempPath,completeFile);
              //     txtDirectory.Text = tempPath;
        }
        private void groupToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Control control = GetSourceControl((sender as ToolStripMenuItem).GetCurrentParent()) as Control;
            MWTreeView treeView = control as MWTreeView;

            if (treeView != null) {

                if (treeView.SelNodes.Count > 0) {

                    SessionKeyAndTitleForm form = new SessionKeyAndTitleForm(txtOutputDir.Text);
                    DialogResult result = form.ShowDialog();
                    SetDefaultFormValues(form);
                    if (result == DialogResult.OK) {
                        int startIndex = Convert.ToInt32(((NumericUpDown)form.Controls["nudStartIndex"]).Value);
                        string outputFolderName = form.Controls["txtFolderName"].Text + "-" + form.eventsGroupNameAndID.OperationGeneratedID;
                        string testTitle = form.Controls["txtTestTitle"].Text;

                        object[] selectedKeys = new object[treeView.SelNodes.Count];
                        treeView.SelNodes.Keys.CopyTo(selectedKeys, 0);

                        List<BulkMacroTreeNode> selectedNodes = new List<BulkMacroTreeNode>();

                        bool emptyParentInSelection = false;

                        for (int i = 0; i < selectedKeys.Length; i++) {
                            BulkMacroTreeNode node = (treeView.SelNodes[selectedKeys[i]] as Automation.Common.MWTreeNodeWrapper).Node as BulkMacroTreeNode;
                            if (!node.Text.Contains("|")) {
                                emptyParentInSelection = true;
                            }
                            selectedNodes.Add(node);
                        }

                        if (!emptyParentInSelection) {
                            tvResults.ClearSelNodes();
                            selectedNodes.Sort(BulkMacroTreeNode.stepIndexComparison);

                            List<BulkMacroTreeNode> movedNodes = new List<BulkMacroTreeNode>();
                            for (int i = 0; i < selectedNodes.Count; i++) {

                                BulkMacroTreeNode bulkMacroTreeNode = selectedNodes[i].Clone() as BulkMacroTreeNode;
                                bulkMacroTreeNode.endStepId = selectedNodes[i].endStepId;
                                bulkMacroTreeNode.filepathEvents = selectedNodes[i].filepathEvents;
                                bulkMacroTreeNode.startStepId = selectedNodes[i].startStepId;
                                bulkMacroTreeNode.stepEvents = selectedNodes[i].stepEvents;
                                bulkMacroTreeNode.filepathCustoms =  selectedNodes[i].filepathCustoms;
                                bulkMacroTreeNode.filepathGui =  selectedNodes[i].filepathGui;
                                bulkMacroTreeNode.filepathGuiGim =  selectedNodes[i].filepathGuiGim;

                                CleanOutputDir(bulkMacroTreeNode);

                                //outputFolderName
                                bulkMacroTreeNode.stepName = Regex.Replace(selectedNodes[i].stepName, "\\d+", ((i) + startIndex).ToString());
                                bulkMacroTreeNode.stepTitle = selectedNodes[i].stepTitle;
                                bulkMacroTreeNode.Text = bulkMacroTreeNode.stepName + " | " + bulkMacroTreeNode.stepTitle;
                                bulkMacroTreeNode.filepathEvents = Directory.GetParent(bulkMacroTreeNode.filepathEvents) + @"\" + outputFolderName + @"\" + bulkMacroTreeNode.stepName;
                                tvResults.DeselectNode(bulkMacroTreeNode, true);
                                selectedNodes[0].Nodes.Add(bulkMacroTreeNode);
                                //outputFolderName
                                selectedNodes[0].isParentNode = true;
                                selectedNodes[0].Tag = outputFolderName;
                                movedNodes.Add(bulkMacroTreeNode);
                                if (i > 0) {
                                    selectedNodes[i].Remove();
                                }
                            }

                            selectedNodes[0].Text = testTitle;
                            selectedNodes[0] = new BulkMacroTreeNode();
                            GenerateSplitFile(movedNodes, form.sessionKey, outputFolderName, startIndex);
                            CreateBuildXmlFile(testTitle, outputFolderName);
                            File.Delete(Directory.GetParent(txtOutputDir.Text) + @"\eventsfiles.xml");
                        }
                    }
                }
                tvResults.ExpandAll();
            }
        }
 private void CleanOutputDir(BulkMacroTreeNode bulkMacroTreeNode)
 {
     File.Delete(bulkMacroTreeNode.filepathEvents);
     if (File.Exists(Directory.GetParent(bulkMacroTreeNode.filepathEvents)+"/eventsfiles.xml")) {
          File.Delete(Directory.GetParent(bulkMacroTreeNode.filepathEvents)+"/eventsfiles.xml");
     }
     string parentDirectory = Directory.GetParent(bulkMacroTreeNode.filepathEvents).ToString();
     if (string.IsNullOrEmpty(bulkMacroTreeNode.filepathCustoms) && File.Exists(parentDirectory + @"\" + Path.GetFileName(bulkMacroTreeNode.filepathEvents).Replace("events", "customs"))) {
         File.Delete(parentDirectory + @"\" + Path.GetFileName(bulkMacroTreeNode.filepathEvents).Replace("events", "customs"));
     }
     if (string.IsNullOrEmpty(bulkMacroTreeNode.filepathGui) && File.Exists(parentDirectory + @"\" + Path.GetFileName(bulkMacroTreeNode.filepathEvents).Replace("events", "gui"))) {
         File.Delete(parentDirectory + @"\" + Path.GetFileName(bulkMacroTreeNode.filepathEvents).Replace("events", "gui"));
     }
 }
        private void UpdateEventsFileWithCompareDialogs(BulkMacroTreeNode bulkMacroTreeNode)
        {
            string eventsFilePath = Directory.GetParent(bulkMacroTreeNode.filepath) + @"\eventsfiles.xml";
            string renameStepRegex = "(<Step.*?(id=\"\\d+\"))(.*?label=\".*?\" events=\"" + bulkMacroTreeNode.stepName + "\".*?></Step>)";
            string eventsFile = FrontendUtils.ReadFile(eventsFilePath);
            eventsFile = Regex.Replace(eventsFile, renameStepRegex, "$1" +
                                       " compareDialogs=\"true\" dialog=\"" +
                                       bulkMacroTreeNode.stepName.Replace("events", "dialog") +
                                       "\" " +
                                       "$3");

            //write step1_dialog.xml
            FrontendUtils.WriteFile(Directory.GetParent(bulkMacroTreeNode.filepath) +
                                    @"\" + bulkMacroTreeNode.stepName.Replace("events", "dialog"), "");
            FrontendUtils.WriteFile(eventsFilePath, eventsFile);
        }
 private void CleanOutputDir(BulkMacroTreeNode bulkMacroTreeNode)
 {
     File.Delete(bulkMacroTreeNode.filepath);
     string parentDirectory = Directory.GetParent(bulkMacroTreeNode.filepath).ToString();
     if (File.Exists(parentDirectory + @"\" + Path.GetFileName(bulkMacroTreeNode.filepath).Replace("events", "customs"))) {
         File.Delete(parentDirectory + @"\" + Path.GetFileName(bulkMacroTreeNode.filepath).Replace("events", "customs"));
     }
     if (File.Exists(parentDirectory + @"\" + Path.GetFileName(bulkMacroTreeNode.filepath).Replace("events", "gui"))) {
         File.Delete(parentDirectory + @"\" + Path.GetFileName(bulkMacroTreeNode.filepath).Replace("events", "gui"));
     }
 }