001    /*
002     * Sonar, open source software quality management tool.
003     * Copyright (C) 2008-2011 SonarSource
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.api.rules;
021    
022    import com.google.common.collect.Maps;
023    import org.sonar.jpa.dao.RulesDao;
024    
025    import java.util.HashMap;
026    import java.util.List;
027    import java.util.Map;
028    
029    /**
030     * A class to manage and access rules defined in Sonar.
031     *
032     * @deprecated UGLY CLASS 
033     */
034    @Deprecated
035    public class DefaultRulesManager extends RulesManager {
036    
037      private final Map<String, Map<String, Rule>> rulesByPluginAndKey = Maps.newHashMap();
038      private final RulesDao rulesDao;
039    
040      public DefaultRulesManager(RulesDao dao) {
041        this.rulesDao = dao;
042      }
043    
044      /**
045       * Gets a list of rules indexed by their key for a given plugin
046       *
047       * @param pluginKey the plugin key
048       * @return a Map with the rule key and the rule
049       */
050      public Map<String, Rule> getPluginRulesIndexedByKey(String pluginKey) {
051        Map<String, Rule> rulesByKey = rulesByPluginAndKey.get(pluginKey);
052        if (rulesByKey == null) {
053          rulesByKey = new HashMap<String, Rule>();
054          List<Rule> rules = rulesDao.getRulesByPlugin(pluginKey);
055          if (rules != null) {
056            for (Rule rule : rules) {
057              rulesByKey.put(rule.getKey(), rule);
058            }
059          }
060          rulesByPluginAndKey.put(pluginKey, rulesByKey);
061        }
062        return rulesByKey;
063      }
064    
065      /**
066       * Gets a rule belonging to a defined plugin based on its key
067       *
068       * @param pluginKey the plugin key
069       * @param ruleKey   the rule key
070       * @return the rule
071       */
072      public Rule getPluginRule(String pluginKey, String ruleKey) {
073        Map<String, Rule> rulesByKey = getPluginRulesIndexedByKey(pluginKey);
074        return rulesByKey.get(ruleKey);
075      }
076    
077    }