From 7838a06e5521db39aee8b3b87785fcb5e32cd7ad Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Thu, 10 Jan 2013 07:32:02 +0100 Subject: o Creating a single (awarded) badge view. --- .../esper/testing/core/badge/BadgeService.java | 39 ++++++++++++++++++++-- .../esper/testing/core/badge/UnbreakableBadge.java | 6 ++++ .../trygvis/esper/testing/core/db/PersonDao.java | 8 ++--- .../esper/testing/web/resource/BadgeJson.java | 16 ++++++--- .../esper/testing/web/resource/CoreResource.java | 25 ++++++++++++-- src/main/webapp/apps/app.js | 3 +- src/main/webapp/apps/core/CoreResources.js | 2 +- src/main/webapp/apps/frontPageApp/badge.html | 30 +++++++++++++++++ src/main/webapp/apps/frontPageApp/frontPageApp.js | 8 +++++ 9 files changed, 121 insertions(+), 16 deletions(-) create mode 100755 src/main/webapp/apps/frontPageApp/badge.html (limited to 'src') diff --git a/src/main/java/io/trygvis/esper/testing/core/badge/BadgeService.java b/src/main/java/io/trygvis/esper/testing/core/badge/BadgeService.java index bf06fbb..d747d5c 100644 --- a/src/main/java/io/trygvis/esper/testing/core/badge/BadgeService.java +++ b/src/main/java/io/trygvis/esper/testing/core/badge/BadgeService.java @@ -15,6 +15,39 @@ public class BadgeService { this.objectMapper = objectMapper; } + public UnbreakableBadge unbreakable(PersonalBadgeDto dto) { + return getProgress(dto.state, UnbreakableBadge.class); + } + + // ----------------------------------------------------------------------- + // Badge + // ----------------------------------------------------------------------- + + public PersonalBadge badge(PersonalBadgeDto dto) { + switch (dto.type) { + case UNBREAKABLE: + return getProgress(dto.state, UnbreakableBadge.class); + } + + throw new RuntimeException("Unknown badge type: " + dto.type); + } + + public T badge(PersonalBadgeDto dto, Class klass) { + switch (dto.type) { + case UNBREAKABLE: + if(!klass.equals(UnbreakableBadgeProgress.class)) { + throw new RuntimeException("Badge is not of the expected type: UNBREAKABLE."); + } + return getProgress(dto.state, klass); + } + + throw new RuntimeException("Unknown badge type: " + dto.type); + } + + // ----------------------------------------------------------------------- + // Badge Progress + // ----------------------------------------------------------------------- + public BadgeProgress badgeProgress(PersonBadgeProgressDto dto) { switch (PersonalBadgeDto.BadgeType.valueOf(dto.badge)) { case UNBREAKABLE: @@ -36,9 +69,9 @@ public class BadgeService { throw new RuntimeException("Unknown badge type: " + dto.badge); } - public UnbreakableBadge unbreakable(PersonalBadgeDto dto) { - return getProgress(dto.state, UnbreakableBadge.class); - } + // ----------------------------------------------------------------------- + // + // ----------------------------------------------------------------------- public String serialize(Object badge) { try { diff --git a/src/main/java/io/trygvis/esper/testing/core/badge/UnbreakableBadge.java b/src/main/java/io/trygvis/esper/testing/core/badge/UnbreakableBadge.java index 6b1c78d..f2d69c4 100644 --- a/src/main/java/io/trygvis/esper/testing/core/badge/UnbreakableBadge.java +++ b/src/main/java/io/trygvis/esper/testing/core/badge/UnbreakableBadge.java @@ -12,6 +12,12 @@ class UnbreakableBadge extends PersonalBadge { public final List builds; + @SuppressWarnings("UnusedDeclaration") + private UnbreakableBadge() { + super(null, 0); + builds = null; + } + UnbreakableBadge(Uuid person, int level, List builds) { super(person, level); this.builds = builds; 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 4b22100..58af119 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 @@ -175,12 +175,10 @@ public class PersonDao { } } - public SqlOption selectBadge(UUID person, BadgeType type, int level) throws SQLException { - try (PreparedStatement s = c.prepareStatement("SELECT " + PERSON_BADGE + " FROM person_badge WHERE person=? AND name=? AND level=? LIMIT 1")) { + public SqlOption selectBadge(Uuid uuid) throws SQLException { + try (PreparedStatement s = c.prepareStatement("SELECT " + PERSON_BADGE + " FROM person_badge WHERE uuid=?")) { int i = 1; - s.setString(i++, person.toString()); - s.setString(i++, type.toString()); - s.setInt(i, level); + s.setString(i, uuid.toUuidString()); return fromRs(s.executeQuery()).map(personBadge); } } 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 index ded97ae..90d9c93 100644 --- a/src/main/java/io/trygvis/esper/testing/web/resource/BadgeJson.java +++ b/src/main/java/io/trygvis/esper/testing/web/resource/BadgeJson.java @@ -1,8 +1,12 @@ package io.trygvis.esper.testing.web.resource; +import io.trygvis.esper.testing.core.badge.*; import org.joda.time.*; +import java.util.*; + public class BadgeJson { + public final UUID uuid; public final DateTime createdDate; public final String name; public final int level; @@ -13,7 +17,8 @@ public class BadgeJson { /** * For completed badges. */ - public BadgeJson(DateTime createdDate, String name, int level) { + public BadgeJson(UUID uuid, DateTime createdDate, String name, int level) { + this.uuid = uuid; this.createdDate = createdDate; this.name = name; this.level = level; @@ -24,8 +29,9 @@ public class BadgeJson { /** * For badges in progress. */ - public BadgeJson(DateTime createdDate, String name, int level, int progress, int goal) { - this.createdDate = createdDate; + public BadgeJson(String name, int level, int progress, int goal) { + this.uuid = null; + this.createdDate = null; this.name = name; this.level = level; this.progress = progress; @@ -35,10 +41,12 @@ public class BadgeJson { class BadgeDetailJson { public final BadgeJson badge; + public final PersonalBadge personalBadge; public final PersonJson person; - BadgeDetailJson(BadgeJson badge, PersonJson person) { + BadgeDetailJson(BadgeJson badge, PersonalBadge personalBadge, PersonJson person) { this.badge = badge; + this.personalBadge = personalBadge; this.person = person; } } 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 bd61855..83f5144 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 @@ -116,6 +116,9 @@ public class CoreResource extends AbstractResource { // Badge // ----------------------------------------------------------------------- + /** + * This shouldn't return a detailed list by default. + */ @GET @Path("/badge") public List getBadges(@MagicParam final PageRequest page, @MagicParam(query = "person") final Uuid person) throws Exception { @@ -132,6 +135,22 @@ public class CoreResource extends AbstractResource { }); } + @GET + @Path("/badge/{uuid}") + public BadgeDetailJson getBadges(@MagicParam final Uuid uuid) throws Exception { + return sql(new CoreDaosCallback>() { + protected SqlOption run() throws SQLException { + return daos.personDao.selectBadge(uuid).map(getBadgeDetailJson); + } + }); + } + + SqlF badge = new SqlF() { + public PersonalBadge apply(PersonalBadgeDto dto) throws SQLException { + return badgeService.badge(dto); + } + }; + abstract class CoreDaosCallback implements DatabaseAccess.DaosCallback { protected Daos daos; @@ -201,17 +220,19 @@ public class CoreResource extends AbstractResource { protected SqlF getBadgeJson = new SqlF() { public BadgeJson apply(PersonalBadgeDto badge) throws SQLException { - return new BadgeJson(badge.createdDate, badge.type.name(), badge.level); + return new BadgeJson(badge.uuid, badge.createdDate, badge.type.name(), badge.level); } }; private BadgeJson getBadge(BadgeProgress progress) { - return new BadgeJson(null, progress.type.name(), progress.progressingAgainstLevel(), progress.progression(), progress.goal()); + return new BadgeJson(progress.type.name(), progress.progressingAgainstLevel(), progress.progression(), progress.goal()); } protected final SqlF getBadgeDetailJson = new SqlF() { public BadgeDetailJson apply(PersonalBadgeDto badgeDto) throws SQLException { + return new BadgeDetailJson(getBadgeJson.apply(badgeDto), + badge.apply(badgeDto), daos.personDao.selectPerson(badgeDto.person).map(getPersonJson).get()); } }; diff --git a/src/main/webapp/apps/app.js b/src/main/webapp/apps/app.js index 8c683b6..068f454 100755 --- a/src/main/webapp/apps/app.js +++ b/src/main/webapp/apps/app.js @@ -55,7 +55,8 @@ directives.directive('badge', function () { ' ' + '' + ' awarded to ' + - '{{badgeDetail.person.name}}' + '{{badgeDetail.person.name}}. ' + + 'More' } }); diff --git a/src/main/webapp/apps/core/CoreResources.js b/src/main/webapp/apps/core/CoreResources.js index aec6a15..b258c85 100644 --- a/src/main/webapp/apps/core/CoreResources.js +++ b/src/main/webapp/apps/core/CoreResources.js @@ -19,7 +19,7 @@ function BuildParticipant($resource) { angular.module('buildParticipant', ['ngResource']).factory('BuildParticipant', BuildParticipant); function Badge($resource) { - return $resource('/resource/core/badge'); + return $resource('/resource/core/badge/:uuid', {uuid: '@uuid'}); } angular.module('badge', ['ngResource']).factory('Badge', Badge); diff --git a/src/main/webapp/apps/frontPageApp/badge.html b/src/main/webapp/apps/frontPageApp/badge.html new file mode 100755 index 0000000..92fc7ae --- /dev/null +++ b/src/main/webapp/apps/frontPageApp/badge.html @@ -0,0 +1,30 @@ +
+ + + + + +
+
+ The badge was awarded to at + {{badge.badge.createdDate | date:'medium'}}. +
+
+ +
+
+

Details

+

+ The badge was awarded for having {{badge.personalBadge.builds.length}} successful builds in a row: +

+ +
+
+ +
diff --git a/src/main/webapp/apps/frontPageApp/frontPageApp.js b/src/main/webapp/apps/frontPageApp/frontPageApp.js index a67e2dc..6f80d0f 100755 --- a/src/main/webapp/apps/frontPageApp/frontPageApp.js +++ b/src/main/webapp/apps/frontPageApp/frontPageApp.js @@ -4,6 +4,7 @@ var frontPageApp = angular.module('frontPageApp', ['ngGrid', 'person', 'badge', $routeProvider. when('/', {controller: FrontPageCtrl, templateUrl: '/apps/frontPageApp/frontPage.html?noCache=' + noCache}). when('/badge/', {controller: BadgeListCtrl, templateUrl: '/apps/frontPageApp/badgeList.html?noCache=' + noCache}). + when('/badge/:badgeUuid', {controller: BadgeCtrl, templateUrl: '/apps/frontPageApp/badge.html?noCache=' + noCache}). when('/person/', {controller: PersonListCtrl, templateUrl: '/apps/frontPageApp/personList.html?noCache=' + noCache}). when('/person/:personUuid', {controller: PersonCtrl, templateUrl: '/apps/frontPageApp/person.html?noCache=' + noCache}). when('/build/', {controller: BuildListCtrl, templateUrl: '/apps/frontPageApp/buildList.html?noCache=' + noCache}); @@ -56,6 +57,13 @@ function BadgeListCtrl($scope, Badge, PagingTableService) { $scope.badgeGroups = []; } +function BadgeCtrl($scope, $routeParams, Badge) { + var badgeUuid = $routeParams.badgeUuid; + Badge.get({uuid: badgeUuid}, function (badge) { + $scope.badge = badge; + }); +} + function PersonListCtrl($scope, Person, PagingTableService) { var groupSize = 4; var personsWatcher = function () { -- cgit v1.2.3