public static CoverRow Parse(XmlNode node) { CoverRow r = new CoverRow(); r.Period = node.ChildNodes[0].InnerText; r.Time = node.ChildNodes[1].InnerText; r.Absent = node.ChildNodes[2].InnerText; r.Class = node.ChildNodes[4].InnerText; r.Room = node.ChildNodes[5].InnerText; r.OldRoom = node.ChildNodes[6].InnerText; if (node.ChildNodes[7].InnerText.Contains('$') && !node.ChildNodes[7].InnerText.Contains(',')) { r.Cover = node.ChildNodes[7].InnerText.Split(new char[] { ' ' })[0].Remove(0, 1) + "," + node.ChildNodes[7].InnerText.Remove(0, node.ChildNodes[7].InnerText.Split(new char[] { ' ' })[0].Length); } else { r.Cover = node.ChildNodes[7].InnerText; } return(r); }
public Appointment CreateApp(CoverRow row, DateTime dt, ExchangeService service) { Dictionary <string, string[]> times = new Dictionary <string, string[]>(); foreach (string s in Properties.Settings.Default.LessonTimes.Split(new char[] { ':' })) { if (!string.IsNullOrEmpty(s)) { times.Add(s.Split(new char[] { '=' })[0], s.Split(new char[] { '=' })[1].Split(new char[] { '-' })); } } if (row.Period.Trim() == "---" && row.Time.Trim() == "---") { return(null); } if (!times.ContainsKey(row.Period.Trim().Split(new char[] { ':' })[1].Trim()) && row.Time.Trim() == "---") { return(null); } Appointment appointment = new Appointment(service); appointment.Subject = appointment.Body = row.Class; appointment.Body += "For Staff: " + row.Absent + "<br />Auto Created: " + DateTime.Now.ToString(); appointment.Subject = "Cover: " + appointment.Subject; if (row.Time == "---") { string[] time = times[row.Period.Trim().Split(new char[] { ':' })[1].Trim()]; appointment.Start = new DateTime(dt.Year, dt.Month, dt.Day, int.Parse(time[0].Split(new char[] { '.' })[0]), int.Parse(time[0].Split(new char[] { '.' })[1]), 0); appointment.End = new DateTime(dt.Year, dt.Month, dt.Day, int.Parse(time[1].Split(new char[] { '.' })[0]), int.Parse(time[1].Split(new char[] { '.' })[1]), 0); } else { appointment.Start = new DateTime(dt.Year, dt.Month, dt.Day, int.Parse(row.Time.Split(new char[] { '-' })[0].Trim().Split(new char[] { ':' })[0]), int.Parse(row.Time.Split(new char[] { '-' })[0].Trim().Split(new char[] { ':' })[1]), 0); appointment.End = new DateTime(dt.Year, dt.Month, dt.Day, int.Parse(row.Time.Split(new char[] { '-' })[1].Trim().Split(new char[] { ':' })[0]), int.Parse(row.Time.Split(new char[] { '-' })[1].Trim().Split(new char[] { ':' })[1]), 0); } appointment.IsReminderSet = false; return(appointment); }
public void Start(object o) { string logpath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "Covererror.log"); if (File.Exists(logpath)) { File.Delete(logpath); } StreamWriter sr = new StreamWriter(logpath); try { ServicePointManager.ServerCertificateValidationCallback = CertificateValidationCallBack; ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2007_SP1); if (string.IsNullOrEmpty(Properties.Settings.Default.Domain)) { service.Credentials = new WebCredentials(Properties.Settings.Default.EXIMPUser, Properties.Settings.Default.EXIMPPassword); } else { service.Credentials = new WebCredentials(Properties.Settings.Default.EXIMPUser, Properties.Settings.Default.EXIMPPassword, Properties.Settings.Default.Domain); } if (string.IsNullOrEmpty(Properties.Settings.Default.ExchangeUri)) { service.AutodiscoverUrl(Properties.Settings.Default.EXIMPUser, RedirectionUrlValidationCallback); } else { service.Url = new Uri(Properties.Settings.Default.ExchangeUri + "/ews/exchange.asmx"); } List <Appointment> Appointments = new List <Appointment>(); int count = 0; for (var i = 0; i <= Properties.Settings.Default.AdditionalCoverDays; i++) { XmlDocument doc = new XmlDocument(); //try //{ DateTime dt = DateTime.Now.AddDays(i).DayOfWeek == DayOfWeek.Saturday ? DateTime.Now.AddDays(i + 2) : DateTime.Now.AddDays(i).DayOfWeek == DayOfWeek.Sunday ? DateTime.Now.AddDays(i + 1) : DateTime.Now.AddDays(i); doc.Load(Path.Combine(Properties.Settings.Default.CoverUNC, "CV" + dt.ToString("ddMMyy") + ".htm")); XmlDocument doc1 = new XmlDocument(); doc1.Load(Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "staffmapping.xml")); count += doc.SelectNodes("/html/body/table/tr[@bgcolor='#f1f1f1']").Count; } if (Initialized != null) { Initialized(count); } for (var i = 0; i <= Properties.Settings.Default.AdditionalCoverDays; i++) { XmlDocument doc = new XmlDocument(); //try //{ DateTime dt = DateTime.Now.AddDays(i).DayOfWeek == DayOfWeek.Saturday ? DateTime.Now.AddDays(i + 2) : DateTime.Now.AddDays(i).DayOfWeek == DayOfWeek.Sunday ? DateTime.Now.AddDays(i + 1) : DateTime.Now.AddDays(i); doc.Load(Path.Combine(Properties.Settings.Default.CoverUNC, "CV" + dt.ToString("ddMMyy") + ".htm")); XmlDocument doc1 = new XmlDocument(); doc1.Load(Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "staffmapping.xml")); List <string> clearedaccounts = new List <string>(); foreach (XmlNode n in doc.SelectNodes("/html/body/table/tr[@bgcolor='#f1f1f1']")) { try { CoverRow r = CoverRow.Parse(n); if (r.Cover != "No Cover Reqd") { string email = ""; foreach (XmlNode c in doc1.SelectNodes("/staffmappings/staff[@last=\"" + r.Cover.Split(new char[] { ',' })[0] + "\"]")) { if (r.Cover.ToLower().EndsWith(c.Attributes["first"].Value.ToLower().ToCharArray()[0].ToString())) { email = c.Attributes["email"].Value; break; } } if (!string.IsNullOrEmpty(email)) { service.ImpersonatedUserId = new ImpersonatedUserId(ConnectingIdType.SmtpAddress, email); if (!clearedaccounts.Contains(email)) { SearchFilter.SearchFilterCollection searchFilter = new SearchFilter.SearchFilterCollection(); searchFilter.Add(new SearchFilter.IsGreaterThanOrEqualTo(AppointmentSchema.Start, dt.Date)); searchFilter.Add(new SearchFilter.ContainsSubstring(AppointmentSchema.Subject, "Cover:")); ItemView view = new ItemView(999); view.PropertySet = new PropertySet(BasePropertySet.IdOnly, AppointmentSchema.Subject, AppointmentSchema.Start, AppointmentSchema.AppointmentType); FindItemsResults <Item> findResults = service.FindItems(WellKnownFolderName.Calendar, searchFilter, view); foreach (Item item in findResults.Items) { ((Appointment)item).Delete(DeleteMode.HardDelete); } clearedaccounts.Add(email); } Appointment a = CreateApp(r, dt, service); if (a != null) { a.Save(); } } } this.Progress++; if (Updated != null) { Updated(); } } catch (Exception ex) { sr.WriteLine(ex.Message); sr.WriteLine(ex.Source); sr.WriteLine(ex); } } } } catch (Exception e) { sr.WriteLine(e.Message); sr.WriteLine(e.Source); sr.WriteLine(e); } finally { sr.Close(); } if (Done != null) { Done(); } }