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 ViolationQuery extends Query<Violation> {
023
024  public static final String BASE_URL = "/api/violations";
025
026  private String resourceKeyOrId;
027  private int depth = 0;
028  private String[] scopes;
029  private String[] qualifiers;
030  private String[] ruleKeys;
031  private String[] categories;
032  private String[] severities;
033  private Integer limit;
034  private Boolean includeReview;
035  private String output;
036
037  public ViolationQuery(String resourceKeyOrId) {
038    this.resourceKeyOrId = resourceKeyOrId;
039  }
040
041  public String[] getScopes() {
042    return scopes;
043  }
044
045  public ViolationQuery setScopes(String... scopes) {
046    this.scopes = scopes;
047    return this;
048  }
049
050  public String[] getQualifiers() {
051    return qualifiers;
052  }
053
054  public ViolationQuery setQualifiers(String... qualifiers) {
055    this.qualifiers = qualifiers;
056    return this;
057  }
058
059  public String[] getRuleKeys() {
060    return ruleKeys;
061  }
062
063  public ViolationQuery setRuleKeys(String... ruleKeys) {
064    this.ruleKeys = ruleKeys;
065    return this;
066  }
067
068  public String[] getCategories() {
069    return categories;
070  }
071
072  public ViolationQuery setCategories(String... categories) {
073    this.categories = categories;
074    return this;
075  }
076
077  /**
078   * @since 2.5
079   */
080  public String[] getSeverities() {
081    return severities;
082  }
083
084  /**
085   * @since 2.5
086   */
087  public ViolationQuery setSeverities(String... severities) {
088    this.severities = severities;
089    return this;
090  }
091
092  /**
093   * @deprecated since 2.5 use {@link #getSeverities()} instead. See http://jira.codehaus.org/browse/SONAR-1829
094   */
095  @Deprecated
096  public String[] getPriorities() {
097    return severities;
098  }
099
100  /**
101   * @deprecated since 2.5 use {@link #setSeverities(String...)} instead. See http://jira.codehaus.org/browse/SONAR-1829
102   */
103  @Deprecated
104  public ViolationQuery setPriorities(String... priorities) {
105    this.severities = priorities;
106    return this;
107  }
108
109  public int getDepth() {
110    return depth;
111  }
112
113  public ViolationQuery setDepth(int depth) {
114    this.depth = depth;
115    return this;
116  }
117
118  /**
119   * @since 2.5
120   */
121  public Integer getLimit() {
122    return limit;
123  }
124
125  /**
126   * @since 2.5
127   */
128  public ViolationQuery setLimit(Integer limit) {
129    this.limit = limit;
130    return this;
131  }
132
133  /**
134   * @since 2.8
135   */
136  public Boolean getIncludeReview() {
137    return includeReview;
138  }
139
140  /**
141   * @since 2.8
142   */
143  public ViolationQuery setIncludeReview(Boolean includeReview) {
144    this.includeReview = includeReview;
145    return this;
146  }
147
148  /**
149   * @since 2.8
150   */
151  public String getOutput() {
152    return output;
153  }
154
155  /**
156   * @since 2.8
157   */
158  public ViolationQuery setOutput(String output) {
159    this.output = output;
160    return this;
161  }
162
163  @Override
164  public String getUrl() {
165    StringBuilder url = new StringBuilder(BASE_URL);
166    url.append('?');
167    appendUrlParameter(url, "resource", resourceKeyOrId);
168    if (depth != 0) {
169      url.append("depth=").append(depth).append("&");
170    }
171    appendUrlParameter(url, "limit", limit);
172    appendUrlParameter(url, "scopes", scopes);
173    appendUrlParameter(url, "qualifiers", qualifiers);
174    appendUrlParameter(url, "rules", ruleKeys);
175    appendUrlParameter(url, "categories", categories);
176    appendUrlParameter(url, "priorities", severities);
177    appendUrlParameter(url, "include_review", includeReview);
178    appendUrlParameter(url, "output", output);
179
180    return url.toString();
181  }
182
183  @Override
184  public Class<Violation> getModelClass() {
185    return Violation.class;
186  }
187
188  public static ViolationQuery createForResource(Resource resource) {
189    return new ViolationQuery(resource.getId().toString());
190  }
191
192  public static ViolationQuery createForResource(String resourceIdOrKey) {
193    return new ViolationQuery(resourceIdOrKey);
194  }
195}