Merge branch 'owenleonard/ht_sandbox' of git@github.com:apippin/tc into owenleonard...
[eq/.git] / mls / windowsGUI / MLSFileTrimmer / Window1.xaml.cs
1 using System;\r
2 using System.ComponentModel;\r
3 using System.IO;\r
4 using System.Text;\r
5 using System.Threading;\r
6 using System.Windows;\r
7 using System.Xml;\r
8 using System.Xml.Serialization;\r
9 using Ionic.Zip;\r
10 using FolderBrowserDialog = System.Windows.Forms.FolderBrowserDialog;\r
11 \r
12 namespace MLSFileTrimmer\r
13 {\r
14     /// <summary>\r
15     /// Interaction logic for Window1.xaml\r
16     /// </summary>\r
17     public partial class Window1 : Window\r
18     {\r
19         // default to My Documents\r
20         private String currentDirectory = String.Empty;\r
21         private ThirdCouncelorMLSFiles thirdCouncelorXml;\r
22         private BackgroundWorker worker;\r
23 \r
24         public Window1()\r
25         {\r
26             InitializeComponent();\r
27             \r
28             // read in xml file\r
29             try\r
30             {\r
31                 TextReader reader = new StreamReader("MLSRequiredFields.xml");\r
32                 XmlSerializer serializer = new XmlSerializer(typeof(ThirdCouncelorMLSFiles));\r
33                 this.thirdCouncelorXml = (ThirdCouncelorMLSFiles)serializer.Deserialize(reader);\r
34                 reader.Close();\r
35             }\r
36             catch (XmlException ex)\r
37             {\r
38                 MessageBox.Show(ex.Message, "XML Parse Error", MessageBoxButton.OK, MessageBoxImage.Error);\r
39             }\r
40             catch (InvalidOperationException ioe)\r
41             {\r
42                 MessageBox.Show(ioe.InnerException.Message, "XML Serialization Error", MessageBoxButton.OK, MessageBoxImage.Error);\r
43             }\r
44         }\r
45 \r
46 \r
47         private void outDirButton_Click(object sender, RoutedEventArgs e)\r
48         {\r
49             FolderBrowserDialog openFolderDialog = new FolderBrowserDialog();\r
50 \r
51             openFolderDialog.RootFolder = Environment.SpecialFolder.MyDocuments;\r
52             if (openFolderDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)\r
53             {\r
54                 outputDirTextBox.Text = openFolderDialog.SelectedPath;\r
55                 currentDirectory = openFolderDialog.SelectedPath;\r
56             }\r
57         }\r
58 \r
59         private void parseButton_Click(object sender, RoutedEventArgs e)\r
60         {\r
61             // make sure the directory exists\r
62             if (outputDirTextBox.Text.Equals(String.Empty))\r
63             {\r
64                 MessageBox.Show("Please select the correct output directory.");\r
65                 return;\r
66             }\r
67             else if (!Directory.Exists(outputDirTextBox.Text))\r
68             {\r
69                 MessageBox.Show(outputDirTextBox.Text + " does not exist. Please select the correct output directory.");\r
70             }\r
71 \r
72             // make sure the original files exist\r
73             foreach (ThirdCouncelorMLSFilesMLSFile csvFile in this.thirdCouncelorXml.CSVFiles)\r
74             {\r
75                 if (!File.Exists(this.currentDirectory + "\\" + csvFile.Name))\r
76                 {\r
77                     MessageBox.Show(csvFile.Name + " does not exist.  Are you sure you have the right directory?");\r
78                     return;\r
79                 }\r
80             }\r
81 \r
82             // TODO: move to new background thread, post progress dialog\r
83             this.worker = new BackgroundWorker();\r
84             this.worker.DoWork +=new DoWorkEventHandler(worker_DoWork);\r
85             this.worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted);\r
86             this.worker.RunWorkerAsync();\r
87 \r
88             this.trimmingLabel.Visibility = Visibility.Visible;\r
89         }\r
90 \r
91         private void worker_DoWork(object sender, DoWorkEventArgs e)\r
92         {\r
93             // create new zip file\r
94             using (ZipFile zip = new ZipFile())\r
95             {\r
96 \r
97                 // trim each csv file\r
98                 foreach (ThirdCouncelorMLSFilesMLSFile csvFile in this.thirdCouncelorXml.CSVFiles)\r
99                 {\r
100                     string newFilename = this.currentDirectory + "\\" + csvFile.Name;\r
101                     string origFilename = newFilename + ".orig";\r
102                     File.Move(newFilename, origFilename);\r
103 \r
104                     using (StreamReader origFile = new StreamReader(origFilename))\r
105                     {\r
106                         using (StreamWriter newFile = new StreamWriter(newFilename))\r
107                         {\r
108                             CSVTrimmer csvTrimmer = new CSVTrimmer(csvFile);\r
109                             csvTrimmer.ExtractData(origFile, newFile);\r
110                         }\r
111                     }\r
112                     // add new to zip file\r
113                     zip.AddItem(newFilename, "");\r
114                 }\r
115                 zip.Save(this.currentDirectory + "\\" + "EQZipFile.zip");\r
116             }\r
117 \r
118             Thread.Sleep(1000);\r
119 \r
120             // delete csv files\r
121             StringBuilder sb = new StringBuilder();\r
122             foreach (ThirdCouncelorMLSFilesMLSFile csvFile in this.thirdCouncelorXml.CSVFiles)\r
123             {\r
124                 sb.Remove(0, sb.Length);\r
125                 sb.Append(this.currentDirectory);\r
126                 sb.Append("\\");\r
127                 sb.Append(csvFile.Name);\r
128                 File.Delete(sb.ToString());\r
129                 sb.Append(".orig");\r
130                 File.Delete(sb.ToString());\r
131             }\r
132         }\r
133 \r
134         private void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)\r
135         {\r
136             this.trimmingLabel.Visibility = Visibility.Hidden;\r
137             this.finishedLabel.Visibility = Visibility.Visible;\r
138         }\r
139 \r
140         private void exitButton_Click(object sender, RoutedEventArgs e)\r
141         {\r
142             this.Close();\r
143         }\r
144     }\r
145 }\r