//!!! Calandar Handel Code !!! //retrives info from database and creates a string for inserting to web page public bool RetriveDayItem(int year, int dayID, out string dayItemInfo) { Calandar.CalanderFormater CF = new Calandar.CalanderFormater(year); dayItemInfo = "<label class=DayItem> <h3>" + CF.getMonthByDay(dayID).getDaysIntoMonth(dayID) + "</h3>"; //int valueDay = 1;//deadcode: removed because passed value (dayID) can be assumed to be equivilant int valueCourse = 404;//test value int valueRoom = 404; string valueStartTime = "08:00"; string valueEndTime = "12:00"; string queryString = "SELECT CLASS_ID, ROOM_ID, START_TIME, END_TIME FROM dbo.CALANDER_LIST WHERE LIST_YEAR = " + year + " AND LIST_DAY = " + dayID + ";"; SqlCommand cmd = new SqlCommand(queryString, connection); try { connection.Open(); SqlDataReader reader = cmd.ExecuteReader(); while (reader.Read()) { valueCourse = (int)reader.GetSqlInt32(0); valueRoom = (int)reader.GetSqlInt32(1); valueStartTime = (string)reader.GetSqlString(2); valueEndTime = (string)reader.GetSqlString(3); dayItemInfo += "<hr />" + valueCourse + " : Room " + valueRoom + " <br /> " + valueStartTime + " - " + valueEndTime + " </label>"; } reader.Close(); connection.Close(); } catch (SqlException excp) { dayItemInfo += "<hr /> failed to read on database"; Debug.WriteLine("failed on read day item" + excp); return(false); } return(true); }
/* * Attempts a brute force insert of all data considered valid by target parameters. * Does not check against data inside database. * If a target parameters are not valid within given context, function will imediantly terminate and no data will be inserted into database [returns false]. * * Parmeters(implicit): * Target Database: Defined by [DataBase Connetion String] in [WebApp/Bin/DataBaseConfig.txt] * * Parmeters(defined): * year(string): target year for upload into database. used by (Calander Logic) and [Calandar/CalanderFormater.cs] * month(string): target month to upload target data. if left as (null) or (0) all months will be targeted * week(string): target day of week to upload target data. if left as (null) or (0) all weeks will be targeted * day(string): target day of target month(s) to upload target data. if left as (null) or (0) all days will be targeted * room(string): roomID of target data to upload into database * course(string): courseID of target data to upload into database * startTime(string): time of day following 12 Clock formated to fit "XX:XX" as a 5 character string, defines begining target time of day for upload into dataBase * endTime(string): time of day following 12 Clock formated to fit "XX:XX" as a 5 character string, defines ending target time of day for upload into dataBase */ public bool InsertSchedualItem(string year, string[] month, string[] week, string[] day, string room, string course, string startTime, string endTime) { //Convert Year Logic int yearParse; if (int.TryParse(year, out yearParse)) { //Pre insert Year Logic Here: } else { Debug.WriteLine("Failed to Parse <STRING_YEAR: " + year + "> too INT"); return(false); } //Convert Month Logic int[] monthParse = new int[month.Length]; for (int i = 0; i < month.Length; i++) { if (int.TryParse(month[i], out monthParse[i])) { //Pre insert Month Logic Here: } else { Debug.WriteLine("Failed to Parse <STRING_MONTH: " + month[i] + "> too INT"); return(false); } } //Convert Week Logic int[] weekParse = new int[week.Length]; for (int i = 0; i < week.Length; i++) { if (int.TryParse(week[i], out weekParse[i])) { //Pre insert Day Logic Here: } else { Debug.WriteLine("Failed to Parse <STRING_WEEK: " + week[i] + "> too INT"); return(false); } } //Convert Day Logic int[] dayParse = new int[day.Length]; for (int i = 0; i < day.Length; i++) { if (int.TryParse(day[i], out dayParse[i])) { //Pre insert Day Logic Here: } else { Debug.WriteLine("Failed to Parse <STRING_DAY: " + day[i] + "> too INT"); return(false); } } //Convert Room Logic int roomParse; if (int.TryParse(room, out roomParse)) { //Pre insert Room Logic Here: } else if (room == null)//Dan: roomID shouldn't be optional either, need to fix before push to master [MASTER] not-note: meh... { roomParse = 0; } else { Debug.WriteLine("Failed to Parse <STRING_ROOM: " + room + "> too INT"); return(false); } //Convert Course Logic int courseParse; if (int.TryParse(course, out courseParse)) { //Pre insert Course Logic Here: } else if (course == null)//Dan: hold up... the course ID shouldn't be optional. this needs fixed before we can update master [MASTER] { courseParse = 0; } else { Debug.WriteLine("Failed to Parse <STRING_COURSE: " + course + "> too INT"); return(false); } TimeItem startTimeParse; try { startTimeParse = new TimeItem(startTime); } catch (FormatException excpt) { Debug.WriteLine("Failed to Parse <STRING_START_TIME: " + startTime + "> too TIMEITEM \n" + excpt); return(false); } TimeItem endTimeParse; try { endTimeParse = new TimeItem(endTime); } catch (FormatException excpt) { Debug.WriteLine("Failed to Parse <STRING_END_TIME: " + endTime + "> too TIMEITEM \n" + excpt); return(false); } //Dan : Calandar logic Calandar.CalanderFormater CF = new Calandar.CalanderFormater(yearParse); SqlCommand cmd = new SqlCommand();//holds information for interacting with database //GregorianCalendar GC = new GregorianCalendar();//deadcode: replaced with CalanderFormater //Dan : find valid days logic for (int curDay = 1; curDay <= CF.GetDaysAmount(); curDay++)//begin for days in year { bool isValid = true; if (monthParse.Length != 0)//if month specified { for (int i = 0; i < monthParse.Length; i++) { if (CF.getMonthByDay(curDay).getMonthID() == monthParse[i]) //looks to see if current day is in specified month { isValid = true; //makes invalid if it is break; } else { isValid = false; } } }//end if month specified if (weekParse.Length != 0 && isValid)//if week specified { for (int i = 0; i < weekParse.Length; i++) { if (CF.getDayOfWeek(curDay) == weekParse[i])//looks to see if current day is specified day of week { isValid = true; break; } else { isValid = false; } } }//end if week specified if (dayParse.Length != 0 && isValid)//if day specified { for (int i = 0; i < dayParse.Length; i++) { if (CF.getMonthByDay(curDay).getDayByMonth(dayParse[i]).getDayID() == curDay)//looks to see if current day is specified days into month { isValid = true; break; } else { isValid = false; } } }//end if day specified if (isValid)//if valid day { string[] badrows; string[] colDats = { null, null, null, null, null, null, CF[curDay].getDayID().ToString(), null, null }; if (!IsConflict("SELECT * FROM dbo.CALANDER_LIST WHERE LIST_YEAR IN (" + yearParse + ") AND ROOM_ID IN (" + roomParse + ");", colDats, out badrows)) //checks for conflicting days of year { colDats = new string[] { null, null, null, null, null, null, null, startTimeParse.ToString(), endTimeParse.ToString() }; //checks for conflicting rooms in days of year string[] rowGet; for (int i = 0; i < badrows.Length; i++) { if (IsConflictSingleRow("SELECT * FROM dbo.CALANDER_LIST WHERE LIST_ID IN (" + badrows[i] + ");", colDats, out rowGet)) { TimeItem gotStart = new TimeItem(rowGet[7]); TimeItem gotend = new TimeItem(rowGet[8]); if ((gotStart <= startTimeParse) && (gotend >= startTimeParse) || (gotend >= endTimeParse) && (gotStart <= endTimeParse))//check for time conflict { isValid = false; } } else { //TODO: check for fail to read on single row : fault talerance [BETA] } } //end for each conflict } //end if Conflict if (isValid) { cmd.CommandText += "INSERT INTO dbo.CALANDER_LIST(CLASS_ID,ROOM_ID,LIST_YEAR,LIST_MONTH,LIST_WEEK,LIST_DAY,START_TIME,END_TIME) VALUES(" + courseParse + "," + roomParse + "," + yearParse + "," + CF.getMonthByDay(curDay).getMonthID() + "," + CF.getDayOfWeek(curDay) + "," + curDay + ", '" + startTimeParse.ToString() + "', '" + endTimeParse.ToString() + "');\n"; } } //end if valid day } //end for days in year try { Debug.WriteLine(cmd.CommandText); connection.Open(); //SqlCommand cmd = new SqlCommand();//moved to : find valid days logic cmd.Connection = connection; //cmd.CommandText = null;//moved to : find valid days logic cmd.ExecuteNonQuery(); connection.Close(); } catch (SqlException excp) { Debug.WriteLine("Failed to run InsertIntoDataBase: " + excp); connection.Close(); return(false); } return(true); }//end InsertSchedualItem