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