public void RunUpdate() { if (!Initalize()) { return; } //get schedule data from tenant graph var programme = client.GetConfSchedule(); if (!programme.Values.Any()) { //there is no event data in graph //get the latest program data from datownia int seq = 0; var data = datowniaService.GetAllEvents(out seq); if (!data.Any()) { return; } //update the programme in the graph var eventList = new List <Event>(); foreach (var dtEvent in data) { var newEvent = new Event(); newEvent.DatowniaId = dtEvent.Id; newEvent.Location = dtEvent.Location; newEvent.Time = dtEvent.Time; newEvent.Speakers = dtEvent.Speakers; newEvent.Title = dtEvent.Title; newEvent.Code = dtEvent.Code; newEvent.Area = dtEvent.Area; newEvent.Day = dtEvent.Day; eventList.Add(newEvent); } programme.Values = eventList; //need to split into four separate programmes client.PutProgramme(programme); sqlService.SetLatestLocalSeq(WaadConfig.ConfScheduleApiFullName, seq); } else { //get sequence from sql var latestScheduleSeq = sqlService.GetLatestLocalSeq(WaadConfig.ConfScheduleApiFullName); //get data from datownia var eventDeltas = datowniaService.GetDeltas(WaadConfig.ConfScheduleApiFullName, latestScheduleSeq); if (!eventDeltas.Any()) { return; } var eventList = programme.Values.ToList(); //apply deltas in sequence to local object foreach (var eventDelta in eventDeltas) { if (eventDelta.IsDelete) { var valToDelete = eventList.Where(e => e.DatowniaId == eventDelta.DataForDelete).FirstOrDefault(); if (valToDelete != null) { eventList.Remove(valToDelete); } } else { var dtEvent = eventDelta.DataAsEvent; var newEvent = new Event(); newEvent.DatowniaId = dtEvent.Id; newEvent.Location = dtEvent.Location; newEvent.Time = dtEvent.Time; newEvent.Speakers = dtEvent.Speakers; newEvent.Title = dtEvent.Title; newEvent.Code = dtEvent.Code; newEvent.Area = dtEvent.Area; newEvent.Day = dtEvent.Day; eventList.Add(newEvent); } } //update tenant graph with programme object programme.Values = eventList; client.PutProgramme(programme); var latestScheduleSeqFromApi = eventDeltas.OrderByDescending(ud => ud.Seq).First().Seq; if (latestScheduleSeqFromApi > latestScheduleSeq) { sqlService.SetLatestLocalSeq(WaadConfig.ConfScheduleApiFullName, latestScheduleSeqFromApi); } } }
public void RunUpdate() { if (!Initalize()) { return; } //try //{ var latestUserSeq = sqlService.GetLatestLocalSeq(WaadConfig.UserApiFullName); var latestGroupSeq = sqlService.GetLatestLocalSeq(WaadConfig.GroupApiFullName); var latestUserGroupSeq = sqlService.GetLatestLocalSeq(WaadConfig.UserGroupApiFullName); var latestUserSeqFromApi = latestUserSeq; var latestGroupSeqFromApi = latestGroupSeq; var latestUserGroupSeqFromApi = latestUserGroupSeq; var userDeltas = datowniaService.GetDeltas(WaadConfig.UserApiFullName, latestUserSeq); var groupDeltas = datowniaService.GetDeltas(WaadConfig.GroupApiFullName, latestGroupSeq); var userGroupDeltas = datowniaService.GetDeltas(WaadConfig.UserGroupApiFullName, latestUserGroupSeq); //1. run user update (to create and delete) var useractions = new List <DeltaAction>(); if (userDeltas.Any()) { useractions = PairUserDeltas(userDeltas); foreach (var deltaAction in useractions) { ResolveUserDelta(deltaAction); } //update sequence number latestUserSeqFromApi = userDeltas.OrderByDescending(ud => ud.Seq).First().Seq; if (latestUserSeqFromApi > latestUserSeq) { sqlService.SetLatestLocalSeq(WaadConfig.UserApiFullName, latestUserSeqFromApi); } } //2.a run role update (to create new) var groupactions = new List <DeltaAction>(); var groupDeletions = new List <DtDelta>(); var groupInsertions = new List <DtDelta>(); if (groupDeltas.Any()) { latestGroupSeqFromApi = groupDeltas.OrderByDescending(ud => ud.Seq).First().Seq; groupactions = PairGroupDeltas(groupDeltas); SplitRoleActions(groupactions, groupInsertions, groupDeletions); foreach (var insert in groupInsertions) { CreateGroup(insert); } } //2.b run userrole update (to add new) //2.c run userrole update (to delete corresponding role deletions) if (userGroupDeltas.Any()) { var userRoleactions = RemoveRedundantDeltas(userGroupDeltas); //process userrole updates foreach (var userRoleaction in userRoleactions.OrderBy(a => a.Seq)) { ResolveUserGroupDelta(userRoleaction); } //update sequence number latestUserGroupSeqFromApi = userGroupDeltas.OrderByDescending(ud => ud.Seq).First().Seq; if (latestUserGroupSeqFromApi > latestUserGroupSeq) { sqlService.SetLatestLocalSeq(WaadConfig.UserGroupApiFullName, latestUserGroupSeqFromApi); } } //3. run role update (to delete unwanted) foreach (var insert in groupDeletions) { DeleteGroup(insert); } //update sequence number if (latestGroupSeqFromApi > latestGroupSeq) { sqlService.SetLatestLocalSeq(WaadConfig.GroupApiFullName, latestGroupSeqFromApi); } //} //catch (Exception ex) //{ // //TODO: Log exception //} }