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 static async Task <DBlocalContact> didScanBarcode(string barcode_, string symbology, StatusLookup callback) { if (callback == null) { throw new ArgumentNullException("Callback must not be null to handle result"); } var barcode = barcode_.Trim(); DBlocalContact temporaryContact = null; if (symbology == "QR") { if (!OfflineLogic.isBarcodeValid(barcode)) { throw new InvalidOperationException("The QR code cannot be used by BoaBee because it is not a contact or an ID."); } else if (barcode.Contains("BEGIN:VCARD")) { //VCard temporaryContact = OfflineLogic.parseAsVCard(barcode); } else if (barcode.StartsWith("[[") && barcode.EndsWith("]]")) { //Artexis temporaryContact = OfflineLogic.parseAsArtexis(barcode); } else { //unknown format temporaryContact = new DBlocalContact(); temporaryContact.uid = barcode; } } else { //unknown format temporaryContact = new DBlocalContact(); temporaryContact.uid = barcode; } DBAppSettings appSettings = DBLocalDataStore.GetInstance().GetAppSettings(); if (appSettings == null) { appSettings = new DBAppSettings(); appSettings.instantContactCheck = true; DBLocalDataStore.GetInstance().SetAppSettings(appSettings); } string message = string.Empty; if (appSettings.instantContactCheck) { var localContact = DBLocalDataStore.GetInstance().GetLocalContactsByUID(temporaryContact.uid); if (localContact != null) { temporaryContact &= localContact; temporaryContact.Id = localContact.Id; } if (await Reachability.isConnected()) { try { int timeout = 10000; var timeoutCancellationTokenSource = new CancellationTokenSource(); var timeoutTask = Task.Delay(timeout, timeoutCancellationTokenSource.Token); var task = NetworkRequests.contactLookup(temporaryContact.uid); DBlocalContact serverContact = null; if (await Task.WhenAny(task, timeoutTask) == task) { // Task completed within timeout. // Consider that the task may have faulted or been canceled. // We re-await the task so that any exceptions/cancellation is rethrown. timeoutCancellationTokenSource.Cancel(); serverContact = await task; if (serverContact != null) { if (serverContact.hasOnlyUID() || serverContact.hasOnlyUIDAndName()) { message = "Check for additional contact details can't be executed at this moment. More details will be added to your report if available."; } else { message = "These are the details we have found for you. Feel free to complete the rest below."; } } else { message = "Check for additional contact details can't be executed at this moment. More details will be added to your report if available."; } Console.Error.WriteLine("Server has contact: {0}", serverContact != null); } else { // timeout/cancellation logic message = "Check for additional contact details can't be executed at this moment. More details will be added to your report if available."; Console.Error.WriteLine("Timed out"); } temporaryContact *= serverContact; } catch (Exception e) { message = "Check for additional contact details can't be executed at this moment. More details will be added to your report if available."; Console.Error.WriteLine("contactLookup exception: {0}", e.Message); } } else { message = "Check for additional contact details can't be executed at this moment. More details will be added to your report if available."; } } else { message = "Instant contact checking is turned off"; } callback(message); return(temporaryContact); }