/// <summary> /// Create a friendly error message. /// </summary> /// <returns></returns> public String ToFriendlyErrorMessage() { String errorMessage; if (this.ErrorMessage.Contains("not recognized as a valid DateTime")) { errorMessage = Resources.UIValidationError_ToFriendlyErrorMessage_date_is_not_a_valid_format_; } else if (this.ErrorMessage.Contains("not in a correct format.")) { errorMessage = Resources.UIValidationError_ToFriendlyErrorMessage_entered_value_is_not_the_correct_data_type_; //TODO - developers - add more ElseIf tests here if required to return back the best message possible without using the default } else { errorMessage = this.ErrorMessage; } var propertyName = CamelCaseString.GetWords(this.PropertyName.Contains(".") ? this.PropertyName.Substring(this.PropertyName.LastIndexOf(".") + 1) : this.PropertyName); return(string.Concat(propertyName, GlobalConstants.STRING_WHITE_SPACE, errorMessage)); }
/// <summary> /// Used to generate an Dictionary(Of String, String) for each property decorated with the AuditAttribute. /// </summary> /// <typeparam name="T">Class Type</typeparam> /// <param name="obj">Instance of class to audit</param> /// <param name="defaultValue">Default message if no class propeties are decorated</param> /// <param name="dictionary">Pass an IDictionary Object that needs to be populated. Typically this would be the Data property of an Exception Object.</param> /// <returns>IDictionary</returns> public static IDictionary <String, String> AuditToIDictionary <T>(T obj, String defaultValue, IDictionary <String, String> dictionary) { var list = new List <SortablePropertyBasket>(); foreach (PropertyInfo prop in obj.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance)) { Object[] auditAttributes = prop.GetCustomAttributes(typeof(AuditAttribute), false); AuditAttribute auditAttribute = null; if (auditAttributes.Length == 1) { auditAttribute = auditAttributes[0] as AuditAttribute; } if (auditAttribute != null) { list.Add(new SortablePropertyBasket(auditAttribute.AuditSequence, prop.Name, CamelCaseString.GetWords(prop.Name), prop.GetValue(obj, null).ToString())); } } if (list.Count > 0) { list.Sort(); foreach (SortablePropertyBasket propertyBasket in list) { dictionary.Add(propertyBasket.PropertyName, propertyBasket.Value); } } else { dictionary.Add(_DEFAULTVALUE, defaultValue); } return(dictionary); }
/// <summary> /// Returns a String with each property and value in the class. The String displays the property name, property friendly name and property value. /// </summary> /// <typeparam name="T">Class Type</typeparam> /// <param name="obj">Instance of the class</param> /// <param name="delimiter">What delimiter do you want between each property. Defaults to comma. Could use vbcrlf, etc.</param> /// <param name="sortByPropertyName">Normally sorts the output by property name. To leave in ordinal order, set to False</param> public static String ClassToString <T>(T obj, String delimiter = GlobalConstants.STRING_DEFAULT_DELIMITER, SortByPropertyName sortByPropertyName = SortByPropertyName.Yes) { var sb = new StringBuilder(4096); var list = new List <SortablePropertyBasket>(); foreach (PropertyInfo prop in obj.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance)) { Object[] auditAttributes = prop.GetCustomAttributes(typeof(AuditAttribute), false); AuditAttribute auditAttribute = null; if (auditAttributes.Length == 1) { auditAttribute = auditAttributes[0] as AuditAttribute; } Int32 auditSequence = 1; if (auditAttribute != null) { auditSequence = auditAttribute.AuditSequence; } list.Add(prop.GetValue(obj, null) != null ? new SortablePropertyBasket(auditSequence, prop.Name, CamelCaseString.GetWords(prop.Name), prop.GetValue(obj, null).ToString()) : new SortablePropertyBasket(auditSequence, prop.Name, CamelCaseString.GetWords(prop.Name), _NULL)); } if (list.Count > 0) { if (sortByPropertyName == SortByPropertyName.Yes) { list.Sort(); } foreach (SortablePropertyBasket propertyBasket in list) { sb.Append(propertyBasket.ToString()); sb.Append(delimiter); } if (sb.Length > delimiter.Length) { sb.Length -= delimiter.Length; } } else { sb.Append(Resources.ClassMessageCreationHelper_ClassToString_Class_has_no_properties); } return(sb.ToString()); }
/// <summary> /// This function returns a String of each property decorated with the AuditAttribute. The String displays the property name, property friendly name and property value. This function is typically used when a developer need to make an audit log entry. It provides a very simple method to generate these messages. /// </summary> /// <typeparam name="T">Class Type</typeparam> /// <param name="obj">Instance of the class</param> /// <param name="defaultValue">If no properties have been decorated with the AuditAttribute, then this message is displayed.</param> /// <param name="delimiter">What delimiter do you want between each property. Defaults to comma. Could use vbcrlf, etc.</param> public static String AuditToString <T>(T obj, String defaultValue, String delimiter = GlobalConstants.STRING_DEFAULT_DELIMITER) { var sb = new StringBuilder(2048); var list = new List <SortablePropertyBasket>(); foreach (PropertyInfo prop in obj.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance)) { Object[] auditAttributes = prop.GetCustomAttributes(typeof(AuditAttribute), false); AuditAttribute auditAttribute = null; if (auditAttributes.Length == 1) { auditAttribute = auditAttributes[0] as AuditAttribute; } if (auditAttribute != null) { list.Add(prop.GetValue(obj, null) != null ? new SortablePropertyBasket(auditAttribute.AuditSequence, prop.Name, CamelCaseString.GetWords(prop.Name), prop.GetValue(obj, null).ToString()) : new SortablePropertyBasket(auditAttribute.AuditSequence, prop.Name, CamelCaseString.GetWords(prop.Name), _NULL)); } } if (list.Count > 0) { list.Sort(); foreach (SortablePropertyBasket propertyBasket in list) { sb.Append(propertyBasket.ToString()); sb.Append(delimiter); } if (sb.Length > delimiter.Length) { sb.Length -= delimiter.Length; } } else { sb.Append(defaultValue); } return(sb.ToString()); }
/// <summary> /// Create an error message with properties separated by white space /// </summary> /// <returns></returns> public String ToErrorMessage() { return(String.Concat(CamelCaseString.GetWords(this.PropertyName), GlobalConstants.STRING_WHITE_SPACE, this.ErrorMessage)); }
/// <summary> /// Builds up a string containing each property and value in the class. The string displays the property name, property friendly name and property value. /// </summary> /// <typeparam name="T">Class Type</typeparam> /// <param name="instance">Instance of the class</param> /// <param name="delimiter">What delimiter do you want between each property? Defaults to comma; can pass others like Environment.NewLine, etc.</param> /// <param name="sortByPropertyName">If set to <c>SortByPropertyName.Yes</c> then output will be sorted by AuditAttribute.AuditSequence and then property name; otherwise no additional sorting is performed and properties; will be left in ordinal order.</param> /// <returns>A string containing each property name, friendly name and value, separated by the delimiter and optionally sorted by property name.</returns> public static String ClassToString <T>(T instance, String delimiter = GlobalConstants.DefaultDelimiter, SortByPropertyName sortByPropertyName = SortByPropertyName.Yes) { var sb = new StringBuilder(4096); var list = new List <SortablePropertyBasket>(); foreach (var propInfo in PclReflection.GetPropertiesInfo(instance)) { var auditAttributes = propInfo.GetCustomAttributes <AuditAttribute>(false); var auditAttributeList = new List <AuditAttribute>(auditAttributes); AuditAttribute auditAttribute = null; if (auditAttributeList.Count == 1) { auditAttribute = auditAttributeList[0]; } var auditSequence = 1; if (auditAttribute != null) { auditSequence = auditAttribute.AuditSequence; } if (propInfo.Name != "Item" && propInfo.Name != "ValidationErrors") { list.Add(propInfo.GetValue(instance, null) != null ? new SortablePropertyBasket(auditSequence, propInfo.Name, CamelCaseString.GetWords(propInfo.Name), propInfo.GetValue(instance, null).ToString()) : new SortablePropertyBasket(auditSequence, propInfo.Name, CamelCaseString.GetWords(propInfo.Name), Null)); } } if (list.Count > 0) { if (sortByPropertyName == SortByPropertyName.Yes) { list.Sort(); } foreach (var propertyBasket in list) { sb.Append(propertyBasket); sb.Append(delimiter); } if (sb.Length > delimiter.Length) { sb.Length -= delimiter.Length; } } else { sb.Append("Class has no properties"); } return(sb.ToString()); }
/// <summary> /// Builds up a string containing each property and value in the class decorated with the AuditAttribute. The string displays the property name, property friendly name and property value. /// </summary> /// <typeparam name="T">Class Type</typeparam> /// <param name="instance">Instance of the class</param> /// <param name="defaultValue">If no class properties are decorated with the <see cref="AuditAttribute"/> then a single entry will be added to the dictionary that is named 'DefaultValue' and will have the value of defaultValue.</param> /// <param name="delimiter">What delimiter do you want between each property? Defaults to comma; can pass others like Environment.NewLine, etc.</param> /// <param name="includeAllProperties">if set to <c>true</c> [include all attributes].</param> /// <returns>A string containing each property name, friendly name and value, separated by the delimiter and sorted by AuditAttribute.AuditSequence and then property name.</returns> public static String AuditToString <T>(T instance, String defaultValue, String delimiter = GlobalConstants.DefaultDelimiter, Boolean includeAllProperties = false) { var sb = new StringBuilder(2048); var list = new List <SortablePropertyBasket>(); var nonAttributedPropertyIndex = 0; foreach (var propInfo in PclReflection.GetPropertiesInfo(instance)) { var auditAttributes = propInfo.GetCustomAttributes <AuditAttribute>(false); var auditAttributeList = new List <AuditAttribute>(auditAttributes); AuditAttribute auditAttribute = null; if (auditAttributeList.Count == 1) { auditAttribute = auditAttributeList[0]; } if (auditAttribute != null) { list.Add(propInfo.GetValue(instance, null) != null ? new SortablePropertyBasket(auditAttribute.AuditSequence, propInfo.Name, CamelCaseString.GetWords(propInfo.Name), propInfo.GetValue(instance, null).ToString()) : new SortablePropertyBasket(auditAttribute.AuditSequence, propInfo.Name, CamelCaseString.GetWords(propInfo.Name), Null)); } else if (includeAllProperties && propInfo.Name != "Item") { nonAttributedPropertyIndex += 1; list.Add(propInfo.GetValue(instance, null) != null ? new SortablePropertyBasket(nonAttributedPropertyIndex, propInfo.Name, CamelCaseString.GetWords(propInfo.Name), propInfo.GetValue(instance, null).ToString()) : new SortablePropertyBasket(nonAttributedPropertyIndex, propInfo.Name, CamelCaseString.GetWords(propInfo.Name), Null)); } } if (list.Count > 0) { list.Sort(); foreach (SortablePropertyBasket propertyBasket in list) { sb.Append(propertyBasket); sb.Append(delimiter); } if (sb.Length > delimiter.Length) { sb.Length -= delimiter.Length; } } else { sb.Append(defaultValue); } return(sb.ToString()); }
/// <summary> /// Populates the dictionary with property's name and value in the class for properties decorated with the <see cref="AuditAttribute"/>. /// </summary> /// <typeparam name="T">Class type.</typeparam> /// <param name="instance">The instance.</param> /// <param name="defaultValue">If no class properties are decorated with the <see cref="AuditAttribute"/> then a single entry will be added to the dictionary that is named 'DefaultValue' and will have the value of defaultValue.</param>. /// <param name="dictionary">Pass an IDictionary object that needs to be populated. This could be the Data property of an exception object that you want to populate, etc.</param> /// <returns>IDictionary populated with properties and values.</returns> public static IDictionary <String, String> AuditToIDictionary <T>(T instance, String defaultValue, IDictionary <String, String> dictionary) { var list = new List <SortablePropertyBasket>(); foreach (var propInfo in PclReflection.GetPropertiesInfo(instance)) { var auditAttributes = propInfo.GetCustomAttributes <AuditAttribute>(false); var auditAttributeList = new List <AuditAttribute>(auditAttributes); AuditAttribute auditAttribute = null; if (auditAttributeList.Count == 1) { auditAttribute = auditAttributeList[0]; } if (auditAttribute != null) { list.Add(new SortablePropertyBasket(auditAttribute.AuditSequence, propInfo.Name, CamelCaseString.GetWords(propInfo.Name), propInfo.GetValue(instance, null).ToString())); } } if (list.Count > 0) { list.Sort(); foreach (var propertyBasket in list) { dictionary.Add(propertyBasket.PropertyName, propertyBasket.Value); } } else { dictionary.Add(DefaultValue, defaultValue); } return(dictionary); }