*/
package org.sonar.batch;
-import java.net.MalformedURLException;
-import java.net.URL;
-import java.util.List;
-
+import com.google.common.collect.Lists;
import org.apache.commons.configuration.Configuration;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.sonar.core.plugin.JpaPluginDao;
import org.sonar.core.plugin.JpaPluginFile;
-import com.google.common.collect.Lists;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.util.List;
public class BatchPluginRepository extends AbstractPluginRepository {
return snapshot;
}
- public Snapshot getPreviousLastSnapshot(Snapshot snapshot) {
- Query query = session.createQuery(
- "SELECT s FROM " + Snapshot.class.getSimpleName() + " s " +
- "WHERE s.last=true AND s.resourceId=:resourceId");
+ public Snapshot getLastSnapshot(Snapshot snapshot, boolean onlyOlder) {
+ String hql = "SELECT s FROM " + Snapshot.class.getSimpleName() + " s WHERE s.last=true AND s.resourceId=:resourceId";
+ if (onlyOlder) {
+ hql += " AND s.createdAt<:date";
+ }
+ Query query = session.createQuery(hql);
query.setParameter("resourceId", snapshot.getResourceId());
+ if (onlyOlder) {
+ query.setParameter("date", snapshot.getCreatedAt());
+ }
return session.getSingleResult(query, null);
}
Snapshot saveResource(Project project, Resource resource);
- Snapshot getPreviousLastSnapshot(Snapshot snapshot);
+ /**
+ * The current snapshot which is flagged as "last"
+ * @param onlyOlder true if the result must be anterior to the snapshot parameter
+ */
+ Snapshot getLastSnapshot(Snapshot snapshot, boolean onlyOlder);
void clear();
}
public List<RuleFailureModel> getPreviousViolations(Resource resource) {
Snapshot snapshot = resourcePersister.getSnapshot(resource);
- Snapshot previousLastSnapshot = resourcePersister.getPreviousLastSnapshot(snapshot);
+ Snapshot previousLastSnapshot = resourcePersister.getLastSnapshot(snapshot, true);
if (previousLastSnapshot == null) {
return Collections.emptyList();
}
}
public void execute() {
- Snapshot previousLastSnapshot = resourcePersister.getPreviousLastSnapshot(snapshot);
+ Snapshot previousLastSnapshot = resourcePersister.getLastSnapshot(snapshot, false);
updateFlags(snapshot, previousLastSnapshot);
}
Snapshot snapshot = getSession().getSingleResult(Snapshot.class, "id", 1000);
ResourcePersister resourcePersister = mock(ResourcePersister.class);
when(resourcePersister.saveResource((Project) anyObject(), eq(javaFile))).thenReturn(snapshot);
- when(resourcePersister.getPreviousLastSnapshot(snapshot)).thenReturn(snapshot);
+ when(resourcePersister.getLastSnapshot(snapshot, true)).thenReturn(snapshot);
when(resourcePersister.getSnapshot(javaFile)).thenReturn(snapshot);
violationPersister = new ViolationPersister(getSession(), resourcePersister, new DefaultRuleFinder(getSessionFactory()));
}
path="[null]"/>
- <!-- the snapshot to process -->
+ <!-- the snapshot to process : before last snapshot -->
<snapshots depth="[null]" id="6" scope="PRJ" qualifier="TRK" created_at="2005-10-01 00:00:00.00" version="[null]"
project_id="1"
parent_snapshot_id="[null]" root_project_id="[null]" root_snapshot_id="[null]" status="[null]"