001 /* 002 * Sonar, open source software quality management tool. 003 * Copyright (C) 2009 SonarSource SA 004 * mailto:contact AT sonarsource DOT com 005 * 006 * Sonar is free software; you can redistribute it and/or 007 * modify it under the terms of the GNU Lesser General Public 008 * License as published by the Free Software Foundation; either 009 * version 3 of the License, or (at your option) any later version. 010 * 011 * Sonar is distributed in the hope that it will be useful, 012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 014 * Lesser General Public License for more details. 015 * 016 * You should have received a copy of the GNU Lesser General Public 017 * License along with Sonar; if not, write to the Free Software 018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 019 */ 020 package org.sonar.api.web.gwt.client.webservices; 021 022 import com.google.gwt.core.client.JavaScriptObject; 023 import com.google.gwt.json.client.JSONArray; 024 import com.google.gwt.json.client.JSONObject; 025 import com.google.gwt.json.client.JSONValue; 026 import org.sonar.api.web.gwt.client.Utils; 027 028 import java.util.ArrayList; 029 import java.util.Date; 030 import java.util.List; 031 032 public final class ResourcesQuery extends AbstractResourceQuery<Resources> { 033 034 public final static int DEPTH_UNLIMITED = -1; 035 036 private Integer depth; 037 private Integer limit; 038 private String scopes; 039 private String qualifiers; 040 private String metrics; 041 private String rules; 042 private String ruleCategories; 043 private String rulePriorities; 044 private boolean verbose = false; 045 046 /** 047 * Alias for build() 048 */ 049 public static ResourcesQuery get(String resourceKey) { 050 return new ResourcesQuery(resourceKey); 051 } 052 053 public static ResourcesQuery build(String resourceKey) { 054 return new ResourcesQuery(resourceKey); 055 } 056 057 public static ResourcesQuery build() { 058 return new ResourcesQuery(null); 059 } 060 061 private ResourcesQuery(String resourceKey) { 062 super(resourceKey); 063 } 064 065 public ResourcesQuery setDepth(Integer depth) { 066 this.depth = depth; 067 return this; 068 } 069 070 public ResourcesQuery setRules(String s) { 071 this.rules = s; 072 return this; 073 } 074 075 public ResourcesQuery filterOnRules(boolean b) { 076 return setRules(b ? "true" : "false"); 077 } 078 079 public ResourcesQuery filterOnRulePriorities(boolean b) { 080 return setRulePriorities(b ? "true" : "false"); 081 } 082 083 public ResourcesQuery filterOnRuleCategories(boolean b) { 084 return setRuleCategories(b ? "true" : "false"); 085 } 086 087 public ResourcesQuery setRulePriorities(String s) { 088 this.rulePriorities = s; 089 return this; 090 } 091 092 public ResourcesQuery setRuleCategories(String s) { 093 this.ruleCategories = s; 094 return this; 095 } 096 097 public ResourcesQuery setLimit(Integer limit) { 098 this.limit = limit; 099 return this; 100 } 101 102 public ResourcesQuery setScopes(String scopes) { 103 this.scopes = scopes; 104 return this; 105 } 106 107 public ResourcesQuery setVerbose(boolean verbose) { 108 this.verbose = verbose; 109 return this; 110 } 111 112 public ResourcesQuery setQualifiers(String qualifiers) { 113 this.qualifiers = qualifiers; 114 return this; 115 } 116 117 public ResourcesQuery setMetrics(List<WSMetrics.Metric> metrics) { 118 this.metrics = getMetricsWSRequest(metrics); 119 return this; 120 } 121 122 public ResourcesQuery setMetric(WSMetrics.Metric m) { 123 this.metrics = m.getKey(); 124 return this; 125 } 126 127 public ResourcesQuery setMetric(String metricKey) { 128 this.metrics = metricKey; 129 return this; 130 } 131 132 private String getMetricsWSRequest(List<WSMetrics.Metric> metrics) { 133 StringBuilder metricsDelimByComma = new StringBuilder(64); 134 for (WSMetrics.Metric metric : metrics) { 135 metricsDelimByComma.append(metric.getKey()).append(","); 136 } 137 return metricsDelimByComma.substring(0, metricsDelimByComma.length() - 1); 138 } 139 140 @Override 141 public String toString() { 142 String url = Utils.getServerApiUrl() + "/resources?"; 143 if (getResourceKey() != null) { 144 url += "resource=" + getResourceKey() + "&"; 145 } 146 if (metrics != null) { 147 url += "metrics=" + metrics + "&"; 148 } 149 if (scopes != null) { 150 url += "scopes=" + scopes + "&"; 151 } 152 if (qualifiers != null) { 153 url += "qualifiers=" + qualifiers + "&"; 154 } 155 if (depth != null) { 156 url += "depth=" + depth + "&"; 157 } 158 if (limit != null) { 159 url += "limit=" + limit + "&"; 160 } 161 if (rules != null) { 162 url += "rules=" + rules + "&"; 163 } 164 if (ruleCategories != null) { 165 url += "rule_categories=" + ruleCategories + "&"; 166 } 167 if (rulePriorities != null) { 168 url += "rule_priorities=" + rulePriorities + "&"; 169 } 170 if (verbose) { 171 url += "verbose=true&"; 172 } 173 return url; 174 } 175 176 @Override 177 public void execute(QueryCallBack<Resources> callback) { 178 JsonUtils.requestJson(this.toString(), new JSONHandlerDispatcher<Resources>(callback) { 179 @Override 180 public Resources parseResponse(JavaScriptObject obj) { 181 return new Resources(parseResources(obj)); 182 } 183 }); 184 } 185 186 public static List<Resource> parseResources(JavaScriptObject json) { 187 JSONArray array = new JSONArray(json); 188 List<Resource> resources = new ArrayList<Resource>(); 189 for (int i = 0; i < array.size(); i++) { 190 JSONObject jsStock = array.get(i).isObject(); 191 if (jsStock != null) { 192 resources.add(parseResource(jsStock)); 193 } 194 } 195 return resources; 196 } 197 198 private static Resource parseResource(JSONObject json) { 199 Double id = JsonUtils.getDouble(json, "id"); 200 String key = JsonUtils.getString(json, "key"); 201 String name = JsonUtils.getString(json, "name"); 202 String longName = JsonUtils.getString(json, "lname"); 203 String qualifier = JsonUtils.getString(json, "qualifier"); 204 String language = JsonUtils.getString(json, "lang"); 205 String scope = JsonUtils.getString(json, "scope"); 206 Integer copy = JsonUtils.getInteger(json, "copy"); 207 Date date = JsonUtils.getDate(json, "date"); 208 209 List<Measure> measures = null; 210 JSONValue measuresJson; 211 if ((measuresJson = json.get("msr")) != null) { 212 measures = parseMeasures(measuresJson, date); 213 } 214 215 final Resource resource = new Resource(id.intValue(), key, name, scope, qualifier, language, copy, measures); 216 resource.setLongName(longName); 217 return resource; 218 } 219 220 private static List<Measure> parseMeasures(JSONValue measures, Date date) { 221 List<Measure> projectMeasures = new ArrayList<Measure>(); 222 int len = JsonUtils.getArraySize(measures); 223 for (int i = 0; i < len; i++) { 224 JSONObject measure = JsonUtils.getArray(measures, i); 225 if (measure != null) { 226 Measure measureEntry = parseMeasure(measure, date); 227 if (measureEntry != null) { 228 projectMeasures.add(measureEntry); 229 } 230 } 231 } 232 return projectMeasures; 233 } 234 235 private static Measure parseMeasure(JSONObject measure, Date date) { 236 String metric = JsonUtils.getString(measure, "key"); 237 if (metric == null) { 238 return null; 239 } 240 241 final Measure m = new Measure(metric, JsonUtils.getDouble(measure, "val"), JsonUtils.getString(measure, "frmt_val")); 242 m.setData(JsonUtils.getString(measure, "data")); 243 String metricName = JsonUtils.getString(measure, "name"); 244 if (metricName != null) { 245 m.setMetricName(metricName); 246 } 247 248 m.setRuleKey(JsonUtils.getString(measure, "rule_key")); 249 m.setRuleName(JsonUtils.getString(measure, "rule_name")); 250 m.setRuleCategory(JsonUtils.getString(measure, "rule_category")); 251 m.setRulePriority(JsonUtils.getString(measure, "rule_priority")); 252 m.setDate(date); 253 return m; 254 } 255 256 }