1 package org.sonar.plugins.core.timemachine;
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.api.utils.SonarException;
10 import org.sonar.batch.index.ResourcePersister;
12 import java.util.Collections;
13 import java.util.List;
15 public class PastViolationsLoader implements BatchExtension {
17 private DatabaseSession session;
18 private ResourcePersister resourcePersister;
20 public PastViolationsLoader(DatabaseSession session, ResourcePersister resourcePersister) {
21 this.session = session;
22 this.resourcePersister = resourcePersister;
25 public List<RuleFailureModel> getPastViolations(Resource resource) {
26 if (resource == null) {
27 return Collections.emptyList();
30 Snapshot snapshot = resourcePersister.getSnapshot(resource);
31 if (snapshot == null) {
32 throw new SonarException("This resource has no snapshot ???" + resource);
34 Snapshot previousLastSnapshot = resourcePersister.getLastSnapshot(snapshot, true);
35 if (previousLastSnapshot == null) {
36 return Collections.emptyList();
38 return session.getResults(RuleFailureModel.class,
39 "snapshotId", previousLastSnapshot.getId());
42 public SnapshotSource getSource(Resource resource) {
43 Snapshot snapshot = resourcePersister.getSnapshot(resource);
44 return session.getSingleResult(SnapshotSource.class,
45 "snapshotId", snapshot.getId());