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 */
020 package org.sonar.wsclient.services;
021
022 import javax.annotation.CheckForNull;
023 import javax.annotation.Nullable;
024
025 import java.util.Collections;
026 import java.util.Date;
027 import java.util.List;
028
029 public class Resource extends Model {
030
031 /* SCOPES */
032 public static final String SCOPE_SET = "PRJ";
033 public static final String SCOPE_SPACE = "DIR";
034 public static final String SCOPE_ENTITY = "FIL";
035
036 /* QUALIFIERS */
037 public static final String QUALIFIER_VIEW = "VW";
038 public static final String QUALIFIER_SUBVIEW = "SVW";
039 public static final String QUALIFIER_LIB = "LIB";
040 public static final String QUALIFIER_PROJECT = "TRK";
041 public static final String QUALIFIER_MODULE = "BRC";
042 public static final String QUALIFIER_PACKAGE = "PAC";
043 public static final String QUALIFIER_DIRECTORY = "DIR";
044 public static final String QUALIFIER_FILE = "FIL";
045 public static final String QUALIFIER_CLASS = "CLA";
046 public static final String QUALIFIER_UNIT_TEST_CLASS = "UTS";
047
048 /* LANGUAGES */
049 public static final String LANGUAGE_JAVA = "java";
050
051 private Integer id;
052 private String key;
053 private String name;
054 private String longName;
055 private String scope;
056 private String qualifier;
057 private String language;
058 private String version;
059 private Integer copy;
060 private String description;
061 private Date date;
062 private List<Measure> measures;
063 private Date creationDate;
064
065 // periods used for variations and tracking of violations
066 private String period1Mode, period2Mode, period3Mode, period4Mode, period5Mode;
067 private String period1Param, period2Param, period3Param, period4Param, period5Param;
068 private Date period1Date, period2Date, period3Date, period4Date, period5Date;
069
070 @CheckForNull
071 public Integer getId() {
072 return id;
073 }
074
075 public Resource setId(@Nullable Integer id) {
076 this.id = id;
077 return this;
078 }
079
080 @CheckForNull
081 public String getKey() {
082 return key;
083 }
084
085 public Resource setKey(@Nullable String key) {
086 this.key = key;
087 return this;
088 }
089
090 @CheckForNull
091 public String getDescription() {
092 return description;
093 }
094
095 public Resource setDescription(@Nullable String description) {
096 this.description = description;
097 return this;
098 }
099
100 @CheckForNull
101 public String getName() {
102 return name;
103 }
104
105 @CheckForNull
106 public String getName(boolean longFormatIfDefined) {
107 if (longFormatIfDefined && longName != null && !"".equals(longName)) {
108 return longName;
109 }
110 return name;
111 }
112
113 @CheckForNull
114 public String getLongName() {
115 return longName;
116 }
117
118 public Resource setLongName(@Nullable String longName) {
119 this.longName = longName;
120 return this;
121 }
122
123 public Resource setName(@Nullable String s) {
124 this.name = s;
125 return this;
126 }
127
128 @CheckForNull
129 public String getScope() {
130 return scope;
131 }
132
133 public Resource setScope(@Nullable String scope) {
134 this.scope = scope;
135 return this;
136 }
137
138 @CheckForNull
139 public String getQualifier() {
140 return qualifier;
141 }
142
143 public Resource setQualifier(@Nullable String qualifier) {
144 this.qualifier = qualifier;
145 return this;
146 }
147
148 @CheckForNull
149 public String getLanguage() {
150 return language;
151 }
152
153 public Resource setLanguage(@Nullable String language) {
154 this.language = language;
155 return this;
156 }
157
158 @CheckForNull
159 public String getVersion() {
160 return version;
161 }
162
163 public Resource setVersion(@Nullable String version) {
164 this.version = version;
165 return this;
166 }
167
168 @CheckForNull
169 public Integer getCopy() {
170 return copy;
171 }
172
173 public Resource setCopy(@Nullable Integer copy) {
174 this.copy = copy;
175 return this;
176 }
177
178 @CheckForNull
179 public Date getDate() {
180 return date;
181 }
182
183 public Resource setDate(@Nullable Date d) {
184 this.date = d;
185 return this;
186 }
187
188 @CheckForNull
189 public Date getCreationDate() {
190 return creationDate;
191 }
192
193 public Resource setCreationDate(@Nullable Date d) {
194 this.creationDate = d;
195 return this;
196 }
197
198 public List<Measure> getMeasures() {
199 if (measures == null) {
200 return Collections.emptyList();
201 }
202 return measures;
203 }
204
205 public Measure getMeasure(String metricKey) {
206 for (Measure measure : getMeasures()) {
207 if (metricKey.equals(measure.getMetricKey())) {
208 return measure;
209 }
210 }
211 return null;
212 }
213
214 public Double getMeasureValue(String metricKey) {
215 Measure measure = getMeasure(metricKey);
216 if (measure != null) {
217 return measure.getValue();
218 }
219 return null;
220 }
221
222 public Integer getMeasureIntValue(String metricKey) {
223 Double d = getMeasureValue(metricKey);
224 if (d != null) {
225 return d.intValue();
226 }
227 return null;
228 }
229
230 public String getMeasureFormattedValue(String metricKey, String defaultValue) {
231 Measure measure = getMeasure(metricKey);
232 if (measure != null) {
233 return measure.getFormattedValue(defaultValue);
234 }
235 return defaultValue;
236 }
237
238 public void setMeasures(List<Measure> measures) {
239 this.measures = measures;
240 }
241
242 /**
243 * @since 2.5 only on projects, else null
244 */
245 @CheckForNull
246 public String getPeriod1Mode() {
247 return period1Mode;
248 }
249
250 /**
251 * @since 2.5
252 */
253 public Resource setPeriod1Mode(@Nullable String period1Mode) {
254 this.period1Mode = period1Mode;
255 return this;
256 }
257
258 /**
259 * @since 2.5 only on projects, else null
260 */
261 @CheckForNull
262 public String getPeriod2Mode() {
263 return period2Mode;
264 }
265
266 /**
267 * @since 2.5
268 */
269 public Resource setPeriod2Mode(@Nullable String period2Mode) {
270 this.period2Mode = period2Mode;
271 return this;
272 }
273
274 /**
275 * @since 2.5 only on projects, else null
276 */
277 @CheckForNull
278 public String getPeriod3Mode() {
279 return period3Mode;
280 }
281
282 /**
283 * @since 2.5
284 */
285 public Resource setPeriod3Mode(@Nullable String period3Mode) {
286 this.period3Mode = period3Mode;
287 return this;
288 }
289
290 /**
291 * @since 2.5 only on projects, else null
292 */
293 @CheckForNull
294 public String getPeriod4Mode() {
295 return period4Mode;
296 }
297
298 /**
299 * @since 2.5
300 */
301 public Resource setPeriod4Mode(@Nullable String period4Mode) {
302 this.period4Mode = period4Mode;
303 return this;
304 }
305
306 /**
307 * @since 2.5 only on projects, else null
308 */
309 @CheckForNull
310 public String getPeriod5Mode() {
311 return period5Mode;
312 }
313
314 /**
315 * @since 2.5
316 */
317 public Resource setPeriod5Mode(@Nullable String period5Mode) {
318 this.period5Mode = period5Mode;
319 return this;
320 }
321
322 /**
323 * @since 2.5 only on projects, else null
324 */
325 @CheckForNull
326 public String getPeriod1Param() {
327 return period1Param;
328 }
329
330 /**
331 * @since 2.5
332 */
333 public Resource setPeriod1Param(@Nullable String period1Param) {
334 this.period1Param = period1Param;
335 return this;
336 }
337
338 /**
339 * @since 2.5 only on projects, else null
340 */
341 @CheckForNull
342 public String getPeriod2Param() {
343 return period2Param;
344 }
345
346 /**
347 * @since 2.5
348 */
349 public Resource setPeriod2Param(@Nullable String period2Param) {
350 this.period2Param = period2Param;
351 return this;
352 }
353
354 /**
355 * @since 2.5 only on projects, else null
356 */
357 @CheckForNull
358 public String getPeriod3Param() {
359 return period3Param;
360 }
361
362 /**
363 * @since 2.5
364 */
365 public Resource setPeriod3Param(@Nullable String period3Param) {
366 this.period3Param = period3Param;
367 return this;
368 }
369
370 /**
371 * @since 2.5 only on projects, else null
372 */
373 @CheckForNull
374 public String getPeriod4Param() {
375 return period4Param;
376 }
377
378 /**
379 * @since 2.5
380 */
381 public Resource setPeriod4Param(@Nullable String period4Param) {
382 this.period4Param = period4Param;
383 return this;
384 }
385
386 /**
387 * @since 2.5 only on projects, else null
388 */
389 @CheckForNull
390 public String getPeriod5Param() {
391 return period5Param;
392 }
393
394 /**
395 * @since 2.5
396 */
397 public Resource setPeriod5Param(@Nullable String period5Param) {
398 this.period5Param = period5Param;
399 return this;
400 }
401
402 /**
403 * @since 2.5 only on projects, else null
404 */
405 @CheckForNull
406 public Date getPeriod1Date() {
407 return period1Date;
408 }
409
410 /**
411 * @since 2.5
412 */
413 public Resource setPeriod1Date(@Nullable Date period1Date) {
414 this.period1Date = period1Date;
415 return this;
416 }
417
418 /**
419 * @since 2.5 only on projects, else null
420 */
421 @CheckForNull
422 public Date getPeriod2Date() {
423 return period2Date;
424 }
425
426 /**
427 * @since 2.5
428 */
429 public Resource setPeriod2Date(@Nullable Date period2Date) {
430 this.period2Date = period2Date;
431 return this;
432 }
433
434 /**
435 * @since 2.5 only on projects, else null
436 */
437 @CheckForNull
438 public Date getPeriod3Date() {
439 return period3Date;
440 }
441
442 /**
443 * @since 2.5
444 */
445 public Resource setPeriod3Date(@Nullable Date period3Date) {
446 this.period3Date = period3Date;
447 return this;
448 }
449
450 /**
451 * @since 2.5 only on projects, else null
452 */
453 @CheckForNull
454 public Date getPeriod4Date() {
455 return period4Date;
456 }
457
458 /**
459 * @since 2.5
460 */
461 public Resource setPeriod4Date(@Nullable Date period4Date) {
462 this.period4Date = period4Date;
463 return this;
464 }
465
466 /**
467 * @since 2.5 only on projects, else null
468 */
469 @CheckForNull
470 public Date getPeriod5Date() {
471 return period5Date;
472 }
473
474 /**
475 * @since 2.5
476 */
477 public Resource setPeriod5Date(@Nullable Date period5Date) {
478 this.period5Date = period5Date;
479 return this;
480 }
481
482 @Override
483 public String toString() {
484 return new StringBuilder()
485 .append("[id=")
486 .append(id)
487 .append(",key=")
488 .append(key)
489 .append("]")
490 .toString();
491 }
492 }