1 package autotest.tko;
2 
3 import autotest.common.JSONArrayList;
4 import autotest.common.JsonRpcProxy;
5 import autotest.common.StaticDataRepository;
6 import autotest.common.Utils;
7 import autotest.common.table.RpcDataSource;
8 import autotest.common.table.DataSource.Query;
9 
10 import com.google.gwt.http.client.URL;
11 import com.google.gwt.json.client.JSONArray;
12 import com.google.gwt.json.client.JSONObject;
13 import com.google.gwt.json.client.JSONString;
14 import com.google.gwt.json.client.JSONValue;
15 import com.google.gwt.user.client.DOM;
16 import com.google.gwt.user.client.ui.FlexTable;
17 import com.google.gwt.user.client.ui.Widget;
18 
19 import java.util.ArrayList;
20 import java.util.Arrays;
21 import java.util.List;
22 
23 public class TkoUtils {
24     private static StaticDataRepository staticData = StaticDataRepository.getRepository();
25     public static final ClassFactory factory = new SiteClassFactory();
26 
27     public static class FieldInfo {
28         public String field;
29         public String name;
30 
FieldInfo(String field, String name)31         public FieldInfo(String field, String name) {
32             this.field = field;
33             this.name = name;
34         }
35     }
36 
getFieldList(String listName)37     public static List<FieldInfo> getFieldList(String listName) {
38         JSONArray fieldArray = staticData.getData(listName).isArray();
39         List<FieldInfo> fields = new ArrayList<FieldInfo>();
40         for (JSONArray fieldTuple : new JSONArrayList<JSONArray>(fieldArray)) {
41             String fieldName = fieldTuple.get(0).isString().stringValue();
42             String field = fieldTuple.get(1).isString().stringValue();
43             fields.add(new FieldInfo(field, fieldName));
44         }
45         return fields;
46     }
47 
getConditionParams(String condition)48     protected static JSONObject getConditionParams(String condition) {
49         JSONObject params = new JSONObject();
50         params.put("extra_where", new JSONString(condition));
51         return params;
52     }
53 
setElementVisible(String elementId, boolean visible)54     static void setElementVisible(String elementId, boolean visible) {
55         DOM.getElementById(elementId).getStyle().setProperty("display", visible ? "" : "none");
56     }
57 
getSqlCondition(JSONObject args)58     static String getSqlCondition(JSONObject args) {
59         final JSONValue condition = args.get("extra_where");
60         if (condition == null) {
61             return "";
62         }
63         return condition.isString().stringValue();
64     }
65 
wrapWithParens(String string)66     static String wrapWithParens(String string) {
67         if (string.equals("")) {
68             return string;
69         }
70         return "(" + string + ")";
71     }
72 
joinWithParens(String joiner, String first, String second)73     static String joinWithParens(String joiner, String first, String second) {
74         first = wrapWithParens(first);
75         second = wrapWithParens(second);
76         return Utils.joinStrings(joiner, Arrays.asList(new String[] {first, second}));
77     }
78 
escapeSqlValue(String value)79     static String escapeSqlValue(String value) {
80         return value.replace("\\", "\\\\").replace("'", "\\'");
81     }
82 
addControlRow(FlexTable table, String text, Widget control)83     static int addControlRow(FlexTable table, String text, Widget control) {
84         int row = table.getRowCount();
85         table.setText(row, 0, text);
86         table.getFlexCellFormatter().setStylePrimaryName(row, 0, "field-name");
87         table.setWidget(row, 1, control);
88         return row;
89     }
90 
doCsvRequest(RpcDataSource dataSource, Query query, JSONObject extraParams)91     static void doCsvRequest(RpcDataSource dataSource, Query query, JSONObject extraParams) {
92         String rpcMethodName = dataSource.getDataMethodName();
93         JSONObject arguments = query.getParams();
94         // remove pagination arguments, since the user will want to export all results
95         arguments.put("query_start", null);
96         arguments.put("query_limit", null);
97 
98         JSONObject request = JsonRpcProxy.buildRequestObject(rpcMethodName, arguments);
99         if (extraParams != null) {
100             Utils.updateObject(request, extraParams);
101         }
102 
103         String url = JsonRpcProxy.TKO_BASE_URL + "csv/?" + URL.encode(request.toString());
104         Utils.openUrlInNewWindow(url);
105     }
106 }
107