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 ResourceQuery extends Query<Resource> {
023 public static final String BASE_URL = "/api/resources";
024
025 public final static int DEPTH_UNLIMITED = -1;
026
027 private Integer depth;
028 private String resourceKeyOrId;
029 private Integer limit;
030 private String[] scopes;
031 private String[] qualifiers;
032 private String[] metrics;
033 private String[] rules;
034 private String[] ruleCategories;
035 private String[] rulePriorities;
036 private boolean excludeRules = true;
037 private boolean excludeRuleCategories = true;
038 private boolean excludeRulePriorities = 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 Integer getLimit() {
081 return limit;
082 }
083
084 public ResourceQuery setLimit(Integer limit) {
085 this.limit = limit;
086 return this;
087 }
088
089 public String[] getScopes() {
090 return scopes;
091 }
092
093 public ResourceQuery setScopes(String... scopes) {
094 this.scopes = scopes;
095 return this;
096 }
097
098 public String[] getQualifiers() {
099 return qualifiers;
100 }
101
102 public ResourceQuery setQualifiers(String... qualifiers) {
103 this.qualifiers = qualifiers;
104 return this;
105 }
106
107 public String[] getMetrics() {
108 return metrics;
109 }
110
111 public ResourceQuery setMetrics(String... metrics) {
112 this.metrics = metrics;
113 return this;
114 }
115
116 public String[] getRules() {
117 return rules;
118 }
119
120 public ResourceQuery setRules(String... rules) {
121 this.rules = rules;
122 this.excludeRules = false;
123 return this;
124 }
125
126 public String[] getRuleCategories() {
127 return ruleCategories;
128 }
129
130 /**
131 * @param ruleCategories values: Maintainability, Usability, Reliability, Efficiency, Portability
132 */
133 public ResourceQuery setRuleCategories(String... ruleCategories) {
134 this.ruleCategories = ruleCategories;
135 this.excludeRuleCategories = false;
136 return this;
137 }
138
139 public String[] getRulePriorities() {
140 return rulePriorities;
141 }
142
143 /**
144 * @param rulePriorities values: BLOCKER, CRITICAL, MAJOR, MINOR, INFO
145 */
146 public ResourceQuery setRulePriorities(String... rulePriorities) {
147 this.rulePriorities = rulePriorities;
148 this.excludeRulePriorities = false;
149 return this;
150 }
151
152 public boolean isExcludeRules() {
153 return excludeRules;
154 }
155
156 public ResourceQuery setExcludeRules(boolean excludeRules) {
157 this.excludeRules = excludeRules;
158 return this;
159 }
160
161 public boolean isExcludeRuleCategories() {
162 return excludeRuleCategories;
163 }
164
165 public ResourceQuery setExcludeRuleCategories(boolean excludeRuleCategories) {
166 this.excludeRuleCategories = excludeRuleCategories;
167 return this;
168 }
169
170 public boolean isExcludeRulePriorities() {
171 return excludeRulePriorities;
172 }
173
174 public ResourceQuery setExcludeRulePriorities(boolean excludeRulePriorities) {
175 this.excludeRulePriorities = excludeRulePriorities;
176 return this;
177 }
178
179 public Boolean isVerbose() {
180 return verbose;
181 }
182
183 public ResourceQuery setVerbose(Boolean verbose) {
184 this.verbose = verbose;
185 return this;
186 }
187
188 public Boolean isIncludeTrends() {
189 return includeTrends;
190 }
191
192 public ResourceQuery setIncludeTrends(Boolean includeTrends) {
193 this.includeTrends = includeTrends;
194 return this;
195 }
196
197 @Override
198 public String getUrl() {
199 StringBuilder url = new StringBuilder(BASE_URL);
200 url.append('?');
201 append(url, "resource", resourceKeyOrId);
202 append(url, "metrics", metrics);
203 append(url, "scopes", scopes);
204 append(url, "qualifiers", qualifiers);
205 append(url, "depth", depth);
206 append(url, "limit", limit);
207 appendRuleField(url, "rules", excludeRules, rules);
208 appendRuleField(url, "rule_categories", excludeRuleCategories, ruleCategories);
209 appendRuleField(url, "rule_priorities", excludeRulePriorities, rulePriorities);
210 append(url, "includetrends", includeTrends);
211 append(url, "verbose", verbose);
212 return url.toString();
213 }
214
215 private void appendRuleField(StringBuilder url, String field, boolean excludeField, String[] list) {
216 if (!excludeField) {
217 if (list == null || list.length == 0) {
218 append(url, field, true);
219 } else {
220 append(url, field, list);
221 }
222 }
223 }
224
225 private void append(StringBuilder url, String paramKey, Object paramValue) {
226 if (paramValue != null) {
227 url.append(paramKey)
228 .append('=')
229 .append(paramValue)
230 .append('&');
231 }
232 }
233
234 private void append(StringBuilder url, String paramKey, Object[] paramValues) {
235 if (paramValues != null && paramValues.length > 0) {
236 url.append(paramKey).append('=');
237 for (int index = 0; index < paramValues.length; index++) {
238 if (index > 0) {
239 url.append(',');
240 }
241 if (paramValues[index] != null) {
242 url.append(paramValues[index]);
243 }
244 }
245 url.append('&');
246 }
247 }
248
249 @Override
250 public final Class<Resource> getModelClass() {
251 return Resource.class;
252 }
253
254 public static ResourceQuery createForMetrics(String resourceKeyOrId, String... metricKeys) {
255 return new ResourceQuery(resourceKeyOrId)
256 .setMetrics(metricKeys);
257 }
258
259 public static ResourceQuery createForResource(Resource resource, String... metricKeys) {
260 return new ResourceQuery(resource.getId().toString())
261 .setMetrics(metricKeys);
262 }
263 }