1 package autotest.tko; 2 3 import autotest.common.JSONArrayList; 4 import autotest.common.Utils; 5 import autotest.common.table.RpcDataSource; 6 7 import com.google.gwt.json.client.JSONArray; 8 import com.google.gwt.json.client.JSONObject; 9 import com.google.gwt.json.client.JSONString; 10 import com.google.gwt.json.client.JSONValue; 11 12 import java.util.ArrayList; 13 import java.util.Arrays; 14 import java.util.List; 15 16 public class TestGroupDataSource extends RpcDataSource { 17 private static final String NUM_GROUPS_RPC = "get_num_groups"; 18 private static final String GROUP_COUNTS_RPC = "get_group_counts"; 19 private static final String STATUS_COUNTS_RPC = "get_status_counts"; 20 private static final String LATEST_TESTS_RPC = "get_latest_tests"; 21 public static final String GROUP_COUNT_FIELD = "group_count"; 22 public static final String PASS_COUNT_FIELD = "pass_count"; 23 public static final String COMPLETE_COUNT_FIELD = "complete_count"; 24 public static final String INCOMPLETE_COUNT_FIELD = "incomplete_count"; 25 26 private JSONArray groupByFields; 27 private JSONArray headerGroups; 28 private JSONArray headerGroupValues; 29 private JSONObject queryParameters; 30 getTestGroupDataSource()31 public static TestGroupDataSource getTestGroupDataSource() { 32 return new TestGroupDataSource(GROUP_COUNTS_RPC); 33 } 34 getStatusCountDataSource()35 public static TestGroupDataSource getStatusCountDataSource() { 36 return new TestGroupDataSource(STATUS_COUNTS_RPC); 37 } 38 getLatestTestsDataSource()39 public static TestGroupDataSource getLatestTestsDataSource() { 40 return new TestGroupDataSource(LATEST_TESTS_RPC); 41 } 42 43 // force construction to go through above factory methods TestGroupDataSource(String dataMethodName)44 private TestGroupDataSource(String dataMethodName) { 45 super(dataMethodName, NUM_GROUPS_RPC); 46 } 47 48 @Override query(JSONObject params, DataCallback callback)49 public void query(JSONObject params, DataCallback callback) { 50 super.query(getFullRequestParams(params), callback); 51 } 52 getFullRequestParams(JSONObject conditionParams)53 public JSONObject getFullRequestParams(JSONObject conditionParams) { 54 JSONObject fullParams = Utils.copyJSONObject(conditionParams); 55 Utils.updateObject(fullParams, queryParameters); 56 fullParams.put("group_by", groupByFields); 57 if (headerGroups != null) { 58 fullParams.put("header_groups", headerGroups); 59 } 60 return fullParams; 61 } 62 63 @Override 64 /** 65 * Process the groups, which come simply as lists, into JSONObjects. 66 */ handleJsonResult(JSONValue result)67 protected List<JSONObject> handleJsonResult(JSONValue result) { 68 JSONObject resultObject = result.isObject(); 69 headerGroupValues = resultObject.get("header_values").isArray(); 70 return new JSONArrayList<JSONObject>(resultObject.get("groups").isArray()); 71 } 72 setGroupColumns(String[] columns)73 public void setGroupColumns(String[] columns) { 74 groupByFields = new JSONArray(); 75 for (String field : columns) { 76 groupByFields.set(groupByFields.size(), new JSONString(field)); 77 } 78 headerGroups = null; 79 } 80 setHeaderGroups(List<List<String>> headerFieldGroups)81 public void setHeaderGroups(List<List<String>> headerFieldGroups) { 82 groupByFields = new JSONArray(); 83 headerGroups = new JSONArray(); 84 for (List<String> headerGroup : headerFieldGroups) { 85 headerGroups.set(headerGroups.size(), Utils.stringsToJSON(headerGroup)); 86 for(String field : headerGroup) { 87 groupByFields.set(groupByFields.size(), new JSONString(field)); 88 } 89 } 90 } 91 92 /** 93 * Get a list of values for the header group with the given index, as specified to 94 * setHeaderGroups(). 95 */ getHeaderGroupValues(int groupIndex)96 public List<List<String>> getHeaderGroupValues(int groupIndex) { 97 JSONArray headerList = headerGroupValues.get(groupIndex).isArray(); 98 List<List<String>> headerValueList = new ArrayList<List<String>>(); 99 for (JSONArray header : new JSONArrayList<JSONArray>(headerList)) { 100 headerValueList.add(Arrays.asList(Utils.JSONtoStrings(header))); 101 } 102 return headerValueList; 103 } 104 setQueryParameters(JSONObject queryParameters)105 public void setQueryParameters(JSONObject queryParameters) { 106 this.queryParameters = queryParameters; 107 } 108 } 109