/BoilingPoint.Web/Helpers/ParseHelper.cs

https://bitbucket.org/aermakov/angryoldlady · C# · 138 lines · 105 code · 25 blank · 8 comment · 19 complexity · b5441dbabd9c90a334739dc3f30b3aba MD5 · raw file

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Reflection;
  5. using BoilingPoint.Portable.Attributes;
  6. using BoilingPoint.Portable.Data;
  7. using BoilingPoint.Portable.Geo;
  8. using Parse;
  9. namespace BoilingPoint.Web.Helpers
  10. {
  11. public static class ParseHelper
  12. {
  13. public static T GetObject<T>(ParseObject parseObject) where T : BaseEntity
  14. {
  15. if (parseObject != null)
  16. {
  17. Type type = typeof (T);
  18. var t = (T) Activator.CreateInstance(type);
  19. t.objectId = parseObject.ObjectId;
  20. t.updatedAt = parseObject.UpdatedAt;
  21. t.createdAt = parseObject.CreatedAt;
  22. UpdateObject(t, parseObject);
  23. return t;
  24. }
  25. return default(T);
  26. }
  27. public static ParseObject GetParseObject<T>(T data) where T : BaseEntity
  28. {
  29. Type type = typeof (T);
  30. string className = type.Name;
  31. var parseObject = new ParseObject(className) {ObjectId = data.objectId};
  32. UpdateParseObject(parseObject, data);
  33. return parseObject;
  34. }
  35. private static void UpdateObject<T>(T t, IEnumerable<KeyValuePair<string, object>> parseObject)
  36. {
  37. Type type = typeof (T);
  38. PropertyInfo[] properties = type.GetProperties();
  39. foreach (var pair in parseObject)
  40. {
  41. PropertyInfo property = properties.SingleOrDefault(x => x.Name == pair.Key);
  42. if (property == null || property.GetSetMethod() == null)
  43. continue;
  44. object value = pair.Value;
  45. if (value != null && property.PropertyType.IsEnum)
  46. {
  47. value = Enum.Parse(property.PropertyType, value.ToString());
  48. }
  49. if (value is ParseGeoPoint)
  50. {
  51. value = new GeoPoint(((ParseGeoPoint) value).Latitude, ((ParseGeoPoint) value).Longitude);
  52. }
  53. SetValue(t, property.Name, value);
  54. }
  55. }
  56. public static void SetValue(object inputObject, string propertyName, object propertyVal)
  57. {
  58. // Find out the type
  59. Type type = inputObject.GetType();
  60. // Get the property information based on the type
  61. PropertyInfo propertyInfo = type.GetProperty(propertyName);
  62. // Find the property type
  63. Type propertyType = propertyInfo.PropertyType;
  64. // Convert.ChangeType does not handle conversion to nullable types
  65. // if the property type is nullable, we need to get the underlying type of the property
  66. Type targetType = IsNullableType(propertyType) ? Nullable.GetUnderlyingType(propertyType) : propertyType;
  67. if (propertyVal is List<object>)
  68. {
  69. var list = (List<object>) propertyVal;
  70. object target = Activator.CreateInstance(targetType);
  71. for (int i = 0; i < list.Count; i++)
  72. {
  73. object value = list[i];
  74. value = Convert.ChangeType(value, targetType.GenericTypeArguments[0]);
  75. propertyType.GetMethod("Add").Invoke(target, new[] {value});
  76. }
  77. SetValue(inputObject, propertyName, target);
  78. }
  79. else
  80. {
  81. // Returns an System.Object with the specified System.Type and whose value is
  82. // equivalent to the specified object.
  83. propertyVal = Convert.ChangeType(propertyVal, targetType);
  84. // Set the value of the property
  85. propertyInfo.SetValue(inputObject, propertyVal, null);
  86. }
  87. }
  88. private static bool IsNullableType(Type type)
  89. {
  90. return type.IsGenericType && type.GetGenericTypeDefinition() == typeof (Nullable<>);
  91. }
  92. public static void UpdateParseObject<T>(ParseObject parseObject, T data)
  93. {
  94. Type type = typeof (T);
  95. PropertyInfo[] properties = type.GetProperties();
  96. foreach (PropertyInfo property in properties)
  97. {
  98. var attribute = property.GetCustomAttribute<IgnoreAttribute>();
  99. if (attribute != null)
  100. continue;
  101. object value = property.GetValue(data);
  102. if (value is GeoPoint)
  103. {
  104. var geoPoint = (GeoPoint) value;
  105. value = new ParseGeoPoint(geoPoint.Latitude, geoPoint.Longitude);
  106. }
  107. if (property.PropertyType.IsEnum)
  108. value = value.ToString();
  109. parseObject[property.Name] = value;
  110. }
  111. }
  112. }
  113. }