diff options
author | Godin <mandrikov@gmail.com> | 2010-12-17 00:05:04 +0000 |
---|---|---|
committer | Godin <mandrikov@gmail.com> | 2010-12-17 00:05:04 +0000 |
commit | d79f46b3733b44ca62a1f92c576d68ddb28ec368 (patch) | |
tree | 4c901a6ea4545069cad85549913dbf273b5f870b /sonar-server/src/main/java | |
parent | dcbf1fc1630fa311fdbbe883e4e286d2fa74bd76 (diff) | |
download | sonarqube-d79f46b3733b44ca62a1f92c576d68ddb28ec368.tar.gz sonarqube-d79f46b3733b44ca62a1f92c576d68ddb28ec368.zip |
* SONAR-2048: Add an option to define parent in quality profile settings
* SONAR-1722: Provide a simple inheritance mechanism on Quality Profiles
** current implementation is some kind of synchronization between profiles
** only one level of inheritance supported, this constraint exists on UI side and not handled on Java side
** inherited rule can't be modified
Diffstat (limited to 'sonar-server/src/main/java')
-rw-r--r-- | sonar-server/src/main/java/org/sonar/server/configuration/ProfilesManager.java | 92 | ||||
-rw-r--r-- | sonar-server/src/main/java/org/sonar/server/ui/JRubyFacade.java | 13 |
2 files changed, 103 insertions, 2 deletions
diff --git a/sonar-server/src/main/java/org/sonar/server/configuration/ProfilesManager.java b/sonar-server/src/main/java/org/sonar/server/configuration/ProfilesManager.java index aea09e1cf01..e92496bfd6f 100644 --- a/sonar-server/src/main/java/org/sonar/server/configuration/ProfilesManager.java +++ b/sonar-server/src/main/java/org/sonar/server/configuration/ProfilesManager.java @@ -23,7 +23,10 @@ import org.sonar.api.Plugins; import org.sonar.api.database.DatabaseSession; import org.sonar.api.database.model.ResourceModel; import org.sonar.api.profiles.RulesProfile; +import org.sonar.api.rules.ActiveRule; +import org.sonar.api.rules.ActiveRuleParam; import org.sonar.api.rules.DefaultRulesManager; +import org.sonar.api.rules.Rule; import org.sonar.jpa.dao.BaseDao; import org.sonar.jpa.dao.RulesDao; @@ -54,6 +57,7 @@ public class ProfilesManager extends BaseDao { } public void deleteProfile(int profileId) { + // TODO should support deletion of profile with children RulesProfile profile = getSession().getEntity(RulesProfile.class, profileId); if (profile != null && !profile.getProvided()) { String hql = "UPDATE " + ResourceModel.class.getSimpleName() + " o SET o.rulesProfile=null WHERE o.rulesProfile=:rulesProfile"; @@ -64,7 +68,8 @@ public class ProfilesManager extends BaseDao { } public void deleteAllProfiles() { - getSession().createQuery("UPDATE " + ResourceModel.class.getSimpleName() + " o SET o.rulesProfile = null WHERE o.rulesProfile IS NOT NULL").executeUpdate(); + String hql = "UPDATE " + ResourceModel.class.getSimpleName() + " o SET o.rulesProfile = null WHERE o.rulesProfile IS NOT NULL"; + getSession().createQuery(hql).executeUpdate(); List profiles = getSession().createQuery("FROM " + RulesProfile.class.getSimpleName()).getResultList(); for (Object profile : profiles) { getSession().removeWithoutFlush(profile); @@ -72,4 +77,89 @@ public class ProfilesManager extends BaseDao { getSession().commit(); } + // Managing inheritance of profiles + // Only one level of inheritance supported + + public void changeParentProfile(Integer profileId, Integer parentId) { + RulesProfile profile = getSession().getEntity(RulesProfile.class, profileId); + if (profile != null && !profile.getProvided() && profileId != parentId) { + RulesProfile oldParent = getProfile(profile.getParentId()); + RulesProfile newParent = getProfile(parentId); + // Deactivate all inherited rules + if (oldParent != null) { + for (ActiveRule activeRule : oldParent.getActiveRules()) { + deactivate(profile, activeRule.getRule()); + } + } + // Activate all inherited rules + if (newParent != null) { + for (ActiveRule activeRule : newParent.getActiveRules()) { + activate(profile, activeRule); + } + } + profile.setParentId(parentId); + getSession().saveWithoutFlush(profile); + getSession().commit(); + } + } + + /** + * Rule was activated/changed in parent profile. + */ + public void activatedOrChanged(int parentProfileId, int activeRuleId) { + List<RulesProfile> children = getChildren(parentProfileId); + ActiveRule parentActiveRule = getSession().getEntity(ActiveRule.class, activeRuleId); + for (RulesProfile child : children) { + activate(child, parentActiveRule); + } + getSession().commit(); + } + + /** + * Rule was deactivated in parent profile. + */ + public void deactivated(int parentProfileId, int ruleId) { + List<RulesProfile> children = getChildren(parentProfileId); + Rule rule = getSession().getEntity(Rule.class, ruleId); + for (RulesProfile child : children) { + deactivate(child, rule); + } + getSession().commit(); + } + + private void activate(RulesProfile profile, ActiveRule parentActiveRule) { + ActiveRule activeRule = profile.getActiveRule(parentActiveRule.getRule()); + if (activeRule != null) { + removeActiveRule(profile, activeRule); + } + activeRule = (ActiveRule) parentActiveRule.clone(); + // TODO Godin: it means that we have bug in ActiveRule.clone() + for (ActiveRuleParam param : activeRule.getActiveRuleParams()) { + param.setActiveRule(activeRule); + } + activeRule.setRulesProfile(profile); + activeRule.setInherited(true); + profile.getActiveRules().add(activeRule); + } + + private void deactivate(RulesProfile profile, Rule rule) { + ActiveRule activeRule = profile.getActiveRule(rule); + if (activeRule != null) { + removeActiveRule(profile, activeRule); + } + } + + private List<RulesProfile> getChildren(int parentId) { + return getSession().getResults(RulesProfile.class, "parentId", parentId, "provided", false); + } + + private void removeActiveRule(RulesProfile profile, ActiveRule activeRule) { + profile.getActiveRules().remove(activeRule); + getSession().removeWithoutFlush(activeRule); + } + + private RulesProfile getProfile(Integer id) { + return id == null ? null : getSession().getEntity(RulesProfile.class, id); + } + } diff --git a/sonar-server/src/main/java/org/sonar/server/ui/JRubyFacade.java b/sonar-server/src/main/java/org/sonar/server/ui/JRubyFacade.java index d6681e35148..a4749ef623b 100644 --- a/sonar-server/src/main/java/org/sonar/server/ui/JRubyFacade.java +++ b/sonar-server/src/main/java/org/sonar/server/ui/JRubyFacade.java @@ -122,7 +122,6 @@ public final class JRubyFacade implements ServerComponent { public ViewProxy<Widget> getWidget(String id) { return getContainer().getComponent(Views.class).getWidget(id); } - public List<ViewProxy<Page>> getPages(String section, String resourceScope, String resourceQualifier, String resourceLanguage) { return getContainer().getComponent(Views.class).getPages(section, resourceScope, resourceQualifier, resourceLanguage); @@ -198,6 +197,18 @@ public final class JRubyFacade implements ServerComponent { getProfilesManager().deleteProfile((int) profileId); } + public void changeParentProfile(int profileId, Integer parentId) { + getProfilesManager().changeParentProfile(profileId, parentId); + } + + public void ruleActivatedOrChanged(int parentProfileId, int activeRuleId) { + getProfilesManager().activatedOrChanged(parentProfileId, activeRuleId); + } + + public void ruleDeactivated(int parentProfileId, int ruleId) { + getProfilesManager().deactivated(parentProfileId, ruleId); + } + public List<Footer> getWebFooters() { return getContainer().getComponents(Footer.class); } |