From ebf90bc059ccf02bcc3cc64a5cc26334d592c267 Mon Sep 17 00:00:00 2001 From: Julien HENRY Date: Thu, 9 Jan 2014 09:27:40 +0100 Subject: [PATCH] SONAR-3024 Fix PastMeasureLoader to consider path to distinguish resources with same key --- .../batch/components/PastMeasuresLoader.java | 23 +++++++++++++++---- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/sonar-batch/src/main/java/org/sonar/batch/components/PastMeasuresLoader.java b/sonar-batch/src/main/java/org/sonar/batch/components/PastMeasuresLoader.java index 2e0225483e1..3797a872bc8 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/components/PastMeasuresLoader.java +++ b/sonar-batch/src/main/java/org/sonar/batch/components/PastMeasuresLoader.java @@ -21,6 +21,7 @@ package org.sonar.batch.components; import com.google.common.collect.Maps; import org.apache.commons.lang.ObjectUtils; +import org.apache.commons.lang.StringUtils; import org.sonar.api.BatchExtension; import org.sonar.api.database.DatabaseSession; import org.sonar.api.database.model.Snapshot; @@ -29,6 +30,9 @@ import org.sonar.api.measures.MetricFinder; import org.sonar.api.resources.Qualifiers; import org.sonar.api.resources.Resource; +import javax.annotation.Nullable; +import javax.persistence.Query; + import java.util.Collection; import java.util.Collections; import java.util.List; @@ -59,23 +63,32 @@ public class PastMeasuresLoader implements BatchExtension { public List getPastMeasures(Resource resource, PastSnapshot projectPastSnapshot) { if (projectPastSnapshot != null && projectPastSnapshot.getProjectSnapshot() != null) { - return getPastMeasures(resource.getEffectiveKey(), projectPastSnapshot.getProjectSnapshot()); + return getPastMeasures(resource.getEffectiveKey(), resource.getPath(), projectPastSnapshot.getProjectSnapshot()); } return Collections.emptyList(); } public List getPastMeasures(String resourceKey, Snapshot projectPastSnapshot) { + return getPastMeasures(resourceKey, null, projectPastSnapshot); + } + + public List getPastMeasures(String resourceKey, @Nullable String path, Snapshot projectPastSnapshot) { String sql = "select m.metric_id, m.characteristic_id, m.person_id, m.rule_id, m.value from project_measures m, snapshots s" + " where m.snapshot_id=s.id and m.metric_id in (:metricIds) " + " and (s.root_snapshot_id=:rootSnapshotId or s.id=:rootSnapshotId) " + - " and s.status=:status and s.project_id=(select p.id from projects p where p.kee=:resourceKey and p.qualifier<>:lib)"; - return session.createNativeQuery(sql) + " and s.status=:status and s.project_id=(select p.id from projects p where p.kee=:resourceKey and p.qualifier<>:lib" + + (StringUtils.isNotBlank(path) ? " and p.path=:path" : "") + + ")"; + Query q = session.createNativeQuery(sql) .setParameter("metricIds", metricByIds.keySet()) .setParameter("rootSnapshotId", ObjectUtils.defaultIfNull(projectPastSnapshot.getRootId(), projectPastSnapshot.getId())) .setParameter("resourceKey", resourceKey) .setParameter("lib", Qualifiers.LIBRARY) - .setParameter("status", Snapshot.STATUS_PROCESSED) - .getResultList(); + .setParameter("status", Snapshot.STATUS_PROCESSED); + if (StringUtils.isNotBlank(path)) { + q.setParameter("path", path); + } + return q.getResultList(); } public static int getMetricId(Object[] row) { -- 2.39.5