public class ICS : System.Web.UI.Page
{
string description;
string summary;
string location;
string shortday;
string day;
string endtimestr;
string starttimestr;
string weekType;
public ICS()
{ }
public void GenerateICS(String userId, String weekT, String sem, String yr, String reminder)
{
SQL dbAdapter1 = new SQL();
DataTable resultTable = new DataTable();
//check if ALL week is selected
if (weekT == ConfigurationSettings.AppSettings["A"])
{
//need filter semester,year only
resultTable = dbAdapter1.GenerateICSAll(userId, sem, yr);
}
else
{
//need filter week_type,semester,year as well
resultTable = dbAdapter1.GenerateICS(userId, weekT, sem, yr);
}
//semester start date should pull from db
SQL dbAdapterSDate = new SQL();
DataTable StartDate = dbAdapterSDate.SDate(sem, yr);
DateTime semesterDate = Convert.ToDateTime(StartDate.Rows[0][0].ToString());
if (resultTable.Rows.Count >= 0)// there are rows returned that matches this user id
{
foreach (DataRow row in resultTable.Rows)
{
{
StringBuilder sb = new StringBuilder();
//start calendar, END:VCALENDAR TO BE PUT AT THE END.
sb.Append("BEGIN:VCALENDAR");
sb.Append("\r\n");
sb.Append("PRODID:-//Microsoft Corporation//Outlook 12.0 MIMEDIR//EN");
sb.Append("\r\n");
sb.Append("VERSION:2.0");
sb.Append("\r\n");
sb.Append("METHOD:PUBLISH");
sb.Append("\r\n");
sb.Append("X-CALSTART:20100804T080000Z");
sb.Append("\r\n");
sb.Append("X-CALEND:20101215T003000Z");
sb.Append("\r\n");
sb.Append("X-WR-RELCALID:{00000018-052E-029C-0FA8-3CF5CAE164FF}");
sb.Append("\r\n");
sb.Append("X-WR-CALNAME:My Calendar");
sb.Append("\r\n");
sb.Append("X-MS-OLK-WKHRSTART;TZID=\"Malay Peninsula Standard Time\":080000");
sb.Append("\r\n");
sb.Append("X-MS-OLK-WKHREND;TZID=\"Malay Peninsula Standard Time\":170000");
sb.Append("\r\n");
sb.Append("X-MS-OLK-WKHRDAYS:MO,TU,WE,TH,FR");
sb.Append("\r\n");
sb.Append("BEGIN:VTIMEZONE");
sb.Append("\r\n");
sb.Append("TZID:Malay Peninsula Standard Time");
sb.Append("\r\n");
sb.Append("BEGIN:STANDARD");
sb.Append("\r\n");
sb.Append("DTSTART:16011104T020000");
sb.Append("\r\n");
sb.Append("RRULE:FREQ=YEARLY;BYDAY=1SU;BYMONTH=11");
sb.Append("\r\n");
sb.Append("TZOFFSETFROM:+0800");
sb.Append("\r\n");
sb.Append("TZOFFSETTO:+0800");
sb.Append("\r\n");
sb.Append("END:STANDARD");
sb.Append("\r\n");
sb.Append("END:VTIMEZONE");
sb.Append("\r\n");
//fill event if have lessons
for (int i = 0; i < resultTable.Rows.Count; i++)
{
//string description = resultTable.Rows[i][1].ToString(); //FAC_CODE - CL21
string location = resultTable.Rows[i][0].ToString() + ConfigurationSettings.AppSettings["COMMA"] + ConfigurationSettings.AppSettings["SPACE"] + resultTable.Rows[i][1].ToString(); // ACT_TYPE - Lab 5
string summary = resultTable.Rows[i][2].ToString(); // Module_Code - IT35771
string weekType = resultTable.Rows[i][6].ToString();
shortday = resultTable.Rows[i][3].ToString().Substring(0,2);
day = resultTable.Rows[i][3].ToString();
starttimestr = resultTable.Rows[i][4].ToString();
endtimestr = resultTable.Rows[i][5].ToString();
Util util = new Util();
string datestr = util.getTodayDate().ToString();
Util util2 = new Util();
string tempstr = util2.getDateStr(semesterDate, day, starttimestr).ToString();
Util util3 = new Util();
string tempstr2 = util3.getDateStr2(semesterDate, day, endtimestr).ToString();
Util util4 = new Util();
string tempstrE = util2.getDateStr(semesterDate.AddDays(int.Parse(ConfigurationSettings.AppSettings["DAYS_PER_WEEK"])), day, starttimestr).ToString();
Util util5 = new Util();
string tempstr2E = util3.getDateStr2(semesterDate.AddDays(int.Parse(ConfigurationSettings.AppSettings["DAYS_PER_WEEK"])), day, endtimestr).ToString();
sb.Append("BEGIN:VEVENT");
sb.Append("\r\n");
sb.Append("CLASS:PUBLIC");
sb.Append("\r\n");
sb.Append("CREATED:" + datestr);
sb.Append("\r\n");
sb.Append("DESCRIPTION:" + description + "\\n");
sb.Append("\r\n");
sb.Append("DTSTAMP:20100721T220121Z");
sb.Append("\r\n");
//generate event/lessons for even week
if (resultTable.Rows[i][6].ToString() == ConfigurationSettings.AppSettings["EVENWEEK"])
{
int repeat = int.Parse(ConfigurationSettings.AppSettings["STUDYWEEKS_PER_SEM"]) / 2;
//nid to edit dtart/dtend +7 days to start
sb.Append("DTSTART;TZID=\"Malay Peninsula Standard Time\":" + tempstrE);
sb.Append("\r\n");
sb.Append("DTEND;TZID=\"Malay Peninsula Standard Time\":" + tempstr2E);
sb.Append("\r\n");
sb.Append("LAST-MODIFIED:" + datestr);
sb.Append("\r\n");
sb.Append("LOCATION:" + location);
sb.Append("\r\n");
sb.Append("PRIORITY:5");
sb.Append("\r\n");
sb.Append("RRULE:FREQ=WEEKLY;COUNT=" + repeat + ";INTERVAL=2;BYDAY=" + shortday);
sb.Append("\r\n");
}
//generate event/lessons for odd week
else if (resultTable.Rows[i][6].ToString() == ConfigurationSettings.AppSettings["ODDWEEK"])
{
//count amount of repeats of timeslot
int repeat = 0;
if (int.Parse(ConfigurationSettings.AppSettings["STUDYWEEKS_PER_SEM"]) % 2 == 1)
{
repeat = (int.Parse(ConfigurationSettings.AppSettings["STUDYWEEKS_PER_SEM"]) / 2) + 1;
}
else
{
repeat = int.Parse(ConfigurationSettings.AppSettings["STUDYWEEKS_PER_SEM"]) / 2;
}
sb.Append("DTSTART;TZID=\"Malay Peninsula Standard Time\":" + tempstr);
sb.Append("\r\n");
sb.Append("DTEND;TZID=\"Malay Peninsula Standard Time\":" + tempstr2);
sb.Append("\r\n");
sb.Append("LAST-MODIFIED:" + datestr);
sb.Append("\r\n");
sb.Append("LOCATION:" + location);
sb.Append("\r\n");
sb.Append("PRIORITY:5");
sb.Append("\r\n");
sb.Append("RRULE:FREQ=WEEKLY;COUNT=" + repeat + ";INTERVAL=2;BYDAY=" + shortday);
sb.Append("\r\n");
}
// generate events/lessons for every
else
{
sb.Append("DTSTART;TZID=\"Malay Peninsula Standard Time\":" + tempstr);
sb.Append("\r\n");
sb.Append("DTEND;TZID=\"Malay Peninsula Standard Time\":" + tempstr2);
sb.Append("\r\n");
sb.Append("LAST-MODIFIED:" + datestr);
sb.Append("\r\n");
sb.Append("LOCATION:" + location);
sb.Append("\r\n");
sb.Append("PRIORITY:5");
sb.Append("\r\n");
sb.Append("RRULE:FREQ=WEEKLY;COUNT=" + int.Parse(ConfigurationSettings.AppSettings["STUDYWEEKS_PER_SEM"]) + ";BYDAY=" + shortday);
sb.Append("\r\n");
}
sb.Append("SEQUENCE:0");
sb.Append("\r\n");
sb.Append("SUMMARY;LANGUAGE=en-us:" + summary);
sb.Append("\r\n");
sb.Append("TRANSP:TRANSPARENT");
sb.Append("\r\n");
sb.Append("UID:" + datestr + i + "@infosys.com");
sb.Append("\r\n");
sb.Append("X-ALT-DESC;FMTTYPE=text/html:");
sb.Append("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 3.2//EN\">");
sb.Append("\\n");
sb.Append("<HTML>");
sb.Append("\\n");
sb.Append("<HEAD>");
sb.Append("\\n");
sb.Append("<META NAME=\"Generator\" CONTENT=\"MS Exchange Server version 08.00.0681.000\">");
sb.Append("\\n");
sb.Append("<TITLE></TITLE>");
sb.Append("\\n");
sb.Append("</HEAD>");
sb.Append("\\n");
sb.Append("<BODY>");
sb.Append("\\n");
sb.Append("<!-- Converted from text/rtf format -->");
sb.Append("\\n");
sb.Append("\\n");
sb.Append("<P DIR=LTR><SPAN LANG=\"en-us\"><FONT FACE=\"Calibri\">" + description + "</FONT></SPAN></P>");
sb.Append("\\n");
sb.Append("\\n");
sb.Append("</BODY>");
sb.Append("\\n");
sb.Append("</BODY");
sb.Append("\\n");
sb.Append("\r\n");
//set busy status
sb.Append("X-MICROSOFT-CDO-BUSYSTATUS:" + ConfigurationSettings.AppSettings["BUSYSTATUS"]);
sb.Append("\r\n");
sb.Append("X-MICROSOFT-CDO-IMPORTANCE:1");
sb.Append("\r\n");
sb.Append("X-MICROSOFT-DISALLOW-COUNTER:FALSE");
sb.Append("\r\n");
sb.Append("X-MS-OLK-ALLOWEXTERNCHECK:TRUE");
sb.Append("\r\n");
sb.Append("X-MS-OLK-AUTOFILLLOCATION:FALSE");
sb.Append("\r\n");
sb.Append("X-MS-OLK-AUTOSTARTCHECK:FALSE");
sb.Append("\r\n");
sb.Append("X-MS-OLK-CONFTYPE:0");
sb.Append("\r\n");
//set reminder
if (reminder != "--None--")
{
sb.Append("BEGIN:VALARM");
sb.Append("\r\n");
//time for reminder in minutes
//sb.Append("TRIGGER:-PT"+ ConfigurationSettings.AppSettings["REMINDERTIME"] +"M");
sb.Append("TRIGGER:-PT" + reminder + "M");
sb.Append("\r\n");
sb.Append("ACTION:DISPLAY");
sb.Append("\r\n");
sb.Append("DESCRIPTION:Reminder");
sb.Append("\r\n");
sb.Append("END:VALARM");
sb.Append("\r\n");
}
sb.Append("END:VEVENT");
sb.Append("\r\n");
}
sb.Append("END:VCALENDAR");
sb.Append("\r\n");
HttpResponse response = System.Web.HttpContext.Current.Response;
response.Write(sb);
response.ContentType = "text/calendar";
response.Charset = "utf-8";
//to export as
response.AddHeader("Content-Disposition", "attachment;filename=\"MyCalendar.ics\"");
response.End();
}
}
}
}
}
|