/// <summary> /// A method used to format the database value to a value that can be used by the editor /// </summary> /// <param name="property"></param> /// <param name="culture"></param> /// <param name="segment"></param> /// <returns></returns> /// <remarks> /// The object returned will always be a string and if the database type is not a valid string type an exception is thrown /// </remarks> public override object ToEditor(IProperty property, string culture = null, string segment = null) { var val = property.GetValue(culture, segment); if (val == null) { return(string.Empty); } switch (ValueTypes.ToStorageType(ValueType)) { case ValueStorageType.Ntext: case ValueStorageType.Nvarchar: return(val.ToString()); case ValueStorageType.Integer: case ValueStorageType.Decimal: case ValueStorageType.Date: default: throw new InvalidOperationException("The " + typeof(TextOnlyValueEditor) + " can only be used with string based property editors"); } }
/// <summary> /// Converts a property value to an Xml fragment. /// </summary> /// <remarks> /// <para>By default, this returns the value of ConvertDbToString but ensures that if the db value type is /// NVarchar or NText, the value is returned as a CDATA fragment - else it's a Text fragment.</para> /// <para>Returns an XText or XCData instance which must be wrapped in a element.</para> /// <para>If the value is empty we will not return as CDATA since that will just take up more space in the file.</para> /// </remarks> public XNode ConvertDbToXml(IPropertyType propertyType, object value) { //check for null or empty value, we don't want to return CDATA if that is the case if (value == null || value.ToString().IsNullOrWhiteSpace()) { return(new XText(ConvertDbToString(propertyType, value))); } switch (ValueTypes.ToStorageType(ValueType)) { case ValueStorageType.Date: case ValueStorageType.Integer: case ValueStorageType.Decimal: return(new XText(ConvertDbToString(propertyType, value))); case ValueStorageType.Nvarchar: case ValueStorageType.Ntext: //put text in cdata return(new XCData(ConvertDbToString(propertyType, value))); default: throw new ArgumentOutOfRangeException(); } }
/// <summary> /// A method to deserialize the string value that has been saved in the content editor /// to an object to be stored in the database. /// </summary> /// <param name="editorValue"></param> /// <param name="currentValue"> /// The current value that has been persisted to the database for this editor. This value may be useful for /// how the value then get's deserialized again to be re-persisted. In most cases it will probably not be used. /// </param> /// <param name="languageId"></param> /// <param name="segment"></param> /// <returns></returns> /// <remarks> /// By default this will attempt to automatically convert the string value to the value type supplied by ValueType. /// /// If overridden then the object returned must match the type supplied in the ValueType, otherwise persisting the /// value to the DB will fail when it tries to validate the value type. /// </remarks> public virtual object FromEditor(ContentPropertyData editorValue, object currentValue) { //if it's json but it's empty json, then return null if (ValueType.InvariantEquals(ValueTypes.Json) && editorValue.Value != null && editorValue.Value.ToString().DetectIsEmptyJson()) { return(null); } var result = TryConvertValueToCrlType(editorValue.Value); if (result.Success == false) { StaticApplicationLogging.Logger.LogWarning("The value {EditorValue} cannot be converted to the type {StorageTypeValue}", editorValue.Value, ValueTypes.ToStorageType(ValueType)); return(null); } return(result.Result); }