private void UpdaterThread() { bool exit = false; XmlDocument xdoc = new XmlDocument(); HttpWebRequest request = null; while (!exit) { try { using (DBArchive db = new DBArchive()) { // try to get user Id if (sharepointUserId == 0) { request = (HttpWebRequest)WebRequest.Create("https://intranet.elettric80.it/_api/web/currentuser"); request.Credentials = new NetworkCredential(UserSettings.Email.Username, UserSettings.Email.EmailPassword); request.Method = "GET"; var webResponse = new StreamReader(request.GetResponse().GetResponseStream()).ReadToEnd(); xdoc.LoadXml(webResponse); sharepointUserId = Convert.ToInt32(xdoc.GetElementsByTagName("content")[0].FirstChild.FirstChild.LastChild.Value.ToString()); } //take in account only if current user id is retrieved if (sharepointUserId > 0) { // try to get all submitted events string req = string.Format("https://intranet.elettric80.it/_api/web/Lists/GetByTitle('Vacations ITA')/Items?$filter=Author/Id eq {0}", sharepointUserId); request = (HttpWebRequest)WebRequest.Create(req); request.Credentials = new NetworkCredential(UserSettings.Email.Username, UserSettings.Email.EmailPassword); request.Method = "GET"; var webResponse = new StreamReader(request.GetResponse().GetResponseStream()).ReadToEnd(); xdoc.LoadXml(webResponse); foreach (XmlElement el in xdoc.GetElementsByTagName("entry")) { Int64 shpid = Convert.ToInt64(el.GetElementsByTagName("content")[0]?.FirstChild["d:Id"].InnerText); int status = Convert.ToInt32(el.GetElementsByTagName("content")[0]?.FirstChild["d:OData__ModerationStatus"].InnerText); var existing = db.Events.SingleOrDefault(x => x.SharepointId == shpid); if (shpid == 0) { continue; } if (existing == null) { //manually added to calendar. Import it! EventEVM tmp = new EventEVM(); tmp.IsSent = true; // the event is on calendar. Not necessary to send it tmp.SharePointId = shpid; tmp.Title = el.GetElementsByTagName("content")[0]?.FirstChild["d:Title"].InnerText.Trim('*'); tmp.Location = el.GetElementsByTagName("content")[0]?.FirstChild["d:Location"].InnerText; tmp.StartDate = Convert.ToDateTime(el.GetElementsByTagName("content")[0]?.FirstChild["d:EventDate"].InnerText); tmp.EndDate = Convert.ToDateTime(el.GetElementsByTagName("content")[0]?.FirstChild["d:EndDate"].InnerText); tmp.Description = el.GetElementsByTagName("content")[0]?.FirstChild["d:Description"].InnerText; tmp.Status = status; tmp.IsAllDay = Convert.ToBoolean(el.GetElementsByTagName("content")[0]?.FirstChild["d:fAllDayEvent"].InnerText); tmp.SendDateTime = Convert.ToDateTime(el.GetElementsByTagName("content")[0]?.FirstChild["d:Created"].InnerText); //FIx 2:00 am on sharepoint calendar that broke timestamp relation! if (tmp.IsAllDay) { tmp.StartDate = new DateTime(tmp.StartDate.Year, tmp.StartDate.Month, tmp.StartDate.Day, 0, 0, 0); tmp.EndDate = new DateTime(tmp.EndDate.Year, tmp.EndDate.Month, tmp.EndDate.Day, 0, 0, 0); } if (el.GetElementsByTagName("content")[0]?.FirstChild["d:Category"].InnerText == "Vacations") { tmp.EType = EEventType.Vacations; } else if (el.GetElementsByTagName("content")[0]?.FirstChild["d:Category"].InnerText == "Customer Visit") { tmp.EType = EEventType.CustomerVisit; } else if (el.GetElementsByTagName("content")[0]?.FirstChild["d:Category"].InnerText == "Business Trip") { tmp.EType = EEventType.BusinessTrip; } else if (el.GetElementsByTagName("content")[0]?.FirstChild["d:Category"].InnerText == "Education") { tmp.EType = EEventType.Education; } else if (el.GetElementsByTagName("content")[0]?.FirstChild["d:Category"].InnerText == "Other") { tmp.EType = EEventType.Other; } else if (el.GetElementsByTagName("content")[0]?.FirstChild["d:Category"].InnerText == "Old Vacations") { tmp.EType = EEventType.OldVacations; } if (tmp.EStatus == EEventStatus.Accepted) { tmp.ApprovationDate = Convert.ToDateTime(el.GetElementsByTagName("content")[0]?.FirstChild["d:Modified"].InnerText); tmp.Approver = el.GetElementsByTagName("content")[0]?.FirstChild["d:Approver"].InnerText; } tmp.Save(db); if (tmp.EStatus != EEventStatus.Rejected) { NotifyEventImported(tmp); } } else { EventEVM tmp = new EventEVM(existing); //if event does not have relations create it this is to patch error on previous releases if (!db.DayEvents.Any(x => x.EventId == tmp.Id)) { if (tmp.EStatus != EEventStatus.Pending || tmp.EType != EEventType.Vacations) { continue; } } if ((EEventStatus)status < tmp.EStatus) { tmp.EStatus = (EEventStatus)status; tmp.ApprovationDate = Convert.ToDateTime(el.GetElementsByTagName("content")[0]?.FirstChild["d:Modified"].InnerText); tmp.Save(db); NotifyEventChanged(tmp); } } } } } } catch (Exception) { } Thread.Sleep(ApplicationSettings.General.WaitForNextEventChek); } }
private void UpdaterThread() { bool exit = false; XmlDocument xdoc = new XmlDocument(); HttpWebRequest request = null; while (!exit) { try { using (DBArchive db = new DBArchive()) { // try to get user Id if (sharepointUserId == 0) { request = (HttpWebRequest)WebRequest.Create($"{ApplicationSettings.General.IntranetAddress}/_api/web/currentuser"); if (UserSettings.Email.UseDefaultCredentials) { request.UseDefaultCredentials = true; } else { request.Credentials = new NetworkCredential(UserSettings.Email.Username, UserSettings.Email.EmailPassword); } request.Method = "GET"; var webResponse = new StreamReader(request.GetResponse().GetResponseStream()).ReadToEnd(); xdoc.LoadXml(webResponse); sharepointUserId = Convert.ToInt32(xdoc.GetElementsByTagName("content")[0].FirstChild.FirstChild.LastChild.Value.ToString()); } //take in account only if current user id is retrieved if (sharepointUserId > 0) { // try to get all submitted events string req = $"{ApplicationSettings.General.IntranetAddress}/_api/web/Lists/GetByTitle('Vacations ITA')/Items?$filter=Author/Id eq {sharepointUserId}"; request = (HttpWebRequest)WebRequest.Create(req); if (UserSettings.Email.UseDefaultCredentials) { request.UseDefaultCredentials = true; } else { request.Credentials = new NetworkCredential(UserSettings.Email.Username, UserSettings.Email.EmailPassword); } request.Method = "GET"; var webResponse = new StreamReader(request.GetResponse().GetResponseStream()).ReadToEnd(); xdoc.LoadXml(webResponse); foreach (XmlElement el in xdoc.GetElementsByTagName("entry")) { Int64 shpid = Convert.ToInt64(el.GetElementsByTagName("content")[0]?.FirstChild["d:Id"].InnerText); int status = Convert.ToInt32(el.GetElementsByTagName("content")[0]?.FirstChild["d:OData__ModerationStatus"].InnerText); if (shpid == 0) { continue; } EventEVM tmp = new EventEVM(); tmp.IsSent = true; // the event is on calendar. Not necessary to send it tmp.SharePointId = shpid; tmp.Title = el.GetElementsByTagName("content")[0]?.FirstChild["d:Title"].InnerText.Trim('*'); tmp.Location = el.GetElementsByTagName("content")[0]?.FirstChild["d:Location"].InnerText; tmp.Description = el.GetElementsByTagName("content")[0]?.FirstChild["d:Description"].InnerText; tmp.Status = status; tmp.IsAllDay = Convert.ToBoolean(el.GetElementsByTagName("content")[0]?.FirstChild["d:fAllDayEvent"].InnerText); tmp.SendDateTime = Convert.ToDateTime(el.GetElementsByTagName("content")[0]?.FirstChild["d:Created"].InnerText); if (tmp.IsAllDay) { tmp.StartDate = Convert.ToDateTime(el.GetElementsByTagName("content")[0]?.FirstChild["d:EventDate"].InnerText.TrimEnd('Z')); tmp.EndDate = Convert.ToDateTime(el.GetElementsByTagName("content")[0]?.FirstChild["d:EndDate"].InnerText.TrimEnd('Z')); } else { tmp.StartDate = Convert.ToDateTime(el.GetElementsByTagName("content")[0]?.FirstChild["d:EventDate"].InnerText); tmp.EndDate = Convert.ToDateTime(el.GetElementsByTagName("content")[0]?.FirstChild["d:EndDate"].InnerText); } if (el.GetElementsByTagName("content")[0]?.FirstChild["d:Category"].InnerText == "Vacations") { tmp.EType = EEventType.Vacations; } else if (el.GetElementsByTagName("content")[0]?.FirstChild["d:Category"].InnerText == "Customer Visit") { tmp.EType = EEventType.CustomerVisit; } else if (el.GetElementsByTagName("content")[0]?.FirstChild["d:Category"].InnerText == "Business Trip") { tmp.EType = EEventType.BusinessTrip; } else if (el.GetElementsByTagName("content")[0]?.FirstChild["d:Category"].InnerText == "Education") { tmp.EType = EEventType.Education; } else if (el.GetElementsByTagName("content")[0]?.FirstChild["d:Category"].InnerText == "Other") { tmp.EType = EEventType.Other; } else if (el.GetElementsByTagName("content")[0]?.FirstChild["d:Category"].InnerText == "Old Vacations") { tmp.EType = EEventType.OldVacations; } if (tmp.EStatus == EEventStatus.Accepted) { tmp.ApprovationDate = Convert.ToDateTime(el.GetElementsByTagName("content")[0]?.FirstChild["d:Modified"].InnerText); tmp.Approver = el.GetElementsByTagName("content")[0]?.FirstChild["d:Approver"].InnerText; } if (db.Events.SingleOrDefault(x => x.SharepointId == shpid) == null) { tmp.Save(db); NotifyEventImported(tmp); } else { var existing = new EventEVM(db.Events.SingleOrDefault(x => x.SharepointId == shpid)); if (existing != null) { tmp.Id = existing.Id; if (!tmp.Equals(existing) && !existing.IsCancelRequested) { tmp.Id = existing.Id; tmp.Notes = existing.Notes; //Global.Mapper.Map(tmp, existing); tmp.Save(db); NotifyEventChanged(tmp); } } } } } } } catch (Exception ex) { } Thread.Sleep(ApplicationSettings.General.WaitForNextEventCheck); } }