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