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 */ 020package org.sonar.wsclient.services; 021 022public 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 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 setCharacteristics(String... keys) { 081 this.characteristicKeys = keys; 082 return this; 083 } 084 085 public Integer getLimit() { 086 return limit; 087 } 088 089 public ResourceQuery setLimit(Integer limit) { 090 this.limit = limit; 091 return this; 092 } 093 094 public String[] getScopes() { 095 return scopes; 096 } 097 098 public ResourceQuery setScopes(String... scopes) { 099 this.scopes = scopes; 100 return this; 101 } 102 103 public String[] getQualifiers() { 104 return qualifiers; 105 } 106 107 public ResourceQuery setQualifiers(String... qualifiers) { 108 this.qualifiers = qualifiers; 109 return this; 110 } 111 112 public String[] getMetrics() { 113 return metrics; 114 } 115 116 public ResourceQuery setMetrics(String... metrics) { 117 this.metrics = metrics; 118 return this; 119 } 120 121 public String[] getRules() { 122 return rules; 123 } 124 125 public ResourceQuery setRules(String... rules) { 126 this.rules = rules; 127 this.excludeRules = false; 128 return this; 129 } 130 131 /** 132 * @deprecated since 2.5 See http://jira.codehaus.org/browse/SONAR-2007 133 */ 134 @Deprecated 135 public String[] getRuleCategories() { 136 return null; 137 } 138 139 /** 140 * @param ruleCategories values: Maintainability, Usability, Reliability, Efficiency, Portability 141 * @deprecated since 2.5 See http://jira.codehaus.org/browse/SONAR-2007 142 */ 143 @Deprecated 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 @Deprecated 194 public boolean isExcludeRuleCategories() { 195 return false; 196 } 197 198 /** 199 * @deprecated since 2.5 not used anymore 200 */ 201 @Deprecated 202 public ResourceQuery setExcludeRuleCategories(boolean b) { 203 return this; 204 } 205 206 /** 207 * @since 2.5 208 */ 209 public boolean isExcludeRuleSeverities() { 210 return excludeRuleSeverities; 211 } 212 213 public ResourceQuery setExcludeRuleSeverities(boolean excludeRuleSeverities) { 214 this.excludeRuleSeverities = excludeRuleSeverities; 215 return this; 216 } 217 218 /** 219 * @deprecated since 2.5 use {@link #isExcludeRuleSeverities()} instead. See http://jira.codehaus.org/browse/SONAR-1829 220 */ 221 @Deprecated 222 public boolean isExcludeRulePriorities() { 223 return excludeRuleSeverities; 224 } 225 226 /** 227 * @deprecated since 2.5 use {@link #setExcludeRuleSeverities(boolean)} instead. See http://jira.codehaus.org/browse/SONAR-1829 228 */ 229 @Deprecated 230 public ResourceQuery setExcludeRulePriorities(boolean b) { 231 this.excludeRuleSeverities = b; 232 return this; 233 } 234 235 public Boolean isVerbose() { 236 return verbose; 237 } 238 239 public ResourceQuery setVerbose(Boolean verbose) { 240 this.verbose = verbose; 241 return this; 242 } 243 244 public Boolean isIncludeTrends() { 245 return includeTrends; 246 } 247 248 public ResourceQuery setIncludeTrends(Boolean includeTrends) { 249 this.includeTrends = includeTrends; 250 return this; 251 } 252 253 @Override 254 public String getUrl() { 255 StringBuilder url = new StringBuilder(BASE_URL); 256 url.append('?'); 257 appendUrlParameter(url, "resource", resourceKeyOrId); 258 appendUrlParameter(url, "metrics", metrics); 259 appendUrlParameter(url, "scopes", scopes); 260 appendUrlParameter(url, "qualifiers", qualifiers); 261 appendUrlParameter(url, "depth", depth); 262 appendUrlParameter(url, "limit", limit); 263 appendRuleField(url, "rules", excludeRules, rules); 264 appendRuleField(url, "rule_priorities", excludeRuleSeverities, ruleSeverities); 265 appendUrlParameter(url, "includetrends", includeTrends); 266 appendUrlParameter(url, "characteristics", characteristicKeys); 267 appendUrlParameter(url, "verbose", verbose); 268 return url.toString(); 269 } 270 271 private void appendRuleField(StringBuilder url, String field, boolean excludeField, String[] list) { 272 if (!excludeField) { 273 if (list == null || list.length == 0) { 274 appendUrlParameter(url, field, true); 275 } else { 276 appendUrlParameter(url, field, list); 277 } 278 } 279 } 280 281 @Override 282 public final Class<Resource> getModelClass() { 283 return Resource.class; 284 } 285 286 public static ResourceQuery createForMetrics(String resourceKeyOrId, String... metricKeys) { 287 return new ResourceQuery(resourceKeyOrId) 288 .setMetrics(metricKeys); 289 } 290 291 public static ResourceQuery createForResource(Resource resource, String... metricKeys) { 292 Integer id = resource.getId(); 293 if (id == null) { 294 throw new IllegalArgumentException("id must be set"); 295 } 296 return new ResourceQuery(id.toString()) 297 .setMetrics(metricKeys); 298 } 299 300 /** 301 * @since 2.10 302 */ 303 public static ResourceQuery create(String resourceKey) { 304 return new ResourceQuery(resourceKey); 305 } 306}