AfeUtils.java revision 2bab8f45adedeacbf2d62d37b90255581adc3c7d
1package autotest.afe; 2 3import autotest.common.JSONArrayList; 4import autotest.common.JsonRpcCallback; 5import autotest.common.JsonRpcProxy; 6import autotest.common.SimpleCallback; 7import autotest.common.StaticDataRepository; 8import autotest.common.Utils; 9import autotest.common.table.JSONObjectSet; 10import autotest.common.ui.NotifyManager; 11import autotest.common.ui.RadioChooser; 12 13import com.google.gwt.json.client.JSONArray; 14import com.google.gwt.json.client.JSONObject; 15import com.google.gwt.json.client.JSONString; 16import com.google.gwt.json.client.JSONValue; 17 18import java.util.ArrayList; 19import java.util.Collection; 20import java.util.Iterator; 21import java.util.List; 22import java.util.Set; 23 24/** 25 * Utility methods. 26 */ 27public class AfeUtils { 28 public static final String PLATFORM_SUFFIX = " (platform)"; 29 30 public static final ClassFactory factory = new SiteClassFactory(); 31 32 private static StaticDataRepository staticData = StaticDataRepository.getRepository(); 33 34 public static String formatStatusCounts(JSONObject counts, String joinWith) { 35 String result = ""; 36 Set<String> statusSet = counts.keySet(); 37 for (Iterator<String> i = statusSet.iterator(); i.hasNext();) { 38 String status = i.next(); 39 int count = (int) counts.get(status).isNumber().doubleValue(); 40 result += Integer.toString(count) + " " + status; 41 if (i.hasNext()) 42 result += joinWith; 43 } 44 return result; 45 } 46 47 public static String[] getLabelStrings() { 48 return getFilteredLabelStrings(false, false); 49 } 50 51 protected static String[] getFilteredLabelStrings(boolean onlyPlatforms, 52 boolean onlyNonPlatforms) { 53 assert( !(onlyPlatforms && onlyNonPlatforms)); 54 JSONArray labels = staticData.getData("labels").isArray(); 55 List<String> result = new ArrayList<String>(); 56 for (int i = 0; i < labels.size(); i++) { 57 JSONObject label = labels.get(i).isObject(); 58 String name = label.get("name").isString().stringValue(); 59 boolean labelIsPlatform = label.get("platform").isBoolean().booleanValue(); 60 if ((onlyPlatforms && labelIsPlatform) || 61 (onlyNonPlatforms && !labelIsPlatform)) { 62 result.add(name); 63 } else if (!onlyPlatforms && !onlyNonPlatforms) { 64 if (labelIsPlatform) { 65 name += PLATFORM_SUFFIX; 66 } 67 result.add(name); 68 } 69 } 70 return result.toArray(new String[result.size()]); 71 } 72 73 public static String[] getPlatformStrings() { 74 return getFilteredLabelStrings(true, false); 75 } 76 77 public static String[] getNonPlatformLabelStrings() { 78 return getFilteredLabelStrings(false, true); 79 } 80 81 public static String decodeLabelName(String labelName) { 82 if (labelName.endsWith(PLATFORM_SUFFIX)) { 83 int nameLength = labelName.length() - PLATFORM_SUFFIX.length(); 84 return labelName.substring(0, nameLength); 85 } 86 return labelName; 87 } 88 89 public static JSONString getLockedText(JSONObject host) { 90 boolean locked = host.get("locked").isBoolean().booleanValue(); 91 return new JSONString(locked ? "Yes" : "No"); 92 } 93 94 public static void abortHostQueueEntries(Collection<JSONObject> entries, 95 final SimpleCallback onSuccess) { 96 if (entries.isEmpty()) { 97 NotifyManager.getInstance().showError("No entries selected to abort"); 98 return; 99 } 100 101 final JSONArray asynchronousEntryIds = new JSONArray(); 102 Set<JSONObject> synchronousEntries = new JSONObjectSet<JSONObject>(); 103 for (JSONObject entry : entries) { 104 JSONObject job = entry.get("job").isObject(); 105 int synchCount = (int) job.get("synch_count").isNumber().doubleValue(); 106 boolean hasExecutionSubdir = 107 !Utils.jsonToString(entry.get("execution_subdir")).equals(""); 108 if (synchCount > 1 && hasExecutionSubdir) { 109 synchronousEntries.add(entry); 110 continue; 111 } 112 113 JSONValue idListValue = entry.get("id_list"); 114 if (idListValue != null) { 115 // metahost row 116 extendJsonArray(asynchronousEntryIds, idListValue.isArray()); 117 } else { 118 asynchronousEntryIds.set(asynchronousEntryIds.size(), entry.get("id")); 119 } 120 } 121 122 SimpleCallback abortAsynchronousEntries = new SimpleCallback() { 123 public void doCallback(Object source) { 124 JSONObject params = new JSONObject(); 125 params.put("id__in", asynchronousEntryIds); 126 AfeUtils.callAbort(params, onSuccess); 127 } 128 }; 129 130 if (synchronousEntries.size() == 0) { 131 abortAsynchronousEntries.doCallback(null); 132 } else { 133 AbortSynchronousDialog dialog = new AbortSynchronousDialog( 134 abortAsynchronousEntries, synchronousEntries, asynchronousEntryIds.size() != 0); 135 dialog.center(); 136 } 137 } 138 139 private static void extendJsonArray(JSONArray array, JSONArray newValues) { 140 for (JSONValue value : new JSONArrayList<JSONValue>(newValues)) { 141 array.set(array.size(), value); 142 } 143 } 144 145 public static void callAbort(JSONObject params, final SimpleCallback onSuccess, 146 final boolean showMessage) { 147 JsonRpcProxy rpcProxy = JsonRpcProxy.getProxy(); 148 rpcProxy.rpcCall("abort_host_queue_entries", params, new JsonRpcCallback() { 149 @Override 150 public void onSuccess(JSONValue result) { 151 if (showMessage) { 152 NotifyManager.getInstance().showMessage("Jobs aborted"); 153 } 154 if (onSuccess != null) { 155 onSuccess.doCallback(null); 156 } 157 } 158 }); 159 } 160 161 public static void callAbort(JSONObject params, final SimpleCallback onSuccess) { 162 callAbort(params, onSuccess, true); 163 } 164 165 public static String getJobTag(JSONObject job) { 166 return Utils.jsonToString(job.get("id")) + "-" + Utils.jsonToString(job.get("owner")); 167 } 168 169 public static void populateRadioChooser(RadioChooser chooser, String name) { 170 JSONArray options = staticData.getData(name + "_options").isArray(); 171 for (JSONString jsonOption : new JSONArrayList<JSONString>(options)) { 172 chooser.addChoice(Utils.jsonToString(jsonOption)); 173 } 174 } 175} 176