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; using System.Collections.Specialized; public partial class _Default : System.Web.UI.Page { public locationList listLocation = new locationList(); protected void Page_Load(object sender, EventArgs e) { //if (hdnUser.Value == "") { } //try //{ // hdnUser.Value = Request.QueryString.Get("LoginID"); // Response.Redirect("maplocation.aspx"); //} //catch { } string id=checkuser(); string initLat = "0", initLng = "0"; if (id != "") { hdnPath.Value = System.AppDomain.CurrentDomain.BaseDirectory.ToString() + "Data\\log.csv"; //read location data TextReader fileReader = new StreamReader(System.AppDomain.CurrentDomain.BaseDirectory.ToString() + @"\Data\location-" + userFilename + ".csv"); try { string line = fileReader.ReadLine(); string[] bits; if (!IsPostBack) { //delete when done lstTimePoint.Items.Clear(); lstBusStop.Items.Clear(); lstInterchange.Items.Clear(); } 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(','); } } fileReader.Close(); } catch { //erase data FileStream stream = new FileStream(System.AppDomain.CurrentDomain.BaseDirectory.ToString() + @"\Data\location-" + userFilename + ".csv", FileMode.Create); stream.Close(); fileReader.Close(); } lblNotification.Text = ""; if (!IsPostBack) { //empty the .csv file //FileStream stream = new FileStream(System.AppDomain.CurrentDomain.BaseDirectory.ToString() + @"\Data\location.csv", FileMode.Create); //stream.Close(); hdnSetCoorIndex.Value = "Marker"; drawMarker("initialLatLng(" + initLat + "," + initLng + ");"); //Page.RegisterStartupScript("myscript", ""); } ddlListType.SelectedIndex = 0; findOldFile(System.AppDomain.CurrentDomain.BaseDirectory.ToString() + "FileSave\\"); } else Response.Redirect("login.aspx"); } string userID,id,userFilename; public string checkuser() { string[] bits; TextReader fileReader; string line; HttpCookie cookie = Request.Cookies["Preferences"]; if (cookie == null || cookie["Name"] == "") Response.Redirect("login.aspx"); else id=cookie["Name"]; // = 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 route_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 source = System.AppDomain.CurrentDomain.BaseDirectory.ToString() + @"\Data\location.csv"; //string target = System.AppDomain.CurrentDomain.BaseDirectory.ToString() + @"\DataBase\location.csv"; ////copy //System.IO.File.Copy(source, target, true); string linkTo = "maproute.aspx";//?loginID=" + userID; Response.Redirect(linkTo); } protected void btnConfirm_Click(object sender, EventArgs e) { int total = Convert.ToInt16(hdnTotal.Value), errorCount=0; string txtError = "", check; string path = System.AppDomain.CurrentDomain.BaseDirectory.ToString() + @"\Data\location-" + userFilename + ".csv"; StreamWriter writeLocation= new StreamWriter(path, true); for (int i = 1; i <= total; i++) { string name = Request.Form["txtName" + i].Trim(); if (name != "" && Request.Form["hdnLat" + i] != "") { double lat = Convert.ToDouble(Request.Form["hdnLat" + i]); double lng = Convert.ToDouble(Request.Form["hdnLng" + i]); check = checkLocationData(name, lat, lng); if (check == "") { try { string desc = Request.Form["txtDesc" + i]; string type = Request.Form["ddlType" + i]; addLocation(name, desc, lat, lng, type); writeLocation.WriteLine(name + "," + desc + "," + type + "," + lat + "," + lng); ListItem addList = new ListItem(); addList.Text = name; addList.Value = desc + "~" + lat + "~" + lng; if (type == "Time Point") lstTimePoint.Items.Add(addList); else if (type == "Bus Stop") lstBusStop.Items.Add(addList); else if (type == "Interchange") lstInterchange.Items.Add(addList); } catch { writeLocation.Close(); } } else { txtError += check + "
"; errorCount++; } } else { errorCount++; } } writeLocation.Close(); lblNotification.Text = (total - errorCount) + " location(s) is/are added to the list(s).
"; if (txtError != "") lblNotification.Text += "Failed to Add:
" + txtError; drawMarker("initialLatLng('','');"); } public string createUniqueName() { // 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; } protected void lnkbtnMap_Click(object sender, EventArgs e) { drawMarker("initialLatLng('','');"); } public void drawMarker(string script) { //find all from listbox and put in map int numTP = lstTimePoint.Items.Count; int numBS = lstBusStop.Items.Count; int numI = lstInterchange.Items.Count; string locationName, javascriptCode = "resetMarker();DrawMap();"+script+"dragMarker(4,0, '', '');"; int k = 0; for (int i = 0; i < numTP; i++) { locationName = lstTimePoint.Items[i].Text; javascriptCode += addLocationList(locationName,0); } for (int i = 0; i < numBS; i++) { locationName = lstBusStop.Items[i].Text; javascriptCode += addLocationList(locationName,1); } for (int i = 0; i < numI; i++) { locationName = lstInterchange.Items[i].Text; javascriptCode += addLocationList(locationName,2); } hdnSetCoorIndex.Value = "Marker"; 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; listLocation.aLocation.Add(addLocation); } public string addLocationList(string locationName, int type) { double lat, lng; MasterEdit.location aLocation; aLocation = listLocation.aLocationAt(locationName); if (aLocation != null) { lat = aLocation.latitude; lng = aLocation.longitude; return "dragMarker(" + type + ", " + (type+1) + "," + lat + " , " + lng + ");"; } else return ""; } protected void btnCreate_Click(object sender, EventArgs e) { if (txtFileName.Text == "") { Response.Write(""); } else { if (tpCount.Text != "0" || bsCount.Text != "0" || iCount.Text != "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); lblNotification.Text = "Filename: " + fileName + " is saved to your download directory."; } else { Response.Write(""); } } drawMarker("initialLatLng('','');"); } public void createFile(string name, string path) { string locationName; MasterEdit.location aLocation; StreamWriter newFile = new StreamWriter(path, true); newFile.WriteLine("Map Location"); newFile.WriteLine(); newFile.WriteLine(); newFile.WriteLine("Name,Address,Type,Latitude,Longitude"); for (int i = 0; i < lstTimePoint.Items.Count; i++) { locationName=lstTimePoint.Items[i].Text; aLocation = listLocation.aLocationAt(locationName); newFile.WriteLine(aLocation.name +"," + aLocation.description +","+aLocation.type+","+aLocation.latitude+","+aLocation.longitude); } for (int i = 0; i < lstBusStop.Items.Count; i++) { locationName = lstBusStop.Items[i].Text; aLocation = listLocation.aLocationAt(locationName); newFile.WriteLine(aLocation.name + "," + aLocation.description + "," + aLocation.type + "," + aLocation.latitude + "," + aLocation.longitude); } for (int i = 0; i < lstInterchange.Items.Count; i++) { locationName = lstInterchange.Items[i].Text; aLocation = listLocation.aLocationAt(locationName); newFile.WriteLine(aLocation.name + "," + aLocation.description + "," + aLocation.type + "," + aLocation.latitude + "," + aLocation.longitude); } newFile.Close(); } 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); 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(); listLocation.aLocation.Clear(); //empty the .csv file FileStream stream = new FileStream(System.AppDomain.CurrentDomain.BaseDirectory.ToString() + @"\Data\location-" + 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(); 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(','); 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 { //loadDataRoute(fileReader); } catch { } //check whether got route list writeLocation.Close(); fileReader.Close(); } else { writeLocation.Close(); fileReader.Close(); try { deleteFile(filename, path); } catch { } Response.Write(""); } } catch { try { deleteFile(filename, path); } catch { } Response.Write(""); } return txtError; } 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(); } } } public string checkLocationData(string locationName, double latitude, double longitude) { MasterEdit.location aLocation; aLocation = listLocation.aLocationAt(locationName); if (aLocation != null) { return "Duplicate Location name: "+ locationName; } aLocation = listLocation.aLocationAt(latitude, longitude); if (aLocation != null) { return "Duplicate Coordinate at Location name: " + locationName; } else return ""; } protected void btnEdit_Click(object sender, EventArgs e) { MasterEdit.location bLocation, aLocation; aLocation = listLocation.aLocationAt(hdnNameTemp.Value); listLocation.aLocation.Remove(aLocation); string tempName = aLocation.name.Trim(); string tempDesc = aLocation.description.Trim(); double tempLat = aLocation.latitude; double tempLng = aLocation.longitude; string tempType = aLocation.type; FileStream stream = new FileStream(System.AppDomain.CurrentDomain.BaseDirectory.ToString() + @"\Data\location-" + userFilename + ".csv", FileMode.Create); stream.Close(); string path = System.AppDomain.CurrentDomain.BaseDirectory.ToString() + @"\Data\location-" + userFilename + ".csv"; StreamWriter writeLocation = new StreamWriter(path, true); for (int i = 0; i < listLocation.aLocation.Count; i++) { bLocation = listLocation.aLocation[i]; writeLocation.WriteLine(bLocation.name + "," + bLocation.description + "," + bLocation.type + "," + bLocation.latitude + "," + bLocation.longitude); } bool exist = false; if (hdnTypeTemp.Value != Request.Form["ddlTypeEdit"]) { if (hdnTypeTemp.Value == "Time Point") lstTimePoint.Items.RemoveAt(Convert.ToInt16(hdnTypeNumTemp.Value)); else if (hdnTypeTemp.Value == "Bus Stop") lstBusStop.Items.RemoveAt(Convert.ToInt16(hdnTypeNumTemp.Value)); else lstInterchange.Items.RemoveAt(Convert.ToInt16(hdnTypeNumTemp.Value)); exist = true; } string txtError = "", check; string name = Request.Form["txtNameEdit"].Trim(); double lat = Convert.ToDouble(Request.Form["hdnLatEdit"]); double lng = Convert.ToDouble(Request.Form["hdnLngEdit"]); check = checkLocationData(name, lat, lng); if (check == "") { string desc = Request.Form["txtDescEdit"].Trim(); string type = Request.Form["ddlTypeEdit"]; addLocation(name, desc, lat, lng, type); writeLocation.WriteLine(name + "," + desc + "," + type + "," + lat + "," + lng); if (exist == false) { if (type == "Time Point") { lstTimePoint.Items[Convert.ToInt16(hdnTypeNumTemp.Value)].Text = name; lstTimePoint.Items[Convert.ToInt16(hdnTypeNumTemp.Value)].Value = desc + "~" + lat + "~" + lng; } else if (type == "Bus Stop") { lstBusStop.Items[Convert.ToInt16(hdnTypeNumTemp.Value)].Text = name; lstBusStop.Items[Convert.ToInt16(hdnTypeNumTemp.Value)].Value = desc + "~" + lat + "~" + lng; } else if (type == "Interchange") { lstInterchange.Items[Convert.ToInt16(hdnTypeNumTemp.Value)].Text = name; lstInterchange.Items[Convert.ToInt16(hdnTypeNumTemp.Value)].Value = desc + "~" + lat + "~" + lng; } } else { ListItem addList = new ListItem(); addList.Text = name; addList.Value = desc + "~" + lat + "~" + lng; if (type == "Time Point") lstTimePoint.Items.Add(addList); else if (type == "Bus Stop") lstBusStop.Items.Add(addList); else if (type == "Interchange") lstInterchange.Items.Add(addList); } } else { txtError= check +"
"; string desc = tempDesc; string type = tempType; addLocation(tempName, tempDesc, tempLat, tempLng, tempType); writeLocation.WriteLine(tempName + "," + tempDesc + "," + tempType + "," + tempLat + "," + tempLng); ListItem addList = new ListItem(); addList.Text = tempName; addList.Value = tempDesc + "~" + tempLat + "~" + tempLng; if (tempType == "Time Point") lstTimePoint.Items.Add(addList); else if (tempType == "Bus Stop") lstBusStop.Items.Add(addList); else if (tempType == "Interchange") lstInterchange.Items.Add(addList); } writeLocation.Close(); lblNotification.Text = "location: "+name+" is edited.
"; if (txtError != "") lblNotification.Text += "Failed to edit:
" + txtError; ddlListType.SelectedIndex = 0; drawMarker("initialLatLng('','');"); } protected void btnEditLocation_Click(object sender, EventArgs e) { int index = Convert.ToInt16(dllIndex.Value), num = 0; string locationName = ""; MasterEdit.location aLocation; if (index == 0 && lstTimePoint.SelectedIndex >= 0) { num = lstTimePoint.SelectedIndex; locationName = lstTimePoint.SelectedItem.Text; } else if (index == 1 && lstBusStop.SelectedIndex >= 0) { num = lstBusStop.SelectedIndex; locationName = lstBusStop.SelectedItem.Text; } else if (index == 2 && lstInterchange.SelectedIndex >= 0) { num = lstInterchange.SelectedIndex; locationName = lstInterchange.SelectedItem.Text; } if (locationName != "") { aLocation = listLocation.aLocationAt(locationName); txtNameEdit.Text = aLocation.name; hdnLatEdit.Value = Convert.ToString(aLocation.latitude); hdnLngEdit.Value = Convert.ToString(aLocation.longitude); txtDescEdit.Text = aLocation.description; if (aLocation.type == "Time Point") ddlTypeEdit.SelectedIndex = 0; else if (aLocation.type == "Bus Stop") ddlTypeEdit.SelectedIndex = 1; else ddlTypeEdit.SelectedIndex = 2; hdnNameTemp.Value = aLocation.name; hdnTypeTemp.Value = aLocation.type; hdnTypeNumTemp.Value = Convert.ToString(num); drawMarker("editLocation_Click();"); } else drawMarker("initialLatLng('','');"); } protected void btnDeleteLocation_Click(object sender, EventArgs e) { int index = Convert.ToInt16(dllIndex.Value); string locationName = ""; MasterEdit.location aLocation,bLocation; if (index == 0 && lstTimePoint.SelectedIndex >= 0) locationName = lstTimePoint.SelectedItem.Text; else if (index == 1 && lstBusStop.SelectedIndex >= 0) locationName = lstBusStop.SelectedItem.Text; else if (index == 2 && lstInterchange.SelectedIndex >= 0) locationName = lstInterchange.SelectedItem.Text; if (locationName != "") { aLocation = listLocation.aLocationAt(locationName); listLocation.aLocation.Remove(aLocation); FileStream stream = new FileStream(System.AppDomain.CurrentDomain.BaseDirectory.ToString() + @"\Data\location-" + userFilename + ".csv", FileMode.Create); stream.Close(); string path = System.AppDomain.CurrentDomain.BaseDirectory.ToString() + @"\Data\location-" + userFilename + ".csv"; StreamWriter writeLocation = new StreamWriter(path, true); for (int i = 0; i < listLocation.aLocation.Count; i++) { bLocation = listLocation.aLocation[i]; writeLocation.WriteLine(bLocation.name + "," + bLocation.description + "," + bLocation.type + "," + bLocation.latitude + "," + bLocation.longitude); } writeLocation.Close(); if (aLocation.type == "Time Point") { lstTimePoint.Items.RemoveAt(lstTimePoint.SelectedIndex); } else if (aLocation.type == "Bus Stop") { lstBusStop.Items.RemoveAt(lstBusStop.SelectedIndex); } else { lstInterchange.Items.RemoveAt(lstInterchange.SelectedIndex); } listLocation.aLocation.Remove(aLocation); lblNotification.Text = "Location: " + locationName +" is removed."; } ddlListType.SelectedIndex = 0; drawMarker("initialLatLng('','');"); } 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"); } }