1package autotest.common.table;
2
3import autotest.common.JSONArrayList;
4import autotest.common.JsonRpcCallback;
5import autotest.common.JsonRpcProxy;
6import autotest.common.Utils;
7
8import com.google.gwt.json.client.JSONArray;
9import com.google.gwt.json.client.JSONNumber;
10import com.google.gwt.json.client.JSONObject;
11import com.google.gwt.json.client.JSONString;
12import com.google.gwt.json.client.JSONValue;
13
14import java.util.List;
15
16/**
17 * Data source that retrieves results via RPC requests to the server.
18 */
19public class RpcDataSource implements DataSource {
20    private class RpcQuery extends DefaultQuery {
21        public RpcQuery(JSONObject params) {
22            super(params);
23        }
24
25        @Override
26        public void getPage(Integer start, Integer maxCount, SortSpec[] sortOn,
27                            final DataCallback callback) {
28            JSONObject pageParams = Utils.copyJSONObject(params);
29            if (start != null) {
30                pageParams.put("query_start", new JSONNumber(start.intValue()));
31            }
32            if (maxCount != null) {
33                pageParams.put("query_limit", new JSONNumber(maxCount.intValue()));
34            }
35            if (sortOn != null) {
36                JSONArray sortList = new JSONArray();
37                for (SortSpec sortSpec : sortOn) {
38                    sortList.set(sortList.size(), new JSONString(sortSpec.toString()));
39                }
40                pageParams.put("sort_by", sortList);
41            }
42            if (dataMethodName == "get_hosts")
43                pageParams.put("include_current_job", new JSONString("true"));
44            JsonRpcProxy.getProxy().rpcCall(dataMethodName, pageParams,
45                                            new JsonRpcCallback() {
46                @Override
47                public void onSuccess(JSONValue result) {
48                    List<JSONObject> resultData = handleJsonResult(result);
49                    callback.handlePage(resultData);
50                }
51
52                @Override
53                public void onError(JSONObject errorObject) {
54                    super.onError(errorObject);
55                    callback.onError(errorObject);
56                }
57            });
58        }
59
60        @Override
61        public void getTotalResultCount(final DataCallback callback) {
62            JsonRpcProxy.getProxy().rpcCall(countMethodName, params,
63                                            new JsonRpcCallback() {
64                @Override
65                public void onSuccess(JSONValue result) {
66                    int count = (int) result.isNumber().doubleValue();
67                    callback.handleTotalResultCount(count);
68                }
69
70                @Override
71                public void onError(JSONObject errorObject) {
72                    super.onError(errorObject);
73                    callback.onError(errorObject);
74                }
75            });
76        }
77    }
78
79    private String dataMethodName, countMethodName;
80
81    public RpcDataSource(String dataMethodName, String countMethodName) {
82        this.dataMethodName = dataMethodName;
83        this.countMethodName = countMethodName;
84    }
85
86    /**
87     * Process the JSON result returned by the server into an list of result
88     * objects.  This default implementation assumes the result itself is an array.
89     * Subclasses may override this to construct a list from the JSON result in a customized way.
90     */
91    protected List<JSONObject> handleJsonResult(JSONValue result) {
92        return new JSONArrayList<JSONObject>(result.isArray());
93    }
94
95    @Override
96    public void query(JSONObject params, DataCallback callback) {
97        callback.onQueryReady(new RpcQuery(params));
98    }
99
100    public String getDataMethodName() {
101        return dataMethodName;
102    }
103}
104