From c77fc7f84763ead9dc262c107e23c0bddba4c8f2 Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Thu, 16 Jan 2014 18:19:25 +0100 Subject: [PATCH] SONAR-4923 Delete profile now uses Java facade --- .../sonar/core/properties/PropertiesDao.java | 43 +++++++----- .../core/qualityprofile/db/ActiveRuleDao.java | 66 +++++++++++++------ .../qualityprofile/db/ActiveRuleMapper.java | 24 ++++--- .../qualityprofile/db/QualityProfileDao.java | 18 ++++- .../qualityprofile/db/ActiveRuleMapper.xml | 8 +++ .../qualityprofile/db/ActiveRuleDaoTest.java | 18 +++++ .../delete_from_profile-result.xml | 12 ++++ ...lete_parameters_from_profile_id-result.xml | 19 ++++++ .../delete_parameters_from_profile_id.xml | 19 ++++++ .../QProfileActiveRuleOperations.java | 3 +- .../server/qualityprofile/QProfileLookup.java | 46 +++++++++++-- .../qualityprofile/QProfileOperations.java | 42 ++++++++++-- .../server/qualityprofile/QProfiles.java | 11 +++- .../org/sonar/server/rule/RuleRegistry.java | 11 +++- .../controllers/api/profiles_controller.rb | 7 +- .../app/controllers/profiles_controller.rb | 10 +-- .../qualityprofile/QProfileLookupTest.java | 31 ++++++++- .../QProfileOperationsTest.java | 46 +++++++++++-- .../server/qualityprofile/QProfilesTest.java | 33 ++++++---- .../sonar/server/rule/RuleRegistryTest.java | 36 +++++++--- .../delete_from_profile/active_rule25.json | 6 ++ .../delete_from_profile/active_rule2702.json | 16 +++++ .../delete_from_profile/active_rule523.json | 6 ++ .../{rules => shared}/rule1.json | 0 .../{rules => shared}/rule2.json | 0 .../{rules => shared}/rule3.json | 0 26 files changed, 431 insertions(+), 100 deletions(-) create mode 100644 sonar-core/src/test/resources/org/sonar/core/qualityprofile/db/ActiveRuleDaoTest/delete_from_profile-result.xml create mode 100644 sonar-core/src/test/resources/org/sonar/core/qualityprofile/db/ActiveRuleDaoTest/delete_parameters_from_profile_id-result.xml create mode 100644 sonar-core/src/test/resources/org/sonar/core/qualityprofile/db/ActiveRuleDaoTest/delete_parameters_from_profile_id.xml create mode 100644 sonar-server/src/test/resources/org/sonar/server/rule/RuleRegistryTest/delete_from_profile/active_rule25.json create mode 100644 sonar-server/src/test/resources/org/sonar/server/rule/RuleRegistryTest/delete_from_profile/active_rule2702.json create mode 100644 sonar-server/src/test/resources/org/sonar/server/rule/RuleRegistryTest/delete_from_profile/active_rule523.json rename sonar-server/src/test/resources/org/sonar/server/rule/RuleRegistryTest/{rules => shared}/rule1.json (100%) rename sonar-server/src/test/resources/org/sonar/server/rule/RuleRegistryTest/{rules => shared}/rule2.json (100%) rename sonar-server/src/test/resources/org/sonar/server/rule/RuleRegistryTest/{rules => shared}/rule3.json (100%) diff --git a/sonar-core/src/main/java/org/sonar/core/properties/PropertiesDao.java b/sonar-core/src/main/java/org/sonar/core/properties/PropertiesDao.java index 2d77d816c97..27e6a86ad24 100644 --- a/sonar-core/src/main/java/org/sonar/core/properties/PropertiesDao.java +++ b/sonar-core/src/main/java/org/sonar/core/properties/PropertiesDao.java @@ -110,20 +110,22 @@ public class PropertiesDao implements BatchComponent, ServerComponent { } } + public void setProperty(PropertyDto property, SqlSession session) { + PropertiesMapper mapper = session.getMapper(PropertiesMapper.class); + PropertyDto persistedProperty = mapper.selectByKey(property); + if (persistedProperty != null && !StringUtils.equals(persistedProperty.getValue(), property.getValue())) { + persistedProperty.setValue(property.getValue()); + mapper.update(persistedProperty); + } else if (persistedProperty == null) { + mapper.insert(property); + } + } + public void setProperty(PropertyDto property) { SqlSession session = mybatis.openSession(); - PropertiesMapper mapper = session.getMapper(PropertiesMapper.class); try { - PropertyDto persistedProperty = mapper.selectByKey(property); - if (persistedProperty != null && !StringUtils.equals(persistedProperty.getValue(), property.getValue())) { - persistedProperty.setValue(property.getValue()); - mapper.update(persistedProperty); - session.commit(); - - } else if (persistedProperty == null) { - mapper.insert(property); - session.commit(); - } + setProperty(property, session); + session.commit(); } finally { MyBatis.closeQuietly(session); } @@ -140,11 +142,15 @@ public class PropertiesDao implements BatchComponent, ServerComponent { } } - public void deleteProjectProperties(String key, String value) { - SqlSession session = mybatis.openSession(); + public void deleteProjectProperties(String key, String value, SqlSession session) { PropertiesMapper mapper = session.getMapper(PropertiesMapper.class); + mapper.deleteProjectProperties(key, value); + } + + public void deleteProjectProperties(String key, String value) { + SqlSession session = mybatis.openSession(); try { - mapper.deleteProjectProperties(key, value); + deleteProjectProperties(key, value, session); session.commit(); } finally { MyBatis.closeQuietly(session); @@ -163,13 +169,16 @@ public class PropertiesDao implements BatchComponent, ServerComponent { } } + public void deleteGlobalProperty(String key, SqlSession session) { + PropertiesMapper mapper = session.getMapper(PropertiesMapper.class); + mapper.deleteGlobalProperty(key); + } + public void deleteGlobalProperty(String key) { SqlSession session = mybatis.openSession(); - PropertiesMapper mapper = session.getMapper(PropertiesMapper.class); try { - mapper.deleteGlobalProperty(key); + deleteGlobalProperty(key, session); session.commit(); - } finally { MyBatis.closeQuietly(session); } diff --git a/sonar-core/src/main/java/org/sonar/core/qualityprofile/db/ActiveRuleDao.java b/sonar-core/src/main/java/org/sonar/core/qualityprofile/db/ActiveRuleDao.java index c92e5a7891d..bdc607fef25 100644 --- a/sonar-core/src/main/java/org/sonar/core/qualityprofile/db/ActiveRuleDao.java +++ b/sonar-core/src/main/java/org/sonar/core/qualityprofile/db/ActiveRuleDao.java @@ -69,11 +69,11 @@ public class ActiveRuleDao implements ServerComponent { } } - public void delete(Integer activeRuleId, SqlSession session) { + public void delete(int activeRuleId, SqlSession session) { session.getMapper(ActiveRuleMapper.class).delete(activeRuleId); } - public void delete(Integer activeRuleId) { + public void delete(int activeRuleId) { SqlSession session = mybatis.openSession(); try { delete(activeRuleId, session); @@ -83,11 +83,11 @@ public class ActiveRuleDao implements ServerComponent { } } - public void deleteFromRule(Integer ruleId, SqlSession session) { + public void deleteFromRule(int ruleId, SqlSession session) { session.getMapper(ActiveRuleMapper.class).deleteFromRule(ruleId); } - public void deleteFromRule(Integer ruleId) { + public void deleteFromRule(int ruleId) { SqlSession session = mybatis.openSession(); try { deleteFromRule(ruleId, session); @@ -97,6 +97,20 @@ public class ActiveRuleDao implements ServerComponent { } } + public void deleteFromProfile(int profileId, SqlSession session) { + session.getMapper(ActiveRuleMapper.class).deleteFromProfile(profileId); + } + + public void deleteFromProfile(int profileId) { + SqlSession session = mybatis.openSession(); + try { + deleteFromProfile(profileId, session); + session.commit(); + } finally { + MyBatis.closeQuietly(session); + } + } + public List selectByIds(List ids) { SqlSession session = mybatis.openSession(); try { @@ -132,7 +146,7 @@ public class ActiveRuleDao implements ServerComponent { return session.getMapper(ActiveRuleMapper.class).selectAll(); } - public List selectByRuleId(Integer ruleId) { + public List selectByRuleId(int ruleId) { SqlSession session = mybatis.openSession(); try { return selectByRuleId(ruleId, session); @@ -141,7 +155,7 @@ public class ActiveRuleDao implements ServerComponent { } } - public List selectByRuleId(Integer ruleId, SqlSession session) { + public List selectByRuleId(int ruleId, SqlSession session) { return session.getMapper(ActiveRuleMapper.class).selectByRuleId(ruleId); } @@ -160,7 +174,7 @@ public class ActiveRuleDao implements ServerComponent { @CheckForNull - public ActiveRuleDto selectById(Integer id) { + public ActiveRuleDto selectById(int id) { SqlSession session = mybatis.openSession(); try { return selectById(id, session); @@ -170,12 +184,12 @@ public class ActiveRuleDao implements ServerComponent { } @CheckForNull - public ActiveRuleDto selectById(Integer id, SqlSession session) { + public ActiveRuleDto selectById(int id, SqlSession session) { return session.getMapper(ActiveRuleMapper.class).selectById(id); } @CheckForNull - public ActiveRuleDto selectByProfileAndRule(Integer profileId, Integer ruleId) { + public ActiveRuleDto selectByProfileAndRule(int profileId, int ruleId) { SqlSession session = mybatis.openSession(); try { return selectByProfileAndRule(profileId, ruleId, session); @@ -185,7 +199,7 @@ public class ActiveRuleDao implements ServerComponent { } @CheckForNull - public ActiveRuleDto selectByProfileAndRule(Integer profileId, Integer ruleId, SqlSession session) { + public ActiveRuleDto selectByProfileAndRule(int profileId, int ruleId, SqlSession session) { return session.getMapper(ActiveRuleMapper.class).selectByProfileAndRule(profileId, ruleId); } @@ -218,11 +232,11 @@ public class ActiveRuleDao implements ServerComponent { } - public void deleteParameter(Integer activeRuleParamId, SqlSession session) { + public void deleteParameter(int activeRuleParamId, SqlSession session) { session.getMapper(ActiveRuleMapper.class).deleteParameter(activeRuleParamId); } - public void deleteParameter(Integer activeRuleParamId) { + public void deleteParameter(int activeRuleParamId) { SqlSession session = mybatis.openSession(); try { deleteParameter(activeRuleParamId, session); @@ -232,11 +246,11 @@ public class ActiveRuleDao implements ServerComponent { } } - public void deleteParameters(Integer activeRuleId, SqlSession session) { + public void deleteParameters(int activeRuleId, SqlSession session) { session.getMapper(ActiveRuleMapper.class).deleteParameters(activeRuleId); } - public void deleteParameters(Integer activeRuleId) { + public void deleteParameters(int activeRuleId) { SqlSession session = mybatis.openSession(); try { deleteParameters(activeRuleId, session); @@ -246,10 +260,24 @@ public class ActiveRuleDao implements ServerComponent { } } - public void deleteParametersWithParamId(Integer id, SqlSession session) { + public void deleteParametersWithParamId(int id, SqlSession session) { session.getMapper(ActiveRuleMapper.class).deleteParametersWithParamId(id); } + public void deleteParametersFromProfile(int profileId) { + SqlSession session = mybatis.openSession(); + try { + deleteParametersFromProfile(profileId, session); + session.commit(); + } finally { + MyBatis.closeQuietly(session); + } + } + + public void deleteParametersFromProfile(int profileId, SqlSession session) { + session.getMapper(ActiveRuleMapper.class).deleteParametersFromProfile(profileId); + } + public ActiveRuleParamDto selectParamById(Integer activeRuleParamId) { SqlSession session = mybatis.openSession(); try { @@ -259,7 +287,7 @@ public class ActiveRuleDao implements ServerComponent { } } - public ActiveRuleParamDto selectParamByActiveRuleAndKey(Integer activeRuleId, String key) { + public ActiveRuleParamDto selectParamByActiveRuleAndKey(int activeRuleId, String key) { SqlSession session = mybatis.openSession(); try { return selectParamByActiveRuleAndKey(activeRuleId, key, session); @@ -268,11 +296,11 @@ public class ActiveRuleDao implements ServerComponent { } } - public ActiveRuleParamDto selectParamByActiveRuleAndKey(Integer activeRuleId, String key, SqlSession session) { + public ActiveRuleParamDto selectParamByActiveRuleAndKey(int activeRuleId, String key, SqlSession session) { return session.getMapper(ActiveRuleMapper.class).selectParamByActiveRuleAndKey(activeRuleId, key); } - public List selectParamsByActiveRuleId(Integer activeRuleId) { + public List selectParamsByActiveRuleId(int activeRuleId) { SqlSession session = mybatis.openSession(); try { return selectParamsByActiveRuleId(activeRuleId, session); @@ -281,7 +309,7 @@ public class ActiveRuleDao implements ServerComponent { } } - public List selectParamsByActiveRuleId(Integer activeRuleId, SqlSession session) { + public List selectParamsByActiveRuleId(int activeRuleId, SqlSession session) { return session.getMapper(ActiveRuleMapper.class).selectParamsByActiveRuleId(activeRuleId); } 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 738606a7ada..aae23290ee5 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 @@ -32,17 +32,19 @@ public interface ActiveRuleMapper { void update(ActiveRuleDto dto); - void delete(Integer activeRuleId); + void delete(int activeRuleId); - void deleteFromRule(Integer ruleId); + void deleteFromRule(int ruleId); + + void deleteFromProfile(int profileId); @CheckForNull ActiveRuleDto selectById(Integer id); @CheckForNull - ActiveRuleDto selectByProfileAndRule(@Param("profileId") Integer profileId, @Param("ruleId") Integer ruleId); + ActiveRuleDto selectByProfileAndRule(@Param("profileId") int profileId, @Param("ruleId") int ruleId); - List selectByRuleId(Integer ruleId); + List selectByRuleId(int ruleId); List selectByProfileId(int profileId); @@ -52,19 +54,21 @@ public interface ActiveRuleMapper { void updateParameter(ActiveRuleParamDto dto); - void deleteParameters(Integer activeRuleId); + void deleteParameters(int activeRuleId); + + void deleteParameter(int activeRuleParamId); - void deleteParameter(Integer activeRuleParamId); + void deleteParametersWithParamId(int id); - void deleteParametersWithParamId(Integer id); + void deleteParametersFromProfile(int profileId); @CheckForNull - ActiveRuleParamDto selectParamById(Integer activeRuleParamId); + ActiveRuleParamDto selectParamById(int activeRuleParamId); @CheckForNull - ActiveRuleParamDto selectParamByActiveRuleAndKey(@Param("activeRuleId") Integer activeRuleId, @Param("key") String key); + ActiveRuleParamDto selectParamByActiveRuleAndKey(@Param("activeRuleId") int activeRuleId, @Param("key") String key); - List selectParamsByActiveRuleId(Integer activeRuleId); + List selectParamsByActiveRuleId(int activeRuleId); List selectAllParams(); 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 251bfb60470..dc28bc4994c 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 @@ -66,10 +66,14 @@ public class QualityProfileDao implements ServerComponent { } } + public void delete(int id, SqlSession session) { + session.getMapper(QualityProfileMapper.class).delete(id); + } + public void delete(int id) { SqlSession session = mybatis.openSession(); try { - session.getMapper(QualityProfileMapper.class).delete(id); + delete(id, session); session.commit(); } finally { MyBatis.closeQuietly(session); @@ -85,10 +89,14 @@ public class QualityProfileDao implements ServerComponent { } } + public QualityProfileDto selectDefaultProfile(String language, String key, SqlSession session) { + return session.getMapper(QualityProfileMapper.class).selectDefaultProfile(language, key); + } + public QualityProfileDto selectDefaultProfile(String language, String key) { SqlSession session = mybatis.openSession(); try { - return session.getMapper(QualityProfileMapper.class).selectDefaultProfile(language, key); + return selectDefaultProfile(language, key, session); } finally { MyBatis.closeQuietly(session); } @@ -151,10 +159,14 @@ public class QualityProfileDao implements ServerComponent { } } + public int countChildren(String name, String language, SqlSession session) { + return session.getMapper(QualityProfileMapper.class).countChildren(StringUtils.upperCase(name), language); + } + public int countChildren(String name, String language) { SqlSession session = mybatis.openSession(); try { - return session.getMapper(QualityProfileMapper.class).countChildren(StringUtils.upperCase(name), language); + return countChildren(StringUtils.upperCase(name), language, session); } finally { MyBatis.closeQuietly(session); } 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 8d4cb19f5a0..cf389856b45 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 @@ -48,6 +48,10 @@ DELETE FROM active_rules WHERE rule_id=#{ruleId} + + DELETE FROM active_rules WHERE profile_id=#{profileId} + +