001/* 002 * SonarQube, open source software quality management tool. 003 * Copyright (C) 2008-2014 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 includeAlerts = null; 041 private Boolean verbose = Boolean.FALSE; 042 043 public ResourceQuery() { 044 } 045 046 public ResourceQuery(String resourceKeyOrId) { 047 this.resourceKeyOrId = resourceKeyOrId; 048 } 049 050 public ResourceQuery(long resourceId) { 051 this.resourceKeyOrId = String.valueOf(resourceId); 052 } 053 054 public Integer getDepth() { 055 return depth; 056 } 057 058 public ResourceQuery setDepth(Integer depth) { 059 this.depth = depth; 060 return this; 061 } 062 063 public ResourceQuery setAllDepths() { 064 return setDepth(DEPTH_UNLIMITED); 065 } 066 067 public String getResourceKeyOrId() { 068 return resourceKeyOrId; 069 } 070 071 public ResourceQuery setResourceKeyOrId(String resourceKeyOrId) { 072 this.resourceKeyOrId = resourceKeyOrId; 073 return this; 074 } 075 076 public ResourceQuery setResourceId(int resourceId) { 077 this.resourceKeyOrId = Integer.toString(resourceId); 078 return this; 079 } 080 081 public ResourceQuery setCharacteristics(String... keys) { 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 @Deprecated 145 public ResourceQuery setRuleCategories(String... ruleCategories) { 146 return this; 147 } 148 149 /** 150 * @since 2.5 151 */ 152 public String[] getRuleSeverities() { 153 return ruleSeverities; 154 } 155 156 /** 157 * @since 2.5 158 * @param ruleSeverities values: BLOCKER, CRITICAL, MAJOR, MINOR, INFO 159 */ 160 public ResourceQuery setRuleSeverities(String... ruleSeverities) { 161 this.ruleSeverities = ruleSeverities; 162 this.excludeRuleSeverities = false; 163 return this; 164 } 165 166 /** 167 * @deprecated since 2.5 use {@link #getRuleSeverities()} instead. See http://jira.codehaus.org/browse/SONAR-1829 168 */ 169 @Deprecated 170 public String[] getRulePriorities() { 171 return ruleSeverities; 172 } 173 174 /** 175 * @deprecated since 2.5 use {@link #setRuleSeverities(String...)} instead. See http://jira.codehaus.org/browse/SONAR-1829 176 */ 177 @Deprecated 178 public ResourceQuery setRulePriorities(String... rulePriorities) { 179 return setRuleSeverities(rulePriorities); 180 } 181 182 public boolean isExcludeRules() { 183 return excludeRules; 184 } 185 186 public ResourceQuery setExcludeRules(boolean excludeRules) { 187 this.excludeRules = excludeRules; 188 return this; 189 } 190 191 /** 192 * @deprecated since 2.5 not used anymore 193 */ 194 @Deprecated 195 public boolean isExcludeRuleCategories() { 196 return false; 197 } 198 199 /** 200 * @deprecated since 2.5 not used anymore 201 */ 202 @Deprecated 203 public ResourceQuery setExcludeRuleCategories(boolean b) { 204 return this; 205 } 206 207 /** 208 * @since 2.5 209 */ 210 public boolean isExcludeRuleSeverities() { 211 return excludeRuleSeverities; 212 } 213 214 public ResourceQuery setExcludeRuleSeverities(boolean excludeRuleSeverities) { 215 this.excludeRuleSeverities = excludeRuleSeverities; 216 return this; 217 } 218 219 /** 220 * @deprecated since 2.5 use {@link #isExcludeRuleSeverities()} instead. See http://jira.codehaus.org/browse/SONAR-1829 221 */ 222 @Deprecated 223 public boolean isExcludeRulePriorities() { 224 return excludeRuleSeverities; 225 } 226 227 /** 228 * @deprecated since 2.5 use {@link #setExcludeRuleSeverities(boolean)} instead. See http://jira.codehaus.org/browse/SONAR-1829 229 */ 230 @Deprecated 231 public ResourceQuery setExcludeRulePriorities(boolean b) { 232 this.excludeRuleSeverities = b; 233 return this; 234 } 235 236 public Boolean isVerbose() { 237 return verbose; 238 } 239 240 public ResourceQuery setVerbose(Boolean verbose) { 241 this.verbose = verbose; 242 return this; 243 } 244 245 public Boolean isIncludeTrends() { 246 return includeTrends; 247 } 248 249 public ResourceQuery setIncludeTrends(Boolean includeTrends) { 250 this.includeTrends = includeTrends; 251 return this; 252 } 253 254 public Boolean isIncludeAlerts() { 255 return includeAlerts; 256 } 257 258 public ResourceQuery setIncludeAlerts(Boolean includeAlerts) { 259 this.includeAlerts = includeAlerts; 260 return this; 261 } 262 263 @Override 264 public String getUrl() { 265 StringBuilder url = new StringBuilder(BASE_URL); 266 url.append('?'); 267 appendUrlParameter(url, "resource", resourceKeyOrId); 268 appendUrlParameter(url, "metrics", metrics); 269 appendUrlParameter(url, "scopes", scopes); 270 appendUrlParameter(url, "qualifiers", qualifiers); 271 appendUrlParameter(url, "depth", depth); 272 appendUrlParameter(url, "limit", limit); 273 appendRuleField(url, "rules", excludeRules, rules); 274 appendRuleField(url, "rule_priorities", excludeRuleSeverities, ruleSeverities); 275 appendUrlParameter(url, "includetrends", includeTrends); 276 appendUrlParameter(url, "characteristics", characteristicKeys); 277 appendUrlParameter(url, "includealerts", includeAlerts); 278 appendUrlParameter(url, "verbose", verbose); 279 return url.toString(); 280 } 281 282 private void appendRuleField(StringBuilder url, String field, boolean excludeField, String[] list) { 283 if (!excludeField) { 284 if (list == null || list.length == 0) { 285 appendUrlParameter(url, field, true); 286 } else { 287 appendUrlParameter(url, field, list); 288 } 289 } 290 } 291 292 @Override 293 public final Class<Resource> getModelClass() { 294 return Resource.class; 295 } 296 297 public static ResourceQuery createForMetrics(String resourceKeyOrId, String... metricKeys) { 298 return new ResourceQuery(resourceKeyOrId) 299 .setMetrics(metricKeys); 300 } 301 302 public static ResourceQuery createForResource(Resource resource, String... metricKeys) { 303 Integer id = resource.getId(); 304 if (id == null) { 305 throw new IllegalArgumentException("id must be set"); 306 } 307 return new ResourceQuery(id.toString()) 308 .setMetrics(metricKeys); 309 } 310 311 /** 312 * @since 2.10 313 */ 314 public static ResourceQuery create(String resourceKey) { 315 return new ResourceQuery(resourceKey); 316 } 317}