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.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      public ResourceQuery setRuleCategories(String... ruleCategories) {
145        return this;
146      }
147    
148      /**
149       * @since 2.5
150       */
151      public String[] getRuleSeverities() {
152        return ruleSeverities;
153      }
154    
155      /**
156       * @since 2.5
157       * @param ruleSeverities values: BLOCKER, CRITICAL, MAJOR, MINOR, INFO
158       */
159      public ResourceQuery setRuleSeverities(String... ruleSeverities) {
160        this.ruleSeverities = ruleSeverities;
161        this.excludeRuleSeverities = false;
162        return this;
163      }
164    
165      /**
166       * @deprecated since 2.5 use {@link #getRuleSeverities()} instead. See http://jira.codehaus.org/browse/SONAR-1829
167       */
168      @Deprecated
169      public String[] getRulePriorities() {
170        return ruleSeverities;
171      }
172    
173      /**
174       * @deprecated since 2.5 use {@link #setRuleSeverities(String...)} instead. See http://jira.codehaus.org/browse/SONAR-1829
175       */
176      @Deprecated
177      public ResourceQuery setRulePriorities(String... rulePriorities) {
178        return setRuleSeverities(rulePriorities);
179      }
180    
181      public boolean isExcludeRules() {
182        return excludeRules;
183      }
184    
185      public ResourceQuery setExcludeRules(boolean excludeRules) {
186        this.excludeRules = excludeRules;
187        return this;
188      }
189    
190      /**
191       * @deprecated since 2.5 not used anymore
192       */
193      public boolean isExcludeRuleCategories() {
194        return false;
195      }
196    
197      /**
198       * @deprecated since 2.5 not used anymore
199       */
200      public ResourceQuery setExcludeRuleCategories(boolean b) {
201        return this;
202      }
203    
204      /**
205       * @since 2.5
206       */
207      public boolean isExcludeRuleSeverities() {
208        return excludeRuleSeverities;
209      }
210    
211      public ResourceQuery setExcludeRuleSeverities(boolean excludeRuleSeverities) {
212        this.excludeRuleSeverities = excludeRuleSeverities;
213        return this;
214      }
215    
216      /**
217       * @deprecated since 2.5 use {@link #isExcludeRuleSeverities()} instead. See http://jira.codehaus.org/browse/SONAR-1829
218       */
219      @Deprecated
220      public boolean isExcludeRulePriorities() {
221        return excludeRuleSeverities;
222      }
223    
224      /**
225       * @deprecated since 2.5 use {@link #setExcludeRuleSeverities(boolean)} instead. See http://jira.codehaus.org/browse/SONAR-1829
226       */
227      @Deprecated
228      public ResourceQuery setExcludeRulePriorities(boolean b) {
229        this.excludeRuleSeverities = b;
230        return this;
231      }
232    
233      public Boolean isVerbose() {
234        return verbose;
235      }
236    
237      public ResourceQuery setVerbose(Boolean verbose) {
238        this.verbose = verbose;
239        return this;
240      }
241    
242      public Boolean isIncludeTrends() {
243        return includeTrends;
244      }
245    
246      public ResourceQuery setIncludeTrends(Boolean includeTrends) {
247        this.includeTrends = includeTrends;
248        return this;
249      }
250    
251      @Override
252      public String getUrl() {
253        StringBuilder url = new StringBuilder(BASE_URL);
254        url.append('?');
255        appendUrlParameter(url, "resource", resourceKeyOrId);
256        appendUrlParameter(url, "metrics", metrics);
257        appendUrlParameter(url, "scopes", scopes);
258        appendUrlParameter(url, "qualifiers", qualifiers);
259        appendUrlParameter(url, "depth", depth);
260        appendUrlParameter(url, "limit", limit);
261        appendRuleField(url, "rules", excludeRules, rules);
262        appendRuleField(url, "rule_priorities", excludeRuleSeverities, ruleSeverities);
263        appendUrlParameter(url, "includetrends", includeTrends);
264        appendUrlParameter(url, "model", model);
265        appendUrlParameter(url, "characteristics", characteristicKeys);
266        appendUrlParameter(url, "verbose", verbose);
267        return url.toString();
268      }
269    
270      private void appendRuleField(StringBuilder url, String field, boolean excludeField, String[] list) {
271        if (!excludeField) {
272          if (list == null || list.length == 0) {
273            appendUrlParameter(url, field, true);
274          } else {
275            appendUrlParameter(url, field, list);
276          }
277        }
278      }
279    
280      @Override
281      public final Class<Resource> getModelClass() {
282        return Resource.class;
283      }
284    
285      public static ResourceQuery createForMetrics(String resourceKeyOrId, String... metricKeys) {
286        return new ResourceQuery(resourceKeyOrId)
287            .setMetrics(metricKeys);
288      }
289    
290      public static ResourceQuery createForResource(Resource resource, String... metricKeys) {
291        return new ResourceQuery(resource.getId().toString())
292            .setMetrics(metricKeys);
293      }
294    }