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