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.Drawing; using System.Text; using System.IO; using MasterEdit; public partial class MapRoute : System.Web.UI.Page { List allLocation = new List(); List allRoute = new List(); List allreRoute = new List(); protected void Page_Load(object sender, EventArgs e) { string id = checkuser(); string initLat = "0", initLng = "0"; if (id != "") { string line; string[] bits; try { TextReader fileReader = new StreamReader(System.AppDomain.CurrentDomain.BaseDirectory.ToString() + @"\Data\location-" + userFilename + ".csv"); try { //read location data line = fileReader.ReadLine(); if (line == null) { bits = "".Split(','); } else { bits = line.Split(','); } while (bits[0] != "") { addLocation(bits[0], bits[1], Convert.ToDouble(bits[3]), Convert.ToDouble(bits[4]), bits[2]); if (!IsPostBack) { ListItem addList = new ListItem(); addList.Text = bits[0]; addList.Value = bits[1] + "~" + bits[3] + "~" + bits[4]; if (bits[2] == "Time Point") lstTimePoint.Items.Add(addList); else if (bits[2] == "Bus Stop") lstBusStop.Items.Add(addList); else if (bits[2] == "Interchange") { lstInterchange.Items.Add(addList); if (initLng == "0" || initLat == "0") { initLat = Convert.ToString(bits[3]); initLng = Convert.ToString(bits[4]); hdnInitLat.Value = bits[3]; hdnInitLng.Value = bits[4]; } } } line = fileReader.ReadLine(); if (line == null) { bits = "".Split(','); } else { bits = line.Split(','); } } } catch { } fileReader.Close(); //read route data if (!IsPostBack) { //delete when done lstRoute.Items.Clear(); lstReRoute.Items.Clear(); } fileReader = new StreamReader(System.AppDomain.CurrentDomain.BaseDirectory.ToString() + @"\Data\route-" + userFilename + ".csv"); try { line = fileReader.ReadLine(); bits = line.Split(','); while (bits[0] != "") { //routename MasterEdit.route addRoute = new MasterEdit.route(); addRoute.name = bits[0]; line = fileReader.ReadLine(); bits = line.Split(','); while (bits[0] != "") { foreach (MasterEdit.location aLocation in allLocation) { if (aLocation.name == bits[0]) addRoute.addLocation(aLocation); } addRoute.addWaitTime(Convert.ToDouble(bits[1]), 0); addRoute.addArrivalTime(Convert.ToDouble(bits[2]), 0); if (bits[3] != "") { addRoute.firstRepeat = bits[3]; addRoute.lastRepeat = bits[4]; addRoute.repeatTime = Convert.ToDouble(bits[5]); } if (bits.Length > 6) { if (bits[6] != "") { addRoute.addWaitTime(Convert.ToDouble(bits[6]), 1); addRoute.addArrivalTime(Convert.ToDouble(bits[7]), 1); if (bits[8] != "") { addRoute.firstRepeatPeak = bits[8]; addRoute.lastRepeatPeak = bits[9]; addRoute.repeatTimePeak = Convert.ToDouble(bits[10]); } } } line = fileReader.ReadLine(); if (line == null) { bits = "".Split(','); } else { bits = line.Split(','); } } allRoute.Add(addRoute); line = fileReader.ReadLine(); if (line == null) { bits = "".Split(','); } else { bits = line.Split(','); } } } catch { } fileReader.Close(); rerouteListAdd(); //reroute data fileReader = new StreamReader(System.AppDomain.CurrentDomain.BaseDirectory.ToString() + @"\Data\reroute-" + userFilename + ".csv"); try { line = fileReader.ReadLine(); bits = line.Split(','); while (bits[0] != "") { MasterEdit.reroute reRoute = new MasterEdit.reroute(); foreach (MasterEdit.route r in allRoute) { if (r.name == bits[0]) reRoute.FromRoute = r; if (r.name == bits[1]) reRoute.ToRoute = r; } reRoute.Name = bits[0] + "<->" + bits[1]; if (reRoute.FromRoute.name != "" && reRoute.ToRoute.name != "") allreRoute.Add(reRoute); line = fileReader.ReadLine(); if (line == null) { bits = "".Split(','); } else { bits = line.Split(','); } } } catch { } fileReader.Close(); if (!IsPostBack) { lstRoute.Items.Clear(); lstReRoute.Items.Clear(); foreach (MasterEdit.reroute rr in allreRoute) { ListItem addList = new ListItem(); addList.Text = rr.Name; addList.Value = rr.FromRoute.name + "~" + rr.ToRoute.name; lstReRoute.Items.Add(addList); } foreach (MasterEdit.route r in allRoute) { ListItem addList = new ListItem(); addList.Text = r.name; addList.Value = r.getDirection(); lstRoute.Items.Add(addList); } } } catch { } ddlListType.SelectedIndex = 0; lblNotification.Text = ""; if (!IsPostBack) { drawMarker("initialLatLng(" + initLat + "," + initLng + ");"); } findOldFile(System.AppDomain.CurrentDomain.BaseDirectory.ToString() + "FileSave\\"); } else Response.Redirect("login.aspx"); } string userID,id,userFilename; public string checkuser() { string[] bits; string ip = Request.UserHostAddress; TextReader fileReader; string line; HttpCookie cookie = Request.Cookies["Preferences"]; if (cookie == null || cookie["Name"]=="") Response.Redirect("login.aspx"); else id = cookie["Name"]; //string id = Request.QueryString.Get("LoginID"); fileReader = new StreamReader(System.AppDomain.CurrentDomain.BaseDirectory.ToString() + @"\Data\user.csv"); try { line = fileReader.ReadLine();//line1 line = fileReader.ReadLine(); while (line != "" || line != null) { bits = line.Split(','); if (bits[0] == id) { userID = bits[2]; lblUser.Text = bits[3]; userFilename = bits[0]; fileReader.Close(); return bits[2]; } else line = fileReader.ReadLine(); } } catch { fileReader.Close(); } return ""; } 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(); } } } protected void lnkbtnMap_Click(object sender, EventArgs e) { //ddlListType.SelectedIndex = 0; drawMarker("initialLatLng('','');"); } public void drawMarker(string script) { string javascriptCode = "resetMarker();DrawMap();"; double lat, lng; int type = 0; foreach (MasterEdit.location location in allLocation) { lat = location.latitude; lng = location.longitude; if (location.type == "Time Point") type = 0; else if (location.type == "Bus Stop") type = 1; else if (location.type == "Interchange") type = 2; javascriptCode += "setLatLong(" + lat + "," + lng + ");dragMarker(" + type + ", " + (type + 1) + "," + lat + " , " + lng + ");"; } Page.RegisterStartupScript("myscript", ""); } public void addLocation(string name, string description, double latitude, double longitude, string type) { MasterEdit.location addLocation = new MasterEdit.location(); addLocation.name = name; addLocation.latitude = latitude; addLocation.longitude = longitude; addLocation.description = description; addLocation.type = type; allLocation.Add(addLocation); } protected void btnDeleteLocation_Click(object sender, EventArgs e) { if (lstRoute.SelectedIndex >= 0) { string script = "The related reroute is deleted:"; List arrayReroute = new List(); string routeName = lstRoute.SelectedItem.Text; lstRoute.Items.RemoveAt(lstRoute.SelectedIndex); foreach (MasterEdit.route r in allRoute) { if (r.name == routeName) { foreach (MasterEdit.reroute rr in allreRoute) { if (rr.FromRoute.name == routeName || rr.ToRoute.name == routeName) { script += "
"+rr.Name; arrayReroute.Add(rr); } } for (int i = 0; i < arrayReroute.Count;i++) { allreRoute.Remove(arrayReroute[i]); for (int j = 0; j < lstReRoute.Items.Count; j++) { if (lstReRoute.Items[j].Text == arrayReroute[i].Name) { lstReRoute.Items.RemoveAt(j); break; } } } FileStream stream = new FileStream(System.AppDomain.CurrentDomain.BaseDirectory.ToString() + @"\Data\reroute-" + userFilename + ".csv", FileMode.Create); stream.Close(); foreach (MasterEdit.reroute rr in allreRoute) { writeFile(rr.FromRoute, rr.ToRoute); } allRoute.Remove(r); break; } } FileStream stream1 = new FileStream(System.AppDomain.CurrentDomain.BaseDirectory.ToString() + @"\Data\route-"+userFilename+".csv", FileMode.Create); stream1.Close(); foreach (MasterEdit.route r in allRoute) { if (r.name != routeName) { writeFile(r); } } lblNotification.Text = "Route: " + routeName + " is removed.
"+script; } if (lstReRoute.SelectedIndex >= 0) { string rerouteName = lstReRoute.SelectedItem.Text; lstReRoute.Items.RemoveAt(lstReRoute.SelectedIndex); foreach (MasterEdit.reroute rr in allreRoute) { if (rr.Name == rerouteName) { allreRoute.Remove(rr); break; } } FileStream stream = new FileStream(System.AppDomain.CurrentDomain.BaseDirectory.ToString() + @"\Data\reroute-" + userFilename + ".csv", FileMode.Create); stream.Close(); foreach (MasterEdit.reroute rr in allreRoute) { if (rr.Name != rerouteName) { writeFile(rr.FromRoute, rr.ToRoute); } } lblNotification.Text = "Reroute: " + rerouteName + "is removed."; } drawMarker("initialLatLng('','');"); } protected void btnEditLocation_Click(object sender, EventArgs e) { if (lstRoute.SelectedIndex >= 0) { string routeName = lstRoute.SelectedItem.Text; int count = 0; string script = ""; string script3 = "
NoLocation NameArrival TimeWait Time
"; string script1 = "", script2 = ""; string javascript = ""; string txtWait, txtArrival, hdnLocation; int type = 0, index = 0; lstRouteList.Items.Clear(); foreach (MasterEdit.route r in allRoute) { if (r.name == routeName) { txtNameEdit.Value = r.name; txtFirstRepeatEdit.Value = r.firstRepeat; txtLastRepeatEdit.Value = r.lastRepeat; count = r.arrivalTime.Count; hdnTotalLocation.Value = Convert.ToString(count); for (int i = 0; i < count; i++) { txtWait = "wait3" + (i + 1); txtArrival = "arrival3" + (i + 1); hdnLocation = "hdnLocation3" + (i + 1); script1 += ""; script1 += ""; script1 += ""; script1 += ""; javascript += "setUpEditLocation(" + r.waitTime[i] + "," + r.arrivalTime[i] + "," + (i + 1) + ",'normal'," + r.repeatTime + ",3);"; ListItem addList = new ListItem(); addList.Text = r.cLocation[i].name; //find index for which it lies on the table if (r.cLocation[i].type == "Time Point") { type = 0; for (int j = 0; j < lstTimePoint.Items.Count; j++) { if (lstTimePoint.Items[j].Text == r.cLocation[i].name) { index = j; break; } } } if (r.cLocation[i].type == "Bus Stop") { type = 1; for (int j = 0; j < lstBusStop.Items.Count; j++) { if (lstBusStop.Items[j].Text == r.cLocation[i].name) { index = j; break; } } } if (r.cLocation[i].type == "Interchange") { type = 2; for (int j = 0; j < lstInterchange.Items.Count; j++) { if (lstInterchange.Items[j].Text == r.cLocation[i].name) { index = j; break; } } } addList.Value = index + "~" + type + "~" + r.waitTime[i] + "~" + r.arrivalTime[i]; txtWait = "wait4" + (i + 1); txtArrival = "arrival4" + (i + 1); hdnLocation = "hdnLocation4" + (i + 1); script2 += ""; script2 += ""; script2 += ""; script2 += ""; chkPeakEdit.Checked = false; txtRepeatTimePeakEdit.Value = ""; txtFirstRepeatPeakEdit.Value =""; txtLastRepeatPeakEdit.Value = ""; if (r.arrivalTimePeak.Count > 0) { chkPeakEdit.Checked = true; txtFirstRepeatPeakEdit.Value = r.firstRepeatPeak; txtLastRepeatPeakEdit.Value = r.lastRepeatPeak; javascript += "setUpEditLocation(" + r.waitTimePeak[i] + "," + r.arrivalTimePeak[i] + "," + (i + 1) + ",'peak'," + r.repeatTimePeak + ",4);"; addList.Value += "~" + r.waitTimePeak[i] + "~" + r.arrivalTimePeak[i]; } lstRouteList.Items.Add(addList); } lblTable.Text = script + script1 + "
NoLocation NameArrival TimeWait Time
" + (i + 1) + "" + r.cLocation[i].name + "
" + (i + 1) + "" + r.cLocation[i].name + "
"; lblTablePeak.Text = script3 + script2 + ""; hdnTotalLocationEdit.Value = Convert.ToString(count); hdnTempLocationName.Value = r.name; hdnTempIndex.Value = Convert.ToString(lstRoute.SelectedIndex); } } drawMarker("editLocation_Click();" + javascript); } else if (lstReRoute.SelectedIndex >= 0) { string rerouteName = lstReRoute.SelectedItem.Text; foreach (MasterEdit.reroute rr in allreRoute) { if (rr.Name == rerouteName) { hdnFrom.Value = rr.FromRoute.name; hdnTo.Value = rr.ToRoute.name; drawMarker("editReRoute_Click();"); break; } } } else drawMarker("initialLatLng('','');"); } public double convertTime(string text) { double time = 0; string[] splitTime = text.ToLower().Trim().Split('h'); if (splitTime.Length > 1) { time += Convert.ToInt16(splitTime[0])*60; text = splitTime[1]; } splitTime = text.ToLower().Trim().Split('m'); if (splitTime.Length > 1) { time += Convert.ToInt16(splitTime[0]); text = splitTime[1]; } splitTime = text.ToLower().Trim().Split('s'); if (splitTime.Length > 1) { time += Convert.ToDouble(splitTime[0])/100; } return time; } protected void btnConfirm_Click(object sender, EventArgs e) { int count = Convert.ToInt16(hdnTotalLocation.Value); string routeName = Request.Form["txtName"].Trim(); string txtError = checkError(routeName); if (txtError == "") { MasterEdit.route newRoute = new MasterEdit.route(); bool checkPeak = chkPeakTime.Checked; if (checkPeak == true) { newRoute.firstRepeatPeak = Request.Form["txtFirstRepeatPeak"]; newRoute.lastRepeatPeak = Request.Form["txtLastRepeatPeak"]; newRoute.repeatTimePeak = convertTime(Request.Form["txtRepeatTimePeak"]); } newRoute.name = routeName; newRoute.firstRepeat = Request.Form["txtFirstRepeat"]; newRoute.lastRepeat = Request.Form["txtLastRepeat"]; newRoute.repeatTime = convertTime(Request.Form["txtRepeatTime"]); string locationName; double waitTime, arrivalTime; for (int i = 1; i <= count; i++) { locationName = Request.Form["hdnLocation0" + i]; waitTime = convertTime(Request.Form["wait0" + i]); arrivalTime =convertTime(Request.Form["arrival0" + i]); foreach (MasterEdit.location location in allLocation) { if (location.name == locationName) { newRoute.addLocation(location); break; } } newRoute.addWaitTime(waitTime, 0); newRoute.addArrivalTime(arrivalTime, 0); if (checkPeak == true) { waitTime = convertTime(Request.Form["wait1" + i]); arrivalTime = convertTime(Request.Form["arrival1" + i]); newRoute.addWaitTime(waitTime, 1); newRoute.addArrivalTime(arrivalTime, 1); } } ListItem addList = new ListItem(); addList.Text = routeName; addList.Value = newRoute.getDirection(); lstRoute.Items.Add(addList); allRoute.Add(newRoute); lstRouteList.Items.Clear(); writeFile(newRoute); } if (txtError != "") lblNotification.Text = "Failed to Add:
" + txtError; else lblNotification.Text = "Route: " + routeName + " added to the list(s).
"; drawMarker("initialLatLng('','');"); } public string checkError(string routeName) { foreach (MasterEdit.route r in allRoute) { if (r.name == routeName) return "Duplicated name: " + routeName; } return ""; } public string checkErrorReroute(string rr1, string rr2) { foreach (MasterEdit.reroute rr in allreRoute) { if (rr.FromRoute.name == rr1 && rr.ToRoute.name == rr2) return "Duplicated."; if (rr.FromRoute.name == rr2 && rr.ToRoute.name == rr1) return "Duplicated."; } return ""; } protected void btnConfirmEdit_Click(object sender, EventArgs e) { int count = Convert.ToInt16(hdnTotalLocationEdit.Value); string txtError = ""; string routeName = Request.Form["txtNameEdit"].Trim(); MasterEdit.route temp = new MasterEdit.route(); foreach (MasterEdit.route r in allRoute) { if (r.name == hdnTempLocationName.Value) { //remove the previous 1 temp = r; break; } } allRoute.Remove(temp); //add back txtError = checkError(routeName); if (txtError == "") { MasterEdit.route newRoute = new MasterEdit.route(); bool checkPeak = chkPeakEdit.Checked; if (checkPeak == true) { newRoute.firstRepeatPeak = Request.Form["txtFirstRepeatPeakEdit"]; newRoute.lastRepeatPeak = Request.Form["txtLastRepeatPeakEdit"]; newRoute.repeatTimePeak = convertTime(Request.Form["txtRepeatTimePeakEdit"]); } newRoute.name = routeName; newRoute.firstRepeat = Request.Form["txtFirstRepeatEdit"]; newRoute.lastRepeat = Request.Form["txtLastRepeatEdit"]; newRoute.repeatTime = convertTime(Request.Form["txtRepeatTimeEdit"]); string locationName; double waitTime, arrivalTime; for (int i = 1; i <= count; i++) { locationName = Request.Form["hdnLocation3" + i]; waitTime = convertTime(Request.Form["wait3" + i]); arrivalTime = convertTime(Request.Form["arrival3" + i]); foreach (MasterEdit.location location in allLocation) { if (location.name == locationName) newRoute.addLocation(location); } newRoute.addWaitTime(waitTime, 0); newRoute.addArrivalTime(arrivalTime, 0); if (checkPeak == true) { waitTime = convertTime(Request.Form["wait4" + i]); arrivalTime = convertTime(Request.Form["arrival4" + i]); newRoute.addWaitTime(waitTime, 1); newRoute.addArrivalTime(arrivalTime, 1); } } lstRoute.Items[Convert.ToInt16(hdnTempIndex.Value)].Text = routeName; lstRoute.Items[Convert.ToInt16(hdnTempIndex.Value)].Value = newRoute.getDirection(); ; allRoute.Add(newRoute); lstRouteList.Items.Clear(); FileStream stream = new FileStream(System.AppDomain.CurrentDomain.BaseDirectory.ToString() + @"\Data\route-" + userFilename + ".csv", FileMode.Create); stream.Close(); foreach (MasterEdit.route r1 in allRoute) { writeFile(r1); } } else { allRoute.Add(temp); ListItem addList = new ListItem(); addList.Text = temp.name; addList.Value = temp.getDirection(); lstRoute.Items.Add(addList); } if (txtError != "") lblNotification.Text = "Failed to Edit:
" + txtError; else lblNotification.Text = "route: " + routeName + " edited in the list(s).
"; drawMarker("initialLatLng('','');"); } public void writeFile(MasterEdit.route route) { string script = ""; string path = System.AppDomain.CurrentDomain.BaseDirectory.ToString() + @"\Data\route-" + userFilename + ".csv"; StreamWriter writeLocation = new StreamWriter(path, true); writeLocation.WriteLine(route.name); for (int i = 0; i < route.cLocation.Count; i++) { if (i == 0) script = route.cLocation[i].name + "," + route.waitTime[i] + "," + route.arrivalTime[i] + "," + route.firstRepeat + "," + route.lastRepeat + "," + route.repeatTime + ","; else script = route.cLocation[i].name + "," + route.waitTime[i] + "," + route.arrivalTime[i] + ",,,,"; if (route.waitTimePeak.Count > 0) { if (i == 0) script += route.waitTimePeak[i] + "," + route.arrivalTimePeak[i] + "," + route.firstRepeatPeak + "," + route.lastRepeatPeak + "," + route.repeatTimePeak; else script += route.waitTimePeak[i] + "," + route.arrivalTimePeak[i] + ","; } writeLocation.WriteLine(script); } writeLocation.WriteLine(); writeLocation.Close(); } public void writeFile(MasterEdit.route route, MasterEdit.route route2) { string path = System.AppDomain.CurrentDomain.BaseDirectory.ToString() + @"\Data\reroute-" + userFilename + ".csv"; StreamWriter writeLocation = new StreamWriter(path, true); writeLocation.WriteLine(route.name + "," + route2.name); writeLocation.Close(); } protected void btnCreate_Click(object sender, EventArgs e) { if (txtFileName.Text == "") { Response.Write(""); } else { if (lstRoute.Items.Count != 0) { string fileName = txtFileName.Text + ".csv"; string path = System.AppDomain.CurrentDomain.BaseDirectory.ToString() + "FileSave"; string pathName = @Path.Combine(path, fileName); //delete old file if duplicated DirectoryInfo directory = new DirectoryInfo(path); foreach (FileInfo file in directory.GetFiles()) { if (file.Name == fileName) { file.Delete(); } } createFile(fileName, pathName); string linkTo = Request.ApplicationPath + @"/FileSave/" + fileName; //save the file. Response.Redirect(linkTo); } else { Response.Write(""); } } drawMarker("initialLatLng('','');"); } public void createFile(string name, string path) { try { StreamWriter newFile = new StreamWriter(path, true); newFile.WriteLine("Transportation System"); newFile.WriteLine("Los Alamos Atomic City Transit"); newFile.WriteLine(); newFile.WriteLine("Name,Address,Type,Latitude,Longitude"); foreach (MasterEdit.location location in allLocation) { newFile.WriteLine(location.name + "," + location.description + "," + location.type + "," + location.latitude + "," + location.longitude); } newFile.WriteLine(); int count; string script = ""; foreach (MasterEdit.route route in allRoute) { newFile.WriteLine(route.name); if (route.arrivalTimePeak.Count > 0) newFile.WriteLine("Location,Arrival Time,Wait Time,Repeat Time,First Repeat,Last Repeat,Arrival Time,Wait Time,Repeat Time,First Repeat,Last Repeat"); else newFile.WriteLine("Location,Arrival Time,Wait Time,Repeat Time,First Repeat,Last Repeat"); count = route.cLocation.Count; for (int i = 0; i < count; i++) { if (i == 0) { script = route.cLocation[i].name + "," + route.arrivalTime[i] + "," + route.waitTime[i] + "," + route.repeatTime + "," + route.firstRepeat + "," + route.lastRepeat + ","; if (route.arrivalTimePeak.Count > 0) script += route.arrivalTimePeak[i] + "," + route.waitTimePeak[i] + "," + route.repeatTimePeak + "," + route.firstRepeatPeak + "," + route.lastRepeatPeak + ","; } else { script = route.cLocation[i].name + "," + route.arrivalTime[i] + "," + route.waitTime[i] + ",,,,"; if (route.arrivalTimePeak.Count > 0) script += route.arrivalTimePeak[i] + "," + route.waitTimePeak[i] + ",,,,"; } newFile.WriteLine(script); } newFile.WriteLine(); } //add the reroute if (allreRoute.Count > 0) { newFile.WriteLine("Bus Reroute"); foreach (MasterEdit.reroute rr in allreRoute) { newFile.WriteLine(rr.FromRoute.name + "," + rr.ToRoute.name); } } newFile.Close(); } catch { Response.Write(""); } } 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\\"; string error = loadData(fileName, path); //deleteFile(fileName, path); //ddlListType.SelectedIndex = 0; lblNotification.Text = "Filename: " + fileName + " is loaded.
"; if (error != "") lblNotification.Text += error; } else { Response.Write(""); } } else { Response.Write(""); } } catch (Exception ex) { Response.Write(""); } drawMarker("initialLatLng('','');"); } public string loadData(string filename, string path) { string check, txtError = ""; try { TextReader fileReader = new StreamReader(path + filename); //clear all in list if (chkMerge.Checked == false) { lstBusStop.Items.Clear(); lstTimePoint.Items.Clear(); lstInterchange.Items.Clear(); lstRoute.Items.Clear(); lstReRoute.Items.Clear(); allLocation.Clear(); allRoute.Clear(); //empty the .csv file FileStream stream = new FileStream(System.AppDomain.CurrentDomain.BaseDirectory.ToString() + @"\Data\location-" + userFilename + ".csv", FileMode.Create); stream.Close(); stream = new FileStream(System.AppDomain.CurrentDomain.BaseDirectory.ToString() + @"\Data\route-" + userFilename + ".csv", FileMode.Create); stream.Close(); } string path2 = System.AppDomain.CurrentDomain.BaseDirectory.ToString() + @"\Data\location-" + userFilename + ".csv"; StreamWriter writeLocation = new StreamWriter(path2, true); string title = fileReader.ReadLine(); string line = fileReader.ReadLine(); string[] bits = title.Split(','); 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(','); while (bits[0] != "") { check = checkLocationData(bits[0], Convert.ToDouble(bits[3]), Convert.ToDouble(bits[4])); if (check == "") { ListItem addList = new ListItem(); addList.Text = bits[0]; addList.Value = bits[1] + "~" + bits[3] + "~" + bits[4]; if (bits[2].ToLower() == "time point") { addLocation(bits[0], bits[1], Convert.ToDouble(bits[3]), Convert.ToDouble(bits[4]), bits[2]); lstTimePoint.Items.Add(addList); } else if (bits[2].ToLower() == "bus stop") { addLocation(bits[0], bits[1], Convert.ToDouble(bits[3]), Convert.ToDouble(bits[4]), bits[2]); lstBusStop.Items.Add(addList); } else if (bits[2].ToLower() == "interchange") { addLocation(bits[0], bits[1], Convert.ToDouble(bits[3]), Convert.ToDouble(bits[4]), bits[2]); lstInterchange.Items.Add(addList); } writeLocation.WriteLine(bits[0] + "," + bits[1] + "," + bits[2] + "," + Convert.ToDouble(bits[3]) + "," + Convert.ToDouble(bits[4])); } else txtError += check + "
"; //new line line2 = fileReader.ReadLine(); if (line2 != null) { bits = line2.Split(','); } else { bits = "".Split(','); } } try { string routeName; line = fileReader.ReadLine(); bits = line.Split(','); string check2 = ""; string[] info2; while (bits[0] != "") { routeName = bits[0].Trim(); check2 = checkError(routeName); if (check2 == "") { line = fileReader.ReadLine(); info2 = line.Split(','); if (info2[0] == "Location" && info2[1] == "Arrival Time" && info2[2] == "Wait Time") { line = fileReader.ReadLine();//line1 bits = line.Split(','); MasterEdit.route r = new MasterEdit.route(); r.name = routeName; while (bits[0] != "") { foreach (MasterEdit.location l in allLocation) { if (l.name == bits[0]) { r.cLocation.Add(l); r.addWaitTime(Convert.ToInt16(bits[2]), 0); r.addArrivalTime(Convert.ToInt16(bits[1]), 0); r.firstRepeat = bits[3]; r.lastRepeat = bits[4]; r.repeatTime = Convert.ToInt16(bits[5]); if (bits.Length > 6) { if (bits[6] != "") { r.addWaitTime(Convert.ToInt16(bits[7]), 1); r.addArrivalTime(Convert.ToInt16(bits[6]), 1); r.firstRepeatPeak = bits[8]; r.lastRepeatPeak = bits[9]; r.repeatTimePeak = Convert.ToInt16(bits[10]); } } } } line2 = fileReader.ReadLine(); if (line2 != null) { bits = line2.Split(','); } else { bits = "".Split(','); } } //add to list ListItem addList = new ListItem(); addList.Text = r.name; addList.Value = r.getDirection(); lstRoute.Items.Add(addList); allRoute.Add(r); } } line2 = fileReader.ReadLine(); if (line2 != null) { bits = line2.Split(','); } else { bits = "".Split(','); } } } catch { Response.Write(""); txtError = "Invalid data format."; } foreach (MasterEdit.route r in allRoute) { writeFile(r); } try { //reroute } catch { Response.Write(""); txtError = "Invalid data format."; } //check whether got route list writeLocation.Close(); fileReader.Close(); } else { writeLocation.Close(); fileReader.Close(); txtError = "Invalid data format."; try { deleteFile(filename, path); } catch { } Response.Write(""); } } catch { try { deleteFile(filename, path); } catch { } Response.Write(""); txtError = "Invalid data format."; } deleteFile(filename, path); return txtError; } public string checkLocationData(string locationName, double latitude, double longitude) { foreach (MasterEdit.location location in allLocation) { if (location.name == locationName) { return "Duplicate Location name: " + locationName; } if (location.latitude == latitude && location.longitude == longitude) { return "Duplicate Coordinate at Location name: " + locationName; } } return ""; } 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); string linkTo = "maplocation.aspx";//?loginID=" + userID; Response.Redirect(linkTo); } protected void btnAddReroute_Click(object sender, EventArgs e) { string txtError = ""; foreach (MasterEdit.route r1 in allRoute) { if (r1.name == hdnFrom.Value) { foreach (MasterEdit.route r2 in allRoute) { if (r2.name == hdnTo.Value) { txtError = checkErrorReroute(r1.name, r2.name); if (txtError == "") { ListItem addList = new ListItem(); addList.Text = r1.name + "<->" + r2.name; addList.Value = r1.name + "~" + r2.name; lstReRoute.Items.Add(addList); MasterEdit.reroute newreRoute = new MasterEdit.reroute(); newreRoute.FromRoute = r1; newreRoute.ToRoute = r2; newreRoute.Name = r1.name + "<->" + r2.name; allreRoute.Add(newreRoute); writeFile(r1, r2); lblNotification.Text = "reRoute: " + r1.name + "<->" + r2.name + " added to the list(s).
"; } else lblNotification.Text = "Failed to Add:
Reason: " + txtError; } } } } drawMarker("initialLatLng('','');"); } protected void reroute_Click(object sender, EventArgs e) { rerouteListAdd(); drawMarker("linkAddNewRR();"); } public void rerouteListAdd() { lstTo.Items.Clear(); lstFrom.Items.Clear(); foreach (MasterEdit.route r in allRoute) { ListItem addList = new ListItem(); addList.Text = r.name; lstTo.Items.Add(addList); lstFrom.Items.Add(addList); } } protected void lnkLogout_Click(object sender, EventArgs e) { userID = ""; string path = System.AppDomain.CurrentDomain.BaseDirectory.ToString() + @"\Data\log.csv"; System.IO.StreamWriter writeLog = new System.IO.StreamWriter(path, true); try { DateTime time = DateTime.Now; writeLog.WriteLine(id + "," + time.Date + "," + time.TimeOfDay + ",logout"); } catch { } writeLog.Close(); HttpCookie cookie = Request.Cookies["Preferences"]; cookie.Expires = DateTime.Now.AddDays(-1d); Response.Cookies.Add(cookie); Response.Redirect("login.aspx"); } }