diff options
Diffstat (limited to 'src/main/java/io')
-rw-r--r-- | src/main/java/io/trygvis/esper/testing/jenkins/JenkinsDao.java | 4 | ||||
-rw-r--r-- | src/main/java/io/trygvis/esper/testing/web/JenkinsResource.java | 74 |
2 files changed, 60 insertions, 18 deletions
diff --git a/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsDao.java b/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsDao.java index ab4b515..bf1954e 100644 --- a/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsDao.java +++ b/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsDao.java @@ -112,11 +112,11 @@ public class JenkinsDao { } } - public Integer selectJobCountForServer(UUID uuid) throws SQLException { + public int selectJobCountForServer(UUID uuid) throws SQLException { try (PreparedStatement s = c.prepareStatement("SELECT count(1) FROM jenkins_job WHERE server=?")) { s.setString(1, uuid.toString()); ResultSet rs = s.executeQuery(); - + rs.next(); return rs.getInt(1); } } diff --git a/src/main/java/io/trygvis/esper/testing/web/JenkinsResource.java b/src/main/java/io/trygvis/esper/testing/web/JenkinsResource.java index a1e2f51..24ed367 100644 --- a/src/main/java/io/trygvis/esper/testing/web/JenkinsResource.java +++ b/src/main/java/io/trygvis/esper/testing/web/JenkinsResource.java @@ -3,13 +3,17 @@ package io.trygvis.esper.testing.web; import fj.data.*; import io.trygvis.esper.testing.*; import io.trygvis.esper.testing.jenkins.*; +import org.joda.time.*; import javax.ws.rs.*; import javax.ws.rs.core.*; +import java.net.*; import java.sql.*; import java.util.*; import java.util.List; +import static fj.data.Option.*; + @Path("/") public class JenkinsResource { @@ -22,12 +26,15 @@ public class JenkinsResource { @GET @Path("/resource/jenkins/server") @Produces(MediaType.APPLICATION_JSON) - public List<JenkinsServerDto> getServers() throws Exception { - System.out.println("JenkinsResource.getServers"); - return da.inTransaction(new DatabaseAccess.DaosCallback<List<JenkinsServerDto>>() { + public List<JenkinsServerJson> getServers() throws Exception { + return da.inTransaction(new DatabaseAccess.DaosCallback<List<JenkinsServerJson>>() { @Override - public List<JenkinsServerDto> run(Daos daos) throws SQLException { - return daos.jenkinsDao.selectServers(false); + public List<JenkinsServerJson> run(Daos daos) throws SQLException { + List<JenkinsServerJson> list = new ArrayList<>(); + for (JenkinsServerDto server : daos.jenkinsDao.selectServers(false)) { + list.add(getJenkinsServerJson(daos, server)); + } + return list; } }); } @@ -35,23 +42,58 @@ public class JenkinsResource { @GET @Path("/resource/jenkins/server/{uuid}") @Produces(MediaType.APPLICATION_JSON) - public JenkinsServerDto getServer(@PathParam("uuid") String s) throws Exception { - try { - final UUID uuid = UUID.fromString(s); - System.out.println("JenkinsResource.getServers"); - Option<JenkinsServerDto> server = da.inTransaction(new DatabaseAccess.DaosCallback<Option<JenkinsServerDto>>() { - public Option<JenkinsServerDto> run(Daos daos) throws SQLException { - return daos.jenkinsDao.selectServer(uuid); + public JenkinsServerJson getServer(@PathParam("uuid") String s) throws Exception { + final UUID uuid = parseUuid(s); + + return get(new DatabaseAccess.DaosCallback<Option<JenkinsServerJson>>() { + public Option<JenkinsServerJson> run(final Daos daos) throws SQLException { + Option<JenkinsServerDto> o = daos.jenkinsDao.selectServer(uuid); + + if (o.isNone()) { + return Option.none(); } - }); - if(server.isNone()) { - throw new WebApplicationException(Response.Status.NOT_FOUND); + return some(getJenkinsServerJson(daos, o.some())); } + }); + } + + private JenkinsServerJson getJenkinsServerJson(Daos daos, JenkinsServerDto server) throws SQLException { + int count = daos.jenkinsDao.selectJobCountForServer(server.uuid); + return new JenkinsServerJson(server.uuid, server.created_date, server.url, server.enabled, count); + } + + private <T> T get(DatabaseAccess.DaosCallback<Option<T>> callback) throws SQLException { + Option<T> server = da.inTransaction(callback); - return server.some(); + if(server.isNone()) { + throw new WebApplicationException(Response.Status.NOT_FOUND); + } + + return server.some(); + } + + private UUID parseUuid(String s) { + try { + return UUID.fromString(s); } catch (IllegalArgumentException e) { throw new WebApplicationException(Response.Status.BAD_REQUEST); } } } + +class JenkinsServerJson { + public final UUID uuid; + public final DateTime createdDate; + public final URI url; + public final boolean enabled; + public final int jobCount; + + JenkinsServerJson(UUID uuid, DateTime createdDate, URI url, boolean enabled, int jobCount) { + this.uuid = uuid; + this.createdDate = createdDate; + this.url = url; + this.enabled = enabled; + this.jobCount = jobCount; + } +} |