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 {library}; if not, write to the Free Software
18   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
19   */
20  package ch.hortis.sonar.service;
21  
22  import ch.hortis.sonar.model.MavenProject;
23  import ch.hortis.sonar.model.Metric;
24  import ch.hortis.sonar.model.ProjectMeasure;
25  import ch.hortis.sonar.model.Snapshot;
26  import org.sonar.commons.database.DatabaseManager;
27  
28  import javax.persistence.Query;
29  import java.util.ArrayList;
30  import java.util.Collection;
31  import java.util.HashMap;
32  import java.util.List;
33  import java.util.Map;
34  
35  public class MeasuresDao {
36  
37    private DatabaseManager databaseManager;
38    private Map<String, Metric> metricsByName = new HashMap<String, Metric>();
39  
40    public MeasuresDao(DatabaseManager databaseManager) {
41      this.databaseManager = databaseManager;
42    }
43  
44    public Metric getMetric(Metric metric) {
45      return getMetricsByName().get(metric.getName());
46    }
47  
48    public Metric getMetric(String metricName) {
49      return getMetricsByName().get(metricName);
50    }
51  
52    public Collection<Metric> getMetrics() {
53      return getMetricsByName().values();
54    }
55  
56    public void persistMetric(Metric metric) {
57      databaseManager.save(metric);
58      metricsByName.clear();
59    }
60  
61    private Map<String, Metric> getMetricsByName() {
62      if (metricsByName.isEmpty()) {
63        List<Metric> metrics = databaseManager.createQuery("SELECT m from " + Metric.class.getSimpleName() + " m").getResultList();
64        for (Metric metric : metrics) {
65          metricsByName.put(metric.getName(), metric);
66        }
67      }
68      return metricsByName;
69    }
70  
71    public List<ProjectMeasure> getMeasures(Integer snapshotId) {
72      Query query = databaseManager.createQuery(
73        "SELECT m from " + ProjectMeasure.class.getSimpleName() + " m where m.snapshotId=:snapshotId");
74      query.setParameter("snapshotId", snapshotId);
75      return query.getResultList();
76    }
77  
78    public Snapshot getSnapshot(int snapshotId) {
79      Query query = databaseManager.createQuery(
80        "SELECT s FROM " + Snapshot.class.getSimpleName() + " s WHERE s.id=:id");
81      query.setParameter("id", snapshotId);
82      return (Snapshot) query.getSingleResult();
83    }
84  
85    public String getSnapshotProjectName(int snapshotId) {
86      Query query = databaseManager.createQuery(
87        "SELECT p.name FROM " + MavenProject.class.getSimpleName() + " p, Snapshot s WHERE s.mavenProject=p AND s.id=:id");
88      query.setParameter("id", snapshotId);
89      return (String) query.getSingleResult();
90    }
91  
92    public List<Snapshot> getChildSnapshots(Snapshot snapshot, boolean includeSubmodules) {
93      List<Snapshot> result = new ArrayList<Snapshot>();
94      Query query = databaseManager.createQuery(
95        "SELECT s FROM " + Snapshot.class.getSimpleName() + " s WHERE s.parent = :parent");
96      query.setParameter("parent", snapshot);
97      List<Snapshot> childrens = query.getResultList();
98      result.addAll(childrens);
99      if (includeSubmodules) {
100       for (Snapshot child : childrens) {
101         List<Snapshot> subchildrens = getChildSnapshots(child, true);
102         result.addAll(subchildrens);
103       }
104     }
105     return result;
106   }
107 }