Updated "Phone 1" field to be "Household Phone" to match MLS
[eq/.git] / mls / windowsGUI / MLSFileTrimmer / Window1.xaml.cs
diff --git a/mls/windowsGUI/MLSFileTrimmer/Window1.xaml.cs b/mls/windowsGUI/MLSFileTrimmer/Window1.xaml.cs
new file mode 100644 (file)
index 0000000..098aa47
--- /dev/null
@@ -0,0 +1,151 @@
+using System;\r
+using System.Collections.Generic;\r
+using System.Linq;\r
+using System.Text;\r
+using System.Windows;\r
+using System.Windows.Controls;\r
+using System.Windows.Data;\r
+using System.Windows.Documents;\r
+using System.Windows.Input;\r
+using System.Windows.Media;\r
+using System.Windows.Media.Imaging;\r
+using System.Windows.Navigation;\r
+using System.Windows.Shapes;\r
+using Microsoft.Win32;\r
+using System.IO;\r
+using FolderBrowserDialog = System.Windows.Forms.FolderBrowserDialog;\r
+using System.Xml.Serialization;\r
+using System.Xml;\r
+using Ionic.Zip;\r
+using System.ComponentModel;\r
+using System.Threading;\r
+\r
+namespace MLSFileTrimmer\r
+{\r
+    /// <summary>\r
+    /// Interaction logic for Window1.xaml\r
+    /// </summary>\r
+    public partial class Window1 : Window\r
+    {\r
+        // default to My Documents\r
+        private String currentDirectory = String.Empty;\r
+        private ThirdCouncelorMLSFiles thirdCouncelorXml;\r
+        private BackgroundWorker worker;\r
+\r
+        public Window1()\r
+        {\r
+            InitializeComponent();\r
+            \r
+            // read in xml file\r
+            try\r
+            {\r
+                TextReader reader = new StreamReader("MLSRequiredFields.xml");\r
+                XmlSerializer serializer = new XmlSerializer(typeof(ThirdCouncelorMLSFiles));\r
+                this.thirdCouncelorXml = (ThirdCouncelorMLSFiles)serializer.Deserialize(reader);\r
+                reader.Close();\r
+            }\r
+            catch (XmlException ex)\r
+            {\r
+                MessageBox.Show(ex.Message, "XML Parse Error", MessageBoxButton.OK, MessageBoxImage.Error);\r
+            }\r
+            catch (InvalidOperationException ioe)\r
+            {\r
+                MessageBox.Show(ioe.InnerException.Message, "XML Serialization Error", MessageBoxButton.OK, MessageBoxImage.Error);\r
+            }\r
+        }\r
+\r
+\r
+        private void outDirButton_Click(object sender, RoutedEventArgs e)\r
+        {\r
+            FolderBrowserDialog openFolderDialog = new FolderBrowserDialog();\r
+\r
+            openFolderDialog.RootFolder = Environment.SpecialFolder.MyDocuments;\r
+            if (openFolderDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)\r
+            {\r
+                outputDirtextBox.Text = openFolderDialog.SelectedPath;\r
+                currentDirectory = openFolderDialog.SelectedPath;\r
+            }\r
+        }\r
+\r
+        private void parseButton_Click(object sender, RoutedEventArgs e)\r
+        {\r
+            // make sure the directory exists\r
+            if (outputDirtextBox.Text.Equals(String.Empty))\r
+            {\r
+                MessageBox.Show("Please select the correct output directory.");\r
+                return;\r
+            }\r
+            else if (!Directory.Exists(outputDirtextBox.Text))\r
+            {\r
+                MessageBox.Show(outputDirtextBox.Text + " does not exist. Please select the correct output directory.");\r
+            }\r
+\r
+            // make sure the original files exist\r
+            foreach (ThirdCouncelorMLSFilesMLSFile csvFile in this.thirdCouncelorXml.CSVFiles)\r
+            {\r
+                if (!File.Exists(this.currentDirectory + "\\" + csvFile.Name))\r
+                {\r
+                    MessageBox.Show(csvFile.Name + " does not exist.  Are you sure you have the right directory?");\r
+                    return;\r
+                }\r
+            }\r
+\r
+            // TODO: move to new background thread, post progress dialog\r
+            this.worker = new BackgroundWorker();\r
+            this.worker.DoWork +=new DoWorkEventHandler(worker_DoWork);\r
+            this.worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted);\r
+            this.worker.RunWorkerAsync();\r
+\r
+            this.trimmingLabel.Visibility = Visibility.Visible;\r
+        }\r
+\r
+        private void worker_DoWork(object sender, DoWorkEventArgs e)\r
+        {\r
+            // create new zip file\r
+            using (ZipFile zip = new ZipFile())\r
+            {\r
+\r
+                // trim each csv file\r
+                foreach (ThirdCouncelorMLSFilesMLSFile csvFile in this.thirdCouncelorXml.CSVFiles)\r
+                {\r
+                    string newFilename = this.currentDirectory + "\\" + csvFile.Name;\r
+                    string origFilename = newFilename + ".orig";\r
+                    File.Move(newFilename, origFilename);\r
+\r
+                    using (StreamReader origFile = new StreamReader(origFilename))\r
+                    {\r
+                        using (StreamWriter newFile = new StreamWriter(newFilename))\r
+                        {\r
+                            CSVTrimmer csvTrimmer = new CSVTrimmer(csvFile);\r
+                            csvTrimmer.ExtractData(origFile, newFile);\r
+                        }\r
+                    }\r
+                    // add new to zip file\r
+                    zip.AddItem(newFilename, "");\r
+                }\r
+                zip.Save(this.currentDirectory + "\\" + "EQZipFile.zip");\r
+            }\r
+\r
+            Thread.Sleep(1000);\r
+\r
+            // delete csv files\r
+            StringBuilder sb = new StringBuilder();\r
+            foreach (ThirdCouncelorMLSFilesMLSFile csvFile in this.thirdCouncelorXml.CSVFiles)\r
+            {\r
+                sb.Remove(0, sb.Length);\r
+                sb.Append(this.currentDirectory);\r
+                sb.Append("\\");\r
+                sb.Append(csvFile.Name);\r
+                File.Delete(sb.ToString());\r
+                sb.Append(".orig");\r
+                File.Delete(sb.ToString());\r
+            }\r
+        }\r
+\r
+        private void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)\r
+        {\r
+            this.trimmingLabel.Visibility = Visibility.Hidden;\r
+            this.finishedLabel.Visibility = Visibility.Visible;\r
+        }\r
+    }\r
+}\r