public void UpdateAppointments() { DateTime start = DateTime.Now; lock (thisLock) { try { foreach (AppointmentList al in AppointmentLists) { try { List <RetrievalCriteria> rc = new List <RetrievalCriteria>(); RetrievalCriteria first = new RetrievalCriteria("ScheduledArrivalTime", CriteraType.GreaterThan, al.SelectedDate.Date); rc.Add(first); RetrievalCriteria second = new RetrievalCriteria("ScheduledArrivalTime", CriteraType.LessThan, al.SelectedDate.Date.AddDays(1)); rc.Add(second); //Create a new retriever item EFDataRetriever spareRetriever = new EFDataRetriever(); List <IDataStoreItem> items = spareRetriever.RetrieveItems(typeof(Appointment), rc, true); foreach (IDataStoreItem dt in items) { //rmember dt = item fress from database //check if this already exists in the list of appointments if (al.Appointments.Where(x => x.ID == dt.ID).Any()) { //this means this items is in the appointment list, we must check for updates IDataStoreItem oldItem = al.Appointments.Where(x => x.ID == dt.ID).First(); if (oldItem.LastEditDate != dt.LastEditDate) { logger.Trace("Found appointment in current list -- Needs to be updated"); retriever.UpdateItem(oldItem); } else { } //Now we check all the tasks for this appointment foreach (BasicTask bt in ((Appointment)dt).Tasks) { try { if (((Appointment)oldItem).Tasks.Where(x => x.ID == bt.ID).Any()) { //this task is already in the appointment - check for updates needed BasicTask ot = ((Appointment)oldItem).Tasks.Where(x => x.ID == bt.ID).First(); if (ot.LastEditDate != bt.LastEditDate) { retriever.UpdateItem(ot); } //foreach (BaseConstraint c in bt.Constraints) //{ // if (ot.Constraints.Where(x => x.ID == c.ID).Any()) // { // //this constraint exists in our version // if (ot.Constraints.Where(x => x.ID == c.ID).First().LastEditDate != c.LastEditDate) // { // retriever.UpdateItem(ot.Constraints.Where(x => x.ID == c.ID).First()); // } // } // else // { // } //} } else { //need to add this task //(oldItem as Appointment).Tasks.Add(bt); retriever.UpdateItem(oldItem); System.Diagnostics.Debug.WriteLine("New task found ... appointment reloaded"); if (bt is ArrivalTask) { System.Diagnostics.Debug.WriteLine("task being added is arrivalTask"); (oldItem as Appointment).FireArrivalEvent(); } else { System.Diagnostics.Debug.WriteLine("task being added is NOT arrivalTask"); } retriever.SaveItem(oldItem); } } catch (Exception ex) { System.Diagnostics.Debug.WriteLine("Exception: " + ex.Message); } } } else { //This one is not in the list of appointments must add it. logger.Trace("Could not find this appointment in the current list"); al.Appointments.Add(dt); } } } catch (Exception ex) { Console.WriteLine("Caught exception during appointment refresh: " + ex.Message); } } } catch { } logger.Trace("UpdateAppointments()...done"); } }