1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
|
/*
* SonarQube, open source software quality management tool.
* Copyright (C) 2008-2014 SonarSource
* mailto:contact AT sonarsource DOT com
*
* SonarQube is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* SonarQube is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.api.rules;
import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableSet;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
import org.sonar.api.database.DatabaseProperties;
import org.sonar.api.rule.RuleKey;
import org.sonar.api.utils.SonarException;
import org.sonar.check.Cardinality;
import javax.annotation.CheckForNull;
import javax.annotation.Nullable;
import javax.persistence.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;
@Entity
@Table(name = "rules")
public class Rule {
/**
* @since 3.6
*/
public static final String STATUS_BETA = "BETA";
/**
* @since 3.6
*/
public static final String STATUS_DEPRECATED = "DEPRECATED";
/**
* @since 3.6
*/
public static final String STATUS_READY = "READY";
/**
* For internal use only.
*
* @since 3.6
*/
public static final String STATUS_REMOVED = "REMOVED";
/**
* List of available status
*
* @since 3.6
*/
private static final Set<String> STATUS_LIST = ImmutableSet.of(STATUS_READY, STATUS_BETA, STATUS_DEPRECATED, STATUS_REMOVED);
/**
* @since 4.2
*/
private static final String[] DEFAULT_TAGS = new String[0];
@Id
@Column(name = "id")
@GeneratedValue
private Integer id;
/**
* The default priority given to a rule if not explicitly set
*/
public static final RulePriority DEFAULT_PRIORITY = RulePriority.MAJOR;
@Column(name = "name", updatable = true, nullable = true, length = 200)
private String name;
@Column(name = "plugin_rule_key", updatable = false, nullable = true, length = 200)
private String key;
@Column(name = "plugin_config_key", updatable = true, nullable = true, length = 500)
private String configKey;
@Column(name = "priority", updatable = true, nullable = true)
@Enumerated(EnumType.ORDINAL)
private RulePriority priority = DEFAULT_PRIORITY;
@Column(name = "description", updatable = true, nullable = true, length = DatabaseProperties.MAX_TEXT_SIZE)
private String description;
@Column(name = "plugin_name", updatable = true, nullable = false)
private String pluginName;
@Enumerated(EnumType.STRING)
@Column(name = "cardinality", updatable = true, nullable = false)
private Cardinality cardinality = Cardinality.SINGLE;
@Column(name = "status", updatable = true, nullable = true)
private String status = STATUS_READY;
@Column(name = "language", updatable = true, nullable = true)
private String language;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "parent_id", updatable = true, nullable = true)
private Rule parent = null;
@Column(name = "characteristic_id", updatable = true, nullable = true)
private Integer characteristicId;
@Column(name = "default_characteristic_id", updatable = true, nullable = true)
private Integer defaultCharacteristicId;
@org.hibernate.annotations.Cascade({org.hibernate.annotations.CascadeType.ALL, org.hibernate.annotations.CascadeType.DELETE_ORPHAN})
@OneToMany(mappedBy = "rule")
private List<RuleParam> params = new ArrayList<RuleParam>();
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "created_at", updatable = true, nullable = true)
private Date createdAt;
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "updated_at", updatable = true, nullable = true)
private Date updatedAt;
private transient String[] tags = DEFAULT_TAGS;
/**
* @deprecated since 2.3. Use the factory method {@link #create()}
*/
@Deprecated
public Rule() {
}
/**
* Creates rule with minimum set of info
*
* @param pluginName the plugin name indicates which plugin the rule belongs to
* @param key the key should be unique within a plugin, but it is even more careful for the time being that it is unique across the
* application
* @deprecated since 2.3. Use the factory method {@link #create()}
*/
@Deprecated
public Rule(String pluginName, String key) {
this.pluginName = pluginName;
this.key = key;
this.configKey = key;
}
public Integer getId() {
return id;
}
/**
* @deprecated since 2.3. visibility should be decreased to protected or package
*/
@Deprecated
public void setId(Integer id) {
this.id = id;
}
@CheckForNull
public String getName() {
return name;
}
/**
* Sets the rule name
*/
public Rule setName(@Nullable String name) {
this.name = removeNewLineCharacters(name);
return this;
}
public String getKey() {
return key;
}
/**
* Sets the rule key
*/
public Rule setKey(String key) {
this.key = key;
return this;
}
/**
* @deprecated since 2.5 use {@link #getRepositoryKey()} instead
*/
@Deprecated
public String getPluginName() {
return pluginName;
}
/**
* @deprecated since 2.5 use {@link #setRepositoryKey(String)} instead
*/
@Deprecated
public Rule setPluginName(String pluginName) {
this.pluginName = pluginName;
return this;
}
public String getConfigKey() {
return configKey;
}
/**
* Sets the configuration key
*/
public Rule setConfigKey(String configKey) {
this.configKey = configKey;
return this;
}
public String getDescription() {
return description;
}
/**
* Sets the rule description
*/
public Rule setDescription(String description) {
this.description = StringUtils.strip(description);
return this;
}
/**
* @deprecated in 3.6. Replaced by {@link #setStatus(String status)}.
*/
@Deprecated
public Rule setEnabled(Boolean enabled) {
throw new UnsupportedOperationException("No more supported since version 3.6.");
}
public Boolean isEnabled() {
return !STATUS_REMOVED.equals(status);
}
public List<RuleParam> getParams() {
return params;
}
public RuleParam getParam(String key) {
for (RuleParam param : params) {
if (StringUtils.equals(key, param.getKey())) {
return param;
}
}
return null;
}
/**
* Sets the rule parameters
*/
public Rule setParams(List<RuleParam> params) {
this.params.clear();
for (RuleParam param : params) {
param.setRule(this);
this.params.add(param);
}
return this;
}
public RuleParam createParameter() {
RuleParam parameter = new RuleParam()
.setRule(this);
params.add(parameter);
return parameter;
}
public RuleParam createParameter(String key) {
RuleParam parameter = new RuleParam()
.setKey(key)
.setRule(this);
params.add(parameter);
return parameter;
}
/**
* @deprecated since 2.5. See http://jira.codehaus.org/browse/SONAR-2007
*/
@Deprecated
public Integer getCategoryId() {
return null;
}
/**
* @since 2.5
*/
public RulePriority getSeverity() {
return priority;
}
/**
* @param severity severity to set, if null, uses the default priority.
* @since 2.5
*/
public Rule setSeverity(RulePriority severity) {
if (severity == null) {
this.priority = DEFAULT_PRIORITY;
} else {
this.priority = severity;
}
return this;
}
/**
* @deprecated since 2.5 use {@link #getSeverity()} instead. See http://jira.codehaus.org/browse/SONAR-1829
*/
@Deprecated
public RulePriority getPriority() {
return priority;
}
/**
* Sets the rule priority. If null, uses the default priority
*
* @deprecated since 2.5 use {@link #setSeverity(RulePriority)} instead. See http://jira.codehaus.org/browse/SONAR-1829
*/
@Deprecated
public Rule setPriority(RulePriority priority) {
return setSeverity(priority);
}
public String getRepositoryKey() {
return pluginName;
}
public Rule setRepositoryKey(String s) {
this.pluginName = s;
return this;
}
public Rule setUniqueKey(String repositoryKey, String key) {
return setRepositoryKey(repositoryKey).setKey(key).setConfigKey(key);
}
public Cardinality getCardinality() {
return cardinality;
}
public Rule setCardinality(Cardinality c) {
this.cardinality = c;
return this;
}
public Rule getParent() {
return parent;
}
public Rule setParent(Rule parent) {
this.parent = parent;
return this;
}
/**
* @since 3.6
*/
public String getStatus() {
return status;
}
/**
* @since 3.6
*/
public Rule setStatus(String status) {
if (!STATUS_LIST.contains(status)) {
throw new SonarException("The status of a rule can only contain : " + Joiner.on(", ").join(STATUS_LIST));
}
this.status = status;
return this;
}
/**
* @since 3.6
*/
public Date getCreatedAt() {
return createdAt;
}
/**
* @since 3.6
*/
public Rule setCreatedAt(Date d) {
this.createdAt = d;
return this;
}
/**
* @since 3.6
*/
public Date getUpdatedAt() {
return updatedAt;
}
/**
* @since 3.6
*/
public Rule setUpdatedAt(Date updatedAt) {
this.updatedAt = updatedAt;
return this;
}
/**
* @since 3.6
*/
public String getLanguage() {
return language;
}
/**
* For internal use only.
*
* @since 3.6
*/
public Rule setLanguage(String language) {
this.language = language;
return this;
}
/**
* For definition of rule only
*/
public String[] getTags() {
return tags;
}
/**
* For definition of rule only
*/
public void setTags(String[] tags) {
this.tags = tags;
}
/**
* For internal use only.
*
* @since 4.3
*/
@CheckForNull
public Integer getCharacteristicId() {
return characteristicId;
}
/**
* For internal use only.
*
* @since 4.3
*/
public Rule setCharacteristicId(@Nullable Integer characteristicId) {
this.characteristicId = characteristicId;
return this;
}
/**
* For internal use only.
*
* @since 4.3
*/
@CheckForNull
public Integer getDefaultCharacteristicId() {
return defaultCharacteristicId;
}
/**
* For internal use only.
*
* @since 4.3
*/
public Rule setDefaultCharacteristicId(@Nullable Integer defaultCharacteristicId) {
this.defaultCharacteristicId = defaultCharacteristicId;
return this;
}
@Override
public boolean equals(Object obj) {
if (!(obj instanceof Rule)) {
return false;
}
if (this == obj) {
return true;
}
Rule other = (Rule) obj;
return new EqualsBuilder()
.append(pluginName, other.getRepositoryKey())
.append(key, other.getKey())
.isEquals();
}
@Override
public int hashCode() {
return new HashCodeBuilder(17, 37)
.append(pluginName)
.append(key)
.toHashCode();
}
@Override
public String toString() {
// Note that ReflectionToStringBuilder will not work here - see SONAR-3077
return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE)
.append("id", id)
.append("name", name)
.append("key", key)
.append("configKey", configKey)
.append("plugin", pluginName)
.append("severity", priority)
.append("cardinality", cardinality)
.append("status", status)
.append("language", language)
.append("parent", parent)
.toString();
}
@CheckForNull
private String removeNewLineCharacters(@Nullable String text) {
String removedCRLF = StringUtils.remove(text, "\n");
removedCRLF = StringUtils.remove(removedCRLF, "\r");
removedCRLF = StringUtils.remove(removedCRLF, "\n\r");
removedCRLF = StringUtils.remove(removedCRLF, "\r\n");
return removedCRLF;
}
public static Rule create() {
return new Rule();
}
/**
* Create with all required fields
*/
public static Rule create(String repositoryKey, String key, String name) {
return new Rule().setUniqueKey(repositoryKey, key).setName(name);
}
/**
* Create with all required fields
*
* @since 2.10
*/
public static Rule create(String repositoryKey, String key) {
return new Rule().setUniqueKey(repositoryKey, key);
}
/**
* @since 3.6
*/
public RuleKey ruleKey() {
return RuleKey.of(getRepositoryKey(), getKey());
}
}
|