TkoUtils.java revision d50ffb4b0ef514fb969d53b82e23ab41d4d3812e
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;
13import com.google.gwt.user.client.DOM;
14
15import java.util.ArrayList;
16import java.util.List;
17
18public class TkoUtils {
19    private static StaticDataRepository staticData = StaticDataRepository.getRepository();
20    private static JsonRpcProxy rpcProxy = JsonRpcProxy.getProxy();
21
22    public static class FieldInfo {
23        public String field;
24        public String name;
25
26        public FieldInfo(String field, String name) {
27            this.field = field;
28            this.name = name;
29        }
30    }
31
32    public static List<FieldInfo> getFieldList(String listName) {
33        JSONArray fieldArray = staticData.getData(listName).isArray();
34        List<FieldInfo> fields = new ArrayList<FieldInfo>();
35        for (JSONArray fieldTuple : new JSONArrayList<JSONArray>(fieldArray)) {
36            String fieldName = fieldTuple.get(0).isString().stringValue();
37            String field = fieldTuple.get(1).isString().stringValue();
38            fields.add(new FieldInfo(field, fieldName));
39        }
40        return fields;
41    }
42
43    protected static JSONObject getConditionParams(String condition) {
44        JSONObject params = new JSONObject();
45        params.put("extra_where", new JSONString(condition));
46        return params;
47    }
48
49    protected static void getTestId(TestSet test, final TestSelectionListener listener) {
50        rpcProxy.rpcCall("get_test_views", getConditionParams(test.getCondition()),
51                         new JsonRpcCallback() {
52            @Override
53            public void onSuccess(JSONValue result) {
54                // just take the first result (there could be more than one due to
55                // a rare and harmless race condition)
56                JSONObject testView = result.isArray().get(0).isObject();
57                int testId = (int) testView.get("test_idx").isNumber().doubleValue();
58                listener.onSelectTest(testId);
59            }
60        });
61    }
62
63    protected static void clearDomChildren(Element elem) {
64        Element child = elem.getFirstChildElement();
65        while (child != null) {
66            Element nextChild = child.getNextSiblingElement();
67            elem.removeChild(child);
68            child = nextChild;
69        }
70    }
71
72    static void setElementVisible(String elementId, boolean visible) {
73        DOM.getElementById(elementId).getStyle().setProperty("display", visible ? "" : "none");
74    }
75
76    static String getSqlCondition(JSONObject args) {
77        final JSONValue condition = args.get("extra_where");
78        if (condition == null) {
79            return "";
80        }
81        return condition.isString().stringValue();
82    }
83}
84