• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package autotest.afe;
2 
3 import autotest.afe.models.Host;
4 import autotest.common.JSONArrayList;
5 import autotest.common.Utils;
6 import autotest.common.table.RpcDataSource;
7 
8 import com.google.gwt.json.client.JSONArray;
9 import com.google.gwt.json.client.JSONObject;
10 import com.google.gwt.json.client.JSONString;
11 import com.google.gwt.json.client.JSONValue;
12 
13 import java.util.ArrayList;
14 import java.util.List;
15 
16 public class HostDataSource extends RpcDataSource {
17     protected static final String LOCKED_TEXT = "locked_text";
18     protected static final String OTHER_LABELS = "other_labels";
19     protected static final String HOST_ACLS = "host_acls";
20 
HostDataSource()21     public HostDataSource() {
22         super("get_hosts", "get_num_hosts");
23     }
24 
25     @Override
26     /**
27      * Convert the raw JSONObjects to Hosts.
28      */
handleJsonResult(JSONValue result)29     protected List<JSONObject> handleJsonResult(JSONValue result) {
30         List<JSONObject> resultList = super.handleJsonResult(result);
31         List<JSONObject> hosts = new ArrayList<JSONObject>();
32         for (JSONObject row : resultList) {
33             Host host = Host.fromJsonObject(row);
34             processHost(host);
35             hosts.add(host);
36         }
37         return hosts;
38     }
39 
processHost(JSONObject host)40     protected void processHost(JSONObject host) {
41         host.put(LOCKED_TEXT, AfeUtils.getLockedText(host));
42 
43         JSONString jsonPlatform = host.get("platform").isString();
44         String platform = "";
45         if (jsonPlatform != null) {
46             platform = jsonPlatform.stringValue();
47         }
48         JSONArray labels = host.get("labels").isArray();
49         StringBuilder labelString = new StringBuilder();
50         for (int i = 0; i < labels.size(); i++) {
51             String label = labels.get(i).isString().stringValue();
52             if (label.equals(platform)) {
53                 continue;
54             }
55             if (labelString.length() > 0) {
56                 labelString.append(", ");
57             }
58             labelString.append(label);
59         }
60         host.put(OTHER_LABELS, new JSONString(labelString.toString()));
61 
62         JSONArrayList<JSONString> aclsList =
63             new JSONArrayList<JSONString>(host.get("acls").isArray());
64         String aclString = Utils.joinStrings(",", aclsList);
65         host.put(HOST_ACLS, new JSONString(aclString));
66     }
67 }
68