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 maplocation : 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\\";
loadData(filename, pathname);
deleteFile(filename, pathname);
}
}
catch { }
findOldFile(System.AppDomain.CurrentDomain.BaseDirectory.ToString() + "FileSave\\");
findOldFile(System.AppDomain.CurrentDomain.BaseDirectory.ToString() + "FileDatabase\\");
findOldFile(System.AppDomain.CurrentDomain.BaseDirectory.ToString() + "FileUpload\\");
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();
}
}
}
protected void btnCreate_Click(object sender, EventArgs e)
{
if (txtFileName.Text == "")
{
Response.Write("");
}
else
{
if (hdnTimePoint.Value != "" || hdnBusStop.Value != "" || hdnInterchange.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)
{
string temp = "";
//string pathName = createUniqueFileName(txtFileName.Text);
StreamWriter newFile = new StreamWriter(path, true);
newFile.WriteLine("Map Location");
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);
}
if (hdnRoute.Value != "") {
createRoute(newFile);
}
newFile.Close();
}
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 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 timePointFile, busStopFile, interchangeFile;
string[] timePointFileArray, busStopFileArray, interchangeFileArray;
string[] info;
public void reload_page()
{
timePointFile = "" + hdnTimePoint.Value;
busStopFile = "" + hdnBusStop.Value;
interchangeFile = "" + hdnInterchange.Value;
//remove all from list box
lstBusStop.Items.Clear();
lstTimePoint.Items.Clear();
lstInterchange.Items.Clear();
//add back
string script = "DrawMap();resetMarker();";
if (timePointFile != "")
{
timePointFile = timePointFile.Replace("\r\n", "!");
timePointFileArray = timePointFile.Split('!');
for (int i = 0; i < timePointFileArray.Length - 1; i++)
{
ListItem addList = new ListItem();
info = timePointFileArray[i].Split(',');
addList.Text = info[0];
addList.Value = info[1] + "~" + info[3] + "~" + info[4];
lstTimePoint.Items.Add(addList);
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++)
{
ListItem addList = new ListItem();
info = busStopFileArray[i].Split(',');
addList.Text = info[0];
addList.Value = info[1] + "~" + info[3] + "~" + info[4];
lstBusStop.Items.Add(addList);
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++)
{
ListItem addList = new ListItem();
info = interchangeFileArray[i].Split(',');
addList.Text = info[0];
addList.Value = info[1] + "~" + info[3] + "~" + info[4];
lstInterchange.Items.Add(addList);
script += "setLatLong(" + info[3] + " , " + info[4] + ");dragMarker('interchange', 3); ";
}
}
Page.RegisterStartupScript("myscript", "");
}
public void loadData(string filename, string path)
{
try
{
TextReader fileReader = new StreamReader(path + filename);
//clear all in list
if (chkMerge.Checked == false)
{
lstBusStop.Items.Clear();
lstTimePoint.Items.Clear();
lstInterchange.Items.Clear();
hdnTimePoint.Value = "";
hdnBusStop.Value = "";
hdnInterchange.Value = "";
}
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(',');
string tempTP = "";
string tempBS = "";
string tempI = "";
while (bits[0] != "")
{
if (bits[2].ToLower() == "time point")
{
tempTP = tempTP + bits[0] + "," + bits[1] + ",time point," + bits[3] + "," + bits[4] + "\r\n";
}
else if (bits[2].ToLower() == "bus stop")
{
tempBS = tempBS + bits[0] + "," + bits[1] + ",bus stop," + bits[3] + "," + bits[4] + "\r\n";
}
else if (bits[2].ToLower() == "interchange")
{
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(',');
}
}
try
{
loadDataRoute(fileReader);
}
catch { }
//check whether got route list
fileReader.Close();
hdnTimePoint.Value += tempTP;
hdnBusStop.Value += tempBS;
hdnInterchange.Value += tempI;
//deleteFile(filename,path);
reload_page();
}
else
{
fileReader.Close();
try
{
deleteFile(filename,path);
}
catch { }
reload_page();
Response.Write("");
}
}
catch
{
try
{
deleteFile(filename,path);
}
catch { }
reload_page();
Response.Write("");
}
}
public void loadDataRoute(TextReader fileReader)
{
string line = fileReader.ReadLine();
string[] bits = line.Split(',');
hdnRoute.Value = "";
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(',');
}
}
}
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\\";
loadData(fileName, path);
deleteFile(fileName, path);
}
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();
}
}
}
public bool findFile(string fileName)
{
//delete file
DirectoryInfo directory = new DirectoryInfo(@"C:\BusScheduler\Downloads");
foreach (FileInfo file in directory.GetFiles())
{
if (file.Name == fileName)
{
return true;
}
}
return false;
}
protected void route_Click(object sender, EventArgs e)
{
if (lstError.Items.Count > 0)
{
Response.Write("");
}
else
{
string id = Request.QueryString.Get("id");
if (hdnTimePoint.Value != "" || hdnBusStop.Value != "" || hdnInterchange.Value != "")
{
string temp = hdnTimePoint.Value.Replace("\r\n", "!");
string[] temp2 = temp.Split('!');
if (temp2.Length > 2)
{
id = createUniqueFileName();
string filename = id + "-location.csv";
string path = System.AppDomain.CurrentDomain.BaseDirectory.ToString() + "/FileDatabase/" + filename;
createFile(filename, path);
string linkTo = "MapRoute.Aspx?id=" + id;
Response.Redirect(linkTo);
}
else
{
Response.Write("");
}
}
else
{
Response.Write("");
}
reload_page();
}
}
}