TkoUtils.java revision 3af93f9e93f65dad8d8e5b808d67320c2da958d5
1package autotest.tko;
2
3import autotest.common.JSONArrayList;
4import autotest.common.StaticDataRepository;
5import autotest.common.Utils;
6
7import com.google.gwt.dom.client.Element;
8import com.google.gwt.json.client.JSONArray;
9import com.google.gwt.json.client.JSONObject;
10import com.google.gwt.json.client.JSONString;
11import com.google.gwt.json.client.JSONValue;
12import com.google.gwt.user.client.DOM;
13import com.google.gwt.user.client.ui.FlexTable;
14import com.google.gwt.user.client.ui.Widget;
15
16import java.util.ArrayList;
17import java.util.Arrays;
18import java.util.List;
19
20public class TkoUtils {
21    private static StaticDataRepository staticData = StaticDataRepository.getRepository();
22    public static final ClassFactory factory = new SiteClassFactory();
23
24    public static class FieldInfo {
25        public String field;
26        public String name;
27
28        public FieldInfo(String field, String name) {
29            this.field = field;
30            this.name = name;
31        }
32    }
33
34    public static List<FieldInfo> getFieldList(String listName) {
35        JSONArray fieldArray = staticData.getData(listName).isArray();
36        List<FieldInfo> fields = new ArrayList<FieldInfo>();
37        for (JSONArray fieldTuple : new JSONArrayList<JSONArray>(fieldArray)) {
38            String fieldName = fieldTuple.get(0).isString().stringValue();
39            String field = fieldTuple.get(1).isString().stringValue();
40            fields.add(new FieldInfo(field, fieldName));
41        }
42        return fields;
43    }
44
45    protected static JSONObject getConditionParams(String condition) {
46        JSONObject params = new JSONObject();
47        params.put("extra_where", new JSONString(condition));
48        return params;
49    }
50
51    protected static void clearDomChildren(Element elem) {
52        Element child = elem.getFirstChildElement();
53        while (child != null) {
54            Element nextChild = child.getNextSiblingElement();
55            elem.removeChild(child);
56            child = nextChild;
57        }
58    }
59
60    static void setElementVisible(String elementId, boolean visible) {
61        DOM.getElementById(elementId).getStyle().setProperty("display", visible ? "" : "none");
62    }
63
64    static String getSqlCondition(JSONObject args) {
65        final JSONValue condition = args.get("extra_where");
66        if (condition == null) {
67            return "";
68        }
69        return condition.isString().stringValue();
70    }
71
72    static String wrapWithParens(String string) {
73        if (string.equals("")) {
74            return string;
75        }
76        return "(" + string + ")";
77    }
78
79    static String joinWithParens(String joiner, String first, String second) {
80        first = wrapWithParens(first);
81        second = wrapWithParens(second);
82        return Utils.joinStrings(joiner, Arrays.asList(new String[] {first, second}));
83    }
84
85    static String escapeSqlValue(String value) {
86        return value.replace("\\", "\\\\").replace("'", "\\'");
87    }
88
89    static int addControlRow(FlexTable table, String text, Widget control) {
90        int row = table.getRowCount();
91        table.setText(row, 0, text);
92        table.getFlexCellFormatter().setStylePrimaryName(row, 0, "field-name");
93        table.setWidget(row, 1, control);
94        return row;
95    }
96}
97