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 /**
033 * @deprecated since 2.5, use {@link org.sonar.wsclient.services.ResourceQuery} instead
034 */
035 @Deprecated
036 public final class ResourcesQuery extends AbstractResourceQuery<Resources> {
037
038 public final static int DEPTH_UNLIMITED = -1;
039
040 private Integer depth;
041 private Integer limit;
042 private String scopes;
043 private String qualifiers;
044 private String metrics;
045 private String rules;
046 private String rulePriorities;
047 private boolean verbose = false;
048
049 /**
050 * Alias for build()
051 */
052 public static ResourcesQuery get(String resourceKey) {
053 return new ResourcesQuery(resourceKey);
054 }
055
056 public static ResourcesQuery build(String resourceKey) {
057 return new ResourcesQuery(resourceKey);
058 }
059
060 public static ResourcesQuery build() {
061 return new ResourcesQuery(null);
062 }
063
064 private ResourcesQuery(String resourceKey) {
065 super(resourceKey);
066 }
067
068 public ResourcesQuery setDepth(Integer depth) {
069 this.depth = depth;
070 return this;
071 }
072
073 public ResourcesQuery setRules(String s) {
074 this.rules = s;
075 return this;
076 }
077
078 public ResourcesQuery filterOnRules(boolean b) {
079 return setRules(b ? "true" : "false");
080 }
081
082 public ResourcesQuery filterOnRulePriorities(boolean b) {
083 return setRulePriorities(b ? "true" : "false");
084 }
085
086 /**
087 * @deprecated since 2.5 See http://jira.codehaus.org/browse/SONAR-2007
088 */
089 @Deprecated
090 public ResourcesQuery filterOnRuleCategories(boolean b) {
091 return this;
092 }
093
094 public ResourcesQuery setRulePriorities(String s) {
095 this.rulePriorities = s;
096 return this;
097 }
098
099 /**
100 * @deprecated since 2.5 See http://jira.codehaus.org/browse/SONAR-2007
101 */
102 @Deprecated
103 public ResourcesQuery setRuleCategories(String s) {
104 return this;
105 }
106
107 public ResourcesQuery setLimit(Integer limit) {
108 this.limit = limit;
109 return this;
110 }
111
112 public ResourcesQuery setScopes(String scopes) {
113 this.scopes = scopes;
114 return this;
115 }
116
117 public ResourcesQuery setVerbose(boolean verbose) {
118 this.verbose = verbose;
119 return this;
120 }
121
122 public ResourcesQuery setQualifiers(String qualifiers) {
123 this.qualifiers = qualifiers;
124 return this;
125 }
126
127 public ResourcesQuery setMetrics(List<WSMetrics.Metric> metrics) {
128 this.metrics = getMetricsWSRequest(metrics);
129 return this;
130 }
131
132 public ResourcesQuery setMetric(WSMetrics.Metric m) {
133 this.metrics = m.getKey();
134 return this;
135 }
136
137 public ResourcesQuery setMetric(String metricKey) {
138 this.metrics = metricKey;
139 return this;
140 }
141
142 private String getMetricsWSRequest(List<WSMetrics.Metric> metrics) {
143 StringBuilder metricsDelimByComma = new StringBuilder(64);
144 for (WSMetrics.Metric metric : metrics) {
145 metricsDelimByComma.append(metric.getKey()).append(",");
146 }
147 return metricsDelimByComma.substring(0, metricsDelimByComma.length() - 1);
148 }
149
150 @Override
151 public String toString() {
152 String url = Utils.getServerApiUrl() + "/resources?";
153 if (getResourceKey() != null) {
154 url += "resource=" + getResourceKey() + "&";
155 }
156 if (metrics != null) {
157 url += "metrics=" + metrics + "&";
158 }
159 if (scopes != null) {
160 url += "scopes=" + scopes + "&";
161 }
162 if (qualifiers != null) {
163 url += "qualifiers=" + qualifiers + "&";
164 }
165 if (depth != null) {
166 url += "depth=" + depth + "&";
167 }
168 if (limit != null) {
169 url += "limit=" + limit + "&";
170 }
171 if (rules != null) {
172 url += "rules=" + rules + "&";
173 }
174 if (rulePriorities != null) {
175 url += "rule_priorities=" + rulePriorities + "&";
176 }
177 if (verbose) {
178 url += "verbose=true&";
179 }
180 return url;
181 }
182
183 @Override
184 public void execute(QueryCallBack<Resources> callback) {
185 JsonUtils.requestJson(this.toString(), new JSONHandlerDispatcher<Resources>(callback) {
186 @Override
187 public Resources parseResponse(JavaScriptObject obj) {
188 return new Resources(parseResources(obj));
189 }
190 });
191 }
192
193 public static List<Resource> parseResources(JavaScriptObject json) {
194 JSONArray array = new JSONArray(json);
195 List<Resource> resources = new ArrayList<Resource>();
196 for (int i = 0; i < array.size(); i++) {
197 JSONObject jsStock = array.get(i).isObject();
198 if (jsStock != null) {
199 resources.add(parseResource(jsStock));
200 }
201 }
202 return resources;
203 }
204
205 private static Resource parseResource(JSONObject json) {
206 Double id = JsonUtils.getDouble(json, "id");
207 String key = JsonUtils.getString(json, "key");
208 String name = JsonUtils.getString(json, "name");
209 String longName = JsonUtils.getString(json, "lname");
210 String qualifier = JsonUtils.getString(json, "qualifier");
211 String language = JsonUtils.getString(json, "lang");
212 String scope = JsonUtils.getString(json, "scope");
213 Integer copy = JsonUtils.getInteger(json, "copy");
214 Date date = JsonUtils.getDate(json, "date");
215
216 List<Measure> measures = null;
217 JSONValue measuresJson;
218 if ((measuresJson = json.get("msr")) != null) {
219 measures = parseMeasures(measuresJson, date);
220 }
221
222 final Resource resource = new Resource(id.intValue(), key, name, scope, qualifier, language, copy, measures);
223 resource.setLongName(longName);
224 return resource;
225 }
226
227 private static List<Measure> parseMeasures(JSONValue measures, Date date) {
228 List<Measure> projectMeasures = new ArrayList<Measure>();
229 int len = JsonUtils.getArraySize(measures);
230 for (int i = 0; i < len; i++) {
231 JSONObject measure = JsonUtils.getArray(measures, i);
232 if (measure != null) {
233 Measure measureEntry = parseMeasure(measure, date);
234 if (measureEntry != null) {
235 projectMeasures.add(measureEntry);
236 }
237 }
238 }
239 return projectMeasures;
240 }
241
242 private static Measure parseMeasure(JSONObject measure, Date date) {
243 String metric = JsonUtils.getString(measure, "key");
244 if (metric == null) {
245 return null;
246 }
247
248 final Measure m = new Measure(metric, JsonUtils.getDouble(measure, "val"), JsonUtils.getString(measure, "frmt_val"));
249 m.setData(JsonUtils.getString(measure, "data"));
250 String metricName = JsonUtils.getString(measure, "name");
251 if (metricName != null) {
252 m.setMetricName(metricName);
253 }
254
255 m.setRuleKey(JsonUtils.getString(measure, "rule_key"));
256 m.setRuleName(JsonUtils.getString(measure, "rule_name"));
257 m.setRulePriority(JsonUtils.getString(measure, "rule_priority"));
258 m.setDate(date);
259 return m;
260 }
261
262 }