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 ViolationQuery extends Query<Violation> {
023      public static final String BASE_URL = "/api/violations";
024    
025      private String[] scopes;
026      private String[] qualifiers;
027      private String[] ruleKeys;
028      private String[] categories;
029      private String[] priorities;
030      private int depth = 0;
031      private String resourceKeyOrId;
032    
033      public ViolationQuery(String resourceKeyOrId) {
034        this.resourceKeyOrId = resourceKeyOrId;
035      }
036    
037      public String[] getScopes() {
038        return scopes;
039      }
040    
041      public ViolationQuery setScopes(String... scopes) {
042        this.scopes = scopes;
043        return this;
044      }
045    
046      public String[] getQualifiers() {
047        return qualifiers;
048      }
049    
050      public ViolationQuery setQualifiers(String... qualifiers) {
051        this.qualifiers = qualifiers;
052        return this;
053      }
054    
055      public String[] getRuleKeys() {
056        return ruleKeys;
057      }
058    
059      public ViolationQuery setRuleKeys(String... ruleKeys) {
060        this.ruleKeys = ruleKeys;
061        return this;
062      }
063    
064      public String[] getCategories() {
065        return categories;
066      }
067    
068      public ViolationQuery setCategories(String... categories) {
069        this.categories = categories;
070        return this;
071      }
072    
073      public String[] getPriorities() {
074        return priorities;
075      }
076    
077      public ViolationQuery setPriorities(String... priorities) {
078        this.priorities = priorities;
079        return this;
080      }
081    
082      public int getDepth() {
083        return depth;
084      }
085    
086      public ViolationQuery setDepth(int depth) {
087        this.depth = depth;
088        return this;
089      }
090    
091      @Override
092      public String getUrl() {
093        StringBuilder url = new StringBuilder(BASE_URL);
094        url.append("?resource=")
095            .append(resourceKeyOrId)
096            .append("&");
097    
098        if (depth != 0) {
099          url.append("depth").append(depth).append("&");
100        }
101        append(url, "scopes", scopes);
102        append(url, "qualifiers", qualifiers);
103        append(url, "rules", ruleKeys);
104        append(url, "categories", categories);
105        append(url, "priorities", priorities);
106        return url.toString();
107      }
108    
109      private void append(StringBuilder url, String paramKey, Object[] paramValues) {
110        if (paramValues != null && paramValues.length > 0) {
111          url.append(paramKey).append('=');
112          for (int index = 0; index < paramValues.length; index++) {
113            if (index > 0) {
114              url.append(',');
115            }
116            url.append(paramValues[index]);
117          }
118          url.append('&');
119        }
120      }
121    
122      @Override
123      public Class<Violation> getModelClass() {
124        return Violation.class;
125      }
126    
127      public static ViolationQuery createForResource(Resource resource) {
128        return new ViolationQuery(resource.getId().toString());
129      }
130    
131      public static ViolationQuery createForResource(String resourceIdOrKey) {
132        return new ViolationQuery(resourceIdOrKey);
133      }
134    }