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.batch.index.ResourcePersister;
11 import java.util.Collections;
12 import java.util.List;
14 public class PastViolationsLoader implements BatchExtension {
16 private DatabaseSession session;
17 private ResourcePersister resourcePersister;
19 public PastViolationsLoader(DatabaseSession session, ResourcePersister resourcePersister) {
20 this.session = session;
21 this.resourcePersister = resourcePersister;
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();
30 return session.getResults(RuleFailureModel.class,
31 "snapshotId", previousLastSnapshot.getId());
34 public SnapshotSource getSource(Resource resource) {
35 Snapshot snapshot = resourcePersister.getSnapshot(resource);
36 return session.getSingleResult(SnapshotSource.class,
37 "snapshotId", snapshot.getId());