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 private final String getMetricsWSRequest(List<WSMetrics.Metric> metrics) { 128 StringBuilder metricsDelimByComma = new StringBuilder(64); 129 for (WSMetrics.Metric metric : metrics) { 130 metricsDelimByComma.append(metric.getKey()).append(","); 131 } 132 return metricsDelimByComma.substring(0, metricsDelimByComma.length() - 1); 133 } 134 135 @Override 136 public String toString() { 137 String url = Utils.getServerApiUrl() + "/resources?"; 138 if (getResourceKey() != null) { 139 url += "resource=" + getResourceKey() + "&"; 140 } 141 if (metrics != null) { 142 url += "metrics=" + metrics + "&"; 143 } 144 if (scopes != null) { 145 url += "scopes=" + scopes + "&"; 146 } 147 if (qualifiers != null) { 148 url += "qualifiers=" + qualifiers + "&"; 149 } 150 if (depth != null) { 151 url += "depth=" + depth + "&"; 152 } 153 if (limit != null) { 154 url += "limit=" + limit + "&"; 155 } 156 if (rules != null) { 157 url += "rules=" + rules + "&"; 158 } 159 if (ruleCategories != null) { 160 url += "rule_categories=" + ruleCategories + "&"; 161 } 162 if (rulePriorities != null) { 163 url += "rule_priorities=" + rulePriorities + "&"; 164 } 165 if (verbose) { 166 url += "verbose=true&"; 167 } 168 return url; 169 } 170 171 @Override 172 public void execute(QueryCallBack<Resources> callback) { 173 JsonUtils.requestJson(this.toString(), new JSONHandlerDispatcher<Resources>(callback) { 174 @Override 175 public Resources parseResponse(JavaScriptObject obj) { 176 return new Resources(parseResources(obj)); 177 } 178 }); 179 } 180 181 public static List<Resource> parseResources(JavaScriptObject json) { 182 JSONArray array = new JSONArray(json); 183 List<Resource> resources = new ArrayList<Resource>(); 184 for (int i = 0; i < array.size(); i++) { 185 JSONObject jsStock = array.get(i).isObject(); 186 if (jsStock != null) { 187 resources.add(parseResource(jsStock)); 188 } 189 } 190 return resources; 191 } 192 193 private static Resource parseResource(JSONObject json) { 194 Double id = JsonUtils.getDouble(json, "id"); 195 String key = JsonUtils.getString(json, "key"); 196 String name = JsonUtils.getString(json, "name"); 197 String longName = JsonUtils.getString(json, "lname"); 198 String qualifier = JsonUtils.getString(json, "qualifier"); 199 String language = JsonUtils.getString(json, "lang"); 200 String scope = JsonUtils.getString(json, "scope"); 201 Integer copy = JsonUtils.getInteger(json, "copy"); 202 Date date = JsonUtils.getDate(json, "date"); 203 204 List<Measure> measures = null; 205 JSONValue measuresJson; 206 if ((measuresJson = json.get("msr")) != null) { 207 measures = parseMeasures(measuresJson, date); 208 } 209 210 final Resource resource = new Resource(id.intValue(), key, name, scope, qualifier, language, copy, measures); 211 resource.setLongName(longName); 212 return resource; 213 } 214 215 private static List<Measure> parseMeasures(JSONValue measures, Date date) { 216 List<Measure> projectMeasures = new ArrayList<Measure>(); 217 int len = JsonUtils.getArraySize(measures); 218 for (int i = 0; i < len; i++) { 219 JSONObject measure = JsonUtils.getArray(measures, i); 220 if (measure != null) { 221 Measure measureEntry = parseMeasure(measure, date); 222 if (measureEntry != null) { 223 projectMeasures.add(measureEntry); 224 } 225 } 226 } 227 return projectMeasures; 228 } 229 230 private static Measure parseMeasure(JSONObject measure, Date date) { 231 String metric = JsonUtils.getString(measure, "key"); 232 if (metric == null) { 233 return null; 234 } 235 236 final Measure m = new Measure(metric, JsonUtils.getDouble(measure, "val"), JsonUtils.getString(measure, "frmt_val")); 237 m.setData(JsonUtils.getString(measure, "data")); 238 String metricName = JsonUtils.getString(measure, "name"); 239 if (metricName != null) { 240 m.setMetricName(metricName); 241 } 242 243 m.setRuleKey(JsonUtils.getString(measure, "rule_key")); 244 m.setRuleName(JsonUtils.getString(measure, "rule_name")); 245 m.setRuleCategory(JsonUtils.getString(measure, "rule_category")); 246 m.setRulePriority(JsonUtils.getString(measure, "rule_priority")); 247 m.setDate(date); 248 return m; 249 } 250 251 }