You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

LuDbo.java 9.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. package com.luticate.utils.dbo;
  2. import com.luticate.utils.dbo.JSONContainer.LuAbstractJsonContainer;
  3. import com.luticate.utils.dbo.JSONContainer.LuJSONArrayContainer;
  4. import com.luticate.utils.dbo.JSONContainer.LuJSONObjectContainer;
  5. import org.joda.time.DateTime;
  6. import org.joda.time.LocalDate;
  7. import org.joda.time.LocalDateTime;
  8. import org.joda.time.format.DateTimeFormat;
  9. import org.json.JSONArray;
  10. import org.json.JSONException;
  11. import org.json.JSONObject;
  12. import java.lang.reflect.Array;
  13. import java.lang.reflect.Field;
  14. import java.lang.reflect.Modifier;
  15. import java.lang.reflect.ParameterizedType;
  16. import java.util.AbstractMap;
  17. import java.util.HashMap;
  18. import java.util.Iterator;
  19. import java.util.List;
  20. import java.util.Vector;
  21. /**
  22. * Created by robin on 11/27/15.
  23. */
  24. public abstract class LuDbo {
  25. public static String DEFAULT_DATE_FORMAT = "yy-MM-dd";
  26. public static String DEFAULT_TIME_FORMAT = "HH:mm:ss";
  27. public static String DEFAULT_DATE_TIME_FORMAT = DEFAULT_DATE_FORMAT + " " + DEFAULT_TIME_FORMAT;
  28. public String getFieldFromJson(String jsonName)
  29. {
  30. if (!jsonName.startsWith("_")) {
  31. return String.format("_%s", jsonName);
  32. }
  33. return jsonName;
  34. }
  35. public String getJsonFromField(String fieldName)
  36. {
  37. if (fieldName.startsWith("_")) {
  38. return fieldName.substring(1);
  39. }
  40. return fieldName;
  41. }
  42. public Object getObjectFromJson(Class clazz, LuAbstractJsonContainer json, Object key) throws Exception {
  43. if (json.isNull(key)) {
  44. return null;
  45. }
  46. if (clazz == Byte.class || clazz == byte.class) {
  47. return (byte) json.getInt(key);
  48. }
  49. if (clazz == Character.class || clazz == char.class) {
  50. return json.getString(key).charAt(0);
  51. }
  52. if (clazz == Short.class || clazz == short.class) {
  53. return (short) json.getInt(key);
  54. }
  55. if (clazz == Integer.class || clazz == int.class) {
  56. return json.getInt(key);
  57. }
  58. if (clazz == Long.class || clazz == long.class) {
  59. return json.getLong(key);
  60. }
  61. if (clazz == Float.class || clazz == float.class) {
  62. return (float) json.getDouble(key);
  63. }
  64. if (clazz == Double.class || clazz == double.class) {
  65. return json.getDouble(key);
  66. }
  67. if (clazz == Boolean.class || clazz == boolean.class) {
  68. return json.getBoolean(key);
  69. }
  70. if (clazz == String.class) {
  71. return json.getString(key);
  72. }
  73. if (clazz == LocalDateTime.class) {
  74. return LocalDateTime.parse(json.getString(key), DateTimeFormat.forPattern(DEFAULT_DATE_TIME_FORMAT));
  75. }
  76. if (clazz == DateTime.class) {
  77. return DateTime.parse(json.getString(key), DateTimeFormat.forPattern(DEFAULT_DATE_TIME_FORMAT));
  78. }
  79. if (LuDbo.class.isAssignableFrom(clazz)) {
  80. LuDbo dbo = (LuDbo) getInstance(clazz);
  81. dbo.fromJson(json.getJSONObject(key));
  82. return dbo;
  83. }
  84. return null;
  85. }
  86. public Object getJsonFromObject(Class clazz, Object obj)
  87. {
  88. if (obj == null) {
  89. return null;
  90. }
  91. if (clazz == char.class || clazz == Character.class) {
  92. return obj.toString();
  93. }
  94. if (clazz.isPrimitive() || clazz == Byte.class || clazz == Character.class
  95. || clazz == Short.class || clazz == Integer.class || clazz == Long.class
  96. || clazz == Float.class || clazz == Double.class || clazz == Boolean.class
  97. || clazz == String.class) {
  98. return obj;
  99. }
  100. if (clazz == LocalDateTime.class) {
  101. return ((LocalDateTime)obj).toString(DateTimeFormat.forPattern(DEFAULT_DATE_TIME_FORMAT));
  102. }
  103. if (clazz == DateTime.class) {
  104. return ((DateTime)obj).toString(DateTimeFormat.forPattern(DEFAULT_DATE_TIME_FORMAT));
  105. }
  106. if (LuDbo.class.isAssignableFrom(clazz)) {
  107. return ((LuDbo)obj).toJson();
  108. }
  109. if (List.class.isAssignableFrom(clazz)) {
  110. List objList = (List) obj;
  111. JSONArray list = new JSONArray();
  112. for (Object item : objList) {
  113. list.put(getJsonFromObject(getClazz(item), item));
  114. }
  115. return list;
  116. }
  117. if (AbstractMap.class.isAssignableFrom(clazz)) {
  118. HashMap objMap = (HashMap) obj;
  119. JSONObject map = new JSONObject();
  120. for (Object k : objMap.keySet()) {
  121. if (k.getClass() == String.class) {
  122. Object item = objMap.get(k);
  123. try {
  124. map.put((String) k, getJsonFromObject(getClazz(item), item));
  125. } catch (JSONException ignored) {
  126. }
  127. }
  128. }
  129. return map;
  130. }
  131. if (clazz.isArray()) {
  132. JSONArray list = new JSONArray();
  133. for (int i = 0; i < Array.getLength(obj); ++i) {
  134. Object item = Array.get(obj, i);
  135. list.put(getJsonFromObject(item == null ? null : item.getClass(), item));
  136. }
  137. return list;
  138. }
  139. return null;
  140. }
  141. public Object getInstance(Class clazz) throws Exception {
  142. if (clazz == List.class) {
  143. return new Vector<>();
  144. }
  145. if (clazz == AbstractMap.class) {
  146. return new HashMap<>();
  147. }
  148. return clazz.newInstance();
  149. }
  150. public Class getClazz(Object obj)
  151. {
  152. return obj == null ? null : obj.getClass();
  153. }
  154. public void fromJson(String data) throws Exception
  155. {
  156. JSONObject json = new JSONObject(data);
  157. fromJson(json);
  158. }
  159. public void fromJson(JSONObject json) throws Exception
  160. {
  161. for (Field field : getClass().getDeclaredFields()) {
  162. if (Modifier.isTransient(field.getModifiers())) {
  163. continue;
  164. }
  165. String key = getJsonFromField(field.getName());
  166. Class clazz = field.getType();
  167. if (json.has(key)) {
  168. Object value = null;
  169. if (List.class.isAssignableFrom(clazz)) {
  170. ParameterizedType parameterizedType = (ParameterizedType) field.getGenericType();
  171. Class type = (Class) parameterizedType.getActualTypeArguments()[0];
  172. List<Object> list = (List<Object>) getInstance(clazz);
  173. JSONArray jsonArray = json.getJSONArray(key);
  174. for (int i = 0; i < jsonArray.length(); ++i) {
  175. list.add(getObjectFromJson(type, new LuJSONArrayContainer(jsonArray), i));
  176. }
  177. value = list;
  178. }
  179. else if (clazz.isArray()) {
  180. Class type = clazz.getComponentType();
  181. JSONArray jsonArray = json.getJSONArray(key);
  182. Object array = Array.newInstance(type, jsonArray.length());
  183. for (int i = 0; i < jsonArray.length(); ++i) {
  184. Array.set(array, i, getObjectFromJson(type, new LuJSONArrayContainer(jsonArray), i));
  185. }
  186. value = array;
  187. }
  188. else if (AbstractMap.class.isAssignableFrom(clazz)) {
  189. ParameterizedType parameterizedType = (ParameterizedType) field.getGenericType();
  190. Class type = (Class) parameterizedType.getActualTypeArguments()[0];
  191. if (type == String.class) {
  192. type = (Class) parameterizedType.getActualTypeArguments()[1];
  193. AbstractMap<String, Object> map = (AbstractMap<String, Object>) getInstance(clazz);
  194. JSONObject obj = json.getJSONObject(key);
  195. Iterator<String> it = obj.keys();
  196. while (it.hasNext()) {
  197. String k = it.next();
  198. map.put(k, getObjectFromJson(type, new LuJSONObjectContainer(obj), k));
  199. }
  200. value = map;
  201. }
  202. }
  203. else {
  204. value = getObjectFromJson(clazz, new LuJSONObjectContainer(json), key);
  205. }
  206. field.setAccessible(true);
  207. field.set(this, value);
  208. field.setAccessible(false);
  209. }
  210. }
  211. }
  212. public HashMap<String, Object> toArray()
  213. {
  214. HashMap<String, Object> map = new HashMap<>();
  215. for (Field field : getClass().getDeclaredFields()) {
  216. if (Modifier.isTransient(field.getModifiers())) {
  217. continue;
  218. }
  219. String key = getJsonFromField(field.getName());
  220. Class clazz = field.getType();
  221. field.setAccessible(true);
  222. Object obj = null;
  223. try {
  224. obj = field.get(this);
  225. } catch (IllegalAccessException ignored) {
  226. }
  227. field.setAccessible(false);
  228. map.put(key, getJsonFromObject(clazz, obj));
  229. }
  230. return map;
  231. }
  232. public JSONObject toJson()
  233. {
  234. HashMap<String, Object> array = toArray();
  235. JSONObject json = new JSONObject(array);
  236. return json;
  237. }
  238. @Override
  239. public String toString()
  240. {
  241. return toJson().toString();
  242. }
  243. }