]> source.dussan.org Git - sonarqube.git/blob
c6a8a503256b80e030f096c6a2ae32ac9a491b4a
[sonarqube.git] /
1 package org.sonar.plugins.core.timemachine;
2
3 import org.sonar.api.BatchExtension;
4 import org.sonar.api.database.DatabaseSession;
5 import org.sonar.api.database.model.RuleFailureModel;
6 import org.sonar.api.database.model.Snapshot;
7 import org.sonar.api.database.model.SnapshotSource;
8 import org.sonar.api.resources.Resource;
9 import org.sonar.batch.index.ResourcePersister;
10
11 import java.util.Collections;
12 import java.util.List;
13
14 public class PastViolationsLoader implements BatchExtension {
15
16   private DatabaseSession session;
17   private ResourcePersister resourcePersister;
18
19   public PastViolationsLoader(DatabaseSession session, ResourcePersister resourcePersister) {
20     this.session = session;
21     this.resourcePersister = resourcePersister;
22   }
23
24   public List<RuleFailureModel> getPastViolations(Resource resource) {
25     Snapshot snapshot = resourcePersister.getSnapshot(resource);
26     Snapshot previousLastSnapshot = resourcePersister.getLastSnapshot(snapshot, true);
27     if (previousLastSnapshot == null) {
28       return Collections.emptyList();
29     }
30     return session.getResults(RuleFailureModel.class,
31         "snapshotId", previousLastSnapshot.getId());
32   }
33
34   public SnapshotSource getSource(Resource resource) {
35     Snapshot snapshot = resourcePersister.getSnapshot(resource);
36     return session.getSingleResult(SnapshotSource.class,
37         "snapshotId", snapshot.getId());
38   }
39
40 }