From 37207267bf3a1149f78a5022ed8e016cac6b85ca Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Sun, 6 Jan 2013 12:43:09 +0100 Subject: o Adding a view for showing a list of people. --- src/main/java/io/trygvis/esper/testing/Util.java | 47 +++++++++- .../trygvis/esper/testing/core/db/PersonDao.java | 12 ++- .../esper/testing/util/sql/PageRequest.java | 16 +--- .../esper/testing/web/JerseyApplication.java | 16 +++- .../esper/testing/web/resource/CoreResource.java | 4 +- src/main/webapp/apps/core/navbar.html | 4 +- src/main/webapp/apps/frontPageApp/frontPage.html | 2 +- src/main/webapp/apps/frontPageApp/frontPageApp.js | 11 ++- src/main/webapp/apps/frontPageApp/person.html | 102 ++++++++++----------- src/main/webapp/apps/frontPageApp/personList.html | 35 +++++++ 10 files changed, 167 insertions(+), 82 deletions(-) create mode 100755 src/main/webapp/apps/frontPageApp/personList.html 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 childText(Element e, String childName) { - return fromNull(e.getChildText(childName)); - } + // ----------------------------------------------------------------------- + // SQL + // ----------------------------------------------------------------------- - public static Option child(Element e, String childName) { - return fromNull(e.getChild(childName)); + public static String orderBy(Iterable 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 childText(Element e, String childName) { + return fromNull(e.getChildText(childName)); + } + + public static Option 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 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 startIndex; public final Option count; - public static final PageRequest FIRST_PAGE = new PageRequest(Option.none(), Option.none()); + public final List orderBy; + public static final PageRequest FIRST_PAGE = new PageRequest(Option.none(), Option.none(), List.nil()); - public PageRequest(Option startIndex, Option count) { + public PageRequest(Option startIndex, Option count, List 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 queryParameters = hc.getRequest().getQueryParameters(); + List list = queryParameters.get("orderBy"); + + fj.data.List 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 getPersons(@Context final HttpServletRequest req) throws Exception { - final PageRequest pageRequest = pageReq(req); - + public List getPersons(@MagicParam final PageRequest pageRequest) throws Exception { return da.inTransaction(new CoreDaosCallback>() { protected List run() throws SQLException { List list = new ArrayList<>(); diff --git a/src/main/webapp/apps/core/navbar.html b/src/main/webapp/apps/core/navbar.html index eb1f798..871bfde 100644 --- a/src/main/webapp/apps/core/navbar.html +++ b/src/main/webapp/apps/core/navbar.html @@ -5,8 +5,8 @@