aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/io
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/io')
-rw-r--r--src/main/java/io/trygvis/esper/testing/core/db/BuildDao.java31
-rw-r--r--src/main/java/io/trygvis/esper/testing/util/sql/PageRequest.java4
-rw-r--r--src/main/java/io/trygvis/esper/testing/web/JerseyApplication.java78
-rw-r--r--src/main/java/io/trygvis/esper/testing/web/MagicParam.java10
-rw-r--r--src/main/java/io/trygvis/esper/testing/web/resource/AbstractResource.java (renamed from src/main/java/io/trygvis/esper/testing/web/AbstractResource.java)2
-rw-r--r--src/main/java/io/trygvis/esper/testing/web/resource/BadgeJson.java21
-rw-r--r--src/main/java/io/trygvis/esper/testing/web/resource/CoreResource.java (renamed from src/main/java/io/trygvis/esper/testing/web/CoreResource.java)99
-rw-r--r--src/main/java/io/trygvis/esper/testing/web/resource/JenkinsResource.java (renamed from src/main/java/io/trygvis/esper/testing/web/JenkinsResource.java)2
-rw-r--r--src/main/java/io/trygvis/esper/testing/web/resource/PersonJson.java17
9 files changed, 215 insertions, 49 deletions
diff --git a/src/main/java/io/trygvis/esper/testing/core/db/BuildDao.java b/src/main/java/io/trygvis/esper/testing/core/db/BuildDao.java
index 24ecfd3..e498017 100644
--- a/src/main/java/io/trygvis/esper/testing/core/db/BuildDao.java
+++ b/src/main/java/io/trygvis/esper/testing/core/db/BuildDao.java
@@ -7,7 +7,9 @@ import org.joda.time.*;
import java.sql.*;
import java.util.*;
+import static io.trygvis.esper.testing.Util.toList;
import static io.trygvis.esper.testing.util.sql.ResultSetF.*;
+import static io.trygvis.esper.testing.util.sql.SqlOption.fromRs;
import static java.lang.System.*;
public class BuildDao {
@@ -59,7 +61,34 @@ public class BuildDao {
try (PreparedStatement s = c.prepareStatement("SELECT person FROM build_participant WHERE build=?")) {
int i = 1;
s.setString(i, build.toString());
- return Util.toList(s, getUuid);
+ return toList(s, getUuid);
+ }
+ }
+
+ public SqlOption<BuildDto> selectBuild(UUID uuid) throws SQLException {
+ try (PreparedStatement s = c.prepareStatement("SELECT " + BUILD + " FROM build WHERE uuid=?")) {
+ int i = 1;
+ s.setString(i, uuid.toString());
+ return fromRs(s.executeQuery()).map(build);
+ }
+ }
+
+ public List<BuildDto> selectBuildsByPerson(UUID person, PageRequest page) throws SQLException {
+ try (PreparedStatement s = c.prepareStatement("SELECT " + BUILD + " FROM build b, build_participant bp WHERE bp.person=? AND b.uuid = bp.build ORDER BY created_date LIMIT ? OFFSET ?")) {
+ int i = 1;
+ s.setString(i++, person.toString());
+ s.setInt(i++, page.count.orSome(10));
+ s.setInt(i, page.startIndex.orSome(0));
+ return toList(s, build);
+ }
+ }
+
+ public List<BuildDto> selectBuilds(PageRequest page) throws SQLException {
+ try (PreparedStatement s = c.prepareStatement("SELECT " + BUILD + " FROM build ORDER BY created_date LIMIT ? OFFSET ?")) {
+ int i = 1;
+ s.setInt(i++, page.count.orSome(10));
+ s.setInt(i, page.startIndex.orSome(0));
+ return toList(s, build);
}
}
}
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 91a00f8..824291c 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
@@ -15,6 +15,10 @@ public class PageRequest {
this.count = count;
}
+ 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),
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 eded64c..46964a0 100644
--- a/src/main/java/io/trygvis/esper/testing/web/JerseyApplication.java
+++ b/src/main/java/io/trygvis/esper/testing/web/JerseyApplication.java
@@ -1,11 +1,23 @@
package io.trygvis.esper.testing.web;
+import com.sun.jersey.api.core.*;
+import com.sun.jersey.core.spi.component.*;
+import com.sun.jersey.server.impl.inject.*;
+import com.sun.jersey.spi.inject.*;
import io.trygvis.esper.testing.*;
import io.trygvis.esper.testing.core.badge.*;
+import io.trygvis.esper.testing.util.sql.*;
+import io.trygvis.esper.testing.web.resource.*;
+import javax.ws.rs.*;
import javax.ws.rs.core.*;
+import javax.ws.rs.ext.*;
+import java.lang.reflect.*;
import java.util.*;
+import static fj.data.Option.*;
+import static io.trygvis.esper.testing.Util.parseInt;
+
public class JerseyApplication extends Application {
private final HashSet<Object> singletons;
@@ -16,12 +28,74 @@ public class JerseyApplication extends Application {
BadgeService badgeService = new BadgeService();
singletons = new HashSet<Object>(Arrays.asList(
- new CoreResource(da, badgeService),
- new JenkinsResource(da)
+ new CoreResource(da, badgeService),
+ new JenkinsResource(da)
));
}
+ public Set<Class<?>> getClasses() {
+ return new HashSet<>(Arrays.<Class<?>>asList(ResourceParamInjector.class));
+ }
+
public Set<Object> getSingletons() {
return singletons;
}
+
+ @Provider
+ public static class ResourceParamInjector implements InjectableProvider<MagicParam, Type> {
+
+ private final ResourceContext rc;
+
+ public ResourceParamInjector(@Context ResourceContext rc) {
+ this.rc = rc;
+ }
+
+ public ComponentScope getScope() {
+ return ComponentScope.PerRequest;
+ }
+
+ public Injectable getInjectable(final ComponentContext ic, final MagicParam a, Type type) {
+ if (PageRequest.class.equals(type)) {
+ return new AbstractHttpContextInjectable() {
+ public Object getValue(HttpContext hc) {
+ MultivaluedMap<String, String> queryParameters = hc.getRequest().getQueryParameters();
+
+ return new PageRequest(
+ fromNull(queryParameters.getFirst("startIndex")).bind(parseInt),
+ fromNull(queryParameters.getFirst("count")).bind(parseInt));
+ }
+ };
+ } else if (UUID.class.equals(type)) {
+
+ return new AbstractHttpContextInjectable() {
+ public Object getValue(HttpContext hc) {
+
+ if (a.query().length() > 0) {
+ return parse(hc.getRequest().getQueryParameters().getFirst(a.query()));
+ } else {
+ MultivaluedMap<String, String> pathParameters = hc.getUriInfo().getPathParameters();
+
+ for (Map.Entry<String, List<String>> entry : pathParameters.entrySet()) {
+ if ("uuid".equals(entry.getKey())) {
+ return parse(entry.getValue().get(0));
+ }
+ }
+ }
+
+ throw new RuntimeException("@MagicParam used with UUID argument with no {uuid} path variable.");
+ }
+
+ private UUID parse(String s) {
+ try {
+ return UUID.fromString(s);
+ } catch (IllegalArgumentException e) {
+ throw new WebApplicationException(400);
+ }
+ }
+ };
+ }
+
+ return null;
+ }
+ }
}
diff --git a/src/main/java/io/trygvis/esper/testing/web/MagicParam.java b/src/main/java/io/trygvis/esper/testing/web/MagicParam.java
new file mode 100644
index 0000000..1eb3bb0
--- /dev/null
+++ b/src/main/java/io/trygvis/esper/testing/web/MagicParam.java
@@ -0,0 +1,10 @@
+package io.trygvis.esper.testing.web;
+
+import java.lang.annotation.*;
+
+@Target({ElementType.PARAMETER})
+@Retention(RetentionPolicy.RUNTIME)
+@Documented
+public @interface MagicParam {
+ String query() default "";
+}
diff --git a/src/main/java/io/trygvis/esper/testing/web/AbstractResource.java b/src/main/java/io/trygvis/esper/testing/web/resource/AbstractResource.java
index c811a2f..cddec2c 100644
--- a/src/main/java/io/trygvis/esper/testing/web/AbstractResource.java
+++ b/src/main/java/io/trygvis/esper/testing/web/resource/AbstractResource.java
@@ -1,4 +1,4 @@
-package io.trygvis.esper.testing.web;
+package io.trygvis.esper.testing.web.resource;
import fj.data.*;
import io.trygvis.esper.testing.*;
diff --git a/src/main/java/io/trygvis/esper/testing/web/resource/BadgeJson.java b/src/main/java/io/trygvis/esper/testing/web/resource/BadgeJson.java
new file mode 100644
index 0000000..65f541a
--- /dev/null
+++ b/src/main/java/io/trygvis/esper/testing/web/resource/BadgeJson.java
@@ -0,0 +1,21 @@
+package io.trygvis.esper.testing.web.resource;
+
+public class BadgeJson {
+ public final String name;
+ public final int level;
+
+ /**
+ * Number of times this badge has been received.
+ */
+ public final int count;
+ public final int progress;
+ public final int goal;
+
+ public BadgeJson(String name, int level, int count, int progress, int goal) {
+ this.name = name;
+ this.level = level;
+ this.count = count;
+ this.progress = progress;
+ this.goal = goal;
+ }
+}
diff --git a/src/main/java/io/trygvis/esper/testing/web/CoreResource.java b/src/main/java/io/trygvis/esper/testing/web/resource/CoreResource.java
index f2fb12a..98b32bb 100644
--- a/src/main/java/io/trygvis/esper/testing/web/CoreResource.java
+++ b/src/main/java/io/trygvis/esper/testing/web/resource/CoreResource.java
@@ -1,10 +1,11 @@
-package io.trygvis.esper.testing.web;
+package io.trygvis.esper.testing.web.resource;
import fj.data.*;
import io.trygvis.esper.testing.*;
import io.trygvis.esper.testing.core.badge.*;
import io.trygvis.esper.testing.core.db.*;
import io.trygvis.esper.testing.util.sql.*;
+import io.trygvis.esper.testing.web.*;
import javax.servlet.http.*;
import javax.ws.rs.*;
@@ -14,7 +15,6 @@ import java.util.*;
import java.util.List;
import static io.trygvis.esper.testing.util.sql.PageRequest.*;
-import static io.trygvis.esper.testing.web.JenkinsResource.*;
@Path("/resource/core")
@Produces(MediaType.APPLICATION_JSON)
@@ -27,6 +27,10 @@ public class CoreResource extends AbstractResource {
this.badgeService = badgeService;
}
+ // -----------------------------------------------------------------------
+ // Person
+ // -----------------------------------------------------------------------
+
@GET
@Path("/person")
public List<PersonJson> getPersons(@Context final HttpServletRequest req) throws Exception {
@@ -43,23 +47,10 @@ public class CoreResource extends AbstractResource {
});
}
- /**
- * This is wrong, but Angular's $resource is a bit dumb.
- */
- @GET
- @Path("/person-count")
- public int getPersonCount() throws Exception {
- return da.inTransaction(new DatabaseAccess.DaosCallback<Integer>() {
- public Integer run(Daos daos) throws SQLException {
- return daos.personDao.selectPersonCount();
- }
- });
- }
-
@GET
@Path("/person/{uuid}")
public PersonJson getPerson(@PathParam("uuid") final String s) throws Exception {
- final UUID uuid = parseUuid(s);
+ final UUID uuid = JenkinsResource.parseUuid(s);
return get(new DatabaseAccess.DaosCallback<Option<PersonJson>>() {
public Option<PersonJson> run(Daos daos) throws SQLException {
@@ -96,37 +87,57 @@ public class CoreResource extends AbstractResource {
);
}
- public static class PersonJson {
- public final UUID uuid;
- public final String name;
- public final List<BadgeJson> badges;
- public final List<BadgeJson> badgesInProgress;
+ // -----------------------------------------------------------------------
+ // Build
+ // -----------------------------------------------------------------------
- public PersonJson(UUID uuid, String name, List<BadgeJson> badges, List<BadgeJson> badgesInProgress) {
- this.uuid = uuid;
- this.name = name;
- this.badges = badges;
- this.badgesInProgress = badgesInProgress;
- }
+ @GET
+ @Path("/build")
+ public List<BuildJson> getBuilds(@MagicParam final PageRequest page, @MagicParam(query = "person") final UUID person) throws Exception {
+ return da.inTransaction(new DatabaseAccess.DaosCallback<List<BuildJson>>() {
+ public List<BuildJson> run(Daos daos) throws SQLException {
+ List<BuildDto> buildDtos;
+
+ if(person != null) {
+ buildDtos = daos.buildDao.selectBuildsByPerson(person, page);
+ }
+ else {
+ buildDtos = daos.buildDao.selectBuilds(page);
+ }
+
+ List<BuildJson> list = new ArrayList<>();
+ for (BuildDto build : buildDtos) {
+ list.add(getBuildJson(daos, build));
+ }
+ return list;
+ }
+ });
}
- public static class BadgeJson {
- public final String name;
- public final int level;
-
- /**
- * Number of times this badge has been received.
- */
- public final int count;
- public final int progress;
- public final int goal;
-
- public BadgeJson(String name, int level, int count, int progress, int goal) {
- this.name = name;
- this.level = level;
- this.count = count;
- this.progress = progress;
- this.goal = goal;
+ @GET
+ @Path("/build/{uuid}")
+ public BuildJson getBuild(@MagicParam final UUID uuid) throws Exception {
+ return get(new DatabaseAccess.DaosCallback<Option<BuildJson>>() {
+ public Option<BuildJson> run(Daos daos) throws SQLException {
+ SqlOption<BuildDto> o = daos.buildDao.selectBuild(uuid);
+ if (o.isNone()) {
+ return Option.none();
+ }
+
+ return Option.some(getBuildJson(daos, o.get()));
+ }
+ });
+ }
+
+ private BuildJson getBuildJson(Daos daos, BuildDto build) {
+ return new BuildJson(build.uuid);
+ }
+
+ public static class BuildJson {
+ public final UUID uuid;
+
+ public BuildJson(UUID uuid) {
+ this.uuid = uuid;
}
}
}
diff --git a/src/main/java/io/trygvis/esper/testing/web/JenkinsResource.java b/src/main/java/io/trygvis/esper/testing/web/resource/JenkinsResource.java
index bd925b6..284d90e 100644
--- a/src/main/java/io/trygvis/esper/testing/web/JenkinsResource.java
+++ b/src/main/java/io/trygvis/esper/testing/web/resource/JenkinsResource.java
@@ -1,4 +1,4 @@
-package io.trygvis.esper.testing.web;
+package io.trygvis.esper.testing.web.resource;
import fj.data.*;
import io.trygvis.esper.testing.*;
diff --git a/src/main/java/io/trygvis/esper/testing/web/resource/PersonJson.java b/src/main/java/io/trygvis/esper/testing/web/resource/PersonJson.java
new file mode 100644
index 0000000..e8008e3
--- /dev/null
+++ b/src/main/java/io/trygvis/esper/testing/web/resource/PersonJson.java
@@ -0,0 +1,17 @@
+package io.trygvis.esper.testing.web.resource;
+
+import java.util.*;
+
+public class PersonJson {
+ public final UUID uuid;
+ public final String name;
+ public final List<BadgeJson> badges;
+ public final List<BadgeJson> badgesInProgress;
+
+ public PersonJson(UUID uuid, String name, List<BadgeJson> badges, List<BadgeJson> badgesInProgress) {
+ this.uuid = uuid;
+ this.name = name;
+ this.badges = badges;
+ this.badgesInProgress = badgesInProgress;
+ }
+}