aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-core
diff options
context:
space:
mode:
authorSimon Brandhof <simon.brandhof@gmail.com>2012-11-29 15:33:01 +0100
committerSimon Brandhof <simon.brandhof@gmail.com>2012-11-29 18:59:33 +0100
commitb57aa56db1bc54a98c04323733635ec664d9ec7f (patch)
treef145212b229da1d040b333c624152b7bd6254acb /sonar-core
parenteb5382359405ef38b5a45a13ac776110c9c6a98b (diff)
downloadsonarqube-b57aa56db1bc54a98c04323733635ec664d9ec7f.tar.gz
sonarqube-b57aa56db1bc54a98c04323733635ec664d9ec7f.zip
SONAR-3825 support the parameter "baseId"
Diffstat (limited to 'sonar-core')
-rw-r--r--sonar-core/src/main/java/org/sonar/core/measure/MeasureFilter.java15
-rw-r--r--sonar-core/src/main/java/org/sonar/core/measure/MeasureFilterCondition.java45
-rw-r--r--sonar-core/src/main/java/org/sonar/core/measure/MeasureFilterDecoder.java2
-rw-r--r--sonar-core/src/main/java/org/sonar/core/measure/MeasureFilterExecutor.java3
-rw-r--r--sonar-core/src/main/java/org/sonar/core/measure/MeasureFilterFactory.java14
-rw-r--r--sonar-core/src/main/java/org/sonar/core/resource/ResourceDao.java6
-rw-r--r--sonar-core/src/main/java/org/sonar/core/resource/ResourceMapper.java4
-rw-r--r--sonar-core/src/main/resources/org/sonar/core/resource/ResourceMapper.xml4
-rw-r--r--sonar-core/src/test/java/org/sonar/core/measure/MeasureFilterExecutorTest.java10
9 files changed, 85 insertions, 18 deletions
diff --git a/sonar-core/src/main/java/org/sonar/core/measure/MeasureFilter.java b/sonar-core/src/main/java/org/sonar/core/measure/MeasureFilter.java
index cbe358cb792..4145203540b 100644
--- a/sonar-core/src/main/java/org/sonar/core/measure/MeasureFilter.java
+++ b/sonar-core/src/main/java/org/sonar/core/measure/MeasureFilter.java
@@ -36,7 +36,11 @@ public class MeasureFilter {
// conditions on resources
private String baseResourceKey;
- private boolean onBaseResourceChildren = false; // only if getBaseResourceKey is set
+ private Long baseResourceId;
+
+ // only if baseResourceKey or baseResourceId are set
+ private boolean onBaseResourceChildren = false;
+
private List<String> resourceScopes = Collections.emptyList();
private List<String> resourceQualifiers = Collections.emptyList();
private List<String> resourceLanguages = Collections.emptyList();
@@ -60,6 +64,15 @@ public class MeasureFilter {
return this;
}
+ public Long getBaseResourceId() {
+ return baseResourceId;
+ }
+
+ public MeasureFilter setBaseResourceId(Long i) {
+ this.baseResourceId = i;
+ return this;
+ }
+
public MeasureFilter setOnBaseResourceChildren(boolean b) {
this.onBaseResourceChildren = b;
return this;
diff --git a/sonar-core/src/main/java/org/sonar/core/measure/MeasureFilterCondition.java b/sonar-core/src/main/java/org/sonar/core/measure/MeasureFilterCondition.java
index f991b2f270f..2485c905583 100644
--- a/sonar-core/src/main/java/org/sonar/core/measure/MeasureFilterCondition.java
+++ b/sonar-core/src/main/java/org/sonar/core/measure/MeasureFilterCondition.java
@@ -24,13 +24,50 @@ import org.apache.commons.lang.builder.ToStringStyle;
import org.sonar.api.measures.Metric;
public class MeasureFilterCondition {
+ public enum Operator {
+ EQUALS("eq", "="), GREATER("gt", ">"), GREATER_OR_EQUALS("gte", ">="), LESS("lt", "<"), LESS_OR_EQUALS("lte", "<=");
+
+ private String code;
+ private String sql;
+
+ private Operator(String code, String sql) {
+ this.code = code;
+ this.sql = sql;
+ }
+
+ public String getCode() {
+ return code;
+ }
+
+ public String getSql() {
+ return sql;
+ }
+
+ public static Operator fromCode(String code) {
+ for (Operator operator : values()) {
+ if (operator.code.equals(code)) {
+ return operator;
+ }
+ }
+ throw new IllegalArgumentException("Unknown operator code: " + code);
+ }
+
+ public static Operator fromSql(String sql) {
+ for (Operator operator : values()) {
+ if (operator.sql.equals(sql)) {
+ return operator;
+ }
+ }
+ throw new IllegalArgumentException("Unknown operator sql: " + sql);
+ }
+ }
private final Metric metric;
- private final String operator;
+ private final Operator operator;
private final double value;
private Integer period = null;
- public MeasureFilterCondition(Metric metric, String operator, double value) {
+ public MeasureFilterCondition(Metric metric, Operator operator, double value) {
this.metric = metric;
this.operator = operator;
this.value = value;
@@ -45,7 +82,7 @@ public class MeasureFilterCondition {
return metric;
}
- public String operator() {
+ public Operator operator() {
return operator;
}
@@ -67,7 +104,7 @@ public class MeasureFilterCondition {
void appendSqlCondition(StringBuilder sql) {
sql.append(" pm.metric_id=");
sql.append(metric.getId());
- sql.append(" AND ").append(valueColumn()).append(operator).append(value);
+ sql.append(" AND ").append(valueColumn()).append(operator.getSql()).append(value);
}
@Override
diff --git a/sonar-core/src/main/java/org/sonar/core/measure/MeasureFilterDecoder.java b/sonar-core/src/main/java/org/sonar/core/measure/MeasureFilterDecoder.java
index c1871be38de..eed8ded2246 100644
--- a/sonar-core/src/main/java/org/sonar/core/measure/MeasureFilterDecoder.java
+++ b/sonar-core/src/main/java/org/sonar/core/measure/MeasureFilterDecoder.java
@@ -106,7 +106,7 @@ public class MeasureFilterDecoder implements ServerComponent {
Metric metric = metricFinder.findByKey((String) c.get("metric"));
String operator = (String) c.get("op");
Double value = (Double) c.get("val");
- MeasureFilterCondition condition = new MeasureFilterCondition(metric, operator, value);
+ MeasureFilterCondition condition = new MeasureFilterCondition(metric, MeasureFilterCondition.Operator.fromSql(operator), value);
if (c.containsKey("period")) {
condition.setPeriod(((Long) c.get("period")).intValue());
}
diff --git a/sonar-core/src/main/java/org/sonar/core/measure/MeasureFilterExecutor.java b/sonar-core/src/main/java/org/sonar/core/measure/MeasureFilterExecutor.java
index 0de8fad5f27..64179399792 100644
--- a/sonar-core/src/main/java/org/sonar/core/measure/MeasureFilterExecutor.java
+++ b/sonar-core/src/main/java/org/sonar/core/measure/MeasureFilterExecutor.java
@@ -72,11 +72,14 @@ public class MeasureFilterExecutor implements ServerComponent {
private void prepareContext(MeasureFilterContext context, MeasureFilter filter, SqlSession session) {
if (filter.getBaseResourceKey() != null) {
context.setBaseSnapshot(resourceDao.getLastSnapshot(filter.getBaseResourceKey(), session));
+ } else if (filter.getBaseResourceId() != null) {
+ context.setBaseSnapshot(resourceDao.getLastSnapshotByResourceId(filter.getBaseResourceId(), session));
}
}
static boolean isValid(MeasureFilter filter, MeasureFilterContext context) {
boolean valid = (Strings.isNullOrEmpty(filter.getBaseResourceKey()) || context.getBaseSnapshot()!=null);
+ valid &= (filter.getBaseResourceId()==null || context.getBaseSnapshot()!=null);
valid &= !(filter.isOnBaseResourceChildren() && context.getBaseSnapshot() == null);
valid &= !(filter.isOnFavourites() && context.getUserId() == null);
valid &= validateMeasureConditions(filter);
diff --git a/sonar-core/src/main/java/org/sonar/core/measure/MeasureFilterFactory.java b/sonar-core/src/main/java/org/sonar/core/measure/MeasureFilterFactory.java
index bff48d90249..e951420881c 100644
--- a/sonar-core/src/main/java/org/sonar/core/measure/MeasureFilterFactory.java
+++ b/sonar-core/src/main/java/org/sonar/core/measure/MeasureFilterFactory.java
@@ -45,6 +45,9 @@ public class MeasureFilterFactory implements ServerComponent {
public MeasureFilter create(Map<String, Object> properties) {
MeasureFilter filter = new MeasureFilter();
filter.setBaseResourceKey((String) properties.get("base"));
+ if (properties.containsKey("baseId")) {
+ filter.setBaseResourceId(Long.valueOf((String) properties.get("baseId")));
+ }
filter.setResourceScopes(toList(properties.get("scopes")));
filter.setResourceQualifiers(toList(properties.get("qualifiers")));
filter.setResourceLanguages(toList(properties.get("languages")));
@@ -92,13 +95,14 @@ public class MeasureFilterFactory implements ServerComponent {
private MeasureFilterCondition toCondition(Map<String, Object> props, int index) {
MeasureFilterCondition condition = null;
- String metricKey = (String) props.get("c" + index + "metric");
- String op = (String) props.get("c" + index + "op");
- String val = (String) props.get("c" + index + "val");
+ String metricKey = (String) props.get("c" + index + "_metric");
+ String op = (String) props.get("c" + index + "_op");
+ String val = (String) props.get("c" + index + "_val");
if (!Strings.isNullOrEmpty(metricKey) && !Strings.isNullOrEmpty(op) && !Strings.isNullOrEmpty(val)) {
Metric metric = metricFinder.findByKey(metricKey);
- condition = new MeasureFilterCondition(metric, op, Double.parseDouble(val));
- String period = (String) props.get("c" + index + "period");
+ MeasureFilterCondition.Operator operator = MeasureFilterCondition.Operator.fromCode(op);
+ condition = new MeasureFilterCondition(metric, operator, Double.parseDouble(val));
+ String period = (String) props.get("c" + index + "_period");
if (period != null) {
condition.setPeriod(Integer.parseInt(period));
}
diff --git a/sonar-core/src/main/java/org/sonar/core/resource/ResourceDao.java b/sonar-core/src/main/java/org/sonar/core/resource/ResourceDao.java
index 1b6a4e1398f..b8a7c88e546 100644
--- a/sonar-core/src/main/java/org/sonar/core/resource/ResourceDao.java
+++ b/sonar-core/src/main/java/org/sonar/core/resource/ResourceDao.java
@@ -77,9 +77,13 @@ public class ResourceDao {
}
public SnapshotDto getLastSnapshot(String resourceKey, SqlSession session) {
- return session.getMapper(ResourceMapper.class).selectLastSnapshotByKey(resourceKey);
+ return session.getMapper(ResourceMapper.class).selectLastSnapshotByResourceKey(resourceKey);
}
+ public SnapshotDto getLastSnapshotByResourceId(long resourceId, SqlSession session) {
+ return session.getMapper(ResourceMapper.class).selectLastSnapshotByResourceId(resourceId);
+ }
+
public List<ResourceDto> getDescendantProjects(long projectId) {
SqlSession session = mybatis.openSession();
try {
diff --git a/sonar-core/src/main/java/org/sonar/core/resource/ResourceMapper.java b/sonar-core/src/main/java/org/sonar/core/resource/ResourceMapper.java
index 05b8ba5d53d..68ac9a3af59 100644
--- a/sonar-core/src/main/java/org/sonar/core/resource/ResourceMapper.java
+++ b/sonar-core/src/main/java/org/sonar/core/resource/ResourceMapper.java
@@ -26,7 +26,9 @@ import java.util.List;
public interface ResourceMapper {
SnapshotDto selectSnapshot(Long snapshotId);
- SnapshotDto selectLastSnapshotByKey(String resourceKey);
+ SnapshotDto selectLastSnapshotByResourceKey(String resourceKey);
+
+ SnapshotDto selectLastSnapshotByResourceId(long resourceId);
ResourceDto selectResource(long id);
diff --git a/sonar-core/src/main/resources/org/sonar/core/resource/ResourceMapper.xml b/sonar-core/src/main/resources/org/sonar/core/resource/ResourceMapper.xml
index 0cc52cbbdf8..044da26c78f 100644
--- a/sonar-core/src/main/resources/org/sonar/core/resource/ResourceMapper.xml
+++ b/sonar-core/src/main/resources/org/sonar/core/resource/ResourceMapper.xml
@@ -83,6 +83,10 @@
select s.* from snapshots s, projects p where p.kee=#{id} and p.enabled=${_true} and p.copy_resource_id is null and s.islast=${_true} and p.id=s.project_id
</select>
+ <select id="selectLastSnapshotByResourceId" parameterType="string" resultMap="snapshotResultMap">
+ select s.* from snapshots s where s.project_id=#{id} and s.islast=${_true}
+ </select>
+
<select id="selectDescendantProjects" parameterType="long" resultMap="resourceResultMap">
select * from projects where scope='PRJ' and root_id=#{id}
</select>
diff --git a/sonar-core/src/test/java/org/sonar/core/measure/MeasureFilterExecutorTest.java b/sonar-core/src/test/java/org/sonar/core/measure/MeasureFilterExecutorTest.java
index d71a364c238..a755be6cdbe 100644
--- a/sonar-core/src/test/java/org/sonar/core/measure/MeasureFilterExecutorTest.java
+++ b/sonar-core/src/test/java/org/sonar/core/measure/MeasureFilterExecutorTest.java
@@ -75,7 +75,7 @@ public class MeasureFilterExecutorTest extends AbstractDaoTestCase {
@Test
public void filter_is_not_valid_if_condition_on_unknown_metric() {
MeasureFilterContext context = new MeasureFilterContext();
- MeasureFilter filter = new MeasureFilter().addCondition(new MeasureFilterCondition(null, "<", 3.0));
+ MeasureFilter filter = new MeasureFilter().addCondition(new MeasureFilterCondition(null, MeasureFilterCondition.Operator.LESS, 3.0));
assertThat(MeasureFilterExecutor.isValid(filter, context)).isFalse();
}
@@ -263,7 +263,7 @@ public class MeasureFilterExecutorTest extends AbstractDaoTestCase {
public void condition_on_numeric_measure() throws SQLException {
MeasureFilter filter = new MeasureFilter().setResourceQualifiers(Arrays.asList("CLA"))
.setSortOnMetric(METRIC_LINES)
- .addCondition(new MeasureFilterCondition(METRIC_LINES, ">", 200));
+ .addCondition(new MeasureFilterCondition(METRIC_LINES, MeasureFilterCondition.Operator.GREATER, 200));
List<MeasureFilterRow> rows = executor.execute(filter, new MeasureFilterContext());
assertThat(rows).hasSize(1);
@@ -274,7 +274,7 @@ public class MeasureFilterExecutorTest extends AbstractDaoTestCase {
public void condition_on_measure_variation() throws SQLException {
MeasureFilter filter = new MeasureFilter().setResourceQualifiers(Arrays.asList("TRK"))
.setSortOnMetric(METRIC_LINES)
- .addCondition(new MeasureFilterCondition(METRIC_LINES, ">", 1000).setPeriod(5));
+ .addCondition(new MeasureFilterCondition(METRIC_LINES, MeasureFilterCondition.Operator.GREATER, 1000).setPeriod(5));
List<MeasureFilterRow> rows = executor.execute(filter, new MeasureFilterContext());
assertThat(rows).hasSize(1);
@@ -285,8 +285,8 @@ public class MeasureFilterExecutorTest extends AbstractDaoTestCase {
public void multiple_conditions_on_numeric_measures() throws SQLException {
MeasureFilter filter = new MeasureFilter().setResourceQualifiers(Arrays.asList("CLA"))
.setSortOnMetric(METRIC_LINES)
- .addCondition(new MeasureFilterCondition(METRIC_LINES, ">", 2))
- .addCondition(new MeasureFilterCondition(METRIC_LINES, "<=", 50));
+ .addCondition(new MeasureFilterCondition(METRIC_LINES, MeasureFilterCondition.Operator.GREATER, 2))
+ .addCondition(new MeasureFilterCondition(METRIC_LINES, MeasureFilterCondition.Operator.LESS_OR_EQUALS, 50));
List<MeasureFilterRow> rows = executor.execute(filter, new MeasureFilterContext());
assertThat(rows).hasSize(1);