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