001    /*
002     * SonarQube, open source software quality management tool.
003     * Copyright (C) 2008-2013 SonarSource
004     * mailto:contact AT sonarsource DOT com
005     *
006     * SonarQube 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     * SonarQube 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 License
017     * along with this program; if not, write to the Free Software Foundation,
018     * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
019     */
020    package org.sonar.wsclient.services;
021    
022    public class ResourceQuery extends Query<Resource> {
023    
024      public static final String BASE_URL = "/api/resources";
025    
026      public static final int DEPTH_UNLIMITED = -1;
027    
028      private Integer depth;
029      private String resourceKeyOrId;
030      private Integer limit;
031      private String[] scopes;
032      private String[] qualifiers;
033      private String[] metrics;
034      private String[] rules;
035      private String[] ruleSeverities;
036      private String[] characteristicKeys;
037      private String[] languages;
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 setCharacteristics(String... keys) {
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      public String[] getLanguages() {
133        return languages;
134      }
135    
136      public ResourceQuery setLanguages(String... languages) {
137        this.languages = languages;
138        return this;
139      }
140    
141      /**
142       * @deprecated since 2.5 See http://jira.codehaus.org/browse/SONAR-2007
143       */
144      @Deprecated
145      public String[] getRuleCategories() {
146        return null;
147      }
148    
149      /**
150       * @param ruleCategories values: Maintainability, Usability, Reliability, Efficiency, Portability
151       * @deprecated since 2.5 See http://jira.codehaus.org/browse/SONAR-2007
152       */
153      @Deprecated
154      public ResourceQuery setRuleCategories(String... ruleCategories) {
155        return this;
156      }
157    
158      /**
159       * @since 2.5
160       */
161      public String[] getRuleSeverities() {
162        return ruleSeverities;
163      }
164    
165      /**
166       * @since 2.5
167       * @param ruleSeverities values: BLOCKER, CRITICAL, MAJOR, MINOR, INFO
168       */
169      public ResourceQuery setRuleSeverities(String... ruleSeverities) {
170        this.ruleSeverities = ruleSeverities;
171        this.excludeRuleSeverities = false;
172        return this;
173      }
174    
175      /**
176       * @deprecated since 2.5 use {@link #getRuleSeverities()} instead. See http://jira.codehaus.org/browse/SONAR-1829
177       */
178      @Deprecated
179      public String[] getRulePriorities() {
180        return ruleSeverities;
181      }
182    
183      /**
184       * @deprecated since 2.5 use {@link #setRuleSeverities(String...)} instead. See http://jira.codehaus.org/browse/SONAR-1829
185       */
186      @Deprecated
187      public ResourceQuery setRulePriorities(String... rulePriorities) {
188        return setRuleSeverities(rulePriorities);
189      }
190    
191      public boolean isExcludeRules() {
192        return excludeRules;
193      }
194    
195      public ResourceQuery setExcludeRules(boolean excludeRules) {
196        this.excludeRules = excludeRules;
197        return this;
198      }
199    
200      /**
201       * @deprecated since 2.5 not used anymore
202       */
203      @Deprecated
204      public boolean isExcludeRuleCategories() {
205        return false;
206      }
207    
208      /**
209       * @deprecated since 2.5 not used anymore
210       */
211      @Deprecated
212      public ResourceQuery setExcludeRuleCategories(boolean b) {
213        return this;
214      }
215    
216      /**
217       * @since 2.5
218       */
219      public boolean isExcludeRuleSeverities() {
220        return excludeRuleSeverities;
221      }
222    
223      public ResourceQuery setExcludeRuleSeverities(boolean excludeRuleSeverities) {
224        this.excludeRuleSeverities = excludeRuleSeverities;
225        return this;
226      }
227    
228      /**
229       * @deprecated since 2.5 use {@link #isExcludeRuleSeverities()} instead. See http://jira.codehaus.org/browse/SONAR-1829
230       */
231      @Deprecated
232      public boolean isExcludeRulePriorities() {
233        return excludeRuleSeverities;
234      }
235    
236      /**
237       * @deprecated since 2.5 use {@link #setExcludeRuleSeverities(boolean)} instead. See http://jira.codehaus.org/browse/SONAR-1829
238       */
239      @Deprecated
240      public ResourceQuery setExcludeRulePriorities(boolean b) {
241        this.excludeRuleSeverities = b;
242        return this;
243      }
244    
245      public Boolean isVerbose() {
246        return verbose;
247      }
248    
249      public ResourceQuery setVerbose(Boolean verbose) {
250        this.verbose = verbose;
251        return this;
252      }
253    
254      public Boolean isIncludeTrends() {
255        return includeTrends;
256      }
257    
258      public ResourceQuery setIncludeTrends(Boolean includeTrends) {
259        this.includeTrends = includeTrends;
260        return this;
261      }
262    
263      @Override
264      public String getUrl() {
265        StringBuilder url = new StringBuilder(BASE_URL);
266        url.append('?');
267        appendUrlParameter(url, "resource", resourceKeyOrId);
268        appendUrlParameter(url, "metrics", metrics);
269        appendUrlParameter(url, "scopes", scopes);
270        appendUrlParameter(url, "qualifiers", qualifiers);
271        appendUrlParameter(url, "depth", depth);
272        appendUrlParameter(url, "limit", limit);
273        appendRuleField(url, "rules", excludeRules, rules);
274        appendRuleField(url, "rule_priorities", excludeRuleSeverities, ruleSeverities);
275        appendUrlParameter(url, "includetrends", includeTrends);
276        appendUrlParameter(url, "characteristics", characteristicKeys);
277        appendUrlParameter(url, "languages", languages);
278        appendUrlParameter(url, "verbose", verbose);
279        return url.toString();
280      }
281    
282      private void appendRuleField(StringBuilder url, String field, boolean excludeField, String[] list) {
283        if (!excludeField) {
284          if (list == null || list.length == 0) {
285            appendUrlParameter(url, field, true);
286          } else {
287            appendUrlParameter(url, field, list);
288          }
289        }
290      }
291    
292      @Override
293      public final Class<Resource> getModelClass() {
294        return Resource.class;
295      }
296    
297      public static ResourceQuery createForMetrics(String resourceKeyOrId, String... metricKeys) {
298        return new ResourceQuery(resourceKeyOrId)
299            .setMetrics(metricKeys);
300      }
301    
302      public static ResourceQuery createForResource(Resource resource, String... metricKeys) {
303        Integer id = resource.getId();
304        if (id == null) {
305          throw new IllegalArgumentException("id must be set");
306        }
307        return new ResourceQuery(id.toString())
308            .setMetrics(metricKeys);
309      }
310    
311      /**
312       * @since 2.10
313       */
314      public static ResourceQuery create(String resourceKey) {
315        return new ResourceQuery(resourceKey);
316      }
317    }