aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/io/trygvis
diff options
context:
space:
mode:
authorTrygve Laugstøl <trygvis@inamo.no>2013-01-05 22:39:18 +0100
committerTrygve Laugstøl <trygvis@inamo.no>2013-01-05 22:39:18 +0100
commit12606726c11efbbf7213b05284c94e28a1ae4b8e (patch)
tree415a400218f3bbaac4f13a43461446f64f74028c /src/main/java/io/trygvis
parentabf160e56be50781b0f78f3f938513ab6102809b (diff)
downloadesper-testing-12606726c11efbbf7213b05284c94e28a1ae4b8e.tar.gz
esper-testing-12606726c11efbbf7213b05284c94e28a1ae4b8e.tar.bz2
esper-testing-12606726c11efbbf7213b05284c94e28a1ae4b8e.tar.xz
esper-testing-12606726c11efbbf7213b05284c94e28a1ae4b8e.zip
o Including state in both badge and badge progress.
o Removing count from badge, inserting one badge per badge received.
Diffstat (limited to 'src/main/java/io/trygvis')
-rwxr-xr-xsrc/main/java/io/trygvis/esper/testing/Config.java28
-rw-r--r--src/main/java/io/trygvis/esper/testing/core/badge/BadgeProgress.java2
-rw-r--r--src/main/java/io/trygvis/esper/testing/core/badge/BadgeService.java45
-rw-r--r--src/main/java/io/trygvis/esper/testing/core/badge/PersonalBadge.java13
-rw-r--r--src/main/java/io/trygvis/esper/testing/core/badge/UnbreakableBadge.java13
-rw-r--r--src/main/java/io/trygvis/esper/testing/core/badge/UnbreakableBadgeProgress.java51
-rwxr-xr-xsrc/main/java/io/trygvis/esper/testing/core/badge/UnbreakablePoller.java39
-rwxr-xr-xsrc/main/java/io/trygvis/esper/testing/core/db/BuildDao.java4
-rwxr-xr-xsrc/main/java/io/trygvis/esper/testing/core/db/PersonDao.java44
-rw-r--r--src/main/java/io/trygvis/esper/testing/core/db/PersonalBadgeDto.java (renamed from src/main/java/io/trygvis/esper/testing/core/db/PersonBadgeDto.java)8
-rwxr-xr-xsrc/main/java/io/trygvis/esper/testing/util/sql/ResultSetF.java10
-rwxr-xr-xsrc/main/java/io/trygvis/esper/testing/web/JerseyApplication.java21
-rwxr-xr-xsrc/main/java/io/trygvis/esper/testing/web/MyObjectMapper.java26
-rw-r--r--src/main/java/io/trygvis/esper/testing/web/resource/BadgeJson.java25
-rwxr-xr-xsrc/main/java/io/trygvis/esper/testing/web/resource/CoreResource.java40
15 files changed, 216 insertions, 153 deletions
diff --git a/src/main/java/io/trygvis/esper/testing/Config.java b/src/main/java/io/trygvis/esper/testing/Config.java
index e184d46..7da14fa 100755
--- a/src/main/java/io/trygvis/esper/testing/Config.java
+++ b/src/main/java/io/trygvis/esper/testing/Config.java
@@ -3,6 +3,9 @@ package io.trygvis.esper.testing;
import com.jolbox.bonecp.*;
import fj.data.*;
import org.apache.abdera.*;
+import org.codehaus.jackson.*;
+import org.codehaus.jackson.map.*;
+import org.codehaus.jackson.map.module.*;
import org.slf4j.*;
import org.slf4j.bridge.*;
@@ -104,8 +107,8 @@ public class Config {
}
LoggerFactory.getILoggerFactory();
-// LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
-// StatusPrinter.print(lc);
+// ch.qos.logback.classic.LoggerContext lc = (ch.qos.logback.classic.LoggerContext) LoggerFactory.getILoggerFactory();
+// ch.qos.logback.core.util.StatusPrinter.print(lc);
SLF4JBridgeHandler.removeHandlersForRootLogger();
SLF4JBridgeHandler.install();
@@ -130,6 +133,15 @@ public class Config {
return new Abdera();
}
+ public ObjectMapper createObjectMapper() {
+ ObjectMapper objectMapper = new ObjectMapper();
+ SimpleModule module = new SimpleModule("wat", Version.unknownVersion());
+ module.addDeserializer(Uuid.class, new UuidDeserializer());
+ module.addSerializer(Uuid.class, new UuidSerializer());
+ objectMapper.registerModule(module);
+ return objectMapper;
+ }
+
public void addShutdownHook(final Thread t, final AtomicBoolean shouldRun) {
Runtime.getRuntime().addShutdownHook(new Thread() {
{
@@ -149,4 +161,16 @@ public class Config {
private static Option<String> getProperty(Properties properties, String key) {
return fromNull(trimToNull(properties.getProperty(key)));
}
+
+ private static class UuidDeserializer extends JsonDeserializer<Uuid> {
+ public Uuid deserialize(JsonParser jp, DeserializationContext context) throws IOException {
+ return Uuid.fromString(jp.getText());
+ }
+ }
+
+ private static class UuidSerializer extends JsonSerializer<Uuid> {
+ public void serialize(Uuid value, JsonGenerator generator, SerializerProvider provider) throws IOException {
+ generator.writeString(value.toStringBase64());
+ }
+ }
}
diff --git a/src/main/java/io/trygvis/esper/testing/core/badge/BadgeProgress.java b/src/main/java/io/trygvis/esper/testing/core/badge/BadgeProgress.java
index eb8d4bd..c2fda15 100644
--- a/src/main/java/io/trygvis/esper/testing/core/badge/BadgeProgress.java
+++ b/src/main/java/io/trygvis/esper/testing/core/badge/BadgeProgress.java
@@ -1,6 +1,6 @@
package io.trygvis.esper.testing.core.badge;
-import io.trygvis.esper.testing.core.db.PersonBadgeDto.*;
+import io.trygvis.esper.testing.core.db.PersonalBadgeDto.*;
public abstract class BadgeProgress {
public final BadgeType type;
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 ea0c0dc..bf06fbb 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
@@ -9,20 +9,38 @@ import java.io.*;
public class BadgeService {
private final Logger logger = LoggerFactory.getLogger(getClass());
- private static final ObjectMapper objectMapper = new ObjectMapper();
+ private final ObjectMapper objectMapper;
- public UnbreakableBadgeProgress unbreakable(PersonBadgeProgressDto dto) {
- String state = dto.state;
+ public BadgeService(ObjectMapper objectMapper) {
+ this.objectMapper = objectMapper;
+ }
- try {
- return objectMapper.readValue(state, UnbreakableBadgeProgress.class);
- } catch (IOException e) {
- logger.error("Could not de-serialize badge state: {}", state);
- throw new RuntimeException(e);
+ public BadgeProgress badgeProgress(PersonBadgeProgressDto dto) {
+ switch (PersonalBadgeDto.BadgeType.valueOf(dto.badge)) {
+ case UNBREAKABLE:
+ return getProgress(dto.state, UnbreakableBadgeProgress.class);
}
+
+ throw new RuntimeException("Unknown badge type: " + dto.badge);
+ }
+
+ public <T extends BadgeProgress> T badgeProgress(PersonBadgeProgressDto dto, Class<T> klass) {
+ switch (PersonalBadgeDto.BadgeType.valueOf(dto.badge)) {
+ 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.badge);
}
- public String serialize(UnbreakableBadgeProgress badge) {
+ public UnbreakableBadge unbreakable(PersonalBadgeDto dto) {
+ return getProgress(dto.state, UnbreakableBadge.class);
+ }
+
+ public String serialize(Object badge) {
try {
CharArrayWriter writer = new CharArrayWriter();
objectMapper.writeValue(writer, badge);
@@ -32,4 +50,13 @@ public class BadgeService {
throw new RuntimeException(e);
}
}
+
+ private <T> T getProgress(String state, Class<T> klass) {
+ try {
+ return objectMapper.readValue(state, klass);
+ } catch (IOException e) {
+ logger.error("Could not de-serialize badge state: {}", state);
+ throw new RuntimeException(e);
+ }
+ }
}
diff --git a/src/main/java/io/trygvis/esper/testing/core/badge/PersonalBadge.java b/src/main/java/io/trygvis/esper/testing/core/badge/PersonalBadge.java
new file mode 100644
index 0000000..c34c35c
--- /dev/null
+++ b/src/main/java/io/trygvis/esper/testing/core/badge/PersonalBadge.java
@@ -0,0 +1,13 @@
+package io.trygvis.esper.testing.core.badge;
+
+import io.trygvis.esper.testing.*;
+
+public class PersonalBadge {
+ public final Uuid person;
+ public final int level;
+
+ public PersonalBadge(Uuid person, int level) {
+ this.person = person;
+ this.level = level;
+ }
+}
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 a4f1e54..6b1c78d 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
@@ -1,14 +1,19 @@
package io.trygvis.esper.testing.core.badge;
-class UnbreakableBadge {
+import io.trygvis.esper.testing.*;
+
+import java.util.*;
+
+class UnbreakableBadge extends PersonalBadge {
// Configuration for this badge
public static final int LEVEL_1_COUNT = 10;
public static final int LEVEL_2_COUNT = 20;
public static final int LEVEL_3_COUNT = 50;
- public final int level;
+ public final List<UUID> builds;
- UnbreakableBadge(int level) {
- this.level = level;
+ 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/badge/UnbreakableBadgeProgress.java b/src/main/java/io/trygvis/esper/testing/core/badge/UnbreakableBadgeProgress.java
index 4cba611..f43ec94 100644
--- a/src/main/java/io/trygvis/esper/testing/core/badge/UnbreakableBadgeProgress.java
+++ b/src/main/java/io/trygvis/esper/testing/core/badge/UnbreakableBadgeProgress.java
@@ -4,30 +4,34 @@ import static fj.P.*;
import fj.*;
import fj.data.*;
import static fj.data.Option.*;
+import static java.util.Collections.singletonList;
+
+import io.trygvis.esper.testing.*;
import io.trygvis.esper.testing.core.db.*;
-import io.trygvis.esper.testing.core.db.PersonBadgeDto.*;
+import io.trygvis.esper.testing.core.db.PersonalBadgeDto.*;
import java.util.*;
+import java.util.List;
public class UnbreakableBadgeProgress extends BadgeProgress {
- public final UUID person;
- public final int count;
+ public final Uuid person;
+ public final List<UUID> builds;
- private UnbreakableBadgeProgress(UUID person, int count) {
+ private UnbreakableBadgeProgress(Uuid person, List<UUID> builds) {
super(BadgeType.UNBREAKABLE);
this.person = person;
- this.count = count;
+ this.builds = Collections.unmodifiableList(builds);
}
@SuppressWarnings("UnusedDeclaration")
private UnbreakableBadgeProgress() {
super(BadgeType.UNBREAKABLE);
person = null;
- count = -1;
+ builds = null;
}
- public static UnbreakableBadgeProgress initial(UUID person) {
- return new UnbreakableBadgeProgress(person, 0);
+ public static UnbreakableBadgeProgress initial(Uuid person) {
+ return new UnbreakableBadgeProgress(person, Collections.<UUID>emptyList());
}
public P2<UnbreakableBadgeProgress, Option<UnbreakableBadge>> onBuild(BuildDto build) {
@@ -35,33 +39,36 @@ public class UnbreakableBadgeProgress extends BadgeProgress {
return p(initial(person), Option.<UnbreakableBadge>none());
}
- int count = this.count + 1;
+ List<UUID> builds = new ArrayList<>(this.builds);
+ builds.add(build.uuid);
- if (count == UnbreakableBadge.LEVEL_3_COUNT) {
- return p(initial(person), some(new UnbreakableBadge(3)));
+ if (progression() == UnbreakableBadge.LEVEL_3_COUNT) {
+ // You have to start from scratch now.
+ builds = singletonList(build.uuid);
+ return p(new UnbreakableBadgeProgress(person, builds), some(new UnbreakableBadge(person, 3, builds)));
}
- if (count == UnbreakableBadge.LEVEL_2_COUNT) {
- return p(new UnbreakableBadgeProgress(person, count), some(new UnbreakableBadge(2)));
+ if (progression() == UnbreakableBadge.LEVEL_2_COUNT) {
+ return p(new UnbreakableBadgeProgress(person, builds), some(new UnbreakableBadge(person, 2, builds)));
}
- if (count == UnbreakableBadge.LEVEL_1_COUNT) {
- return p(new UnbreakableBadgeProgress(person, count), some(new UnbreakableBadge(1)));
+ if (progression() == UnbreakableBadge.LEVEL_1_COUNT) {
+ return p(new UnbreakableBadgeProgress(person, builds), some(new UnbreakableBadge(person, 1, builds)));
}
- return p(new UnbreakableBadgeProgress(person, count), Option.<UnbreakableBadge>none());
+ return p(new UnbreakableBadgeProgress(person, builds), Option.<UnbreakableBadge>none());
}
public int progression() {
- return count;
+ return builds.size();
}
public int goal() {
- if (count > UnbreakableBadge.LEVEL_2_COUNT) {
+ if (progression() > UnbreakableBadge.LEVEL_2_COUNT) {
return UnbreakableBadge.LEVEL_3_COUNT;
}
- if (count > UnbreakableBadge.LEVEL_1_COUNT) {
+ if (progression() > UnbreakableBadge.LEVEL_1_COUNT) {
return UnbreakableBadge.LEVEL_2_COUNT;
}
@@ -69,11 +76,11 @@ public class UnbreakableBadgeProgress extends BadgeProgress {
}
public int progressingAgainstLevel() {
- if (count > UnbreakableBadge.LEVEL_2_COUNT) {
+ if (progression() > UnbreakableBadge.LEVEL_2_COUNT) {
return 3;
}
- if (count > UnbreakableBadge.LEVEL_1_COUNT) {
+ if (progression() > UnbreakableBadge.LEVEL_1_COUNT) {
return 2;
}
@@ -81,6 +88,6 @@ public class UnbreakableBadgeProgress extends BadgeProgress {
}
public String toString() {
- return "UnbreakableBadgeProgress{person=" + person + ", count=" + count + '}';
+ return "UnbreakableBadgeProgress{person=" + person + ", #builds=" + builds.size() + '}';
}
}
diff --git a/src/main/java/io/trygvis/esper/testing/core/badge/UnbreakablePoller.java b/src/main/java/io/trygvis/esper/testing/core/badge/UnbreakablePoller.java
index ab0abd8..76dee02 100755
--- a/src/main/java/io/trygvis/esper/testing/core/badge/UnbreakablePoller.java
+++ b/src/main/java/io/trygvis/esper/testing/core/badge/UnbreakablePoller.java
@@ -11,25 +11,28 @@ import org.slf4j.*;
import java.sql.*;
import java.util.List;
-import java.util.*;
-import static fj.data.Option.some;
import static io.trygvis.esper.testing.Config.*;
-import static io.trygvis.esper.testing.core.db.PersonBadgeDto.BadgeType.*;
+import static io.trygvis.esper.testing.core.db.PersonalBadgeDto.BadgeType.*;
public class UnbreakablePoller implements TablePoller.NewRowCallback<BuildDto> {
private final Logger logger = LoggerFactory.getLogger(getClass());
- private final BadgeService badgeService = new BadgeService();
+ private final BadgeService badgeService;
+
+ public UnbreakablePoller(BadgeService badgeService) {
+ this.badgeService = badgeService;
+ }
public static void main(String[] args) throws Exception {
String pollerName = "unbreakable";
String tableName = "build";
String columnNames = BuildDao.BUILD;
SqlF<ResultSet, BuildDto> f = BuildDao.build;
- TablePoller.NewRowCallback<BuildDto> callback = new UnbreakablePoller();
Config config = loadFromDisk("unbreakable-poller");
+ BadgeService badgeService = new BadgeService(config.createObjectMapper());
+ TablePoller.NewRowCallback<BuildDto> callback = new UnbreakablePoller(badgeService);
BoneCPDataSource dataSource = config.createBoneCp();
@@ -41,11 +44,11 @@ public class UnbreakablePoller implements TablePoller.NewRowCallback<BuildDto> {
public void process(Connection c, BuildDto build) throws SQLException {
Daos daos = new Daos(c);
- List<UUID> persons = daos.buildDao.selectBuildParticipantByBuild(build.uuid);
+ List<Uuid> persons = daos.buildDao.selectBuildParticipantByBuild(build.uuid);
logger.info("Processing build={}, success={}, #persons={}", build.uuid, build.success, persons.size());
- for (UUID person : persons) {
- logger.info("person={}", person);
+ for (Uuid person : persons) {
+ logger.info("person={}", person.toUuidString());
SqlOption<PersonBadgeProgressDto> o = daos.personDao.selectBadgeProgress(person, UNBREAKABLE);
@@ -53,37 +56,31 @@ public class UnbreakablePoller implements TablePoller.NewRowCallback<BuildDto> {
UnbreakableBadgeProgress badge = UnbreakableBadgeProgress.initial(person);
logger.info("New badge progress");
String state = badgeService.serialize(badge);
- daos.personDao.insertBadgeProgress(new Uuid(person), UNBREAKABLE, state);
+ daos.personDao.insertBadgeProgress(person, UNBREAKABLE, state);
continue;
}
- UnbreakableBadgeProgress badge = badgeService.unbreakable(o.get());
+ UnbreakableBadgeProgress badge = badgeService.badgeProgress(o.get(), UnbreakableBadgeProgress.class);
- logger.info("Existing badge progress: count={}", badge.count);
+ logger.info("Existing badge progress: progression={}", badge.progression());
P2<UnbreakableBadgeProgress, Option<UnbreakableBadge>> p = badge.onBuild(build);
badge = p._1();
- logger.info("New badge progress: count={}", badge.count);
+ logger.info("New badge progress: progression={}", badge.progression());
if (p._2().isSome()) {
UnbreakableBadge b = p._2().some();
- logger.info("New unbreakable badge: person={}, level={}", person, b.level);
-
- SqlOption<PersonBadgeDto> option = daos.personDao.selectBadge(person, UNBREAKABLE, b.level);
+ logger.info("New unbreakable badge: person={}, level={}", person.toUuidString(), b.level);
- if (option.isNone()) {
- daos.personDao.insertBadge(person, UNBREAKABLE, b.level, 1);
- } else {
- daos.personDao.incrementBadgeCount(person, UNBREAKABLE, b.level);
- }
+ daos.personDao.insertBadge(build.createdDate, person, UNBREAKABLE, b.level, badgeService.serialize(b));
}
String state = badgeService.serialize(badge);
- daos.personDao.updateBadgeProgress(new Uuid(person), UNBREAKABLE, state);
+ daos.personDao.updateBadgeProgress(person, UNBREAKABLE, state);
}
}
}
diff --git a/src/main/java/io/trygvis/esper/testing/core/db/BuildDao.java b/src/main/java/io/trygvis/esper/testing/core/db/BuildDao.java
index cd103d6..32afe42 100755
--- a/src/main/java/io/trygvis/esper/testing/core/db/BuildDao.java
+++ b/src/main/java/io/trygvis/esper/testing/core/db/BuildDao.java
@@ -57,7 +57,7 @@ public class BuildDao {
}
}
- public List<UUID> selectBuildParticipantByBuild(UUID build) throws SQLException {
+ public List<Uuid> selectBuildParticipantByBuild(UUID build) throws SQLException {
try (PreparedStatement s = c.prepareStatement("SELECT person FROM build_participant WHERE build=?")) {
int i = 1;
s.setString(i, build.toString());
@@ -86,7 +86,7 @@ public class BuildDao {
int i = 1;
s.setString(i++, ref.type);
s.setString(i, ref.uuid.toString());
- return fromRs(s.executeQuery()).map(getUuid);
+ return fromRs(s.executeQuery()).map(getUUID);
}
}
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 0f80219..7a682dc 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
@@ -2,7 +2,7 @@ package io.trygvis.esper.testing.core.db;
import fj.data.*;
import io.trygvis.esper.testing.*;
-import io.trygvis.esper.testing.core.db.PersonBadgeDto.*;
+import io.trygvis.esper.testing.core.db.PersonalBadgeDto.*;
import io.trygvis.esper.testing.util.sql.*;
import org.joda.time.*;
@@ -30,18 +30,18 @@ public class PersonDao {
}
};
- public static final String PERSON_BADGE = "uuid, created_date, person, name, level, count";
+ public static final String PERSON_BADGE = "uuid, created_date, person, name, level, state";
- public static final SqlF<ResultSet, PersonBadgeDto> personBadge = new SqlF<ResultSet, PersonBadgeDto>() {
- public PersonBadgeDto apply(ResultSet rs) throws SQLException {
+ public static final SqlF<ResultSet, PersonalBadgeDto> personBadge = new SqlF<ResultSet, PersonalBadgeDto>() {
+ public PersonalBadgeDto apply(ResultSet rs) throws SQLException {
int i = 1;
- return new PersonBadgeDto(
+ return new PersonalBadgeDto(
UUID.fromString(rs.getString(i++)),
new DateTime(rs.getTimestamp(i++).getTime()),
Uuid.fromString(rs.getString(i++)),
BadgeType.valueOf(rs.getString(i++)),
rs.getInt(i++),
- rs.getInt(i));
+ rs.getString(i));
}
};
@@ -146,41 +146,31 @@ public class PersonDao {
// Badge
// -----------------------------------------------------------------------
- public UUID insertBadge(UUID person, BadgeType type, int level, int count) throws SQLException {
+ public UUID insertBadge(DateTime createdDate, Uuid person, BadgeType type, int level, String state) throws SQLException {
try (PreparedStatement s = c.prepareStatement("INSERT INTO person_badge(" + PERSON_BADGE + ") VALUES(?, ?, ?, ?, ?, ?)")) {
UUID uuid = UUID.randomUUID();
int i = 1;
s.setString(i++, uuid.toString());
- s.setTimestamp(i++, new Timestamp(currentTimeMillis()));
- s.setString(i++, person.toString());
+ s.setTimestamp(i++, new Timestamp(createdDate.getMillis()));
+ s.setString(i++, person.toUuidString());
s.setString(i++, type.toString());
s.setInt(i++, level);
- s.setInt(i, count);
+ s.setString(i, state);
s.executeUpdate();
return uuid;
}
}
- public void incrementBadgeCount(UUID person, BadgeType type, int level) throws SQLException {
- try (PreparedStatement s = c.prepareStatement("UPDATE person_badge SET count=count+1 WHERE person=? AND name=? AND level=?")) {
- int i = 1;
- s.setString(i++, person.toString());
- s.setString(i++, type.toString());
- s.setInt(i, level);
- s.executeUpdate();
- }
- }
-
- public List<PersonBadgeDto> selectBadges(Uuid person) throws SQLException {
- try (PreparedStatement s = c.prepareStatement("SELECT " + PERSON_BADGE + " FROM person_badge WHERE person=? ORDER BY name, level DESC")) {
+ public List<PersonalBadgeDto> selectBadges(Uuid person) throws SQLException {
+ try (PreparedStatement s = c.prepareStatement("SELECT " + PERSON_BADGE + " FROM person_badge WHERE person=? ORDER BY created_date DESC")) {
int i = 1;
s.setString(i, person.toUuidString());
return toList(s, personBadge);
}
}
- public SqlOption<PersonBadgeDto> 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=?")) {
+ 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")) {
int i = 1;
s.setString(i++, person.toString());
s.setString(i++, type.toString());
@@ -189,7 +179,7 @@ public class PersonDao {
}
}
- public List<PersonBadgeDto> selectBadges(Option<Uuid> person, Option<BadgeType> type, Option<Integer> level, PageRequest page) throws SQLException {
+ public List<PersonalBadgeDto> selectBadges(Option<Uuid> person, Option<BadgeType> type, Option<Integer> level, PageRequest page) throws SQLException {
String sql = "SELECT " + PERSON_BADGE + " FROM person_badge WHERE 1=1";
if (person.isSome()) {
@@ -227,10 +217,10 @@ public class PersonDao {
// Badge Progress
// -----------------------------------------------------------------------
- public SqlOption<PersonBadgeProgressDto> selectBadgeProgress(UUID person, BadgeType type) throws SQLException {
+ public SqlOption<PersonBadgeProgressDto> selectBadgeProgress(Uuid person, BadgeType type) throws SQLException {
try (PreparedStatement s = c.prepareStatement("SELECT " + PERSON_BADGE_PROGRESS + " FROM person_badge_progress WHERE person=? AND badge=?")) {
int i = 1;
- s.setString(i++, person.toString());
+ s.setString(i++, person.toUuidString());
s.setString(i, type.toString());
return fromRs(s.executeQuery()).map(personBadgeProgress);
}
diff --git a/src/main/java/io/trygvis/esper/testing/core/db/PersonBadgeDto.java b/src/main/java/io/trygvis/esper/testing/core/db/PersonalBadgeDto.java
index 0f34347..cc2a739 100644
--- a/src/main/java/io/trygvis/esper/testing/core/db/PersonBadgeDto.java
+++ b/src/main/java/io/trygvis/esper/testing/core/db/PersonalBadgeDto.java
@@ -5,7 +5,7 @@ import org.joda.time.*;
import java.util.*;
-public class PersonBadgeDto extends AbstractEntity {
+public class PersonalBadgeDto extends AbstractEntity {
public enum BadgeType {
UNBREAKABLE
}
@@ -13,13 +13,13 @@ public class PersonBadgeDto extends AbstractEntity {
public final Uuid person;
public final BadgeType type;
public final int level;
- public final int count;
+ public final String state;
- public PersonBadgeDto(UUID uuid, DateTime createdDate, Uuid person, BadgeType type, int level, int count) {
+ public PersonalBadgeDto(UUID uuid, DateTime createdDate, Uuid person, BadgeType type, int level, String state) {
super(uuid, createdDate);
this.person = person;
this.type = type;
this.level = level;
- this.count = count;
+ this.state = state;
}
}
diff --git a/src/main/java/io/trygvis/esper/testing/util/sql/ResultSetF.java b/src/main/java/io/trygvis/esper/testing/util/sql/ResultSetF.java
index 7169372..5c51e6f 100755
--- a/src/main/java/io/trygvis/esper/testing/util/sql/ResultSetF.java
+++ b/src/main/java/io/trygvis/esper/testing/util/sql/ResultSetF.java
@@ -1,5 +1,7 @@
package io.trygvis.esper.testing.util.sql;
+import io.trygvis.esper.testing.*;
+
import java.sql.*;
import java.util.*;
@@ -17,9 +19,15 @@ public class ResultSetF {
}
};
- public static final SqlF<ResultSet, UUID> getUuid = new SqlF<ResultSet, UUID>() {
+ public static final SqlF<ResultSet, UUID> getUUID = new SqlF<ResultSet, UUID>() {
public UUID apply(ResultSet rs) throws SQLException {
return UUID.fromString(rs.getString(1));
}
};
+
+ public static final SqlF<ResultSet, Uuid> getUuid = new SqlF<ResultSet, Uuid>() {
+ public Uuid apply(ResultSet rs) throws SQLException {
+ return Uuid.fromString(rs.getString(1));
+ }
+ };
}
diff --git a/src/main/java/io/trygvis/esper/testing/web/JerseyApplication.java b/src/main/java/io/trygvis/esper/testing/web/JerseyApplication.java
index 8f66548..3c86581 100755
--- a/src/main/java/io/trygvis/esper/testing/web/JerseyApplication.java
+++ b/src/main/java/io/trygvis/esper/testing/web/JerseyApplication.java
@@ -8,6 +8,7 @@ import io.trygvis.esper.testing.*;
import io.trygvis.esper.testing.core.badge.*;
import io.trygvis.esper.testing.util.sql.*;
import io.trygvis.esper.testing.web.resource.*;
+import org.codehaus.jackson.map.*;
import javax.ws.rs.*;
import javax.ws.rs.core.*;
@@ -23,18 +24,22 @@ public class JerseyApplication extends Application {
private final HashSet<Object> singletons;
public JerseyApplication() throws Exception {
- DatabaseAccess da = new DatabaseAccess(WebConfig.config.createBoneCp());
+ Config config = WebConfig.config;
- BadgeService badgeService = new BadgeService();
+ DatabaseAccess da = new DatabaseAccess(config.createBoneCp());
+ ObjectMapper objectMapper = config.createObjectMapper();
- singletons = new HashSet<Object>(Arrays.asList(
+ BadgeService badgeService = new BadgeService(objectMapper);
+
+ singletons = new HashSet<>(Arrays.asList(
new CoreResource(da, badgeService),
- new JenkinsResource(da)
+ new JenkinsResource(da),
+ new MyObjectMapper(objectMapper)
));
}
public Set<Class<?>> getClasses() {
- return new HashSet<>(Arrays.<Class<?>>asList(ResourceParamInjector.class, MyObjectMapper.class));
+ return new HashSet<>(Arrays.<Class<?>>asList(ResourceParamInjector.class));
}
public Set<Object> getSingletons() {
@@ -44,12 +49,6 @@ public class JerseyApplication extends Application {
@Provider
public static class ResourceParamInjector implements InjectableProvider<MagicParam, Type> {
- private final ResourceContext rc;
-
- public ResourceParamInjector(@Context ResourceContext rc) {
- this.rc = rc;
- }
-
public ComponentScope getScope() {
return ComponentScope.PerRequest;
}
diff --git a/src/main/java/io/trygvis/esper/testing/web/MyObjectMapper.java b/src/main/java/io/trygvis/esper/testing/web/MyObjectMapper.java
index bfbd6eb..c684010 100755
--- a/src/main/java/io/trygvis/esper/testing/web/MyObjectMapper.java
+++ b/src/main/java/io/trygvis/esper/testing/web/MyObjectMapper.java
@@ -1,37 +1,17 @@
package io.trygvis.esper.testing.web;
-import io.trygvis.esper.testing.*;
-import org.codehaus.jackson.*;
import org.codehaus.jackson.map.*;
-import org.codehaus.jackson.map.module.*;
-import java.io.*;
import javax.ws.rs.ext.*;
public class MyObjectMapper implements ContextResolver<ObjectMapper> {
- private ObjectMapper objectMapper;
+ private final ObjectMapper objectMapper;
- public MyObjectMapper() throws Exception {
- objectMapper = new ObjectMapper();
- SimpleModule module = new SimpleModule("wat", Version.unknownVersion());
- module.addDeserializer(Uuid.class, new UuidDeserializer());
- module.addSerializer(Uuid.class, new UuidSerializer());
- objectMapper.registerModule(module);
+ public MyObjectMapper(ObjectMapper objectMapper) throws Exception {
+ this.objectMapper = objectMapper;
}
public ObjectMapper getContext(Class<?> type) {
return objectMapper;
}
-
- private static class UuidDeserializer extends JsonDeserializer<Uuid> {
- public Uuid deserialize(JsonParser jp, DeserializationContext context) throws IOException {
- return Uuid.fromString(jp.getText());
- }
- }
-
- private static class UuidSerializer extends JsonSerializer<Uuid> {
- public void serialize(Uuid value, JsonGenerator generator, SerializerProvider provider) throws IOException {
- generator.writeString(value.toStringBase64());
- }
- }
}
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 8eea335..ded97ae 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,20 +1,33 @@
package io.trygvis.esper.testing.web.resource;
+import org.joda.time.*;
+
public class BadgeJson {
+ public final DateTime createdDate;
public final String name;
public final int level;
+ public final Integer progress;
+ public final Integer goal;
+
/**
- * Number of times this badge has been received.
+ * For completed badges.
*/
- public final int count;
- public final int progress;
- public final int goal;
+ public BadgeJson(DateTime createdDate, String name, int level) {
+ this.createdDate = createdDate;
+ this.name = name;
+ this.level = level;
+ this.progress = null;
+ this.goal = null;
+ }
- public BadgeJson(String name, int level, int count, int progress, int goal) {
+ /**
+ * For badges in progress.
+ */
+ public BadgeJson(DateTime createdDate, String name, int level, int progress, int goal) {
+ this.createdDate = createdDate;
this.name = name;
this.level = level;
- this.count = count;
this.progress = progress;
this.goal = goal;
}
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 f68d6ec..a129cf3 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
@@ -124,14 +124,14 @@ public class CoreResource extends AbstractResource {
@GET
@Path("/badge")
- public List<BadgeDetailJson> getBadges(@MagicParam final PageRequest page, @MagicParam(query = "person") final Uuid person) throws Exception {
- return da.inTransaction(new CoreDaosCallback<List<BadgeDetailJson>>() {
- protected List<BadgeDetailJson> run() throws SQLException {
- List<PersonBadgeDto> badgeDtos = daos.personDao.selectBadges(fromNull(person), Option.<PersonBadgeDto.BadgeType>none(), Option.<Integer>none(), page);
-
- List<BadgeDetailJson> list = new ArrayList<>();
- for (PersonBadgeDto badge : badgeDtos) {
- list.add(getBadgeDetailJson.apply(badge));
+ public List<BadgeJson> getBadges(@MagicParam final PageRequest page, @MagicParam(query = "person") final Uuid person) throws Exception {
+ return da.inTransaction(new CoreDaosCallback<List<BadgeJson>>() {
+ protected List<BadgeJson> run() throws SQLException {
+ List<PersonalBadgeDto> badgeDtos = daos.personDao.selectBadges(fromNull(person), Option.<PersonalBadgeDto.BadgeType>none(), Option.<Integer>none(), page);
+
+ List<BadgeJson> list = new ArrayList<>();
+ for (PersonalBadgeDto badge : badgeDtos) {
+ list.add(getBadgeJson.apply(badge));
}
return list;
}
@@ -157,15 +157,13 @@ public class CoreResource extends AbstractResource {
protected final SqlF<PersonDto, PersonDetailJson> getPersonDetailJson = new SqlF<PersonDto, PersonDetailJson>() {
public PersonDetailJson apply(PersonDto person) throws SQLException {
List<BadgeJson> badges = new ArrayList<>();
-
- for (PersonBadgeDto badge : daos.personDao.selectBadges(person.uuid)) {
- badges.add(getBadge(badge));
+ for (PersonalBadgeDto badge : daos.personDao.selectBadges(person.uuid)) {
+ badges.add(getBadgeJson.apply(badge));
}
List<BadgeJson> badgesInProgress = new ArrayList<>();
-
for (PersonBadgeProgressDto badgeProgressDto : daos.personDao.selectBadgeProgresses(person.uuid)) {
- UnbreakableBadgeProgress progress = badgeService.unbreakable(badgeProgressDto);
+ BadgeProgress progress = badgeService.badgeProgress(badgeProgressDto);
badgesInProgress.add(getBadge(progress));
}
@@ -177,17 +175,19 @@ public class CoreResource extends AbstractResource {
}
};
- private BadgeJson getBadge(PersonBadgeDto badge) {
- return new BadgeJson(badge.type.name(), badge.level, badge.count, 100, 100);
- }
+ 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);
+ }
+ };
private BadgeJson getBadge(BadgeProgress progress) {
- return new BadgeJson(progress.type.name(), progress.progressingAgainstLevel(), 0, progress.progression(), progress.goal());
+ return new BadgeJson(null, progress.type.name(), progress.progressingAgainstLevel(), progress.progression(), progress.goal());
}
- protected final SqlF<PersonBadgeDto, BadgeDetailJson> getBadgeDetailJson = new SqlF<PersonBadgeDto, BadgeDetailJson>() {
- public BadgeDetailJson apply(PersonBadgeDto badgeDto) throws SQLException {
- return new BadgeDetailJson(getBadge(badgeDto),
+ protected final SqlF<PersonalBadgeDto, BadgeDetailJson> getBadgeDetailJson = new SqlF<PersonalBadgeDto, BadgeDetailJson>() {
+ public BadgeDetailJson apply(PersonalBadgeDto badgeDto) throws SQLException {
+ return new BadgeDetailJson(getBadgeJson.apply(badgeDto),
daos.personDao.selectPerson(badgeDto.person).map(getPersonJson).get());
}
};