From ee86231bc83d50505fca356f808bc7d13a2c9502 Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Thu, 20 Dec 2012 10:53:48 +0100 Subject: o Adding breadcrumbs for easier navigation. --- .../trygvis/esper/testing/jenkins/JenkinsDao.java | 4 +- .../trygvis/esper/testing/web/JenkinsResource.java | 74 +++++++++++++++++----- src/main/webapp/apps/jenkinsApp/jenkinsApp.js | 16 ++++- src/main/webapp/apps/jenkinsApp/server-list.html | 5 ++ src/main/webapp/apps/jenkinsApp/server.html | 11 +++- 5 files changed, 89 insertions(+), 21 deletions(-) (limited to 'src') 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 getServers() throws Exception { - System.out.println("JenkinsResource.getServers"); - return da.inTransaction(new DatabaseAccess.DaosCallback>() { + public List getServers() throws Exception { + return da.inTransaction(new DatabaseAccess.DaosCallback>() { @Override - public List run(Daos daos) throws SQLException { - return daos.jenkinsDao.selectServers(false); + public List run(Daos daos) throws SQLException { + List 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 server = da.inTransaction(new DatabaseAccess.DaosCallback>() { - public Option 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>() { + public Option run(final Daos daos) throws SQLException { + Option 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 get(DatabaseAccess.DaosCallback> callback) throws SQLException { + Option 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; + } +} diff --git a/src/main/webapp/apps/jenkinsApp/jenkinsApp.js b/src/main/webapp/apps/jenkinsApp/jenkinsApp.js index 5370496..5477039 100644 --- a/src/main/webapp/apps/jenkinsApp/jenkinsApp.js +++ b/src/main/webapp/apps/jenkinsApp/jenkinsApp.js @@ -11,19 +11,31 @@ var jenkinsApp = angular.module('jenkinsApp', ['jenkinsServerService']).config(f // $locationProvider.html5Mode(true); }); -function ServerListCtrl($scope, $route, $routeParams, $location, JenkinsServerService) { +function ServerListCtrl($scope, $location, JenkinsServerService) { JenkinsServerService.query(function (servers) { $scope.servers = servers; }); + $scope.showServers = function (uuid) { + $location.path('/'); + }; + $scope.showServer = function (uuid) { $location.path('/server/' + uuid); }; } -function ServerCtrl($scope, $routeParams, JenkinsServerService) { +function ServerCtrl($scope, $location, $routeParams, JenkinsServerService) { window.x = $routeParams; JenkinsServerService.get({uuid: $routeParams.uuid}, function (server) { $scope.server = server; }); + + $scope.showServers = function (uuid) { + $location.path('/'); + }; + + $scope.showServer = function (uuid) { + $location.path('/server/' + uuid); + }; } diff --git a/src/main/webapp/apps/jenkinsApp/server-list.html b/src/main/webapp/apps/jenkinsApp/server-list.html index 0ca65d6..f335c1f 100644 --- a/src/main/webapp/apps/jenkinsApp/server-list.html +++ b/src/main/webapp/apps/jenkinsApp/server-list.html @@ -1,6 +1,11 @@ + + + diff --git a/src/main/webapp/apps/jenkinsApp/server.html b/src/main/webapp/apps/jenkinsApp/server.html index e15f43e..9213c73 100644 --- a/src/main/webapp/apps/jenkinsApp/server.html +++ b/src/main/webapp/apps/jenkinsApp/server.html @@ -2,6 +2,11 @@

Jenkins Server

+ +
@@ -14,7 +19,11 @@ - + + + + +
Visit{{server.visit}}{{server.url}}
Job Count{{server.jobCount}}
-- cgit v1.2.3