public static void prepareSync(bool isKiosk = false) { var dateNow = string.Format("{0:yyyy-MM-ddTH:mm:sszzz}", DateTime.Now); var activeLocalContacts = DBLocalDataStore.GetInstance().GetLocalContacts().Where(c => c.activeContact).ToList(); if (activeLocalContacts.Count == 0 && !isKiosk) { throw new InvalidOperationException("Please select at least one contact."); } var allactivefiles = DBLocalDataStore.GetInstance().GetAllLocalFiles().Where(s => s.activeFile).ToList(); var appInfo = DBLocalDataStore.GetInstance().GetAppInfo(); var user = DBLocalDataStore.GetInstance().GetLocalUserInfo(); var contactsToSend = DBLocalDataStore.GetInstance().GetContactsToServer(); var invalidContactsToSend = contactsToSend.Where(c => string.IsNullOrWhiteSpace(c.uid)); foreach (var invalidItem in invalidContactsToSend) { DBLocalDataStore.GetInstance().RemoveContactToServer(invalidItem); } contactsToSend.RemoveAll(c => string.IsNullOrWhiteSpace(c.uid)); var answersToSend = DBLocalDataStore.GetInstance().getAnswers(); var formDefinition = DBLocalDataStore.GetInstance().GetLocalFormDefinitions().Find(s => s.uuid == DBLocalDataStore.GetInstance().GetSelectedQuestionPosition()); var questions = DBLocalDataStore.GetInstance().GetLocalQuestions(formDefinition.uuid); List <DBOrder> orders = new List <DBOrder>(); List <DBOrderLine> orderLines = new List <DBOrderLine>(); allactivefiles.ForEach((f) => { DBOrderLine orderLine = new DBOrderLine(); orderLine.itemDescription = f.name; orderLine.item = f.uuid; orderLines.Add(orderLine); }); activeLocalContacts.ForEach((lc) => { if (string.IsNullOrWhiteSpace(lc.uid)) { return; } var serverContact = contactsToSend.Find(sc => sc.uid.Equals(lc.uid)); if (serverContact == null) { DBContactToServer newServerContact = new DBContactToServer(true); newServerContact.uid = lc.uid; DBLocalDataStore.GetInstance().AddContactToServer(newServerContact); contactsToSend.Add(newServerContact); } DBOrder order = new DBOrder(); order.contactUid = lc.uid; order.created = DateTime.Now.ToString("yyyy-MM-ddTH:mm:sszzz"); order.creator = user.username; order.orderLine = orderLines; orders.Add(order); }); SyncContext scr = new SyncContext(); scr.context = new RequestData(); scr.contacts = new List <CustomerType>(); scr.forms = new List <AnsweredForm>(); scr.orders = orders; contactsToSend.ForEach((c) => { //scr.contacts.Add(new CustomerType(c)); if (!string.IsNullOrWhiteSpace(c.uid)) { scr.contacts.Add(new CustomerType(c)); } }); bool isAnyQuestionAnswered = answersToSend.Count(s => OfflineLogic.isAnswerValid(s.answer)) > 0; string profileName = DBLocalDataStore.GetInstance().GetSelectProfile().shortName; foreach (var contact in activeLocalContacts) { bool isAnswersValid = true; if (isAnyQuestionAnswered) { var form = new AnsweredForm(); List <Answer> tmpAnswers = new List <Answer>(); form.contactUid = contact.uid; form.startdate = dateNow; form.enddate = dateNow; form.name = formDefinition.objectName; form.user = new DeviceUser { username = user.username, profile = profileName }; form.answers = new Answer[] { }; for (int index = 0; (index < answersToSend.Count) && isAnyQuestionAnswered; index++) { if (OfflineLogic.isAnswerValid(answersToSend[index].answer)) { Answer answer = new Answer(); answer.answer = answersToSend[index].answer; answer.name = questions[index].name; answer.type = questions[index].type; tmpAnswers.Add(answer); } else { if (OfflineLogic.isQuestionRequired(questions[index])) { isAnswersValid = false; } } } if (isAnswersValid) { form.answers = tmpAnswers.ToArray(); scr.forms.Add(form); } else { if (!isKiosk) { throw new InvalidOperationException("Not all mandatory questions are completed"); } } } } var Counts = DBLocalDataStore.GetInstance().GetCountHomeScreen(); for (int i = 0; i < scr.contacts.Count; i++) { activeLocalContacts[i].useInRequest = true; DBLocalDataStore.GetInstance().UpdateLocalContact(activeLocalContacts[i]); } if (scr.orders[0].orderLine.Count != 0) { Counts.countShare += scr.orders.Count; } Counts.countQuestion += scr.forms.Count; Counts.countContacts = DBLocalDataStore.GetInstance().GetLocalContacts().Where(c => c.useInRequest).ToList().Count; DBLocalDataStore.GetInstance().SetCountHomeScreen(Counts); JsonSerializerSettings serializationSettings = new JsonSerializerSettings(); serializationSettings.DefaultValueHandling = DefaultValueHandling.Ignore; string json = JsonConvert.SerializeObject(scr, Formatting.Indented, serializationSettings); DBSyncRequest syncRequest = new DBSyncRequest(); syncRequest.serializedSyncContext = json; Console.Error.WriteLine("Saving request: {0}", json); DBLocalDataStore.GetInstance().addSyncRequest(syncRequest); // DBLocalDataStore.GetInstance().SetCountHomeScreen(new DBHomeScreenCounts() { countContacts = ActivityHomescreen.counts[0], countShare = ActivityHomescreen.counts[1], countQuestion = ActivityHomescreen.counts[2] }); if (!isKiosk) { OfflineLogic.ClearDataSelected(); } }
public static void updateForms(string uid, List <Answer> answers, int originalRequestID) { if (answers == null) { throw new ArgumentException("Answers must not be null"); } var dateNow = string.Format("{0:yyyy-MM-ddTH:mm:sszzz}", DateTime.Now); //var answersToSend = DBLocalDataStore.GetInstance().getAnswers(); var appInfo = DBLocalDataStore.GetInstance().GetAppInfo(); var user = DBLocalDataStore.GetInstance().GetLocalUserInfo(); var formDefinition = DBLocalDataStore.GetInstance().GetLocalFormDefinitions().Find(s => s.uuid == DBLocalDataStore.GetInstance().GetSelectedQuestionPosition()); var questions = DBLocalDataStore.GetInstance().GetLocalQuestions(formDefinition.uuid); UpdateForms scr = new UpdateForms(); scr.forms = new List <AnsweredForm>(); string profileName = DBLocalDataStore.GetInstance().GetSelectProfile().shortName; var form = new AnsweredForm(); form.contactUid = uid; form.startdate = dateNow; form.enddate = dateNow; form.name = formDefinition.objectName; form.user = new DeviceUser { username = user.username, profile = profileName }; bool isAnyQuestionAnswered = answers.Count(s => OfflineLogic.isAnswerValid(s.answer)) > 0; bool isAnswersValid = true; List <Answer> tmpAnswers = new List <Answer>(); for (int index = 0; (index < answers.Count) && isAnyQuestionAnswered; index++) { if (OfflineLogic.isAnswerValid(answers[index].answer)) { Answer answer = new Answer(); answer.answer = answers[index].answer; answer.name = questions[index].name; answer.type = questions[index].type; tmpAnswers.Add(answer); } else { if (OfflineLogic.isQuestionRequired(questions[index])) { isAnswersValid = false; } } } if (isAnswersValid) { form.answers = tmpAnswers.ToArray(); scr.forms.Add(form); string json = JsonConvert.SerializeObject(scr); DBSyncRequest syncRequest = new DBSyncRequest(); syncRequest.serializedSyncContext = json; DBLocalDataStore.GetInstance().addSyncRequest(syncRequest); var originalSyncRequest = DBLocalDataStore.GetInstance().getSyncRequests().Find(r => r.Id == originalRequestID); var originalContext = JsonConvert.DeserializeObject <SyncContext>(originalSyncRequest.serializedSyncContext); originalContext.forms.RemoveAll(c => c.contactUid.Equals(uid)); if (originalContext.contacts.isNullOrEmpty() && originalContext.forms.isNullOrEmpty() && originalContext.orders.isNullOrEmpty()) { DBLocalDataStore.GetInstance().deleteSyncRequest(originalSyncRequest); } else { originalSyncRequest.serializedSyncContext = JsonConvert.SerializeObject(originalContext); DBLocalDataStore.GetInstance().updateSyncReqest(originalSyncRequest); } } else { throw new InvalidOperationException("You did not complete all mandatory fields in the info screen. Please correct."); } }
public static void updateForms(string uid) { var dateNow = string.Format("{0:yyyy-MM-ddTH:mm:sszzz}", DateTime.Now); var answersToSend = DBLocalDataStore.GetInstance().getAnswers(); var appInfo = DBLocalDataStore.GetInstance().GetAppInfo(); var user = DBLocalDataStore.GetInstance().GetLocalUserInfo(); var formDefinition = DBLocalDataStore.GetInstance().GetLocalFormDefinitions().Find(s => s.uuid == DBLocalDataStore.GetInstance().GetSelectedQuestionPosition()); var questions = DBLocalDataStore.GetInstance().GetLocalQuestions(formDefinition.uuid); UpdateForms scr = new UpdateForms(); scr.context = new RequestData(); scr.forms = new List <AnsweredForm>(); var context = scr.context; context.password = user.password; context.username = user.username; context.profile = DBLocalDataStore.GetInstance().GetSelectProfile().shortName; context.tags = new string[] { user.tags }; context.campaignReference = appInfo.campaignReference; var form = new AnsweredForm(); List <Answer> tmpAnswers = new List <Answer>(); form.contactUid = uid; form.startdate = dateNow; form.enddate = dateNow; form.name = formDefinition.objectName; form.user = new DeviceUser { username = user.username, profile = context.profile }; form.answers = new Answer[] { }; bool isAnyQuestionAnswered = answersToSend.Count(s => OfflineLogic.isAnswerValid(s.answer)) > 0; bool isAnswersValid = true; for (int index = 0; (index < answersToSend.Count) && isAnyQuestionAnswered; index++) { if (OfflineLogic.isAnswerValid(answersToSend[index].answer)) { Answer answer = new Answer(); answer.answer = answersToSend[index].answer; answer.name = questions[index].name; answer.type = questions[index].type; tmpAnswers.Add(answer); } else { if (OfflineLogic.isQuestionRequired(questions[index])) { isAnswersValid = false; } } } if (isAnswersValid) { form.answers = tmpAnswers.ToArray(); scr.forms.Add(form); } else { throw new InvalidOperationException("You did not complete all mandatory fields in the info screen. Please correct."); } string json = JsonConvert.SerializeObject(scr); DBSyncRequest syncRequest = new DBSyncRequest(); syncRequest.serializedSyncContext = json; DBLocalDataStore.GetInstance().addSyncRequest(syncRequest); OfflineLogic.ClearDataSelected(); }
public void CreateDomainLevel4() { try { var ctx = ApplicationDbContext.Create(); //already OK if (ctx.Records.Any()) { return; } var reader = new ReadOnlyContext(); var db = MemoryDB.LoadDomainLevel4(reader); var container = new MemoryDB(); foreach (var item in db.Records) { var record = new Record { Id = item.Id, //Form = form, //FormId = form?.Id, FormId = item.FormId, //User = user, //UserId = user?.Id, UserId = item.UserId, //CallCode = callCode, //CallCodeId = callCode.Id, CallCodeId = item.CallCodeId, //Contact = contact, //ContactId = contact.Id, ContactId = item.ContactId, CreationDate = item.CreationDate, Duration = item.Duration, EndTime = item.EndTime, IsDeleted = item.IsDeleted, LastUpdate = item.LastUpdate, StartTime = item.StartTime, }; container.Records.Add(record); } foreach (var item in db.AnsweredForms) { var answeredForm = new AnsweredForm { Id = item.Id, //Question = question, //QuestionId = question.Id, QuestionId = item.QuestionId, //User = user, //UserId = user.Id, //Record = record, //RecordId = record.Id, Answer = item.Answer, CreationDate = item.CreationDate, IsDeleted = item.IsDeleted, LastUpdate = item.LastUpdate, }; container.AnsweredForms.Add(answeredForm); } foreach (var item in db.Appointments) { var appointment = new Appointments { Id = item.Id, //Agent = agent, //AgentId = agent?.Id, AgentId = item.AgentId, //Campaign = campaign, //CampaignId = campaign.Id, CampaignId = item.CampaignId, //Record = record, //RecordId = record?.Id, RecordId = item.RecordId, IsDeleted = item.IsDeleted, CreationDate = item.CreationDate, DateScheduled = item.DateScheduled, IsConfirmed = item.IsConfirmed, IsScheduled = item.IsScheduled, LastUpdate = item.LastUpdate, Notes = item.Notes, Notified = item.Notified, ReminderDate = item.ReminderDate, TimesScheduled = item.TimesScheduled, }; container.Appointments.Add(appointment); } //foreach (var item in db.ExtraFieldValues) //{ // var extraFieldValue = new ExtraFieldValue // { // Id = item.Id, // //Contact = contact, // //ContactId = contact.Id, // ContactId = item.ContactId, // //ExtraField = extraField, // //ExtraFieldId = extraField.Id, // ExtraFieldId = item.ExtraFieldId, // CreationDate = item.CreationDate, // IsDeleted = item.IsDeleted, // LastUpdate = item.LastUpdate, // Value = item.Value, // }; // //container.ExtraFieldValues.Add(extraFieldValue); //} ctx.Records.AddRange(container.Records); ctx.AnsweredForms.AddRange(container.AnsweredForms); ctx.Appointments.AddRange(container.Appointments); // ctx.ExtraFieldValues.AddRange(container.ExtraFieldValues); ctx.SaveChanges(); } catch (Exception ex) { var str = ex.ToString(); System.Diagnostics.Debug.WriteLine(str); } }