Skip to content

Commit

Permalink
Moved the sort order to the SQL fixing a paging bug.
Browse files Browse the repository at this point in the history
The sort order was currently only be applied to the page. This has since been fixed and is now applied to the SQL therefore the entire result set, not just the current page, will be affected by the sort order.
  • Loading branch information
John Jenkins committed Feb 23, 2012
1 parent 532829e commit 2808475
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 24 deletions.
2 changes: 1 addition & 1 deletion src/org/ohmage/domain/campaign/SurveyResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -687,7 +687,7 @@ public static enum SortParameter {
USER ("username");

// The string used to represent the columns on which to order.
private String sqlColumn;
private final String sqlColumn;

/**
* Creates a sort parameter with a String representing the SQL
Expand Down
35 changes: 20 additions & 15 deletions src/org/ohmage/query/impl/SurveyResponseQueries.java
Original file line number Diff line number Diff line change
Expand Up @@ -1012,25 +1012,30 @@ else if(onSurveyResponse || forCount) {

// Finally, add some ordering to facilitate consistent results in the
// paging system.
sqlBuilder.append(" ORDER BY ");
boolean firstPass = true;
for(SortParameter sortParameter : sortOrder) {
if(sortOrder == null) {
sqlBuilder.append(" ORDER BY epoch_millis DESC, uuid");
}
else {
sqlBuilder.append(" ORDER BY ");
boolean firstPass = true;
for(SortParameter sortParameter : sortOrder) {
if(firstPass) {
firstPass = false;
}
else {
sqlBuilder.append(", ");
}

sqlBuilder.append(sortParameter.getSqlColumn());
}
// We must always include the UUID in the order to guarantee that
// all survey responses are grouped together.
if(firstPass) {
firstPass = false;
sqlBuilder.append("uuid");
}
else {
sqlBuilder.append(", ");
sqlBuilder.append(", uuid");
}

sqlBuilder.append(sortParameter.getSqlColumn());
}
// We must always include the UUID in the order to guarantee that all
// survey responses are grouped together.
if(firstPass) {
sqlBuilder.append("uuid");
}
else {
sqlBuilder.append(", uuid");
}

return sqlBuilder.toString();
Expand Down
7 changes: 1 addition & 6 deletions src/org/ohmage/request/survey/SurveyResponseReadRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -248,14 +248,9 @@ public final class SurveyResponseReadRequest extends UserRequest {

public static final String URN_SPECIAL_ALL = "urn:ohmage:special:all";
public static final Collection<String> URN_SPECIAL_ALL_LIST;
public static final List<SortParameter> DEFAULT_SORT_ORDER;
static {
URN_SPECIAL_ALL_LIST = new HashSet<String>();
URN_SPECIAL_ALL_LIST.add(URN_SPECIAL_ALL);

List<SortParameter> tmpSortOrder = new ArrayList<SortParameter>();
tmpSortOrder.add(SortParameter.TIMESTAMP);
DEFAULT_SORT_ORDER = Collections.unmodifiableList(tmpSortOrder);
}

private static final int MAX_NUMBER_OF_USERS = 10;
Expand Down Expand Up @@ -309,7 +304,7 @@ public SurveyResponseReadRequest(HttpServletRequest httpRequest) {
Date tStartDate = null;
Date tEndDate = null;

List<SortParameter> tSortOrder = DEFAULT_SORT_ORDER;
List<SortParameter> tSortOrder = null;

SurveyResponse.PrivacyState tPrivacyState = null;

Expand Down
2 changes: 1 addition & 1 deletion src/org/ohmage/validator/SurveyResponseValidators.java
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ public static List<SortParameter> validateSortOrder(
throws ValidationException {

if(StringUtils.isEmptyOrWhitespaceOnly(sortOrder)) {
return SurveyResponseReadRequest.DEFAULT_SORT_ORDER;
return null;
}

List<SortParameter> result = new ArrayList<SortParameter>(3);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ public void testValidateOutputFormat() {
public void testValidateSortOrder() {
try {
for(String emptyValue : ParameterSets.getEmptyValues()) {
Assert.assertEquals(SurveyResponseValidators.validateSortOrder(emptyValue), SurveyResponseReadRequest.DEFAULT_SORT_ORDER);
Assert.assertNull(SurveyResponseValidators.validateSortOrder(emptyValue));
}

try {
Expand Down

0 comments on commit 2808475

Please sign in to comment.