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.

LuPaginatedDbo.java 1.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. package com.luticate.utils.dbo;
  2. import org.json.JSONArray;
  3. import org.json.JSONException;
  4. import org.json.JSONObject;
  5. import java.lang.reflect.ParameterizedType;
  6. import java.util.HashMap;
  7. import java.util.List;
  8. import java.util.Vector;
  9. /**
  10. *
  11. * Created by robin on 11/27/15.
  12. */
  13. public class LuPaginatedDbo<T extends LuDbo> extends LuDbo {
  14. public List<T> getData() {
  15. return _data;
  16. }
  17. public void setData(List<T> data) {
  18. _data = data;
  19. }
  20. public int getCount() {
  21. return _count;
  22. }
  23. public void setCount(int count) {
  24. _count = count;
  25. }
  26. private List<T> _data = new Vector<>();
  27. private int _count;
  28. @Override
  29. public void fromJson(JSONObject json) throws JSONException {
  30. _count = json.getInt("count");
  31. JSONArray array = json.getJSONArray("data");
  32. for (int i = 0; i < array.length(); ++i) {
  33. try {
  34. T dbo = (T)((Class)((ParameterizedType)this.getClass().
  35. getGenericSuperclass()).getActualTypeArguments()[0]).newInstance();
  36. dbo.fromJson(array.getJSONObject(i));
  37. _data.add(dbo);
  38. } catch (Exception e) {
  39. e.printStackTrace();
  40. }
  41. }
  42. }
  43. @Override
  44. public HashMap<String, Object> toArray() {
  45. HashMap<String, Object> array = new HashMap<>();
  46. array.put("count", _count);
  47. Vector<HashMap<String, Object>> data = new Vector<>();
  48. for (T value : _data) {
  49. data.add(value.toArray());
  50. }
  51. array.put("data", data);
  52. return array;
  53. }
  54. }