1 package autotest.common.table; 2 3 import autotest.common.Utils; 4 5 import com.google.gwt.json.client.JSONObject; 6 7 import java.util.List; 8 9 public interface DataSource { 10 public static enum SortDirection {ASCENDING, DESCENDING} 11 12 public static class SortSpec { 13 private String field; 14 private SortDirection direction; 15 SortSpec(String field, SortDirection direction)16 public SortSpec(String field, SortDirection direction) { 17 this.field = field; 18 this.direction = direction; 19 } 20 SortSpec(String field)21 public SortSpec(String field) { 22 this(field, SortDirection.ASCENDING); 23 } 24 getDirectionMultiplier()25 public int getDirectionMultiplier() { 26 return direction == SortDirection.ASCENDING ? 1 : -1; 27 } 28 getField()29 public String getField() { 30 return field; 31 } 32 getDirection()33 public SortDirection getDirection() { 34 return direction; 35 } 36 37 @Override toString()38 public String toString() { 39 String prefix = ""; 40 if (direction == SortDirection.DESCENDING) { 41 prefix = "-"; 42 } 43 return prefix + field; 44 } 45 fromString(String sortString)46 public static SortSpec fromString(String sortString) { 47 if (sortString.charAt(0) == '-') { 48 return new SortSpec(sortString.substring(1), SortDirection.DESCENDING); 49 } else { 50 return new SortSpec(sortString, SortDirection.ASCENDING); 51 } 52 } 53 } 54 55 public interface Query { getParams()56 public JSONObject getParams(); 57 58 /** 59 * Get the total number of results matching this query. After completion, 60 * callback.handleTotalResultCount() will be called with the count. 61 */ getTotalResultCount(final DataCallback callback)62 public void getTotalResultCount(final DataCallback callback); 63 64 /** 65 * Get a page of data. After completion, callback.handlePage() will be 66 * called with the data. 67 * @param start row to start with (for pagination) 68 * @param maxCount maximum rows to be returned 69 * @param sortOn list of columns + directions to sort on; results will be sorted by the 70 * first field, then the second, etc. 71 */ getPage(Integer start, Integer maxCount, SortSpec[] sortOn, final DataCallback callback)72 public void getPage(Integer start, Integer maxCount, SortSpec[] sortOn, 73 final DataCallback callback); 74 } 75 76 abstract class DefaultQuery implements Query { 77 protected JSONObject params; 78 DefaultQuery(JSONObject params)79 public DefaultQuery(JSONObject params) { 80 if (params == null) { 81 this.params = new JSONObject(); 82 } else { 83 this.params = Utils.copyJSONObject(params); 84 } 85 } 86 87 @Override getParams()88 public JSONObject getParams() { 89 return Utils.copyJSONObject(params); 90 } 91 92 @Override getPage(Integer start, Integer maxCount, SortSpec[] sortOn, DataCallback callback)93 public abstract void getPage(Integer start, Integer maxCount, SortSpec[] sortOn, 94 DataCallback callback); 95 96 @Override getTotalResultCount(DataCallback callback)97 public abstract void getTotalResultCount(DataCallback callback); 98 } 99 100 public interface DataCallback { onQueryReady(Query query)101 public void onQueryReady(Query query); handlePage(List<JSONObject> data)102 public void handlePage(List<JSONObject> data); handleTotalResultCount(int totalCount)103 public void handleTotalResultCount(int totalCount); onError(JSONObject errorObject)104 public void onError(JSONObject errorObject); 105 } 106 107 public static class DefaultDataCallback implements DataCallback { handlePage(List<JSONObject> data)108 public void handlePage(List<JSONObject> data) {} handleTotalResultCount(int totalCount)109 public void handleTotalResultCount(int totalCount) {} onQueryReady(Query query)110 public void onQueryReady(Query query) {} onError(JSONObject errorObject)111 public void onError(JSONObject errorObject) {} 112 } 113 query(JSONObject params, final DataCallback callback)114 public void query(JSONObject params, final DataCallback callback); 115 } 116