Home | History | Annotate | Download | only in table
      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 
     16         public SortSpec(String field, SortDirection direction) {
     17             this.field = field;
     18             this.direction = direction;
     19         }
     20 
     21         public SortSpec(String field) {
     22             this(field, SortDirection.ASCENDING);
     23         }
     24 
     25         public int getDirectionMultiplier() {
     26             return direction == SortDirection.ASCENDING ? 1 : -1;
     27         }
     28 
     29         public String getField() {
     30             return field;
     31         }
     32 
     33         public SortDirection getDirection() {
     34             return direction;
     35         }
     36 
     37         @Override
     38         public String toString() {
     39             String prefix = "";
     40             if (direction == SortDirection.DESCENDING) {
     41                 prefix = "-";
     42             }
     43             return prefix + field;
     44         }
     45 
     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 {
     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          */
     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          */
     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 
     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
     88         public JSONObject getParams() {
     89             return Utils.copyJSONObject(params);
     90         }
     91 
     92         @Override
     93         public abstract void getPage(Integer start, Integer maxCount, SortSpec[] sortOn,
     94                                      DataCallback callback);
     95 
     96         @Override
     97         public abstract void getTotalResultCount(DataCallback callback);
     98     }
     99 
    100     public interface DataCallback {
    101         public void onQueryReady(Query query);
    102         public void handlePage(List<JSONObject> data);
    103         public void handleTotalResultCount(int totalCount);
    104         public void onError(JSONObject errorObject);
    105     }
    106 
    107     public static class DefaultDataCallback implements DataCallback {
    108         public void handlePage(List<JSONObject> data) {}
    109         public void handleTotalResultCount(int totalCount) {}
    110         public void onQueryReady(Query query) {}
    111         public void onError(JSONObject errorObject) {}
    112     }
    113 
    114     public void query(JSONObject params, final DataCallback callback);
    115 }
    116