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.core.components;
021    
022    import org.apache.commons.lang.StringUtils;
023    import org.sonar.api.database.DatabaseSession;
024    import org.sonar.api.rules.Rule;
025    import org.sonar.api.rules.RuleFinder;
026    import org.sonar.api.rules.RuleQuery;
027    import org.sonar.jpa.session.DatabaseSessionFactory;
028    
029    import javax.persistence.Query;
030    import java.util.Collection;
031    import java.util.HashMap;
032    import java.util.Map;
033    
034    public class DefaultRuleFinder implements RuleFinder {
035    
036      private DatabaseSessionFactory sessionFactory;
037    
038      public DefaultRuleFinder(DatabaseSessionFactory sessionFactory) {
039        this.sessionFactory = sessionFactory;
040      }
041    
042      public Rule findById(int ruleId) {
043        return doFindById(ruleId);
044      }
045    
046      protected final Rule doFindById(int ruleId) {
047        return sessionFactory.getSession().getSingleResult(Rule.class, "id", ruleId, "enabled", true);
048      }
049    
050      public Rule findByKey(String repositoryKey, String key) {
051        return doFindByKey(repositoryKey, key);
052      }
053    
054      protected final Rule doFindByKey(String repositoryKey, String key) {
055        return sessionFactory.getSession().getSingleResult(Rule.class, "pluginName", repositoryKey, "key", key, "enabled", true);
056      }
057    
058      public final Rule find(RuleQuery query) {
059        DatabaseSession session = sessionFactory.getSession();
060        return (Rule)session.getSingleResult(createHqlQuery(session, query), null);
061    
062      }
063    
064      public final Collection<Rule> findAll(RuleQuery query) {
065        DatabaseSession session = sessionFactory.getSession();
066        return createHqlQuery(session, query).getResultList();
067      }
068    
069      private Query createHqlQuery(DatabaseSession session, RuleQuery query) {
070        StringBuilder hql = new StringBuilder().append("from ").append(Rule.class.getSimpleName()).append(" where enabled=true ");
071        Map<String,Object> params = new HashMap<String,Object>();
072        if (StringUtils.isNotBlank(query.getRepositoryKey())) {
073          hql.append("AND pluginName=:repositoryKey ");
074          params.put("repositoryKey", query.getRepositoryKey());
075        }
076        if (StringUtils.isNotBlank(query.getKey())) {
077          hql.append("AND key=:key ");
078          params.put("key", query.getKey());
079        }
080        if (StringUtils.isNotBlank(query.getConfigKey())) {
081          hql.append("AND configKey=:configKey ");
082          params.put("configKey", query.getConfigKey());
083        }
084    
085        Query hqlQuery = session.createQuery(hql.toString());
086        for (Map.Entry<String, Object> entry : params.entrySet()) {
087          hqlQuery.setParameter(entry.getKey(), entry.getValue());
088        }
089        return hqlQuery;
090      }
091    }