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.core.rule;
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 findByKey(String repositoryKey, String key) {
043 return sessionFactory.getSession().getSingleResult(Rule.class, "pluginName", repositoryKey, "key", key, "enabled", true);
044 }
045
046 public Rule find(RuleQuery query) {
047 DatabaseSession session = sessionFactory.getSession();
048 return (Rule)session.getSingleResult(createHqlQuery(session, query), null);
049
050 }
051
052 public Collection<Rule> findAll(RuleQuery query) {
053 DatabaseSession session = sessionFactory.getSession();
054 return createHqlQuery(session, query).getResultList();
055 }
056
057 private Query createHqlQuery(DatabaseSession session, RuleQuery query) {
058 StringBuilder hql = new StringBuilder().append("from ").append(Rule.class.getSimpleName()).append(" where enabled=true ");
059 Map<String,Object> params = new HashMap<String,Object>();
060 if (StringUtils.isNotBlank(query.getRepositoryKey())) {
061 hql.append("AND pluginName=:repositoryKey ");
062 params.put("repositoryKey", query.getRepositoryKey());
063 }
064 if (StringUtils.isNotBlank(query.getKey())) {
065 hql.append("AND key=:key ");
066 params.put("key", query.getKey());
067 }
068 if (StringUtils.isNotBlank(query.getConfigKey())) {
069 hql.append("AND configKey=:configKey ");
070 params.put("configKey", query.getConfigKey());
071 }
072
073 Query hqlQuery = session.createQuery(hql.toString());
074 for (Map.Entry<String, Object> entry : params.entrySet()) {
075 hqlQuery.setParameter(entry.getKey(), entry.getValue());
076 }
077 return hqlQuery;
078 }
079 }