001/*
002 * Sonar, open source software quality management tool.
003 * Copyright (C) 2008-2012 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 */
020package org.sonar.core.rule;
021
022import org.apache.commons.lang.StringUtils;
023import org.sonar.api.database.DatabaseSession;
024import org.sonar.api.rules.Rule;
025import org.sonar.api.rules.RuleFinder;
026import org.sonar.api.rules.RuleQuery;
027import org.sonar.jpa.session.DatabaseSessionFactory;
028
029import javax.persistence.Query;
030import java.util.Collection;
031import java.util.HashMap;
032import java.util.Map;
033
034public 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=:enabled ");
071    Map<String,Object> params = new HashMap<String,Object>();
072    params.put("enabled", Boolean.TRUE);
073    if (StringUtils.isNotBlank(query.getRepositoryKey())) {
074      hql.append("AND pluginName=:repositoryKey ");
075      params.put("repositoryKey", query.getRepositoryKey());
076    }
077    if (StringUtils.isNotBlank(query.getKey())) {
078      hql.append("AND key=:key ");
079      params.put("key", query.getKey());
080    }
081    if (StringUtils.isNotBlank(query.getConfigKey())) {
082      hql.append("AND configKey=:configKey ");
083      params.put("configKey", query.getConfigKey());
084    }
085
086    Query hqlQuery = session.createQuery(hql.toString());
087    for (Map.Entry<String, Object> entry : params.entrySet()) {
088      hqlQuery.setParameter(entry.getKey(), entry.getValue());
089    }
090    return hqlQuery;
091  }
092}