001 /* 002 * Sonar, open source software quality management tool. 003 * Copyright (C) 2009 SonarSource SA 004 * mailto:contact AT sonarsource DOT com 005 * 006 * Sonar is free software; you can redistribute it and/or 007 * modify it under the terms of the GNU Lesser General Public 008 * License as published by the Free Software Foundation; either 009 * version 3 of the License, or (at your option) any later version. 010 * 011 * Sonar is distributed in the hope that it will be useful, 012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 014 * Lesser General Public License for more details. 015 * 016 * You should have received a copy of the GNU Lesser General Public 017 * License along with Sonar; if not, write to the Free Software 018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 019 */ 020 package org.sonar.jpa.dao; 021 022 import org.sonar.api.database.DatabaseSession; 023 import org.sonar.api.database.model.RuleFailureModel; 024 import org.sonar.api.database.model.Snapshot; 025 import org.sonar.api.profiles.RulesProfile; 026 import org.sonar.api.rules.*; 027 028 import java.util.ArrayList; 029 import java.util.Iterator; 030 import java.util.List; 031 032 public class RulesDao extends BaseDao { 033 034 private List<RulesCategory> rulesCategories; 035 036 public RulesDao(DatabaseSession session) { 037 super(session); 038 } 039 040 public List<Rule> getRules() { 041 return getSession().getResults(Rule.class, "enabled", true); 042 } 043 044 public List<Rule> getRulesByPlugin(String pluginKey) { 045 return getSession().getResults(Rule.class, "pluginName", pluginKey, "enabled", true); 046 } 047 048 public List<Rule> getRulesByCategory(RulesCategory categ) { 049 List<Rule> result = new ArrayList<Rule>(); 050 for (Rule rule : getRules()) { 051 if (rule.getRulesCategory().equals(categ)) { 052 result.add(rule); 053 } 054 } 055 return result; 056 } 057 058 public Rule getRuleByKey(String pluginKey, String ruleKey) { 059 return getSession().getSingleResult(Rule.class, "key", ruleKey, "pluginName", pluginKey, "enabled", true); 060 } 061 062 public Long countRules(List<String> plugins, String categoryName) { 063 return (Long) getSession().createQuery( 064 "SELECT COUNT(r) FROM Rule r WHERE r.pluginName IN (:pluginNames) AND r.rulesCategory=:rulesCategory AND r.enabled=true"). 065 setParameter("pluginNames", plugins). 066 setParameter("rulesCategory", getCategory(categoryName)). 067 getSingleResult(); 068 } 069 070 public List<RulesCategory> getCategories() { 071 if (rulesCategories == null) { 072 rulesCategories = getSession().getResults(RulesCategory.class); 073 } 074 return rulesCategories; 075 } 076 077 public RulesCategory getCategory(String key) { 078 return getSession().getSingleResult(RulesCategory.class, "name", key); 079 } 080 081 082 public List<RuleParam> getRuleParams() { 083 return getSession().getResults(RuleParam.class); 084 } 085 086 public RuleParam getRuleParam(Rule rule, String paramKey) { 087 return getSession().getSingleResult(RuleParam.class, "rule", rule, "key", paramKey); 088 } 089 090 public void addActiveRulesToProfile(List<ActiveRule> activeRules, int profileId, String pluginKey) { 091 RulesProfile rulesProfile = getProfileById(profileId); 092 for (ActiveRule activeRule : activeRules) { 093 synchronizeRuleOfActiveRule(activeRule, pluginKey); 094 activeRule.setRulesProfile(rulesProfile); 095 getSession().save(activeRule); 096 } 097 } 098 099 public void deleteActiveRuleParameters(RuleParam ruleParam) { 100 getSession().createQuery( 101 "DELETE FROM ActiveRuleParam arp WHERE ruleParam=:param") 102 .setParameter("param", ruleParam) 103 .executeUpdate(); 104 } 105 106 public List<RuleFailureModel> getViolations(Snapshot snapshot) { 107 return getSession().getResults(RuleFailureModel.class, "snapshotId", snapshot.getId()); 108 } 109 110 public void synchronizeRuleOfActiveRule(ActiveRule activeRule, String pluginKey) { 111 Rule rule = activeRule.getRule(); 112 Rule ruleFromDataBase = getRuleByKey(pluginKey, rule.getKey()); 113 activeRule.setRule(ruleFromDataBase); 114 List<RuleParam> ruleParamsFromDataBase = getRuleParams(); 115 for (ActiveRuleParam activeRuleParam : activeRule.getActiveRuleParams()) { 116 boolean found = false; 117 Iterator<RuleParam> iterator = ruleParamsFromDataBase.iterator(); 118 while (iterator.hasNext() && !found) { 119 RuleParam ruleParamFromDataBase = iterator.next(); 120 if (isRuleParamEqual(activeRuleParam.getRuleParam(), ruleParamFromDataBase, rule.getKey(), pluginKey)) { 121 activeRuleParam.setRuleParam(ruleParamFromDataBase); 122 found = true; 123 } 124 } 125 } 126 } 127 128 public boolean isRuleParamEqual(RuleParam ruleParam, RuleParam ruleParamFromDatabase, String ruleKey, String pluginKey) { 129 return ruleParam.getKey().equals(ruleParamFromDatabase.getKey()) && 130 ruleKey.equals(ruleParamFromDatabase.getRule().getKey()) && 131 ruleParamFromDatabase.getRule().getPluginName().equals(pluginKey); 132 } 133 134 public RulesProfile getProfileById(int profileId) { 135 return getSession().getEntityManager().getReference(RulesProfile.class, profileId); 136 } 137 138 }