diff options
author | Trygve Laugstøl <trygvis@inamo.no> | 2012-12-29 18:11:08 +0100 |
---|---|---|
committer | Trygve Laugstøl <trygvis@inamo.no> | 2012-12-29 18:19:19 +0100 |
commit | 84fb49088a14cff8b453e0e32f6ad7007eb2184a (patch) | |
tree | b306c923570b26501da8df1f6e41ec2404a19faa /src/main/java/io/trygvis/esper/testing/web/resource/JenkinsResource.java | |
parent | 1b83af30a4e935f2037a6e9153cb438c29adfbfc (diff) | |
download | esper-testing-84fb49088a14cff8b453e0e32f6ad7007eb2184a.tar.gz esper-testing-84fb49088a14cff8b453e0e32f6ad7007eb2184a.tar.bz2 esper-testing-84fb49088a14cff8b453e0e32f6ad7007eb2184a.tar.xz esper-testing-84fb49088a14cff8b453e0e32f6ad7007eb2184a.zip |
o Adding jobs view for jenkins app.
Diffstat (limited to 'src/main/java/io/trygvis/esper/testing/web/resource/JenkinsResource.java')
-rw-r--r-- | src/main/java/io/trygvis/esper/testing/web/resource/JenkinsResource.java | 130 |
1 files changed, 100 insertions, 30 deletions
diff --git a/src/main/java/io/trygvis/esper/testing/web/resource/JenkinsResource.java b/src/main/java/io/trygvis/esper/testing/web/resource/JenkinsResource.java index 33b3f88..292be71 100644 --- a/src/main/java/io/trygvis/esper/testing/web/resource/JenkinsResource.java +++ b/src/main/java/io/trygvis/esper/testing/web/resource/JenkinsResource.java @@ -1,6 +1,5 @@ package io.trygvis.esper.testing.web.resource; -import fj.data.*; import io.trygvis.esper.testing.*; import io.trygvis.esper.testing.jenkins.*; import io.trygvis.esper.testing.util.sql.*; @@ -14,8 +13,6 @@ import java.sql.*; import java.util.*; import java.util.List; -import static fj.data.Option.*; - @Path("/resource/jenkins") public class JenkinsResource extends AbstractResource { @@ -27,11 +24,11 @@ public class JenkinsResource extends AbstractResource { @Path("/server") @Produces(MediaType.APPLICATION_JSON) public List<JenkinsServerJson> getServers() throws Exception { - return da.inTransaction(new DatabaseAccess.DaosCallback<List<JenkinsServerJson>>() { - public List<JenkinsServerJson> run(Daos daos) throws SQLException { + return da.inTransaction(new JenkinsDaosCallback<List<JenkinsServerJson>>() { + protected List<JenkinsServerJson> run() throws SQLException { List<JenkinsServerJson> list = new ArrayList<>(); for (JenkinsServerDto server : daos.jenkinsDao.selectServers(false)) { - list.add(getJenkinsServerJson(daos, server)); + list.add(getJenkinsServerJson.apply(server)); } return list; } @@ -44,15 +41,9 @@ public class JenkinsResource extends AbstractResource { 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(); - } - - return some(getJenkinsServerJson(daos, o.some())); + return sql(new JenkinsDaosCallback<SqlOption<JenkinsServerJson>>() { + protected SqlOption<JenkinsServerJson> run() throws SQLException { + return daos.jenkinsDao.selectServer(uuid).map(getJenkinsServerJson); } }); } @@ -61,30 +52,41 @@ public class JenkinsResource extends AbstractResource { @Path("/job") @Produces(MediaType.APPLICATION_JSON) public List<JenkinsJobJson> getJobs(@MagicParam(query = "server") final UUID server, @MagicParam final PageRequest page) throws Exception { - return da.inTransaction(new DatabaseAccess.DaosCallback<List<JenkinsJobJson>>() { - public List<JenkinsJobJson> run(final Daos daos) throws SQLException { + return da.inTransaction(new JenkinsDaosCallback<List<JenkinsJobJson>>() { + protected List<JenkinsJobJson> run() throws SQLException { List<JenkinsJobJson> jobs = new ArrayList<>(); for (JenkinsJobDto job : daos.jenkinsDao.selectJobsByServer(server, page)) { - jobs.add(getJenkinsJobJson(job)); + jobs.add(getJenkinsJobJson.apply(job)); } return jobs; } }); } - private JenkinsServerJson getJenkinsServerJson(Daos daos, JenkinsServerDto server) throws SQLException { - int count = daos.jenkinsDao.selectJobCountForServer(server.uuid); - - List<JenkinsJobJson> jobs = new ArrayList<>(); - for (JenkinsJobDto jobDto : daos.jenkinsDao.selectJobsByServer(server.uuid, PageRequest.FIRST_PAGE)) { - jobs.add(getJenkinsJobJson(jobDto)); - } - - return new JenkinsServerJson(server.uuid, server.createdDate, server.url, server.enabled, count, jobs); + @GET + @Path("/job/{uuid}") + @Produces(MediaType.APPLICATION_JSON) + public JenkinsJobJson getJob(@MagicParam final UUID uuid) throws Exception { + return sql(new JenkinsDaosCallback<SqlOption<JenkinsJobJson>>() { + protected SqlOption<JenkinsJobJson> run() throws SQLException { + return daos.jenkinsDao.selectJob(uuid).map(getJenkinsJobJsonDetail); + } + }); } - private JenkinsJobJson getJenkinsJobJson(JenkinsJobDto job) { - return new JenkinsJobJson(job.uuid, job.createdDate, job.displayName.toNull()); + @GET + @Path("/build") + @Produces(MediaType.APPLICATION_JSON) + public List<JenkinsBuildJson> getBuilds(@MagicParam(query = "job") final UUID job, @MagicParam final PageRequest page) throws Exception { + return da.inTransaction(new JenkinsDaosCallback<List<JenkinsBuildJson>>() { + protected List<JenkinsBuildJson> run() throws SQLException { + List<JenkinsBuildJson> builds = new ArrayList<>(); + for (JenkinsBuildDto dto : daos.jenkinsDao.selectBuildByJob(job, page)) { + builds.add(getJenkinsBuildJson.apply(dto)); + } + return builds; + } + }); } public static UUID parseUuid(String s) { @@ -94,6 +96,49 @@ public class JenkinsResource extends AbstractResource { throw new WebApplicationException(Response.Status.BAD_REQUEST); } } + + abstract class JenkinsDaosCallback<T> implements DatabaseAccess.DaosCallback<T> { + protected Daos daos; + + protected abstract T run() throws SQLException; + + public T run(Daos daos) throws SQLException { + this.daos = daos; + return run(); + } + + protected SqlF<JenkinsServerDto, JenkinsServerJson> getJenkinsServerJson = new SqlF<JenkinsServerDto, JenkinsServerJson>() { + public JenkinsServerJson apply(JenkinsServerDto server) throws SQLException { + int count = daos.jenkinsDao.selectJobCountForServer(server.uuid); + + List<JenkinsJobJson> jobs = new ArrayList<>(); + for (JenkinsJobDto jobDto : daos.jenkinsDao.selectJobsByServer(server.uuid, PageRequest.FIRST_PAGE)) { + jobs.add(getJenkinsJobJson.apply(jobDto)); + } + + return new JenkinsServerJson(server.uuid, server.createdDate, server.url, server.enabled, count, jobs); + } + }; + + protected SqlF<JenkinsJobDto,JenkinsJobJson> getJenkinsJobJson = new SqlF<JenkinsJobDto, JenkinsJobJson>() { + public JenkinsJobJson apply(JenkinsJobDto job) throws SQLException { + return new JenkinsJobJson(job.uuid, job.createdDate, job.server, job.displayName.toNull()); + } + }; + + protected SqlF<JenkinsJobDto,JenkinsJobJson> getJenkinsJobJsonDetail = new SqlF<JenkinsJobDto, JenkinsJobJson>() { + public JenkinsJobJson apply(JenkinsJobDto job) throws SQLException { + int buildCount = daos.jenkinsDao.selectBuildCountByJob(job.uuid); + return new JenkinsJobJson(job.uuid, job.createdDate, job.server, job.displayName.toNull(), buildCount); + } + }; + + protected SqlF<JenkinsBuildDto,JenkinsBuildJson> getJenkinsBuildJson = new SqlF<JenkinsBuildDto, JenkinsBuildJson>() { + public JenkinsBuildJson apply(JenkinsBuildDto dto) throws SQLException { + return new JenkinsBuildJson(dto.uuid, dto.createdDate, dto.result); + } + }; + } } class JenkinsServerJson { @@ -117,11 +162,36 @@ class JenkinsServerJson { class JenkinsJobJson { public final UUID uuid; public final DateTime createdDate; + public final UUID server; public final String displayName; - JenkinsJobJson(UUID uuid, DateTime createdDate, String displayName) { + public final Integer buildCount; + + JenkinsJobJson(UUID uuid, DateTime createdDate, UUID server, String displayName) { this.uuid = uuid; this.createdDate = createdDate; + this.server = server; this.displayName = displayName; + this.buildCount = null; + } + + JenkinsJobJson(UUID uuid, DateTime createdDate, UUID server, String displayName, int buildCount) { + this.uuid = uuid; + this.createdDate = createdDate; + this.server = server; + this.displayName = displayName; + this.buildCount = buildCount; + } +} + +class JenkinsBuildJson { + public final UUID uuid; + public final DateTime createdDate; + public final String result; + + JenkinsBuildJson(UUID uuid, DateTime createdDate, String result) { + this.uuid = uuid; + this.createdDate = createdDate; + this.result = result; } } |