private void UpdateExisting(DateTime now) { // Status Customizations edit foreach (var bindableHazardousCondition in BindableHazardousConditions) { if (bindableHazardousCondition.Item3.BoolValue) { // true if (!PersonFollowUp.PeopleFollowUpHazardousConditions.Any(a => a.HazardousConditionInternalId == bindableHazardousCondition.Item2.InternalId)) { // add value PersonFollowUp.PeopleFollowUpHazardousConditions.Add(new PersonFollowUpHazardousCondition { PersonFollowUp = PersonFollowUp, HazardousCondition = bindableHazardousCondition.Item2 }); } } else { // false var query = PersonFollowUp.PeopleFollowUpHazardousConditions.Where(a => a.HazardousConditionInternalId == bindableHazardousCondition.Item2.InternalId); if (query.Any()) { // delete the value var ids = query.Select(a => a.HazardousConditionInternalId).ToList(); foreach (var x in ids) { PersonFollowUp.PeopleFollowUpHazardousConditions.Remove(PersonFollowUp.PeopleFollowUpHazardousConditions.First(a => a.HazardousConditionInternalId == x)); } } } } foreach (var bindableWorkActivity in BindableWorkActivities) { if (bindableWorkActivity.Item3.BoolValue) { if (!PersonFollowUp.PeopleFollowUpWorkActivities.Any(a => a.WorkActivityInternalId == bindableWorkActivity.Item2.InternalId)) { PersonFollowUp.PeopleFollowUpWorkActivities.Add(new PersonFollowUpWorkActivity { PersonFollowUp = PersonFollowUp, WorkActivity = bindableWorkActivity.Item2 }); } } else { var query = PersonFollowUp.PeopleFollowUpWorkActivities.Where(a => a.WorkActivityInternalId == bindableWorkActivity.Item2.InternalId); if (query.Any()) { var ids = query.Select(a => a.WorkActivityInternalId).ToList(); foreach (var x in ids) { PersonFollowUp.PeopleFollowUpWorkActivities.Remove(PersonFollowUp.PeopleFollowUpWorkActivities.First(a => a.WorkActivityInternalId == x)); } } } } foreach (var bindableHouseholdTask in BindableHouseholdTasks) { if (bindableHouseholdTask.Item3.BoolValue) { if (!PersonFollowUp.PeopleFollowUpHouseholdTasks.Any(a => a.HouseholdTaskInternalId == bindableHouseholdTask.Item2.InternalId)) { PersonFollowUp.PeopleFollowUpHouseholdTasks.Add(new PersonFollowUpHouseholdTask { PersonFollowUp = PersonFollowUp, HouseholdTask = bindableHouseholdTask.Item2 }); } } else { var query = PersonFollowUp.PeopleFollowUpHouseholdTasks.Where(a => a.HouseholdTaskInternalId == bindableHouseholdTask.Item2.InternalId); if (query.Any()) { var ids = query.Select(a => a.HouseholdTaskInternalId).ToList(); foreach (var x in ids) { PersonFollowUp.PeopleFollowUpHouseholdTasks.Remove(PersonFollowUp.PeopleFollowUpHouseholdTasks.First(a => a.HouseholdTaskInternalId == x)); } } } } // Custom Values edit for (var i = 0; i < CustomFields.Count(); i++) { // has value // is same // delete if no value but previously existed // add if has value but not existing // edit value if it changed var jsonValue = @""; var hasValue = false; switch (CustomFields[i].FieldType) { case @"text": var textValue = ((CustomFieldStringValueViewModel)CustomFieldControls[i].BindingContext).EntryValue; if (textValue != null && !textValue.Equals(string.Empty)) { hasValue = true; jsonValue = Helpers.CustomValueConverter.ConvertCustomValueToJsonText(textValue); } break; case @"textarea": var textAreaValue = ((CustomFieldStringValueViewModel)CustomFieldControls[i].BindingContext).EntryValue; if (textAreaValue != null && !textAreaValue.Equals(string.Empty)) { hasValue = true; jsonValue = Helpers.CustomValueConverter.ConvertCustomValueToJsonTextArea(textAreaValue); } break; case @"check_box": var checkBoxValues = ((CustomFieldSwitchArrayView)CustomFieldControls[i]).GetSelectedValuesAsList(); if (checkBoxValues.Any()) { hasValue = true; jsonValue = Helpers.CustomValueConverter.ConvertCustomValueToJsonCheckBox(checkBoxValues); } break; case @"radio_button": var radioButtonValue = ((CustomFieldPickerViewModel)CustomFieldControls[i].BindingContext).SelectedBindableOption; if (radioButtonValue != null && !radioButtonValue.Equals(string.Empty)) { hasValue = true; jsonValue = Helpers.CustomValueConverter.ConvertCustomValueToJsonRadioButton(radioButtonValue); } break; case @"select": var selectValue = ((CustomFieldPickerViewModel)CustomFieldControls[i].BindingContext).SelectedBindableOption; if (selectValue != null && !selectValue.Equals(string.Empty)) { hasValue = true; jsonValue = Helpers.CustomValueConverter.ConvertCustomValueToJsonSelect(selectValue); } break; case @"number": var numberValue = ((CustomFieldDoubleValueViewModel)CustomFieldControls[i].BindingContext).GetDoubleValue(); if (numberValue != null) { hasValue = true; jsonValue = Helpers.CustomValueConverter.ConvertCustomValueToJsonNumber((double)numberValue); } break; case @"date": var dateValue = ((CustomFieldDateTimeValueViewModel)CustomFieldControls[i].BindingContext).DateValue; if (dateValue != null && !dateValue.ToString().Equals(string.Empty)) { hasValue = true; jsonValue = Helpers.CustomValueConverter.ConvertCustomValueToJsonDate((DateTime)dateValue); } break; case @"rank_list": var rankedValues = ((CustomFieldRankListViewModel)CustomFieldControls[i].BindingContext).GetRankedValues(); if (!rankedValues.Equals(@"")) { hasValue = true; jsonValue = Helpers.CustomValueConverter.ConvertCustomValueToJsonRankList(((CustomFieldRankListViewModel)CustomFieldControls[i].BindingContext).Entries.ToList()); } break; default: break; } var existingValueQuery = ApplicationInstanceData.Data.CustomPersonFollowUpValues .Where(a => a.PersonFollowUp.InternalId == PersonFollowUp.InternalId && a.CustomField.InternalId == CustomFields[i].InternalId); if (hasValue) { // check to see if new needed if (!existingValueQuery.Any()) { // add a record var newCustomValue = new CustomPersonFollowUpValue { CreatedAt = now, LastUpdatedAt = now, SoftDeleted = false, CustomField = CustomFields[i], Value = jsonValue, PersonFollowUp = PersonFollowUp, InternalParentId = PersonFollowUp.InternalId }; ApplicationInstanceData.Data.CustomPersonFollowUpValues.Add(newCustomValue); } else { // if existing compare and update if necesary if (existingValueQuery.First().Value != jsonValue) { existingValueQuery.First().Value = jsonValue; } } } else { // delete value if it exists if (existingValueQuery.Any()) { ApplicationInstanceData.Data.CustomPersonFollowUpValues.Remove(existingValueQuery.First()); } } } ApplicationInstanceData.Data.SaveChanges(); }
private void UpdateExisting(DateTime now) { // Custom Values edit for (var i = 0; i < CustomFields.Count(); i++) { // has value // is same // delete if no value but previously existed // add if has value but not existing // edit value if it changed var jsonValue = @""; var hasValue = false; switch (CustomFields[i].FieldType) { case @"text": var textValue = ((CustomFieldStringValueViewModel)CustomFieldControls[i].BindingContext).EntryValue; if (textValue != null && !textValue.Equals(string.Empty)) { hasValue = true; jsonValue = Helpers.CustomValueConverter.ConvertCustomValueToJsonText(textValue); } break; case @"textarea": var textAreaValue = ((CustomFieldStringValueViewModel)CustomFieldControls[i].BindingContext).EntryValue; if (textAreaValue != null && !textAreaValue.Equals(string.Empty)) { hasValue = true; jsonValue = Helpers.CustomValueConverter.ConvertCustomValueToJsonTextArea(textAreaValue); } break; case @"check_box": var checkBoxValues = ((CustomFieldSwitchArrayView)CustomFieldControls[i]).GetSelectedValuesAsList(); if (checkBoxValues.Any()) { hasValue = true; jsonValue = Helpers.CustomValueConverter.ConvertCustomValueToJsonCheckBox(checkBoxValues); } break; case @"radio_button": var radioButtonValue = ((CustomFieldPickerViewModel)CustomFieldControls[i].BindingContext).SelectedBindableOption; if (radioButtonValue != null && !radioButtonValue.Equals(string.Empty)) { hasValue = true; jsonValue = Helpers.CustomValueConverter.ConvertCustomValueToJsonRadioButton(radioButtonValue); } break; case @"select": var selectValue = ((CustomFieldPickerViewModel)CustomFieldControls[i].BindingContext).SelectedBindableOption; if (selectValue != null && !selectValue.Equals(string.Empty)) { hasValue = true; jsonValue = Helpers.CustomValueConverter.ConvertCustomValueToJsonSelect(selectValue); } break; case @"number": var numberValue = ((CustomFieldDoubleValueViewModel)CustomFieldControls[i].BindingContext).GetDoubleValue(); if (numberValue != null) { hasValue = true; jsonValue = Helpers.CustomValueConverter.ConvertCustomValueToJsonNumber((double)numberValue); } break; case @"date": var dateValue = ((CustomFieldDateTimeValueViewModel)CustomFieldControls[i].BindingContext).DateValue; if (dateValue != null && !dateValue.ToString().Equals(string.Empty)) { hasValue = true; jsonValue = Helpers.CustomValueConverter.ConvertCustomValueToJsonDate((DateTime)dateValue); } break; case @"rank_list": var rankedValues = ((CustomFieldRankListViewModel)CustomFieldControls[i].BindingContext).GetRankedValues(); if (!rankedValues.Equals(@"")) { hasValue = true; jsonValue = Helpers.CustomValueConverter.ConvertCustomValueToJsonRankList(((CustomFieldRankListViewModel)CustomFieldControls[i].BindingContext).Entries.ToList()); } break; default: break; } var existingValueQuery = ApplicationInstanceData.Data.CustomHouseholdValues .Where(a => a.Household.InternalId == Household.InternalId && a.CustomField.InternalId == CustomFields[i].InternalId); if (hasValue) { // check to see if new needed if (!existingValueQuery.Any()) { // add a record var newCustomValue = new CustomHouseholdValue { CreatedAt = now, LastUpdatedAt = now, SoftDeleted = false, CustomField = CustomFields[i], Value = jsonValue, Household = Household, InternalParentId = Household.InternalId }; ApplicationInstanceData.Data.CustomHouseholdValues.Add(newCustomValue); } else { // if existing compare and update if necesary if (existingValueQuery.First().Value != jsonValue) { existingValueQuery.First().Value = jsonValue; } } } else { // delete value if it exists if (existingValueQuery.Any()) { ApplicationInstanceData.Data.CustomHouseholdValues.Remove(existingValueQuery.First()); } } } ApplicationInstanceData.Data.SaveChanges(); }