aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/io
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/io')
-rwxr-xr-xsrc/main/java/io/trygvis/esper/testing/Util.java47
-rwxr-xr-xsrc/main/java/io/trygvis/esper/testing/core/db/PersonDao.java12
-rw-r--r--src/main/java/io/trygvis/esper/testing/util/sql/PageRequest.java16
-rwxr-xr-xsrc/main/java/io/trygvis/esper/testing/web/JerseyApplication.java16
-rwxr-xr-xsrc/main/java/io/trygvis/esper/testing/web/resource/CoreResource.java4
5 files changed, 71 insertions, 24 deletions
diff --git a/src/main/java/io/trygvis/esper/testing/Util.java b/src/main/java/io/trygvis/esper/testing/Util.java
index c33e64c..6b9d1b6 100755
--- a/src/main/java/io/trygvis/esper/testing/Util.java
+++ b/src/main/java/io/trygvis/esper/testing/Util.java
@@ -73,12 +73,37 @@ public class Util {
}
};
- public static Option<String> childText(Element e, String childName) {
- return fromNull(e.getChildText(childName));
- }
+ // -----------------------------------------------------------------------
+ // SQL
+ // -----------------------------------------------------------------------
- public static Option<Element> child(Element e, String childName) {
- return fromNull(e.getChild(childName));
+ public static String orderBy(Iterable<String> inputs, String... allowed) {
+ StringBuilder buffer = new StringBuilder();
+
+ for (String input : inputs) {
+ boolean desc = false;
+
+ if (input.endsWith("-")) {
+ desc = true;
+ input = input.substring(0, input.length() - 1);
+ }
+
+ for (String s : allowed) {
+ if (s.equals(input)) {
+ if (buffer.length() == 0) {
+ buffer.append(" ORDER BY ");
+ } else {
+ buffer.append(", ");
+ }
+ buffer.append(s);
+ if (desc) {
+ buffer.append(" DESC");
+ }
+ }
+ }
+ }
+
+ return buffer.toString();
}
public static UUID[] toUuidArray(ResultSet rs, int index) throws SQLException {
@@ -102,4 +127,16 @@ public class Util {
}
return list;
}
+
+ // -----------------------------------------------------------------------
+ // XML
+ // -----------------------------------------------------------------------
+
+ public static Option<String> childText(Element e, String childName) {
+ return fromNull(e.getChildText(childName));
+ }
+
+ public static Option<Element> child(Element e, String childName) {
+ return fromNull(e.getChild(childName));
+ }
}
diff --git a/src/main/java/io/trygvis/esper/testing/core/db/PersonDao.java b/src/main/java/io/trygvis/esper/testing/core/db/PersonDao.java
index ec460ec..ba3f628 100755
--- a/src/main/java/io/trygvis/esper/testing/core/db/PersonDao.java
+++ b/src/main/java/io/trygvis/esper/testing/core/db/PersonDao.java
@@ -7,8 +7,8 @@ import io.trygvis.esper.testing.util.sql.*;
import org.joda.time.*;
import java.sql.*;
-import java.util.*;
import java.util.List;
+import java.util.*;
import static io.trygvis.esper.testing.Util.*;
import static io.trygvis.esper.testing.util.sql.SqlOption.*;
@@ -97,7 +97,13 @@ public class PersonDao {
}
public List<PersonDto> selectPersons(PageRequest pageRequest) throws SQLException {
- try (PreparedStatement s = c.prepareStatement("SELECT " + PERSON + " FROM person ORDER BY created_date DESC, name LIMIT ? OFFSET ?")) {
+ String sql = "SELECT " + PERSON + " FROM person";
+
+ sql += orderBy(pageRequest.orderBy, "name", "created_date");
+
+ sql += " LIMIT ? OFFSET ?";
+
+ try (PreparedStatement s = c.prepareStatement(sql)) {
int i = 1;
s.setInt(i++, pageRequest.count.orSome(10));
s.setInt(i, pageRequest.startIndex.orSome(0));
@@ -199,7 +205,7 @@ public class PersonDao {
try (PreparedStatement s = c.prepareStatement(sql)) {
int i = 1;
- if(person.isSome()) {
+ if (person.isSome()) {
s.setString(i++, person.some().toUuidString());
}
if (type.isSome()) {
diff --git a/src/main/java/io/trygvis/esper/testing/util/sql/PageRequest.java b/src/main/java/io/trygvis/esper/testing/util/sql/PageRequest.java
index 38d417d..b7c6750 100644
--- a/src/main/java/io/trygvis/esper/testing/util/sql/PageRequest.java
+++ b/src/main/java/io/trygvis/esper/testing/util/sql/PageRequest.java
@@ -1,28 +1,20 @@
package io.trygvis.esper.testing.util.sql;
import fj.data.*;
-import static fj.data.Option.fromNull;
-import io.trygvis.esper.testing.*;
-
-import javax.servlet.http.*;
public class PageRequest {
public final Option<Integer> startIndex;
public final Option<Integer> count;
- public static final PageRequest FIRST_PAGE = new PageRequest(Option.<Integer>none(), Option.<Integer>none());
+ public final List<String> orderBy;
+ public static final PageRequest FIRST_PAGE = new PageRequest(Option.<Integer>none(), Option.<Integer>none(), List.<String>nil());
- public PageRequest(Option<Integer> startIndex, Option<Integer> count) {
+ public PageRequest(Option<Integer> startIndex, Option<Integer> count, List<String> orderBy) {
this.startIndex = startIndex;
this.count = count;
+ this.orderBy = orderBy;
}
public String toString() {
return "PageRequest{startIndex=" + startIndex + ", count=" + count + '}';
}
-
- public static PageRequest pageReq(HttpServletRequest req) {
- return new PageRequest(
- fromNull(req.getParameter("startIndex")).bind(Util.parseInt),
- fromNull(req.getParameter("count")).bind(Util.parseInt));
- }
}
diff --git a/src/main/java/io/trygvis/esper/testing/web/JerseyApplication.java b/src/main/java/io/trygvis/esper/testing/web/JerseyApplication.java
index 3c86581..38cfd9d 100755
--- a/src/main/java/io/trygvis/esper/testing/web/JerseyApplication.java
+++ b/src/main/java/io/trygvis/esper/testing/web/JerseyApplication.java
@@ -15,7 +15,12 @@ import javax.ws.rs.core.*;
import javax.ws.rs.ext.*;
import java.lang.reflect.*;
import java.util.*;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+import static fj.data.List.iterableList;
+import static fj.data.List.nil;
import static fj.data.Option.*;
import static io.trygvis.esper.testing.Util.parseInt;
@@ -59,9 +64,18 @@ public class JerseyApplication extends Application {
public Object getValue(HttpContext hc) {
MultivaluedMap<String, String> queryParameters = hc.getRequest().getQueryParameters();
+ List<String> list = queryParameters.get("orderBy");
+
+ fj.data.List<String> orderBy = nil();
+
+ if (list != null) {
+ orderBy = iterableList(list);
+ }
+
return new PageRequest(
fromNull(queryParameters.getFirst("startIndex")).bind(parseInt),
- fromNull(queryParameters.getFirst("count")).bind(parseInt));
+ fromNull(queryParameters.getFirst("count")).bind(parseInt),
+ orderBy);
}
};
} else if (UUID.class.equals(type)) {
diff --git a/src/main/java/io/trygvis/esper/testing/web/resource/CoreResource.java b/src/main/java/io/trygvis/esper/testing/web/resource/CoreResource.java
index c58bc13..4ab95c6 100755
--- a/src/main/java/io/trygvis/esper/testing/web/resource/CoreResource.java
+++ b/src/main/java/io/trygvis/esper/testing/web/resource/CoreResource.java
@@ -35,9 +35,7 @@ public class CoreResource extends AbstractResource {
@GET
@Path("/person")
- public List<PersonDetailJson> getPersons(@Context final HttpServletRequest req) throws Exception {
- final PageRequest pageRequest = pageReq(req);
-
+ public List<PersonDetailJson> getPersons(@MagicParam final PageRequest pageRequest) throws Exception {
return da.inTransaction(new CoreDaosCallback<List<PersonDetailJson>>() {
protected List<PersonDetailJson> run() throws SQLException {
List<PersonDetailJson> list = new ArrayList<>();