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 Sonar; if not, write to the Free Software
18   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
19   */
20  package org.sonar.plugins.java;
21  
22  import ch.hortis.sonar.model.MavenProject;
23  import ch.hortis.sonar.model.ProjectMeasure;
24  import ch.hortis.sonar.model.Rule;
25  import ch.hortis.sonar.model.RuleFailure;
26  import ch.hortis.sonar.model.RuleFailureLevel;
27  import ch.hortis.sonar.model.RuleFailureParam;
28  import ch.hortis.sonar.model.Snapshot;
29  import ch.hortis.sonar.model.SnapshotSource;
30  import ch.hortis.sonar.service.MeasureKey;
31  import org.sonar.commons.DaoFacade;
32  import org.sonar.plugins.api.AbstractMeasuresRecorder;
33  
34  public class JavaMeasuresRecorder extends AbstractMeasuresRecorder {
35    public JavaMeasuresRecorder(DaoFacade daoFacade, Snapshot rootSnapshot) {
36      super(daoFacade, rootSnapshot);
37    }
38  
39    protected JavaMeasuresRecorder() {
40      super(null, null);
41    }
42  
43    public ProjectMeasure createPackageMeasure(String packageName, MeasureKey key, Double value) {
44      MavenProject packageEntity = JavaLanguage.newPackage(root.getKey(), packageName, root.getId());
45      Snapshot packageSnapshot = getOrCreateSnapshot(packageEntity, rootSnapshot);
46  
47      key.setMetric(daoFacade.getMeasuresDao().getMetric(key.getMetric()));
48      return (ProjectMeasure) daoFacade.getDatabaseManager().save(new ProjectMeasure(packageSnapshot, key, value));
49    }
50  
51    public ProjectMeasure createClassMeasure(String packageName, String className, boolean unitTest, MeasureKey key, Double value) {
52      Snapshot classSnapshot = getOrCreateClassSnapshot(packageName, className, unitTest);
53  
54      key.setMetric(daoFacade.getMeasuresDao().getMetric(key.getMetric()));
55      return (ProjectMeasure) daoFacade.getDatabaseManager().save(new ProjectMeasure(classSnapshot, key, value));
56    }
57  
58    public RuleFailure createClassRuleFailure(Rule rule, String packageName, String className, boolean unitTest, String message, RuleFailureLevel level, RuleFailureParam... params) {
59      Snapshot classSnapshot = getOrCreateClassSnapshot(packageName, className, unitTest);
60  
61      RuleFailure failure = new RuleFailure(rule, level);
62      failure.setMessage(message);
63      failure.setSnapshot(classSnapshot);
64      failure = (RuleFailure) daoFacade.getDatabaseManager().save(failure);
65  
66      for (RuleFailureParam param : params) {
67        if (param != null) {
68          param.setRuleFailure(failure);
69          param.setSnapshot(classSnapshot);
70          failure.getParameters().add(param);
71          daoFacade.getDatabaseManager().save(param);
72        }
73      }
74      return failure;
75    }
76  
77    public SnapshotSource createClassSource(String packageName, String className, String data, boolean unitTest) {
78      Snapshot classSnapshot = getOrCreateClassSnapshot(packageName, className, unitTest);
79      SnapshotSource source = new SnapshotSource(classSnapshot, data);
80      return (SnapshotSource) daoFacade.getDatabaseManager().save(source);
81    }
82  
83    private Snapshot getOrCreateClassSnapshot(String packageName, String className, boolean unitTest) {
84      MavenProject packageEntity = JavaLanguage.newPackage(root.getKey(), packageName, root.getId());
85      MavenProject classEntity = JavaLanguage.newClass(root.getKey(), packageName, className, unitTest, root.getId());
86  
87      Snapshot packageSnapshot = getOrCreateSnapshot(packageEntity, rootSnapshot);
88      return getOrCreateSnapshot(classEntity, packageSnapshot);
89    }
90  }