View Javadoc

1   /*
2    * Sonar, entreprise quality control tool.
3    * Copyright (C) 2007-2008 Hortis-GRC SA
4    * mailto:be_agile HAT hortis DOT ch
5    *
6    * Sonar is free software; you can redistribute it and/or
7    * modify it under the terms of the GNU Lesser General Public
8    * License as published by the Free Software Foundation; either
9    * version 3 of the License, or (at your option) any later version.
10   *
11   * Sonar is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   * Lesser General Public License for more details.
15   *
16   * You should have received a copy of the GNU Lesser General Public
17   * License along with Sonar; if not, write to the Free Software
18   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
19   */
20  package org.sonar.commons.rules;
21  
22  import ch.hortis.sonar.model.*;
23  import org.sonar.commons.database.DatabaseManager;
24  
25  import javax.persistence.NoResultException;
26  import javax.persistence.Query;
27  import java.util.ArrayList;
28  import java.util.Iterator;
29  import java.util.List;
30  
31  public class RulesDao {
32    private DatabaseManager databaseManager;
33    private List<RulesCategory> rulesCategories;
34  
35    public RulesDao(DatabaseManager manager) {
36      this.databaseManager = manager;
37    }
38  
39    public Rule getRule(String pluginKey, String ruleKey) {
40      try {
41        return (Rule) databaseManager.createQuery("SELECT r FROM Rule r WHERE r.pluginName=:pluginKey AND r.key=:ruleKey")
42            .setParameter("pluginKey", pluginKey)
43            .setParameter("ruleKey", ruleKey)
44            .getSingleResult();
45      } catch (NoResultException e) {
46        return null;
47      }
48    }
49  
50    public List<Rule> getRules() {
51      return databaseManager.createQuery("SELECT r FROM Rule r").getResultList();
52    }
53  
54  
55    public List<Rule> getRulesByPlugin(String pluginKey) {
56      return databaseManager.createQuery("SELECT r FROM Rule r WHERE r.pluginName=:pluginKey").setParameter("pluginKey", pluginKey).getResultList();
57    }
58  
59    public List<Rule> getRulesByCategory(RulesCategory categ) {
60      List<Rule> result = new ArrayList<Rule>();
61      for (Rule rule : getRules()) {
62        if (rule.getRulesCategory().equals(categ)) {
63          result.add(rule);
64        }
65      }
66      return result;
67    }
68  
69    public Rule getRuleByKey(String pluginKey, String ruleKey) {
70      try {
71        return (Rule) databaseManager.createQuery(
72            "SELECT r FROM Rule r WHERE r.key=:ruleKey AND r.pluginName=:pluginKey").
73            setParameter("ruleKey", ruleKey).
74            setParameter("pluginKey", pluginKey).
75            getSingleResult();
76      } catch (NoResultException ex) {
77        return null;
78      }
79    }
80  
81    public Long getRulesCount(List<String> plugins, String categoryName) {
82      return (Long) databaseManager.createQuery(
83          "SELECT COUNT(r) FROM Rule r WHERE r.pluginName IN (:pluginNames) AND r.rulesCategory=:rulesCategory").
84          setParameter("pluginNames", plugins).
85          setParameter("rulesCategory", getCategory(categoryName)).
86          getSingleResult();
87    }
88  
89  
90    public List<RulesCategory> getCategories() {
91      if (rulesCategories == null) {
92        Query query = databaseManager.createQuery("SELECT r FROM RulesCategory r");
93        rulesCategories = query.getResultList();
94      }
95      return rulesCategories;
96    }
97  
98    public RulesCategory getCategory(String key) {
99      try {
100       return (RulesCategory) databaseManager
101           .createQuery("select rc from RulesCategory rc where rc.name=:key")
102           .setParameter("key", key)
103           .getSingleResult();
104     } catch (NoResultException ex) {
105       return null;
106     }
107   }
108 
109 
110   public List<RuleParam> getRuleParams() {
111     return (List<RuleParam>) databaseManager.createQuery("FROM RuleParam").getResultList();
112   }
113 
114 
115   public List<RulesProfile> getActiveProfiles() {
116     return databaseManager.createQuery("select rp from RulesProfile rp where rp.active=true").getResultList();
117   }
118 
119   public RulesProfile getActiveProfile(String languageKey) {
120     try {
121       return (RulesProfile) databaseManager
122           .createQuery("select rp from RulesProfile rp where rp.active=true and rp.language=:languageKey")
123           .setParameter("languageKey", languageKey)
124           .getSingleResult();
125     } catch (NoResultException e) {
126       return null;
127     }
128   }
129 
130   public List<RulesProfile> getProfiles(String languageKey) {
131     return (List<RulesProfile>) databaseManager.createQuery(
132         "SELECT rp FROM RulesProfile rp WHERE language=:languageKey")
133         .setParameter("languageKey", languageKey)
134         .getResultList();
135   }
136 
137   public List<RulesProfile> getProvidedProfiles() {
138     return (List<RulesProfile>) databaseManager.createQuery(
139         "SELECT rp FROM RulesProfile rp WHERE rp.provided=true")
140         .getResultList();
141   }
142 
143   public RulesProfile getProfile(String languageKey, String profileName) {
144     try {
145       return (RulesProfile) databaseManager.createQuery(
146           "SELECT rp FROM RulesProfile rp WHERE rp.language=:languageKey AND rp.name=:profileName")
147           .setParameter("languageKey", languageKey)
148           .setParameter("profileName", profileName)
149           .getSingleResult();
150     } catch (NoResultException e) {
151       return null;
152     }
153   }
154 
155   public void addActiveRulesToProfile(List<ActiveRule> activeRules, int profileId, String pluginKey) {
156     RulesProfile rulesProfile = getProfileById(profileId);
157     for (ActiveRule activeRule : activeRules) {
158       synchronizeRuleOfActiveRule(activeRule, pluginKey);
159       activeRule.setRulesProfile(rulesProfile);
160       databaseManager.save(activeRule);
161     }
162   }
163 
164 //  public List<ActiveRule> getActiveRules(int profileId, String pluginKey) {
165 //    return (List<ActiveRule>) databaseManager.createQuery(
166 //        "SELECT ar FROM ActiveRule ar WHERE ar.rule.pluginName=:name AND ar.rulesProfile.id=:id").
167 //        setParameter("id", profileId).
168 //        setParameter("name", pluginKey).
169 //        getResultList();
170 //  }
171 
172 
173   public List<RuleFailure> getRuleFailures(Snapshot snapshot) {
174     return databaseManager.createQuery(
175         "SELECT rf FROM RuleFailure rf " +
176             "WHERE rf.snapshotId=:snapshotId")
177         .setParameter("snapshotId", snapshot.getId())
178         .getResultList();
179   }
180 
181 
182   public void synchronizeRuleOfActiveRule(ActiveRule activeRule, String pluginKey) {
183     Rule rule = activeRule.getRule();
184     Rule ruleFromDataBase = getRuleByKey(pluginKey, rule.getKey());
185     activeRule.setRule(ruleFromDataBase);
186     List<RuleParam> ruleParamsFromDataBase = getRuleParams();
187     for (ActiveRuleParam activeRuleParam : activeRule.getActiveRuleParams()) {
188       boolean found = false;
189       Iterator<RuleParam> iterator = ruleParamsFromDataBase.iterator();
190       while (iterator.hasNext() && !found) {
191         RuleParam ruleParamFromDataBase = iterator.next();
192         if (isRuleParamEqual(activeRuleParam.getRuleParam(), ruleParamFromDataBase, rule.getKey(), pluginKey)) {
193           activeRuleParam.setRuleParam(ruleParamFromDataBase);
194           found = true;
195         }
196       }
197     }
198   }
199 
200   public boolean isRuleParamEqual(RuleParam ruleParam, RuleParam ruleParamFromDatabase, String ruleKey, String pluginKey) {
201     return ruleParam.getKey().equals(ruleParamFromDatabase.getKey()) &&
202         ruleKey.equals(ruleParamFromDatabase.getRule().getKey()) &&
203         ruleParamFromDatabase.getRule().getPluginName().equals(pluginKey);
204   }
205 
206   public RulesProfile getProfileById(int profileId) {
207     return databaseManager.getEntityManager().getReference(RulesProfile.class, profileId);
208   }
209 
210 }