using System; using System.Collections.Generic; //using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; using System.Text; using System.IO; //using Subgurim.Controles; using System.Text.RegularExpressions; using System.Diagnostics; public partial class MapRoute : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { try { string id = Request.QueryString.Get("id"); if (!(id == null || id == "")) { string filename = id + "-location.csv"; string pathname = System.AppDomain.CurrentDomain.BaseDirectory.ToString() + "FileDatabase\\"; try { TextReader fileReader = new StreamReader(pathname + filename); loadDataRoute(fileReader); fileReader.Close(); deleteFile(filename, pathname); } catch { //reload_page(); Response.Write(""); } findOldFile(System.AppDomain.CurrentDomain.BaseDirectory.ToString() + "FileSave\\"); findOldFile(System.AppDomain.CurrentDomain.BaseDirectory.ToString() + "FileDatabase\\"); findOldFile(System.AppDomain.CurrentDomain.BaseDirectory.ToString() + "FileUpload\\"); reload_page(); } else { Page.RegisterStartupScript("myscript", ""); } } catch { Page.RegisterStartupScript("myscript", ""); } } } //find file approx 1 month away and delete protected void findOldFile(string path) { DirectoryInfo directory = new DirectoryInfo(path); DateTime creationDate, currentDate; foreach (FileInfo file in directory.GetFiles()) { creationDate = file.CreationTime; currentDate = System.DateTime.Now; if ((creationDate.Month < currentDate.Month && creationDate.Day <= currentDate.Day) || (creationDate.Month + 1 < currentDate.Month)) { file.Delete(); } } } public void loadDataRoute(TextReader fileReader) { try { //load location file loadData(fileReader); //load route file //clear all in list if (chkMerge.Checked == false) { lstRoute.Items.Clear(); lstReRoute.Items.Clear(); hdnRoute.Value = ""; hdnReRoute.Value = ""; } string line = fileReader.ReadLine(); string[] bits = line.Split(','); while (bits[0] != "") { hdnRoute.Value += bits[0].Trim() + "~"; line = fileReader.ReadLine();//title string[] info = line.Split(','); if (info[0] == "Location" && info[1] == "Arrival Time" && info[2] == "Wait Time") line = fileReader.ReadLine();//line1 bits = line.Split(','); while (bits[0] != "") { hdnRoute.Value += line.Trim() + "\r\n"; line = fileReader.ReadLine();//line1 if (line != null) { bits = line.Split(','); } else { bits = "".Split(','); } } //addList.Value = temp; //lstRoute.Items.Add(addList); hdnRoute.Value += "!"; line = fileReader.ReadLine();//line1 //line = fileReader.ReadLine();//line1 if (line != null) { bits = line.Split(','); } else { bits = "".Split(','); } } } catch { Response.Write(""); } } public void loadData(TextReader fileReader) { string title = fileReader.ReadLine(); title = fileReader.ReadLine(); string[] bits = title.Split(','); string line = fileReader.ReadLine(); line = fileReader.ReadLine(); string[] info = line.Split(','); if (info[0] == "Name" && info[1] == "Address" && info[2] == "Type" && info[3] == "Latitude" && info[4] == "Longitude") { string line2 = fileReader.ReadLine(); bits = line2.Split(','); hdnTimePoint.Value = ""; hdnBusStop.Value = ""; hdnInterchange.Value = ""; string tempTP = ""; string tempBS = ""; string tempI = ""; string script = "DrawMap();resetMarker();"; while (bits[0] != "") { ListItem addList = new ListItem(); addList.Text = bits[0]; addList.Value = bits[1] + "~" + bits[3] + "~" + bits[4]; if (bits[2].ToLower() == "time point") { lstTimePoint.Items.Add(addList); script += "setLatLong(" + bits[3] + " , " + bits[4] + ");dragMarker('timePoint', 1); "; tempTP = tempTP + bits[0] + "," + bits[1] + ",time point," + bits[3] + "," + bits[4] + "\r\n"; } else if (bits[2].ToLower() == "bus stop") { lstBusStop.Items.Add(addList); script += "setLatLong(" + bits[3] + " , " + bits[4] + ");dragMarker('busStop', 2); "; tempBS = tempBS + bits[0] + "," + bits[1] + ",bus stop," + bits[3] + "," + bits[4] + "\r\n"; } else if (bits[2].ToLower() == "interchange") { lstInterchange.Items.Add(addList); script += "setLatLong(" + bits[3] + " , " + bits[4] + ");dragMarker('interchange', 3); "; tempI = tempI + bits[0] + "," + bits[1] + ",interchange," + bits[3] + "," + bits[4] + "\r\n"; } //new line line2 = fileReader.ReadLine(); if (line2 != null) { bits = line2.Split(','); } else { bits = "".Split(','); } } hdnTimePoint.Value = tempTP; hdnBusStop.Value = tempBS; hdnInterchange.Value = tempI; Page.RegisterStartupScript("myscript", ""); } else { reload_page(); Response.Write(""); } } protected void btnCreate_Click(object sender, EventArgs e) { if (txtFileName.Text == "") { Response.Write(""); } else { if (hdnRoute.Value != "") { string fileName = txtFileName.Text + ".csv"; string path = System.AppDomain.CurrentDomain.BaseDirectory.ToString() + "FileSave"; string pathName = @Path.Combine(path, fileName); createFile(fileName, pathName); string linkTo = Request.ApplicationPath + @"/FileSave/" + fileName; //save the file. Response.Redirect(linkTo); } else { Response.Write(""); } } reload_page(); } public void createFile(string name, string path) { try { string temp = ""; //string pathName = createUniqueFileName(txtFileName.Text); StreamWriter newFile = new StreamWriter(path, true); newFile.WriteLine("Map Route"); newFile.WriteLine(); newFile.WriteLine(); newFile.WriteLine("Name,Address,Type,Latitude,Longitude"); if (hdnTimePoint.Value != "") { temp = hdnTimePoint.Value; temp = temp.Substring(0, temp.Length - 2); newFile.WriteLine(temp); } if (hdnBusStop.Value != "") { temp = hdnBusStop.Value; temp = temp.Substring(0, temp.Length - 2); newFile.WriteLine(temp); } if (hdnInterchange.Value != "") { temp = hdnInterchange.Value; temp = temp.Substring(0, temp.Length - 2); newFile.WriteLine(temp); } createRoute(newFile); newFile.Close(); } catch { Response.Write(""); } reload_page(); } public void createRoute(StreamWriter file) { file.WriteLine(); string[] splitList = hdnRoute.Value.Split('!'); string[] information; // string DirectionInfo = ""; string timeSheet = ""; int count = splitList.Length; for (int i = 0; i < count - 1; i++) { information = splitList[i].Split('~'); file.WriteLine(information[0]);//route name timeSheet = information[1]; string[] temp1 = timeSheet.Replace("\r\n", "!").Split('!'); string[] temp2 = temp1[0].Split(','); if (temp2.Length == 11) { file.WriteLine("Location,Arrival Time,Wait Time,Repeat Time,First Repeat,Last Repeat,Peak,Wait Time,Repeat Time,First Repeat,Last Repeat"); } else { file.WriteLine("Location,Arrival Time,Wait Time,Repeat Time,First Repeat,Last Repeat"); } file.WriteLine(timeSheet); //DirectionInfo = DirectionInfo + direction; } //file.WriteLine(DirectionInfo); if (hdnReRoute.Value != "") { file.WriteLine(); file.WriteLine("From,To,"); string[] info = hdnReRoute.Value.Split('~'); for (int i = 0; i < info.Length; i++) { file.WriteLine(info[i]); } } } public bool findFile(string fileName) { //delete file DirectoryInfo directory = new DirectoryInfo(@"C:\Users\" + System.Environment.UserName + @"\Downloads\"); foreach (FileInfo file in directory.GetFiles()) { if (file.Name == fileName) { return true; } } return false; } public string createUniqueFileName() { // create a writer and open the file DateTime time = DateTime.Now; string timeConvert = time + ""; timeConvert = timeConvert.Replace("/", ""); timeConvert = timeConvert.Replace(":", ""); timeConvert = timeConvert.Replace(" ", ""); timeConvert = timeConvert.Replace("AM", ""); timeConvert = timeConvert.Replace("PM", ""); return timeConvert; } string routeFile, reRouteFile; string[] routeFileArray, reRouteFileArray; string timePointFile, busStopFile, interchangeFile; string[] timePointFileArray, busStopFileArray, interchangeFileArray; string[] info; public void reload_page() { routeFile = "" + hdnRoute.Value; reRouteFile = "" + hdnReRoute.Value; timePointFile = "" + hdnTimePoint.Value; busStopFile = "" + hdnBusStop.Value; interchangeFile = "" + hdnInterchange.Value; //remove all from list box lstRoute.Items.Clear(); lstReRoute.Items.Clear(); //add back string script = "DrawMap();resetMarker();"; if (routeFile != "") { routeFileArray = routeFile.Split('!'); for (int i = 0; i < routeFileArray.Length - 1; i++) { ListItem addList = new ListItem(); info = routeFileArray[i].Split('~'); addList.Text = info[0]; addList.Value = info[1]; lstRoute.Items.Add(addList); } } if (reRouteFile != "") { reRouteFileArray = reRouteFile.Split('!'); for (int i = 0; i < reRouteFileArray.Length - 1; i++) { ListItem addList = new ListItem(); info = reRouteFileArray[i].Split('~'); addList.Text = info[0]; addList.Value = info[1]; lstReRoute.Items.Add(addList); } } if (timePointFile != "") { timePointFile = timePointFile.Replace("\r\n", "!"); timePointFileArray = timePointFile.Split('!'); for (int i = 0; i < timePointFileArray.Length - 1; i++) { info = timePointFileArray[i].Split(','); script += "setLatLong(" + info[3] + " , " + info[4] + ");dragMarker('timePoint', 1);"; } } if (busStopFile != "") { busStopFile = busStopFile.Replace("\r\n", "!"); busStopFileArray = busStopFile.Split('!'); for (int i = 0; i < busStopFileArray.Length - 1; i++) { info = busStopFileArray[i].Split(','); script += "setLatLong(" + info[3] + " , " + info[4] + ");dragMarker('busStop', 2); "; } } if (interchangeFile != "") { interchangeFile = interchangeFile.Replace("\r\n", "!"); interchangeFileArray = interchangeFile.Split('!'); for (int i = 0; i < interchangeFileArray.Length - 1; i++) { info = interchangeFileArray[i].Split(','); script += "setLatLong(" + info[3] + " , " + info[4] + ");dragMarker('interchange', 3); "; } } Page.RegisterStartupScript("myscript", ""); } protected void btnLoad_Click(object sender, EventArgs e) { try { // Get the HttpFileCollection if (FileUploadSingle.HasFile) { string path = Server.MapPath("~/FileUpload/"); string fileName = Path.GetFileName(FileUploadSingle.PostedFile.FileName); // check for the valid file extension string fileExtension = Path.GetExtension(fileName).ToLower(); if (fileExtension.Equals(".csv")) { // now save the file to the disk FileUploadSingle.SaveAs(path + fileName); string loadpath = System.AppDomain.CurrentDomain.BaseDirectory.ToString() + "FileUpload\\"; TextReader fileReader = new StreamReader(path + fileName); loadDataRoute(fileReader); fileReader.Close(); deleteFile(fileName, path); reload_page(); } else { reload_page(); Response.Write(""); } } else { reload_page(); Response.Write(""); } } catch (Exception ex) { reload_page(); Response.Write(""); } } public void deleteFile(string fileName, string path) { //delete file DirectoryInfo directory = new DirectoryInfo(path); foreach (FileInfo file in directory.GetFiles()) { if (file.Name == fileName) { file.Delete(); } } } protected void location_Click(object sender, EventArgs e) { string id = Request.QueryString.Get("id"); if (hdnRoute.Value != "" || hdnReRoute.Value != "") { id = createUniqueFileName(); string filename = id + "-location.csv"; string path = System.AppDomain.CurrentDomain.BaseDirectory.ToString() + "/FileDatabase/" + filename; createFile(filename, path); } string linkTo = "MapLocation.Aspx?id=" + id; Response.Redirect(linkTo); } }