TkoUtils.java revision 35444864c7b6f49865a7e17aa0052987b72e4728
1package autotest.tko;
2
3import autotest.common.JSONArrayList;
4import autotest.common.JsonRpcCallback;
5import autotest.common.JsonRpcProxy;
6import autotest.common.StaticDataRepository;
7
8import com.google.gwt.dom.client.Element;
9import com.google.gwt.json.client.JSONArray;
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.ArrayList;
15import java.util.List;
16
17public class TkoUtils {
18    private static StaticDataRepository staticData = StaticDataRepository.getRepository();
19    private static JsonRpcProxy rpcProxy = JsonRpcProxy.getProxy();
20
21    public static class FieldInfo {
22        public String field;
23        public String name;
24
25        public FieldInfo(String field, String name) {
26            this.field = field;
27            this.name = name;
28        }
29    }
30
31    public static List<FieldInfo> getFieldList(String listName) {
32        JSONArray fieldArray = staticData.getData(listName).isArray();
33        List<FieldInfo> fields = new ArrayList<FieldInfo>();
34        for (JSONArray fieldTuple : new JSONArrayList<JSONArray>(fieldArray)) {
35            String fieldName = fieldTuple.get(0).isString().stringValue();
36            String field = fieldTuple.get(1).isString().stringValue();
37            fields.add(new FieldInfo(field, fieldName));
38        }
39        return fields;
40    }
41
42    protected static JSONObject getConditionParams(String condition) {
43        JSONObject params = new JSONObject();
44        params.put("extra_where", new JSONString(condition));
45        return params;
46    }
47
48    protected static void getTestId(TestSet test, final TestSelectionListener listener) {
49        rpcProxy.rpcCall("get_test_views", getConditionParams(test.getCondition()),
50                         new JsonRpcCallback() {
51            @Override
52            public void onSuccess(JSONValue result) {
53                // just take the first result (there could be more than one due to
54                // a rare and harmless race condition)
55                JSONObject testView = result.isArray().get(0).isObject();
56                int testId = (int) testView.get("test_idx").isNumber().doubleValue();
57                listener.onSelectTest(testId);
58            }
59        });
60    }
61
62    protected static void clearDomChildren(Element elem) {
63        Element child = elem.getFirstChildElement();
64        while (child != null) {
65            Element nextChild = child.getNextSiblingElement();
66            elem.removeChild(child);
67            child = nextChild;
68        }
69    }
70}
71