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.1KB

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