/// <summary> /// Converts object array containing data from Content Repository to string list. /// </summary> /// <param name="handlerValues">String array with a single string containing average rating and numbers of rating separated by pipe character.</param> /// <returns>String list containing average rating and numbers of ratings</returns> protected override object ConvertTo(object[] handlerValues) { var fs = FieldSetting as RatingFieldSetting; if (fs == null) { throw new NotSupportedException("Invalid field setting."); } var items = handlerValues[0] as string; VoteData data = string.IsNullOrEmpty(items) ? VoteData.CreateVoteData(fs.Range, fs.Split) : VoteData.CreateVoteData(items); return(data); }
/// <summary> /// Converts string list to object array to store data in Content Repository. /// </summary> /// <param name="value">String list containing average rating and numbers of ratings</param> /// <returns>String array with a single string containing average rating and numbers of rating separated by pipe character.</returns> protected override object[] ConvertFrom(object value) { var fs = FieldSetting as RatingFieldSetting; if (fs == null) { throw new NotSupportedException("Invalid field setting."); } if (fs.Range <= 0) { throw new NotSupportedException("Range is invalid, must be greater than 0."); } var voteData = value as VoteData; if (voteData == null) { throw new InvalidCastException("Unable to cast the given value to VoteData."); } var originalValueData = OriginalValue as VoteData; if (originalValueData == null) { throw new NotSupportedException("Invalid items in the field."); } //If there was no stored rating or it is invalid, reset votes if ((originalValueData.MaxVotes != fs.Range * fs.Split || originalValueData.Split != fs.Split) || voteData.SelectedValue == -1) { originalValueData = VoteData.CreateVoteData(fs.Range, fs.Split); } //Adding vote if present if (voteData.SelectedValue.HasValue && voteData.SelectedValue.Value > 0) { if (voteData.OldValue.HasValue && voteData.OldValue.Value > 0) { originalValueData.RemoveVote(voteData.OldValue.Value); } originalValueData.AddVote(voteData.SelectedValue.Value); } return(new object[] { originalValueData.Serialize(), originalValueData.SumVotes, originalValueData.AverageRate }); }
protected override void ImportData(System.Xml.XmlNode fieldNode, ImportContext context) { var text = fieldNode.InnerText; VoteData vd = null; var fs = FieldSetting as RatingFieldSetting; if (fs == null) { throw new NotSupportedException("Invalid field setting."); } if (fs.Range <= 0) { throw new NotSupportedException("Range is invalid, must be greater than 0."); } vd = string.IsNullOrEmpty(text) ? VoteData.CreateVoteData(fs.Range, fs.Split) : VoteData.CreateVoteData(text); if (vd.MaxVotes != fs.Range * fs.Split || vd.Split != fs.Split) { vd = VoteData.CreateVoteData(fs.Range, fs.Split); } this.SetData(vd); }