diff options
author | Trygve Laugstøl <trygvis@inamo.no> | 2013-01-10 07:32:02 +0100 |
---|---|---|
committer | Trygve Laugstøl <trygvis@inamo.no> | 2013-01-10 07:32:02 +0100 |
commit | 7838a06e5521db39aee8b3b87785fcb5e32cd7ad (patch) | |
tree | f10592e5f062d17522dcf08c11faa6ad166254e1 /src/main/java | |
parent | bc54871dac7ef10102edecf91a747d6595a4640f (diff) | |
download | esper-testing-7838a06e5521db39aee8b3b87785fcb5e32cd7ad.tar.gz esper-testing-7838a06e5521db39aee8b3b87785fcb5e32cd7ad.tar.bz2 esper-testing-7838a06e5521db39aee8b3b87785fcb5e32cd7ad.tar.xz esper-testing-7838a06e5521db39aee8b3b87785fcb5e32cd7ad.zip |
o Creating a single (awarded) badge view.
Diffstat (limited to 'src/main/java')
5 files changed, 80 insertions, 14 deletions
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 extends PersonalBadge> T badge(PersonalBadgeDto dto, Class<T> 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<UUID> builds; + @SuppressWarnings("UnusedDeclaration") + private UnbreakableBadge() { + super(null, 0); + builds = null; + } + UnbreakableBadge(Uuid person, int level, List<UUID> 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<PersonalBadgeDto> 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<PersonalBadgeDto> 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<BadgeDetailJson> 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<SqlOption<BadgeDetailJson>>() { + protected SqlOption<BadgeDetailJson> run() throws SQLException { + return daos.personDao.selectBadge(uuid).map(getBadgeDetailJson); + } + }); + } + + SqlF<PersonalBadgeDto, PersonalBadge> badge = new SqlF<PersonalBadgeDto, PersonalBadge>() { + public PersonalBadge apply(PersonalBadgeDto dto) throws SQLException { + return badgeService.badge(dto); + } + }; + abstract class CoreDaosCallback<T> implements DatabaseAccess.DaosCallback<T> { protected Daos daos; @@ -201,17 +220,19 @@ public class CoreResource extends AbstractResource { protected SqlF<PersonalBadgeDto, BadgeJson> getBadgeJson = new SqlF<PersonalBadgeDto, BadgeJson>() { 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<PersonalBadgeDto, BadgeDetailJson> getBadgeDetailJson = new SqlF<PersonalBadgeDto, BadgeDetailJson>() { public BadgeDetailJson apply(PersonalBadgeDto badgeDto) throws SQLException { + return new BadgeDetailJson(getBadgeJson.apply(badgeDto), + badge.apply(badgeDto), daos.personDao.selectPerson(badgeDto.person).map(getPersonJson).get()); } }; |