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 String[] characteristicKeys;
037 private String model;
038 private boolean excludeRules = true;
039 private boolean excludeRuleCategories = true;
040 private boolean excludeRulePriorities = true;
041 private Boolean includeTrends = null;
042 private Boolean verbose = Boolean.FALSE;
043
044 public ResourceQuery() {
045 }
046
047 public ResourceQuery(String resourceKeyOrId) {
048 this.resourceKeyOrId = resourceKeyOrId;
049 }
050
051 public ResourceQuery(long resourceId) {
052 this.resourceKeyOrId = String.valueOf(resourceId);
053 }
054
055 public Integer getDepth() {
056 return depth;
057 }
058
059 public ResourceQuery setDepth(Integer depth) {
060 this.depth = depth;
061 return this;
062 }
063
064 public ResourceQuery setAllDepths() {
065 return setDepth(DEPTH_UNLIMITED);
066 }
067
068 public String getResourceKeyOrId() {
069 return resourceKeyOrId;
070 }
071
072 public ResourceQuery setResourceKeyOrId(String resourceKeyOrId) {
073 this.resourceKeyOrId = resourceKeyOrId;
074 return this;
075 }
076
077 public ResourceQuery setResourceId(int resourceId) {
078 this.resourceKeyOrId = Integer.toString(resourceId);
079 return this;
080 }
081
082 public ResourceQuery setCharacteristicKeys(String model, String... keys) {
083 this.model=model;
084 this.characteristicKeys = keys;
085 return this;
086 }
087
088
089 public Integer getLimit() {
090 return limit;
091 }
092
093 public ResourceQuery setLimit(Integer limit) {
094 this.limit = limit;
095 return this;
096 }
097
098 public String[] getScopes() {
099 return scopes;
100 }
101
102 public ResourceQuery setScopes(String... scopes) {
103 this.scopes = scopes;
104 return this;
105 }
106
107 public String[] getQualifiers() {
108 return qualifiers;
109 }
110
111 public ResourceQuery setQualifiers(String... qualifiers) {
112 this.qualifiers = qualifiers;
113 return this;
114 }
115
116 public String[] getMetrics() {
117 return metrics;
118 }
119
120 public ResourceQuery setMetrics(String... metrics) {
121 this.metrics = metrics;
122 return this;
123 }
124
125 public String[] getRules() {
126 return rules;
127 }
128
129 public ResourceQuery setRules(String... rules) {
130 this.rules = rules;
131 this.excludeRules = false;
132 return this;
133 }
134
135 public String[] getRuleCategories() {
136 return ruleCategories;
137 }
138
139 /**
140 * @param ruleCategories values: Maintainability, Usability, Reliability, Efficiency, Portability
141 */
142 public ResourceQuery setRuleCategories(String... ruleCategories) {
143 this.ruleCategories = ruleCategories;
144 this.excludeRuleCategories = false;
145 return this;
146 }
147
148 public String[] getRulePriorities() {
149 return rulePriorities;
150 }
151
152 /**
153 * @param rulePriorities values: BLOCKER, CRITICAL, MAJOR, MINOR, INFO
154 */
155 public ResourceQuery setRulePriorities(String... rulePriorities) {
156 this.rulePriorities = rulePriorities;
157 this.excludeRulePriorities = false;
158 return this;
159 }
160
161 public boolean isExcludeRules() {
162 return excludeRules;
163 }
164
165 public ResourceQuery setExcludeRules(boolean excludeRules) {
166 this.excludeRules = excludeRules;
167 return this;
168 }
169
170 public boolean isExcludeRuleCategories() {
171 return excludeRuleCategories;
172 }
173
174 public ResourceQuery setExcludeRuleCategories(boolean excludeRuleCategories) {
175 this.excludeRuleCategories = excludeRuleCategories;
176 return this;
177 }
178
179 public boolean isExcludeRulePriorities() {
180 return excludeRulePriorities;
181 }
182
183 public ResourceQuery setExcludeRulePriorities(boolean excludeRulePriorities) {
184 this.excludeRulePriorities = excludeRulePriorities;
185 return this;
186 }
187
188 public Boolean isVerbose() {
189 return verbose;
190 }
191
192 public ResourceQuery setVerbose(Boolean verbose) {
193 this.verbose = verbose;
194 return this;
195 }
196
197 public Boolean isIncludeTrends() {
198 return includeTrends;
199 }
200
201 public ResourceQuery setIncludeTrends(Boolean includeTrends) {
202 this.includeTrends = includeTrends;
203 return this;
204 }
205
206 @Override
207 public String getUrl() {
208 StringBuilder url = new StringBuilder(BASE_URL);
209 url.append('?');
210 appendUrlParameter(url, "resource", resourceKeyOrId);
211 appendUrlParameter(url, "metrics", metrics);
212 appendUrlParameter(url, "scopes", scopes);
213 appendUrlParameter(url, "qualifiers", qualifiers);
214 appendUrlParameter(url, "depth", depth);
215 appendUrlParameter(url, "limit", limit);
216 appendRuleField(url, "rules", excludeRules, rules);
217 appendRuleField(url, "rule_categories", excludeRuleCategories, ruleCategories);
218 appendRuleField(url, "rule_priorities", excludeRulePriorities, rulePriorities);
219 appendUrlParameter(url, "includetrends", includeTrends);
220 appendUrlParameter(url, "model", model);
221 appendUrlParameter(url, "characteristics", characteristicKeys);
222 appendUrlParameter(url, "verbose", verbose);
223 return url.toString();
224 }
225
226 private void appendRuleField(StringBuilder url, String field, boolean excludeField, String[] list) {
227 if (!excludeField) {
228 if (list == null || list.length == 0) {
229 appendUrlParameter(url, field, true);
230 } else {
231 appendUrlParameter(url, field, list);
232 }
233 }
234 }
235
236 @Override
237 public final Class<Resource> getModelClass() {
238 return Resource.class;
239 }
240
241 public static ResourceQuery createForMetrics(String resourceKeyOrId, String... metricKeys) {
242 return new ResourceQuery(resourceKeyOrId)
243 .setMetrics(metricKeys);
244 }
245
246 public static ResourceQuery createForResource(Resource resource, String... metricKeys) {
247 return new ResourceQuery(resource.getId().toString())
248 .setMetrics(metricKeys);
249 }
250 }