001/*
002 * Sonar, open source software quality management tool.
003 * Copyright (C) 2008-2012 SonarSource
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 */
020package org.sonar.wsclient.services;
021
022public class ResourceQuery extends Query<Resource> {
023  public static final String BASE_URL = "/api/resources";
024
025  public static final int DEPTH_UNLIMITED = -1;
026
027  private Integer depth;
028  private String resourceKeyOrId;
029  private Integer limit;
030  private String[] scopes;
031  private String[] qualifiers;
032  private String[] metrics;
033  private String[] rules;
034  private String[] ruleSeverities;
035  private String[] characteristicKeys;
036  private String[] languages;
037  private String model;
038  private boolean excludeRules = true;
039  private boolean excludeRuleSeverities = true;
040  private Boolean includeTrends = null;
041  private Boolean verbose = Boolean.FALSE;
042
043  public ResourceQuery() {
044  }
045
046  public ResourceQuery(String resourceKeyOrId) {
047    this.resourceKeyOrId = resourceKeyOrId;
048  }
049
050  public ResourceQuery(long resourceId) {
051    this.resourceKeyOrId = String.valueOf(resourceId);
052  }
053
054  public Integer getDepth() {
055    return depth;
056  }
057
058  public ResourceQuery setDepth(Integer depth) {
059    this.depth = depth;
060    return this;
061  }
062
063  public ResourceQuery setAllDepths() {
064    return setDepth(DEPTH_UNLIMITED);
065  }
066
067  public String getResourceKeyOrId() {
068    return resourceKeyOrId;
069  }
070
071  public ResourceQuery setResourceKeyOrId(String resourceKeyOrId) {
072    this.resourceKeyOrId = resourceKeyOrId;
073    return this;
074  }
075
076  public ResourceQuery setResourceId(int resourceId) {
077    this.resourceKeyOrId = Integer.toString(resourceId);
078    return this;
079  }
080
081  public ResourceQuery setCharacteristicKeys(String model, String... keys) {
082    this.model = model;
083    this.characteristicKeys = keys;
084    return this;
085  }
086
087  public Integer getLimit() {
088    return limit;
089  }
090
091  public ResourceQuery setLimit(Integer limit) {
092    this.limit = limit;
093    return this;
094  }
095
096  public String[] getScopes() {
097    return scopes;
098  }
099
100  public ResourceQuery setScopes(String... scopes) {
101    this.scopes = scopes;
102    return this;
103  }
104
105  public String[] getQualifiers() {
106    return qualifiers;
107  }
108
109  public ResourceQuery setQualifiers(String... qualifiers) {
110    this.qualifiers = qualifiers;
111    return this;
112  }
113
114  public String[] getMetrics() {
115    return metrics;
116  }
117
118  public ResourceQuery setMetrics(String... metrics) {
119    this.metrics = metrics;
120    return this;
121  }
122
123  public String[] getRules() {
124    return rules;
125  }
126
127  public ResourceQuery setRules(String... rules) {
128    this.rules = rules;
129    this.excludeRules = false;
130    return this;
131  }
132
133  public String[] getLanguages() {
134    return languages;
135  }
136
137  public ResourceQuery setLanguages(String... languages) {
138    this.languages = languages;
139    return this;
140  }
141
142  /**
143   * @deprecated since 2.5 See http://jira.codehaus.org/browse/SONAR-2007
144   */
145  @Deprecated
146  public String[] getRuleCategories() {
147    return null;
148  }
149
150  /**
151   * @param ruleCategories values: Maintainability, Usability, Reliability, Efficiency, Portability
152   * @deprecated since 2.5 See http://jira.codehaus.org/browse/SONAR-2007
153   */
154  @Deprecated
155  public ResourceQuery setRuleCategories(String... ruleCategories) {
156    return this;
157  }
158
159  /**
160   * @since 2.5
161   */
162  public String[] getRuleSeverities() {
163    return ruleSeverities;
164  }
165
166  /**
167   * @since 2.5
168   * @param ruleSeverities values: BLOCKER, CRITICAL, MAJOR, MINOR, INFO
169   */
170  public ResourceQuery setRuleSeverities(String... ruleSeverities) {
171    this.ruleSeverities = ruleSeverities;
172    this.excludeRuleSeverities = false;
173    return this;
174  }
175
176  /**
177   * @deprecated since 2.5 use {@link #getRuleSeverities()} instead. See http://jira.codehaus.org/browse/SONAR-1829
178   */
179  @Deprecated
180  public String[] getRulePriorities() {
181    return ruleSeverities;
182  }
183
184  /**
185   * @deprecated since 2.5 use {@link #setRuleSeverities(String...)} instead. See http://jira.codehaus.org/browse/SONAR-1829
186   */
187  @Deprecated
188  public ResourceQuery setRulePriorities(String... rulePriorities) {
189    return setRuleSeverities(rulePriorities);
190  }
191
192  public boolean isExcludeRules() {
193    return excludeRules;
194  }
195
196  public ResourceQuery setExcludeRules(boolean excludeRules) {
197    this.excludeRules = excludeRules;
198    return this;
199  }
200
201  /**
202   * @deprecated since 2.5 not used anymore
203   */
204  @Deprecated
205  public boolean isExcludeRuleCategories() {
206    return false;
207  }
208
209  /**
210   * @deprecated since 2.5 not used anymore
211   */
212  @Deprecated
213  public ResourceQuery setExcludeRuleCategories(boolean b) {
214    return this;
215  }
216
217  /**
218   * @since 2.5
219   */
220  public boolean isExcludeRuleSeverities() {
221    return excludeRuleSeverities;
222  }
223
224  public ResourceQuery setExcludeRuleSeverities(boolean excludeRuleSeverities) {
225    this.excludeRuleSeverities = excludeRuleSeverities;
226    return this;
227  }
228
229  /**
230   * @deprecated since 2.5 use {@link #isExcludeRuleSeverities()} instead. See http://jira.codehaus.org/browse/SONAR-1829
231   */
232  @Deprecated
233  public boolean isExcludeRulePriorities() {
234    return excludeRuleSeverities;
235  }
236
237  /**
238   * @deprecated since 2.5 use {@link #setExcludeRuleSeverities(boolean)} instead. See http://jira.codehaus.org/browse/SONAR-1829
239   */
240  @Deprecated
241  public ResourceQuery setExcludeRulePriorities(boolean b) {
242    this.excludeRuleSeverities = b;
243    return this;
244  }
245
246  public Boolean isVerbose() {
247    return verbose;
248  }
249
250  public ResourceQuery setVerbose(Boolean verbose) {
251    this.verbose = verbose;
252    return this;
253  }
254
255  public Boolean isIncludeTrends() {
256    return includeTrends;
257  }
258
259  public ResourceQuery setIncludeTrends(Boolean includeTrends) {
260    this.includeTrends = includeTrends;
261    return this;
262  }
263
264  @Override
265  public String getUrl() {
266    StringBuilder url = new StringBuilder(BASE_URL);
267    url.append('?');
268    appendUrlParameter(url, "resource", resourceKeyOrId);
269    appendUrlParameter(url, "metrics", metrics);
270    appendUrlParameter(url, "scopes", scopes);
271    appendUrlParameter(url, "qualifiers", qualifiers);
272    appendUrlParameter(url, "depth", depth);
273    appendUrlParameter(url, "limit", limit);
274    appendRuleField(url, "rules", excludeRules, rules);
275    appendRuleField(url, "rule_priorities", excludeRuleSeverities, ruleSeverities);
276    appendUrlParameter(url, "includetrends", includeTrends);
277    appendUrlParameter(url, "model", model);
278    appendUrlParameter(url, "characteristics", characteristicKeys);
279    appendUrlParameter(url, "languages", languages);
280    appendUrlParameter(url, "verbose", verbose);
281    return url.toString();
282  }
283
284  private void appendRuleField(StringBuilder url, String field, boolean excludeField, String[] list) {
285    if (!excludeField) {
286      if (list == null || list.length == 0) {
287        appendUrlParameter(url, field, true);
288      } else {
289        appendUrlParameter(url, field, list);
290      }
291    }
292  }
293
294  @Override
295  public final Class<Resource> getModelClass() {
296    return Resource.class;
297  }
298
299  public static ResourceQuery createForMetrics(String resourceKeyOrId, String... metricKeys) {
300    return new ResourceQuery(resourceKeyOrId)
301        .setMetrics(metricKeys);
302  }
303
304  public static ResourceQuery createForResource(Resource resource, String... metricKeys) {
305    return new ResourceQuery(resource.getId().toString())
306        .setMetrics(metricKeys);
307  }
308
309  /**
310   * @since 2.10
311   */
312  public static ResourceQuery create(String resourceKey) {
313    return new ResourceQuery(resourceKey);
314  }
315}