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.api;
21  
22  import ch.hortis.sonar.model.MavenProject;
23  import ch.hortis.sonar.model.ProjectMeasure;
24  import ch.hortis.sonar.model.Snapshot;
25  import ch.hortis.sonar.service.MeasureKey;
26  import org.sonar.commons.DaoFacade;
27  
28  import javax.persistence.NoResultException;
29  import javax.persistence.Query;
30  import java.util.HashMap;
31  import java.util.Map;
32  
33  public abstract class AbstractMeasuresRecorder implements MeasuresRecorder {
34    protected DaoFacade daoFacade;
35    protected Map<MavenProject, Snapshot> snapshotByEntity;
36    protected Snapshot rootSnapshot;
37    protected MavenProject root;
38  
39    public AbstractMeasuresRecorder(DaoFacade daoFacade, Snapshot rootSnapshot) {
40      this.snapshotByEntity = new HashMap<MavenProject, Snapshot>();
41      this.daoFacade = daoFacade;
42      this.rootSnapshot = rootSnapshot;
43      if (rootSnapshot!=null) {
44        this.root = rootSnapshot.getMavenProject();
45      }
46    }
47  
48    public ProjectMeasure createProjectMeasure(MeasureKey key, Double value) {
49      key.setMetric(daoFacade.getMeasuresDao().getMetric(key.getMetric()));
50      return (ProjectMeasure) daoFacade.getDatabaseManager().save(new ProjectMeasure(rootSnapshot, key, value));
51    }
52  
53    private MavenProject getOrCreateEntity(MavenProject entity) {
54      if (entity.equals(rootSnapshot.getMavenProject())) {
55        return rootSnapshot.getMavenProject();
56      }
57      try {
58        String hql = "SELECT p FROM MavenProject p WHERE p.scope=:scope AND p.key=:key AND p.rootId=:rootId AND p.enabled=true";
59        if (entity.getQualifier() == null) {
60          hql += " AND p.qualifier IS NULL";
61        } else {
62          hql += " AND p.qualifier=:qualifier";
63        }
64        Query query = daoFacade.getDatabaseManager().getEntityManager().createQuery(hql);
65        query.setParameter("scope", entity.getScope());
66        query.setParameter("key", entity.getKey());
67        query.setParameter("rootId", root.getId());
68        if (entity.getQualifier() != null) {
69          query.setParameter("qualifier", entity.getQualifier());
70        }
71        return (MavenProject) query.getSingleResult();
72  
73      } catch (NoResultException e) {
74        entity.setRootId(root.getId());
75        return (MavenProject) daoFacade.getDatabaseManager().save(entity);
76      }
77    }
78  
79    protected Snapshot getOrCreateSnapshot(MavenProject entity, Snapshot parent) {
80      Snapshot snapshot = snapshotByEntity.get(entity);
81      if (snapshot == null) {
82        snapshot = new Snapshot();
83        snapshot.setMavenProject(daoFacade.getDatabaseManager().reattach(MavenProject.class, getOrCreateEntity(entity).getId()));
84  
85        snapshot.setCreatedAt(rootSnapshot.getCreatedAt());
86        if (parent != null) {
87          parent = daoFacade.getDatabaseManager().reattach(Snapshot.class, parent.getId());
88          snapshot.setParent(parent);
89  
90          if (parent.getRoot() == null) {
91            snapshot.setRoot(parent);
92          } else {
93            snapshot.setRoot(daoFacade.getDatabaseManager().reattach(Snapshot.class, parent.getRoot().getId()));
94          }
95        }
96        snapshot = (Snapshot) daoFacade.getDatabaseManager().save(snapshot);
97        snapshotByEntity.put(entity, snapshot);
98      }
99      return snapshot;
100   }
101 }