/// <summary> /// Need to deal with the legacy way of storing pre-values and turn them into nice values for the editor /// </summary> /// <param name="defaultPreVals"></param> /// <param name="persistedPreVals"></param> /// <returns></returns> public override IDictionary<string, object> ConvertDbToEditor(IDictionary<string, object> defaultPreVals, PreValueCollection persistedPreVals) { var preVals = persistedPreVals.FormatAsDictionary(); var stringVal = preVals.Any() ? preVals.First().Value.Value : ""; var returnVal = new Dictionary<string, object> { { "min", 0 }, { "max", 0 } }; if (stringVal.IsNullOrWhiteSpace() == false) { try { var json = JsonConvert.DeserializeObject<JObject>(stringVal); if (json["Minimum"] != null) { //by default pre-values are sent out with an id/value pair returnVal["min"] = json["Minimum"].Value<int>(); } if (json["Maximum"] != null) { returnVal["max"] = json["Maximum"].Value<int>(); } } catch (Exception e) { //this shouldn't happen unless there's already a bad formatted pre-value LogHelper.WarnWithException<MultipleTextStringPreValueEditor>("Could not deserialize value to json " + stringVal, e); return returnVal; } } return returnVal; }
/// <summary> /// The editor is expecting a json array for a field with a key named "items" so we need to format the persisted values /// to this format to be used in the editor. /// </summary> /// <param name="defaultPreVals"></param> /// <param name="persistedPreVals"></param> /// <returns></returns> public override IDictionary<string, object> ConvertDbToEditor(IDictionary<string, object> defaultPreVals, PreValueCollection persistedPreVals) { var dictionary = persistedPreVals.FormatAsDictionary(); var arrayOfVals = dictionary.Select(item => item.Value).ToList(); //the items list will be a dictionary of it's id -> value we need to use the id for persistence for backwards compatibility return new Dictionary<string, object> { { "items", arrayOfVals.ToDictionary(x => x.Id, x => x.Value) } }; }
/// <summary> /// Format the persisted value to work with our multi-val editor. /// </summary> /// <param name="defaultPreVals"></param> /// <param name="persistedPreVals"></param> /// <returns></returns> public override IDictionary<string, object> ConvertDbToEditor(IDictionary<string, object> defaultPreVals, PreValueCollection persistedPreVals) { var result = new Dictionary<string, object>(); //the pre-values just take up one field with a semi-colon delimiter so we'll just parse var dictionary = persistedPreVals.FormatAsDictionary(); if (dictionary.Any()) { //there should only be one val var delimited = dictionary.First().Value.Value.Split(new[] {';'}, StringSplitOptions.RemoveEmptyEntries); for (var index = 0; index < delimited.Length; index++) { result.Add(index.ToInvariantString(), delimited[index]); } } //the items list will be a dictionary of it's id -> value we need to use the id for persistence for backwards compatibility return new Dictionary<string, object> { { "items", result } }; }