]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-8867 RuleDto aggregates RuleDefinitionDto and RuleMetadataDto
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Mon, 20 Mar 2017 14:43:28 +0000 (15:43 +0100)
committerSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Thu, 23 Mar 2017 16:54:56 +0000 (17:54 +0100)
server/sonar-db-dao/src/main/java/org/sonar/db/rule/RuleDefinitionDto.java
server/sonar-db-dao/src/main/java/org/sonar/db/rule/RuleDto.java
server/sonar-db-dao/src/main/java/org/sonar/db/rule/RuleMetadataDto.java
server/sonar-db-dao/src/main/resources/org/sonar/db/rule/RuleMapper.xml
server/sonar-db-dao/src/test/java/org/sonar/db/rule/RuleTesting.java

index a5a9bafb4769979d97d53d2fa7c40c2b16055014..5a7b7ca6495ccc099d46a21098c441333ff8962d 100644 (file)
@@ -28,8 +28,6 @@ import javax.annotation.Nullable;
 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.ReflectionToStringBuilder;
-import org.apache.commons.lang.builder.ToStringStyle;
 import org.sonar.api.rule.RuleKey;
 import org.sonar.api.rule.RuleStatus;
 import org.sonar.api.rules.RuleType;
@@ -69,6 +67,10 @@ public class RuleDefinitionDto {
     return key;
   }
 
+  void setKey(RuleKey key) {
+    this.key = key;
+  }
+
   public Integer getId() {
     return id;
   }
@@ -98,6 +100,12 @@ public class RuleDefinitionDto {
     return this;
   }
 
+  public RuleDefinitionDto setRuleKey(RuleKey ruleKey) {
+    this.repositoryKey = ruleKey.repository();
+    this.ruleKey = ruleKey.rule();
+    return this;
+  }
+
   public String getDescription() {
     return description;
   }
@@ -240,7 +248,7 @@ public class RuleDefinitionDto {
     return systemTags;
   }
 
-  private void setSystemTagsField(String s) {
+  void setSystemTagsField(String s) {
     systemTags = s;
   }
 
@@ -291,29 +299,49 @@ public class RuleDefinitionDto {
     }
     RuleDto other = (RuleDto) obj;
     return new EqualsBuilder()
-        .append(repositoryKey, other.getRepositoryKey())
-        .append(ruleKey, other.getRuleKey())
-        .isEquals();
+      .append(repositoryKey, other.getRepositoryKey())
+      .append(ruleKey, other.getRuleKey())
+      .isEquals();
   }
 
   @Override
   public int hashCode() {
     return new HashCodeBuilder(17, 37)
-        .append(repositoryKey)
-        .append(ruleKey)
-        .toHashCode();
-  }
-
-  @Override
-  public String toString() {
-    return new ReflectionToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE).toString();
+      .append(repositoryKey)
+      .append(ruleKey)
+      .toHashCode();
   }
 
   public static RuleDto createFor(RuleKey key) {
     return new RuleDto()
-        .setRepositoryKey(key.repository())
-        .setRuleKey(key.rule());
+      .setRepositoryKey(key.repository())
+      .setRuleKey(key.rule());
   }
 
+  @Override
+  public String toString() {
+    return "RuleDefinitionDto{" +
+      "id=" + id +
+      ", repositoryKey='" + repositoryKey + '\'' +
+      ", ruleKey='" + ruleKey + '\'' +
+      ", description='" + description + '\'' +
+      ", descriptionFormat=" + descriptionFormat +
+      ", status=" + status +
+      ", name='" + name + '\'' +
+      ", configKey='" + configKey + '\'' +
+      ", severity=" + severity +
+      ", isTemplate=" + isTemplate +
+      ", language='" + language + '\'' +
+      ", templateId=" + templateId +
+      ", defRemediationFunction='" + defRemediationFunction + '\'' +
+      ", defRemediationGapMultiplier='" + defRemediationGapMultiplier + '\'' +
+      ", defRemediationBaseEffort='" + defRemediationBaseEffort + '\'' +
+      ", gapDescription='" + gapDescription + '\'' +
+      ", systemTags='" + systemTags + '\'' +
+      ", type=" + type +
+      ", key=" + key +
+      ", createdAt=" + createdAt +
+      ", updatedAt=" + updatedAt +
+      '}';
+  }
 }
-
index 898a6fcb14c3a7127b19a1e45037491c65e8237f..e2d8f2e16e4d8c8d85935ccced934f9738ddff4e 100644 (file)
  */
 package org.sonar.db.rule;
 
-import java.util.Arrays;
-import java.util.Collections;
 import java.util.Date;
 import java.util.Set;
-import java.util.TreeSet;
 import javax.annotation.CheckForNull;
 import javax.annotation.Nullable;
-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.ReflectionToStringBuilder;
-import org.apache.commons.lang.builder.ToStringStyle;
 import org.sonar.api.rule.RuleKey;
 import org.sonar.api.rule.RuleStatus;
 import org.sonar.api.rules.RuleType;
 
-import static com.google.common.base.Preconditions.checkArgument;
-
 public class RuleDto {
 
   public enum Format {
     HTML, MARKDOWN
   }
 
-  private Integer id;
-  private String repositoryKey;
-  private String ruleKey;
-  private String description;
-  private Format descriptionFormat;
-  private RuleStatus status;
-  private String name;
-  private String configKey;
-  private Integer severity;
-  private boolean isTemplate;
-  private String language;
-  private Integer templateId;
-  private String noteData;
-  private String noteUserLogin;
-  private Date noteCreatedAt;
-  private Date noteUpdatedAt;
-  private String remediationFunction;
-  private String defRemediationFunction;
-  private String remediationGapMultiplier;
-  private String defRemediationGapMultiplier;
-  private String remediationBaseEffort;
-  private String defRemediationBaseEffort;
-  private String gapDescription;
-  private String tags;
-  private String systemTags;
-  private int type;
-
-  private RuleKey key;
-
-  private long createdAt;
-  private long updatedAt;
+  private final RuleDefinitionDto definition = new RuleDefinitionDto();
+  private final RuleMetadataDto metadata = new RuleMetadataDto();
+
+  public RuleDefinitionDto getDefinition() {
+    return definition;
+  }
+
+  public RuleMetadataDto getMetadata() {
+    return metadata;
+  }
 
   public RuleKey getKey() {
-    if (key == null) {
-      key = RuleKey.of(getRepositoryKey(), getRuleKey());
+    if (definition.getKey() == null) {
+      definition.setKey(RuleKey.of(getRepositoryKey(), getRuleKey()));
     }
-    return key;
+    return definition.getKey();
   }
 
   public Integer getId() {
-    return id;
+    return definition.getId();
   }
 
   public RuleDto setId(Integer id) {
-    this.id = id;
+    definition.setId(id);
+    metadata.setRuleId(id);
     return this;
   }
 
   public String getRepositoryKey() {
-    return repositoryKey;
+    return definition.getRepositoryKey();
   }
 
   public RuleDto setRepositoryKey(String s) {
-    checkArgument(s.length() <= 255, "Rule repository is too long: %s", s);
-    this.repositoryKey = s;
+    definition.setRepositoryKey(s);
     return this;
   }
 
   public String getRuleKey() {
-    return ruleKey;
+    return definition.getRuleKey();
   }
 
   public RuleDto setRuleKey(String s) {
-    checkArgument(s.length() <= 200, "Rule key is too long: %s", s);
-    this.ruleKey = s;
+    definition.setRuleKey(s);
     return this;
   }
 
   public String getDescription() {
-    return description;
+    return definition.getDescription();
   }
 
   public RuleDto setDescription(String description) {
-    this.description = description;
+    definition.setDescription(description);
     return this;
   }
 
   public Format getDescriptionFormat() {
-    return descriptionFormat;
+    return definition.getDescriptionFormat();
   }
 
   public RuleDto setDescriptionFormat(Format descriptionFormat) {
-    this.descriptionFormat = descriptionFormat;
+    definition.setDescriptionFormat(descriptionFormat);
     return this;
   }
 
   public RuleStatus getStatus() {
-    return status;
+    return definition.getStatus();
   }
 
   public RuleDto setStatus(@Nullable RuleStatus s) {
-    this.status = s;
+    definition.setStatus(s);
     return this;
   }
 
   public String getName() {
-    return name;
+    return definition.getName();
   }
 
   public RuleDto setName(@Nullable String s) {
-    checkArgument(s == null || s.length() <= 255, "Rule name is too long: %s", s);
-    this.name = s;
+    definition.setName(s);
     return this;
   }
 
   public String getConfigKey() {
-    return configKey;
+    return definition.getConfigKey();
   }
 
   public RuleDto setConfigKey(@Nullable String configKey) {
-    this.configKey = configKey;
+    definition.setConfigKey(configKey);
     return this;
   }
 
   @CheckForNull
   public Integer getSeverity() {
-    return severity;
+    return definition.getSeverity();
   }
 
   @CheckForNull
   public String getSeverityString() {
-    return severity != null ? SeverityUtil.getSeverityFromOrdinal(severity) : null;
+    return definition.getSeverityString();
   }
 
   public RuleDto setSeverity(@Nullable String severity) {
-    return this.setSeverity(severity != null ? SeverityUtil.getOrdinalFromSeverity(severity) : null);
+    definition.setSeverity(severity);
+    return this;
   }
 
   public RuleDto setSeverity(@Nullable Integer severity) {
-    this.severity = severity;
+    definition.setSeverity(severity);
     return this;
   }
 
   public boolean isTemplate() {
-    return isTemplate;
+    return definition.isTemplate();
   }
 
   public RuleDto setIsTemplate(boolean isTemplate) {
-    this.isTemplate = isTemplate;
+    definition.setIsTemplate(isTemplate);
     return this;
   }
 
   @CheckForNull
   public String getLanguage() {
-    return language;
+    return definition.getLanguage();
   }
 
   public RuleDto setLanguage(String language) {
-    this.language = language;
+    definition.setLanguage(language);
     return this;
   }
 
   @CheckForNull
   public Integer getTemplateId() {
-    return templateId;
+    return definition.getTemplateId();
   }
 
   public RuleDto setTemplateId(@Nullable Integer templateId) {
-    this.templateId = templateId;
+    definition.setTemplateId(templateId);
     return this;
   }
 
   @CheckForNull
-  public String getNoteData() {
-    return noteData;
+  public String getDefaultRemediationFunction() {
+    return definition.getDefaultRemediationFunction();
   }
 
-  public RuleDto setNoteData(@Nullable String s) {
-    this.noteData = s;
+  public RuleDto setDefaultRemediationFunction(@Nullable String defaultRemediationFunction) {
+    definition.setDefaultRemediationFunction(defaultRemediationFunction);
     return this;
   }
 
   @CheckForNull
-  public String getNoteUserLogin() {
-    return noteUserLogin;
+  public String getDefaultRemediationGapMultiplier() {
+    return definition.getDefaultRemediationGapMultiplier();
   }
 
-  public RuleDto setNoteUserLogin(@Nullable String noteUserLogin) {
-    this.noteUserLogin = noteUserLogin;
+  public RuleDto setDefaultRemediationGapMultiplier(@Nullable String defaultRemediationGapMultiplier) {
+    definition.setDefaultRemediationGapMultiplier(defaultRemediationGapMultiplier);
     return this;
   }
 
   @CheckForNull
-  public Date getNoteCreatedAt() {
-    return noteCreatedAt;
+  public String getDefaultRemediationBaseEffort() {
+    return definition.getDefaultRemediationBaseEffort();
   }
 
-  public RuleDto setNoteCreatedAt(@Nullable Date noteCreatedAt) {
-    this.noteCreatedAt = noteCreatedAt;
+  public RuleDto setDefaultRemediationBaseEffort(@Nullable String defaultRemediationBaseEffort) {
+    definition.setDefaultRemediationBaseEffort(defaultRemediationBaseEffort);
     return this;
   }
 
   @CheckForNull
-  public Date getNoteUpdatedAt() {
-    return noteUpdatedAt;
+  public String getGapDescription() {
+    return definition.getGapDescription();
   }
 
-  public RuleDto setNoteUpdatedAt(@Nullable Date noteUpdatedAt) {
-    this.noteUpdatedAt = noteUpdatedAt;
+  public RuleDto setGapDescription(@Nullable String s) {
+    definition.setGapDescription(s);
     return this;
   }
 
-  @CheckForNull
-  public String getRemediationFunction() {
-    return remediationFunction;
+  public RuleDto setSystemTags(Set<String> tags) {
+    this.definition.setSystemTags(tags);
+    return this;
   }
 
-  public RuleDto setRemediationFunction(@Nullable String remediationFunction) {
-    this.remediationFunction = remediationFunction;
-    return this;
+  public int getType() {
+    return definition.getType();
   }
 
-  @CheckForNull
-  public String getDefaultRemediationFunction() {
-    return defRemediationFunction;
+  public RuleDto setType(int type) {
+    definition.setType(type);
+    return this;
   }
 
-  public RuleDto setDefaultRemediationFunction(@Nullable String defaultRemediationFunction) {
-    this.defRemediationFunction = defaultRemediationFunction;
+  public RuleDto setType(RuleType type) {
+    definition.setType(type);
     return this;
   }
 
-  @CheckForNull
-  public String getRemediationGapMultiplier() {
-    return remediationGapMultiplier;
+  public long getCreatedAt() {
+    return definition.getCreatedAt();
   }
 
-  public RuleDto setRemediationGapMultiplier(@Nullable String remediationGapMultiplier) {
-    this.remediationGapMultiplier = remediationGapMultiplier;
+  public RuleDto setCreatedAt(long createdAt) {
+    definition.setCreatedAt(createdAt);
     return this;
   }
 
-  @CheckForNull
-  public String getDefaultRemediationGapMultiplier() {
-    return defRemediationGapMultiplier;
+  public long getUpdatedAt() {
+    return definition.getUpdatedAt();
   }
 
-  public RuleDto setDefaultRemediationGapMultiplier(@Nullable String defaultRemediationGapMultiplier) {
-    this.defRemediationGapMultiplier = defaultRemediationGapMultiplier;
+  public RuleDto setUpdatedAt(long updatedAt) {
+    definition.setUpdatedAt(updatedAt);
     return this;
   }
 
-  @CheckForNull
-  public String getRemediationBaseEffort() {
-    return remediationBaseEffort;
+  public String getOrganizationUuid() {
+    return metadata.getOrganizationUuid();
   }
 
-  public RuleDto setRemediationBaseEffort(@Nullable String remediationBaseEffort) {
-    this.remediationBaseEffort = remediationBaseEffort;
+  public RuleDto setOrganizationUuid(String organizationUuid) {
+    metadata.setOrganizationUuid(organizationUuid);
     return this;
   }
 
   @CheckForNull
-  public String getDefaultRemediationBaseEffort() {
-    return defRemediationBaseEffort;
+  public String getNoteData() {
+    return metadata.getNoteData();
   }
 
-  public RuleDto setDefaultRemediationBaseEffort(@Nullable String defaultRemediationBaseEffort) {
-    this.defRemediationBaseEffort = defaultRemediationBaseEffort;
+  public RuleDto setNoteData(@Nullable String s) {
+    metadata.setNoteData(s);
     return this;
   }
 
   @CheckForNull
-  public String getGapDescription() {
-    return gapDescription;
+  public String getNoteUserLogin() {
+    return metadata.getNoteUserLogin();
   }
 
-  public RuleDto setGapDescription(@Nullable String s) {
-    this.gapDescription = s;
+  public RuleDto setNoteUserLogin(@Nullable String noteUserLogin) {
+    metadata.setNoteUserLogin(noteUserLogin);
     return this;
   }
 
-  public Set<String> getTags() {
-    return tags == null ? Collections.emptySet() : new TreeSet<>(Arrays.asList(StringUtils.split(tags, ',')));
+  @CheckForNull
+  public Date getNoteCreatedAt() {
+    return metadata.getNoteCreatedAt();
   }
 
-  public Set<String> getSystemTags() {
-    return systemTags == null ? Collections.emptySet() : new TreeSet<>(Arrays.asList(StringUtils.split(systemTags, ',')));
+  public RuleDto setNoteCreatedAt(@Nullable Date noteCreatedAt) {
+    metadata.setNoteCreatedAt(noteCreatedAt);
+    return this;
   }
 
-  private String getTagsField() {
-    return tags;
+  @CheckForNull
+  public Date getNoteUpdatedAt() {
+    return metadata.getNoteUpdatedAt();
   }
 
-  private String getSystemTagsField() {
-    return systemTags;
+  public RuleDto setNoteUpdatedAt(@Nullable Date noteUpdatedAt) {
+    metadata.setNoteUpdatedAt(noteUpdatedAt);
+    return this;
   }
 
-  private void setTagsField(String s) {
-    tags = s;
+  @CheckForNull
+  public String getRemediationFunction() {
+    return metadata.getRemediationFunction();
   }
 
-  private void setSystemTagsField(String s) {
-    systemTags = s;
+  public RuleDto setRemediationFunction(@Nullable String remediationFunction) {
+    metadata.setRemediationFunction(remediationFunction);
+    return this;
   }
 
-  public RuleDto setTags(Set<String> tags) {
-    String raw = tags.isEmpty() ? null : StringUtils.join(tags, ',');
-    checkArgument(raw == null || raw.length() <= 4000, "Rule tags are too long: %s", raw);
-    this.tags = raw;
-    return this;
+  @CheckForNull
+  public String getRemediationGapMultiplier() {
+    return metadata.getRemediationGapMultiplier();
   }
 
-  public RuleDto setSystemTags(Set<String> tags) {
-    this.systemTags = tags.isEmpty() ? null : StringUtils.join(tags, ',');
+  public RuleDto setRemediationGapMultiplier(@Nullable String remediationGapMultiplier) {
+    metadata.setRemediationGapMultiplier(remediationGapMultiplier);
     return this;
   }
 
-  public int getType() {
-    return type;
+  @CheckForNull
+  public String getRemediationBaseEffort() {
+    return metadata.getRemediationBaseEffort();
   }
 
-  public RuleDto setType(int type) {
-    this.type = type;
+  public RuleDto setRemediationBaseEffort(@Nullable String remediationBaseEffort) {
+    metadata.setRemediationBaseEffort(remediationBaseEffort);
     return this;
   }
 
-  public RuleDto setType(RuleType type) {
-    this.type = type.getDbConstant();
-    return this;
+  public Set<String> getTags() {
+    return metadata.getTags();
   }
 
-  public long getCreatedAt() {
-    return createdAt;
+  private void setTagsField(String s) {
+    metadata.setTagsField(s);
   }
 
-  public RuleDto setCreatedAt(long createdAt) {
-    this.createdAt = createdAt;
-    return this;
+  public Set<String> getSystemTags() {
+    return definition.getSystemTags();
   }
 
-  public long getUpdatedAt() {
-    return updatedAt;
+  private void setSystemTagsField(String s) {
+    definition.setSystemTagsField(s);
   }
 
-  public RuleDto setUpdatedAt(long updatedAt) {
-    this.updatedAt = updatedAt;
+  public RuleDto setTags(Set<String> tags) {
+    this.metadata.setTags(tags);
     return this;
   }
 
@@ -393,22 +362,25 @@ public class RuleDto {
     }
     RuleDto other = (RuleDto) obj;
     return new EqualsBuilder()
-      .append(repositoryKey, other.getRepositoryKey())
-      .append(ruleKey, other.getRuleKey())
+      .append(getRepositoryKey(), other.getRepositoryKey())
+      .append(getRuleKey(), other.getRuleKey())
       .isEquals();
   }
 
   @Override
   public int hashCode() {
     return new HashCodeBuilder(17, 37)
-      .append(repositoryKey)
-      .append(ruleKey)
+      .append(getRepositoryKey())
+      .append(getRuleKey())
       .toHashCode();
   }
 
   @Override
   public String toString() {
-    return new ReflectionToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE).toString();
+    return "RuleDto{" +
+      "definition=" + definition +
+      ", metadata=" + metadata +
+      '}';
   }
 
   public static RuleDto createFor(RuleKey key) {
index 29ebe3da75fc089d638a63d1daa8a65d33271145..3ce1e937f040ad9884a726b5db6964bd6a0579a2 100644 (file)
@@ -32,6 +32,7 @@ import static com.google.common.base.Preconditions.checkArgument;
 
 public class RuleMetadataDto {
   private int ruleId;
+  private String organizationUuid;
   private String noteData;
   private String noteUserLogin;
   private Date noteCreatedAt;
@@ -51,6 +52,15 @@ public class RuleMetadataDto {
     return this;
   }
 
+  public String getOrganizationUuid() {
+    return organizationUuid;
+  }
+
+  public RuleMetadataDto setOrganizationUuid(String organizationUuid) {
+    this.organizationUuid = organizationUuid;
+    return this;
+  }
+
   @CheckForNull
   public String getNoteData() {
     return noteData;
@@ -136,7 +146,7 @@ public class RuleMetadataDto {
     return tags;
   }
 
-  private void setTagsField(String s) {
+  void setTagsField(String s) {
     tags = s;
   }
 
@@ -148,4 +158,21 @@ public class RuleMetadataDto {
     this.updatedAt = updatedAt;
     return this;
   }
+
+  @Override
+  public String toString() {
+    return "RuleMetadataDto{" +
+      "ruleId=" + ruleId +
+      ", organizationUuid='" + organizationUuid + '\'' +
+      ", noteData='" + noteData + '\'' +
+      ", noteUserLogin='" + noteUserLogin + '\'' +
+      ", noteCreatedAt=" + noteCreatedAt +
+      ", noteUpdatedAt=" + noteUpdatedAt +
+      ", remediationFunction='" + remediationFunction + '\'' +
+      ", remediationGapMultiplier='" + remediationGapMultiplier + '\'' +
+      ", remediationBaseEffort='" + remediationBaseEffort + '\'' +
+      ", tags='" + tags + '\'' +
+      ", updatedAt=" + updatedAt +
+      '}';
+  }
 }
index 263c13afe7c42d0dc1bed7cd95dee9e3dd77e5e3..185195a465a85e48d7bbc077e8e564bbd9b02d27 100644 (file)
     r.note_created_at as "noteCreatedAt",
     r.note_updated_at as "noteUpdatedAt",
     r.remediation_function as "remediationFunction",
-    r.def_remediation_function as "defRemediationFunction",
+    r.def_remediation_function as "defaultRemediationFunction",
     r.remediation_gap_mult as "remediationGapMultiplier",
-    r.def_remediation_gap_mult as "defRemediationGapMultiplier",
+    r.def_remediation_gap_mult as "defaultRemediationGapMultiplier",
     r.remediation_base_effort as "remediationBaseEffort",
-    r.def_remediation_base_effort as "defRemediationBaseEffort",
+    r.def_remediation_base_effort as "defaultRemediationBaseEffort",
     r.gap_description as "gapDescription",
     r.tags as "tagsField",
     r.system_tags as "systemTagsField",
index bb5f4ad295244652be3e8c546e3d96d7ec41a2a7..c14178c3aa9205b6bab1d0a0aee2a79d8c5232ed 100644 (file)
@@ -122,6 +122,7 @@ public class RuleTesting {
       .setUpdatedAt(new Date().getTime());
     if (organization != null) {
       res
+        .setOrganizationUuid(organization.getUuid())
         .setTags(ImmutableSet.of("tag1", "tag2"))
         .setRemediationFunction("LINEAR")
         .setRemediationGapMultiplier("1h");