}
static boolean isValid(MeasureFilter filter, MeasureFilterContext context) {
- boolean valid =
- !(filter.isOnBaseResourceChildren() && context.getBaseSnapshot() == null) &&
- !(filter.isOnFavourites() && context.getUserId() == null);
+ boolean valid = !(filter.isOnBaseResourceChildren() && context.getBaseSnapshot() == null);
+ valid &= !(filter.isOnFavourites() && context.getUserId() == null);
+ valid &= validateMeasureConditions(filter);
+ valid &= validateSort(filter);
+ return valid;
+ }
+
+ private static boolean validateMeasureConditions(MeasureFilter filter) {
+ boolean valid = true;
for (MeasureFilterCondition condition : filter.getMeasureConditions()) {
if (condition.period() != null && condition.period() < 1) {
valid = false;
valid = false;
}
}
+ return valid;
+ }
+
+ private static boolean validateSort(MeasureFilter filter) {
+ boolean valid = true;
if (filter.sort().getPeriod() != null && filter.sort().getPeriod() < 1) {
valid = false;
}
sql.append(" AND p.language IN ");
appendInStatement(filter.getResourceLanguages(), sql);
}
+ appendDateConditions();
+ appendFavouritesCondition();
+ appendResourceNameCondition();
+ appendResourceKeyCondition();
+ appendResourceBaseCondition();
+ }
+
+ private void appendDateConditions() {
if (filter.getFromDate() != null) {
sql.append(" AND s.created_at >= ? ");
- dateParameters.add(new java.sql.Date(filter.getFromDate().getTime()));
+ dateParameters.add(new Date(filter.getFromDate().getTime()));
}
if (filter.getToDate() != null) {
sql.append(" AND s.created_at <= ? ");
- dateParameters.add(new java.sql.Date(filter.getToDate().getTime()));
+ dateParameters.add(new Date(filter.getToDate().getTime()));
}
+ }
+
+ private void appendFavouritesCondition() {
if (filter.isOnFavourites()) {
sql.append(" AND props.prop_key='favourite' AND props.resource_id IS NOT NULL AND props.user_id=");
sql.append(context.getUserId());
sql.append(" ");
}
- if (StringUtils.isNotBlank(filter.getResourceName())) {
- sql.append(" AND s.project_id IN (SELECT rindex.resource_id FROM resource_index rindex WHERE rindex.kee like '");
- sql.append(StringEscapeUtils.escapeSql(StringUtils.lowerCase(filter.getResourceName())));
- sql.append("%'");
- if (!filter.getResourceQualifiers().isEmpty()) {
- sql.append(" AND rindex.qualifier IN ");
- appendInStatement(filter.getResourceQualifiers(), sql);
+ }
+
+ private void appendResourceBaseCondition() {
+ SnapshotDto baseSnapshot = context.getBaseSnapshot();
+ if (baseSnapshot != null) {
+ if (filter.isOnBaseResourceChildren()) {
+ sql.append(" AND s.parent_snapshot_id=").append(baseSnapshot.getId());
+ } else {
+ Long rootSnapshotId = (baseSnapshot.getRootId() != null ? baseSnapshot.getRootId() : baseSnapshot.getId());
+ sql.append(" AND s.root_snapshot_id=").append(rootSnapshotId);
+ sql.append(" AND s.path LIKE '").append(StringUtils.defaultString(baseSnapshot.getPath())).append(baseSnapshot.getId()).append(".%'");
}
- sql.append(") ");
}
+ }
+
+ private void appendResourceKeyCondition() {
if (StringUtils.isNotBlank(filter.getResourceKeyRegexp())) {
sql.append(" AND UPPER(p.kee) LIKE '");
// limitation : special characters _ and % are not escaped
regexp = StringUtils.replaceChars(regexp, '?', '_');
sql.append(StringUtils.upperCase(regexp)).append("'");
}
- SnapshotDto baseSnapshot = context.getBaseSnapshot();
- if (baseSnapshot != null) {
- if (filter.isOnBaseResourceChildren()) {
- sql.append(" AND s.parent_snapshot_id=").append(baseSnapshot.getId());
- } else {
- Long rootSnapshotId = (baseSnapshot.getRootId() != null ? baseSnapshot.getRootId() : baseSnapshot.getId());
- sql.append(" AND s.root_snapshot_id=").append(rootSnapshotId);
- sql.append(" AND s.path LIKE '").append(StringUtils.defaultString(baseSnapshot.getPath())).append(baseSnapshot.getId()).append(".%'");
+ }
+
+ private void appendResourceNameCondition() {
+ if (StringUtils.isNotBlank(filter.getResourceName())) {
+ sql.append(" AND s.project_id IN (SELECT rindex.resource_id FROM resource_index rindex WHERE rindex.kee like '");
+ sql.append(StringEscapeUtils.escapeSql(StringUtils.lowerCase(filter.getResourceName())));
+ sql.append("%'");
+ if (!filter.getResourceQualifiers().isEmpty()) {
+ sql.append(" AND rindex.qualifier IN ");
+ appendInStatement(filter.getResourceQualifiers(), sql);
}
+ sql.append(") ");
}
}
verifyJavaTinyFile(rows.get(1));
}
+ @Test
+ public void sort_by_ascending_resource_key() throws SQLException {
+ MeasureFilter filter = new MeasureFilter().setResourceQualifiers("CLA").setSortAsc(true).setSortOn(MeasureFilterSort.Field.KEY);
+ List<MeasureFilterRow> rows = executor.execute(filter, new MeasureFilterContext());
+
+ // Big -> Tiny
+ assertThat(rows).hasSize(2);
+ verifyJavaBigFile(rows.get(0));
+ verifyJavaTinyFile(rows.get(1));
+ }
+
+ @Test
+ public void sort_by_ascending_resource_version() throws SQLException {
+ MeasureFilter filter = new MeasureFilter().setResourceQualifiers("TRK").setSortAsc(true).setSortOn(MeasureFilterSort.Field.VERSION);
+ List<MeasureFilterRow> rows = executor.execute(filter, new MeasureFilterContext());
+
+ // Java Project 1.0 then Php Project 3.0
+ assertThat(rows).hasSize(2);
+ verifyJavaProject(rows.get(0));
+ verifyPhpProject(rows.get(1));
+ }
+
@Test
public void sort_by_descending_resource_name() throws SQLException {
MeasureFilter filter = new MeasureFilter().setResourceQualifiers("CLA").setSortAsc(false);