From 50c30e71a6c051dbb9610c7f7af4775722ad1d27 Mon Sep 17 00:00:00 2001 From: Stephane Gamard Date: Tue, 20 May 2014 14:43:36 +0200 Subject: [PATCH] SONAR-5007 - Updated RegisterQualityProfile for DAOv.2 --- .../core/qualityprofile/db/ActiveRuleDto.java | 19 +- .../core/qualityprofile/db/ActiveRuleKey.java | 1 + .../qualityprofile/db/ActiveRuleMapper.java | 4 +- .../qualityprofile/db/QualityProfileDao.java | 21 ++- .../qualityprofile/db/ActiveRuleMapper.xml | 33 +++- .../RegisterQualityProfiles.java | 107 ++++++----- .../persistence/ActiveRuleDao.java | 37 +++- .../qualityprofile/QProfilesMediumTest.java | 33 ---- .../RegisterQualityProfilesMediumTest.java | 178 ++++++++++++++++++ .../RegisterQualityProfilesTest.java | 6 +- .../persistence/ActiveRuleDaoTest.java | 62 ++++++ 11 files changed, 406 insertions(+), 95 deletions(-) create mode 100644 sonar-server/src/test/java/org/sonar/server/qualityprofile/RegisterQualityProfilesMediumTest.java create mode 100644 sonar-server/src/test/java/org/sonar/server/qualityprofile/persistence/ActiveRuleDaoTest.java diff --git a/sonar-core/src/main/java/org/sonar/core/qualityprofile/db/ActiveRuleDto.java b/sonar-core/src/main/java/org/sonar/core/qualityprofile/db/ActiveRuleDto.java index 47e14519a82..1682757237a 100644 --- a/sonar-core/src/main/java/org/sonar/core/qualityprofile/db/ActiveRuleDto.java +++ b/sonar-core/src/main/java/org/sonar/core/qualityprofile/db/ActiveRuleDto.java @@ -23,6 +23,7 @@ package org.sonar.core.qualityprofile.db; import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.builder.ReflectionToStringBuilder; import org.apache.commons.lang.builder.ToStringStyle; +import org.sonar.api.rule.RuleKey; import org.sonar.core.persistence.Dto; import org.sonar.core.rule.RuleDto; import org.sonar.core.rule.SeverityUtil; @@ -37,6 +38,11 @@ public class ActiveRuleDto extends Dto { public static final String INHERITED = "INHERITED"; public static final String OVERRIDES = "OVERRIDES"; + private String repository; + private String rule; + private String language; + private String profile; + private Integer id; private Integer profileId; private Integer ruleId; @@ -47,15 +53,18 @@ public class ActiveRuleDto extends Dto { private String noteUserLogin; private String noteData; - private transient ActiveRuleKey key; - - public ActiveRuleDto setKey(@Nullable ActiveRuleKey key) { - this.key = key; + @Deprecated + public ActiveRuleDto setKey(ActiveRuleKey key) { + this.repository = key.ruleKey().repository(); + this.rule = key.ruleKey().rule(); + this.language = key.qProfile().lang(); + this.profile = key.qProfile().name(); return this; } public ActiveRuleKey getKey() { - return this.key; + return ActiveRuleKey.of(QualityProfileKey.of(this.profile, this.language), + RuleKey.of(this.repository, this.rule)); } // This field do not exists in db, it's only retrieve by joins diff --git a/sonar-core/src/main/java/org/sonar/core/qualityprofile/db/ActiveRuleKey.java b/sonar-core/src/main/java/org/sonar/core/qualityprofile/db/ActiveRuleKey.java index 5421b67c5ee..f71b11413e9 100644 --- a/sonar-core/src/main/java/org/sonar/core/qualityprofile/db/ActiveRuleKey.java +++ b/sonar-core/src/main/java/org/sonar/core/qualityprofile/db/ActiveRuleKey.java @@ -29,6 +29,7 @@ import java.io.Serializable; * @since 4.4 */ public class ActiveRuleKey implements Serializable { + private final QualityProfileKey qualityProfileKey; private final RuleKey ruleKey; diff --git a/sonar-core/src/main/java/org/sonar/core/qualityprofile/db/ActiveRuleMapper.java b/sonar-core/src/main/java/org/sonar/core/qualityprofile/db/ActiveRuleMapper.java index 6160b21b43c..27d71c0265b 100644 --- a/sonar-core/src/main/java/org/sonar/core/qualityprofile/db/ActiveRuleMapper.java +++ b/sonar-core/src/main/java/org/sonar/core/qualityprofile/db/ActiveRuleMapper.java @@ -21,10 +21,8 @@ package org.sonar.core.qualityprofile.db; import org.apache.ibatis.annotations.Param; -import org.sonar.api.rule.RuleKey; import javax.annotation.CheckForNull; - import java.util.List; public interface ActiveRuleMapper { @@ -75,4 +73,6 @@ public interface ActiveRuleMapper { List selectAllParams(); + ActiveRuleDto selectByKey(@Param("profile") String profile, @Param("language") String language, + @Param("repository") String repository, @Param("rule") String rule ); } diff --git a/sonar-core/src/main/java/org/sonar/core/qualityprofile/db/QualityProfileDao.java b/sonar-core/src/main/java/org/sonar/core/qualityprofile/db/QualityProfileDao.java index 2d3ca003407..276b5dcfe19 100644 --- a/sonar-core/src/main/java/org/sonar/core/qualityprofile/db/QualityProfileDao.java +++ b/sonar-core/src/main/java/org/sonar/core/qualityprofile/db/QualityProfileDao.java @@ -20,16 +20,18 @@ package org.sonar.core.qualityprofile.db; +import com.google.common.base.Preconditions; import org.apache.ibatis.session.SqlSession; import org.sonar.api.DaoComponent; import org.sonar.api.ServerComponent; import org.sonar.core.component.ComponentDto; +import org.sonar.core.persistence.DbSession; import org.sonar.core.persistence.MyBatis; import javax.annotation.CheckForNull; import java.util.List; -public class QualityProfileDao implements DaoComponent, ServerComponent { +public class QualityProfileDao implements ServerComponent, DaoComponent { private final MyBatis mybatis; @@ -65,10 +67,27 @@ public class QualityProfileDao implements DaoComponent, ServerComponent { } } + + public void delete(QualityProfileDto profile, DbSession session) { + Preconditions.checkNotNull(profile.getId(), "Profile is not persisted"); + session.getMapper(QualityProfileMapper.class).delete(profile.getId()); + } + + /** + * @deprecated use {@link #delete(QualityProfileDto, DbSession)} + * @param id + * @param session + */ + @Deprecated public void delete(int id, SqlSession session) { session.getMapper(QualityProfileMapper.class).delete(id); } + /** + * @deprecated use {@link #delete(QualityProfileDto, DbSession)} + * @param id + */ + @Deprecated public void delete(int id) { SqlSession session = mybatis.openSession(false); try { diff --git a/sonar-core/src/main/resources/org/sonar/core/qualityprofile/db/ActiveRuleMapper.xml b/sonar-core/src/main/resources/org/sonar/core/qualityprofile/db/ActiveRuleMapper.xml index 93d62d979d4..29274f2d227 100644 --- a/sonar-core/src/main/resources/org/sonar/core/qualityprofile/db/ActiveRuleMapper.xml +++ b/sonar-core/src/main/resources/org/sonar/core/qualityprofile/db/ActiveRuleMapper.xml @@ -3,7 +3,25 @@ - + + a.id, + a.profile_id as profileId, + a.rule_id as ruleId, + a.failure_level as severity, + a.inheritance as inheritance, + rule.plugin_rule_key as rule, + rule.plugin_name as repository, + profile.name as profile, + profile.language as language + + + + LEFT JOIN rules_profiles profile ON profile.id=a.profile_id + LEFT JOIN rules rule ON rule.id = a.rule_id + + + + a.id, a.profile_id as profileId, a.rule_id as ruleId, @@ -65,6 +83,19 @@ + + +