From b895e15d3c9bd4c7a1d420c99af9d1f2efca8a0b Mon Sep 17 00:00:00 2001
From: Trygve Laugstøl <trygvis@inamo.no>
Date: Fri, 4 Jan 2013 23:42:17 +0100
Subject: o Showing recent badges on the front page.

---
 .../testing/core/badge/UnbreakablePoller.java      |   1 +
 .../esper/testing/core/db/PersonBadgeDto.java      |   4 +-
 .../trygvis/esper/testing/core/db/PersonDao.java   |  38 +++++-
 .../esper/testing/web/resource/BadgeJson.java      |  10 ++
 .../esper/testing/web/resource/CoreResource.java   | 150 ++++++++++++++-------
 .../esper/testing/web/resource/PersonJson.java     |  14 +-
 src/main/webapp/apps/core/CoreResources.js         |   6 +
 src/main/webapp/apps/frontPageApp/frontPage.html   |  15 ++-
 src/main/webapp/apps/frontPageApp/frontPageApp.js  |   5 +-
 9 files changed, 180 insertions(+), 63 deletions(-)

(limited to 'src')

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 7fb057a..ab0abd8 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
@@ -13,6 +13,7 @@ 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.*;
 
diff --git a/src/main/java/io/trygvis/esper/testing/core/db/PersonBadgeDto.java b/src/main/java/io/trygvis/esper/testing/core/db/PersonBadgeDto.java
index 463da9c..0f34347 100644
--- a/src/main/java/io/trygvis/esper/testing/core/db/PersonBadgeDto.java
+++ b/src/main/java/io/trygvis/esper/testing/core/db/PersonBadgeDto.java
@@ -10,12 +10,12 @@ public class PersonBadgeDto extends AbstractEntity {
         UNBREAKABLE
     }
 
-    public final UUID person;
+    public final Uuid person;
     public final BadgeType type;
     public final int level;
     public final int count;
 
-    public PersonBadgeDto(UUID uuid, DateTime createdDate, UUID person, BadgeType type, int level, int count) {
+    public PersonBadgeDto(UUID uuid, DateTime createdDate, Uuid person, BadgeType type, int level, int count) {
         super(uuid, createdDate);
         this.person = person;
         this.type = type;
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 ec1620b..0f80219 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
@@ -1,5 +1,6 @@
 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.util.sql.*;
@@ -7,6 +8,7 @@ import org.joda.time.*;
 
 import java.sql.*;
 import java.util.*;
+import java.util.List;
 
 import static io.trygvis.esper.testing.Util.*;
 import static io.trygvis.esper.testing.util.sql.SqlOption.*;
@@ -36,7 +38,7 @@ public class PersonDao {
             return new PersonBadgeDto(
                     UUID.fromString(rs.getString(i++)),
                     new DateTime(rs.getTimestamp(i++).getTime()),
-                    UUID.fromString(rs.getString(i++)),
+                    Uuid.fromString(rs.getString(i++)),
                     BadgeType.valueOf(rs.getString(i++)),
                     rs.getInt(i++),
                     rs.getInt(i));
@@ -187,6 +189,40 @@ public class PersonDao {
         }
     }
 
+    public List<PersonBadgeDto> 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()) {
+            sql += " AND person=?";
+        }
+
+        if (type.isSome()) {
+            sql += " AND name=?";
+        }
+
+        if (level.isSome()) {
+            sql += " AND level=?";
+        }
+
+        sql += " LIMIT ? OFFSET ?";
+
+        try (PreparedStatement s = c.prepareStatement(sql)) {
+            int i = 1;
+            if(person.isSome()) {
+                s.setString(i++, person.some().toUuidString());
+            }
+            if (type.isSome()) {
+                s.setString(i++, type.some().toString());
+            }
+            if (level.isSome()) {
+                s.setInt(i, level.some());
+            }
+            s.setInt(i++, page.count.orSome(10));
+            s.setInt(i, page.startIndex.orSome(0));
+            return toList(s, personBadge);
+        }
+    }
+
     // -----------------------------------------------------------------------
     // Badge Progress
     // -----------------------------------------------------------------------
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 65f541a..8eea335 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
@@ -19,3 +19,13 @@ public class BadgeJson {
         this.goal = goal;
     }
 }
+
+class BadgeDetailJson {
+    public final BadgeJson badge;
+    public final PersonJson person;
+
+    BadgeDetailJson(BadgeJson badge, PersonJson person) {
+        this.badge = badge;
+        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 aa7a5ee..f68d6ec 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
@@ -15,6 +15,7 @@ import java.sql.*;
 import java.util.*;
 import java.util.List;
 
+import static fj.data.Option.fromNull;
 import static io.trygvis.esper.testing.util.sql.PageRequest.*;
 
 @Path("/resource/core")
@@ -34,14 +35,14 @@ public class CoreResource extends AbstractResource {
 
     @GET
     @Path("/person")
-    public List<PersonJson> getPersons(@Context final HttpServletRequest req) throws Exception {
+    public List<PersonDetailJson> getPersons(@Context final HttpServletRequest req) throws Exception {
         final PageRequest pageRequest = pageReq(req);
 
-        return da.inTransaction(new DatabaseAccess.DaosCallback<List<PersonJson>>() {
-            public List<PersonJson> run(Daos daos) throws SQLException {
-                List<PersonJson> list = new ArrayList<>();
+        return da.inTransaction(new CoreDaosCallback<List<PersonDetailJson>>() {
+            protected List<PersonDetailJson> run() throws SQLException {
+                List<PersonDetailJson> list = new ArrayList<>();
                 for (PersonDto person : daos.personDao.selectPersons(pageRequest)) {
-                    list.add(getPersonJson(daos, person));
+                    list.add(super.getPersonDetailJson.apply(person));
                 }
                 return list;
             }
@@ -50,44 +51,14 @@ public class CoreResource extends AbstractResource {
 
     @GET
     @Path("/person/{uuid}")
-    public PersonJson getPerson(@PathParam("uuid") final Uuid uuid) throws Exception {
-        System.out.println("uuid.toStringBase64() = " + uuid.toStringBase64());
-        System.out.println("uuid.toUuidString() = " + uuid.toUuidString());
-        return get(new DatabaseAccess.DaosCallback<Option<PersonJson>>() {
-            public Option<PersonJson> run(Daos daos) throws SQLException {
-                SqlOption<PersonDto> o = daos.personDao.selectPerson(uuid);
-                if (o.isNone()) {
-                    return Option.none();
-                }
-
-                return Option.some(getPersonJson(daos, o.get()));
+    public PersonDetailJson getPerson(@PathParam("uuid") final Uuid uuid) throws Exception {
+        return sql(new CoreDaosCallback<SqlOption<PersonDetailJson>>() {
+            protected SqlOption<PersonDetailJson> run() throws SQLException {
+                return daos.personDao.selectPerson(uuid).map(super.getPersonDetailJson);
             }
         });
     }
 
-    private PersonJson getPersonJson(Daos daos, PersonDto person) throws SQLException {
-        List<BadgeJson> badges = new ArrayList<>();
-
-        for (PersonBadgeDto badge : daos.personDao.selectBadges(person.uuid)) {
-            badges.add(new BadgeJson(badge.type.name(), badge.level, badge.count, 100, 100));
-        }
-
-        List<BadgeJson> badgesInProgress = new ArrayList<>();
-
-        for (PersonBadgeProgressDto badgeProgressDto : daos.personDao.selectBadgeProgresses(person.uuid)) {
-            UnbreakableBadgeProgress progress = badgeService.unbreakable(badgeProgressDto);
-            badgesInProgress.add(new BadgeJson(progress.type.name(), progress.progressingAgainstLevel(), 0,
-                    progress.progression(), progress.goal()));
-        }
-
-        return new PersonJson(
-                person.uuid,
-                person.name,
-                badges,
-                badgesInProgress
-        );
-    }
-
     // -----------------------------------------------------------------------
     // Build
     // -----------------------------------------------------------------------
@@ -116,12 +87,12 @@ public class CoreResource extends AbstractResource {
 
     @GET
     @Path("/build-participant/{uuid}")
-    public List<PersonJson> getBuildParticipants(@MagicParam final UUID build) throws Exception {
-        return da.inTransaction(new DatabaseAccess.DaosCallback<List<PersonJson>>() {
-            public List<PersonJson> run(Daos daos) throws SQLException {
-                List<PersonJson> list = new ArrayList<>();
+    public List<PersonDetailJson> getBuildParticipants(@MagicParam final UUID build) throws Exception {
+        return da.inTransaction(new CoreDaosCallback<List<PersonDetailJson>>() {
+            protected List<PersonDetailJson> run() throws SQLException {
+                List<PersonDetailJson> list = new ArrayList<>();
                 for (PersonDto person : daos.buildDao.selectPersonsFromBuildParticipant(build)) {
-                    list.add(getPersonJson(daos, person));
+                    list.add(super.getPersonDetailJson.apply(person));
                 }
                 return list;
             }
@@ -147,15 +118,90 @@ public class CoreResource extends AbstractResource {
         return new BuildJson(build.uuid, build.timestamp, build.success);
     }
 
-    public static class BuildJson {
-        public final UUID uuid;
-        public final DateTime timestamp;
-        public final boolean success;
+    // -----------------------------------------------------------------------
+    // Badge
+    // -----------------------------------------------------------------------
+
+    @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));
+                }
+                return list;
+            }
+        });
+    }
+
+    abstract class CoreDaosCallback<T> implements DatabaseAccess.DaosCallback<T> {
+        protected Daos daos;
 
-        public BuildJson(UUID uuid, DateTime timestamp, boolean success) {
-            this.uuid = uuid;
-            this.timestamp = timestamp;
-            this.success = success;
+        protected abstract T run() throws SQLException;
+
+        public T run(Daos daos) throws SQLException {
+            this.daos = daos;
+            return run();
         }
+
+        protected final SqlF<PersonDto, PersonJson> getPersonJson = new SqlF<PersonDto, PersonJson>() {
+            public PersonJson apply(PersonDto person) throws SQLException {
+                return new PersonJson(person.uuid, person.name);
+            }
+        };
+
+        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));
+                }
+
+                List<BadgeJson> badgesInProgress = new ArrayList<>();
+
+                for (PersonBadgeProgressDto badgeProgressDto : daos.personDao.selectBadgeProgresses(person.uuid)) {
+                    UnbreakableBadgeProgress progress = badgeService.unbreakable(badgeProgressDto);
+                    badgesInProgress.add(getBadge(progress));
+                }
+
+                return new PersonDetailJson(
+                        getPersonJson.apply(person),
+                        badges,
+                        badgesInProgress
+                );
+            }
+        };
+
+        private BadgeJson getBadge(PersonBadgeDto badge) {
+            return new BadgeJson(badge.type.name(), badge.level, badge.count, 100, 100);
+        }
+
+        private BadgeJson getBadge(BadgeProgress progress) {
+            return new BadgeJson(progress.type.name(), progress.progressingAgainstLevel(), 0, 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),
+                        daos.personDao.selectPerson(badgeDto.person).map(getPersonJson).get());
+            }
+        };
+    }
+}
+
+class BuildJson {
+    public final UUID uuid;
+    public final DateTime timestamp;
+    public final boolean success;
+
+    public BuildJson(UUID uuid, DateTime timestamp, boolean success) {
+        this.uuid = uuid;
+        this.timestamp = timestamp;
+        this.success = success;
     }
 }
diff --git a/src/main/java/io/trygvis/esper/testing/web/resource/PersonJson.java b/src/main/java/io/trygvis/esper/testing/web/resource/PersonJson.java
index e26b120..65b081c 100755
--- a/src/main/java/io/trygvis/esper/testing/web/resource/PersonJson.java
+++ b/src/main/java/io/trygvis/esper/testing/web/resource/PersonJson.java
@@ -7,12 +7,20 @@ import java.util.*;
 public class PersonJson {
     public final Uuid uuid;
     public final String name;
-    public final List<BadgeJson> badges;
-    public final List<BadgeJson> badgesInProgress;
 
-    public PersonJson(Uuid uuid, String name, List<BadgeJson> badges, List<BadgeJson> badgesInProgress) {
+    public PersonJson(Uuid uuid, String name) {
         this.uuid = uuid;
         this.name = name;
+    }
+}
+
+class PersonDetailJson {
+    public final PersonJson person;
+    public final List<BadgeJson> badges;
+    public final List<BadgeJson> badgesInProgress;
+
+    public PersonDetailJson(PersonJson person, List<BadgeJson> badges, List<BadgeJson> badgesInProgress) {
+        this.person = person;
         this.badges = badges;
         this.badgesInProgress = badgesInProgress;
     }
diff --git a/src/main/webapp/apps/core/CoreResources.js b/src/main/webapp/apps/core/CoreResources.js
index 74ac184..aec6a15 100644
--- a/src/main/webapp/apps/core/CoreResources.js
+++ b/src/main/webapp/apps/core/CoreResources.js
@@ -17,3 +17,9 @@ function BuildParticipant($resource) {
 }
 
 angular.module('buildParticipant', ['ngResource']).factory('BuildParticipant', BuildParticipant);
+
+function Badge($resource) {
+  return $resource('/resource/core/badge');
+}
+
+angular.module('badge', ['ngResource']).factory('Badge', Badge);
diff --git a/src/main/webapp/apps/frontPageApp/frontPage.html b/src/main/webapp/apps/frontPageApp/frontPage.html
index 3c433ee..8e067e1 100755
--- a/src/main/webapp/apps/frontPageApp/frontPage.html
+++ b/src/main/webapp/apps/frontPageApp/frontPage.html
@@ -1,10 +1,19 @@
 <div class="container">
   <navbar/>
-<!-- <ng-include src="'/apps/core/navbar.html'" /> -->
 
   <div class="page-header">
-    <h1>Newcomers</h1>
+    <h1>Recent</h1>
   </div>
+
+  <h3>Badges</h3>
+  <div>
+    <span ng-repeat="b in recentBadges">
+      <a href="/person/{{b.person.uuid}}/"> {{b.person.name}} </a>
+      <span class="badge-level-{{badge.level}} badge">{{b.badge.name}}</span>
+    </span>
+  </div>
+
+  <h3>Newcomers</h3>
   <table class="table">
     <thead>
     <tr>
@@ -14,7 +23,7 @@
     </thead>
     <tbody>
     <tr ng-repeat="person in persons.rows">
-      <td><a href="/person/{{person.uuid}}/">{{person.name}}</a></td>
+      <td><a href="/person/{{person.person.uuid}}/">{{person.person.name}}</a></td>
       <td>{{person.badges.length}}</td>
     </tr>
     </tbody>
diff --git a/src/main/webapp/apps/frontPageApp/frontPageApp.js b/src/main/webapp/apps/frontPageApp/frontPageApp.js
index 624c484..d5af99f 100755
--- a/src/main/webapp/apps/frontPageApp/frontPageApp.js
+++ b/src/main/webapp/apps/frontPageApp/frontPageApp.js
@@ -1,10 +1,11 @@
 'use strict';
 
-var frontPageApp = angular.module('frontPageApp', ['ngGrid', 'person', 'pagingTableService', 'core.directives']).config(function ($routeProvider, $locationProvider) {
+var frontPageApp = angular.module('frontPageApp', ['ngGrid', 'person', 'badge', 'pagingTableService', 'core.directives']).config(function ($routeProvider, $locationProvider) {
   $routeProvider.
       when('/', {controller: FrontPageCtrl, templateUrl: '/apps/frontPageApp/frontPage.html?noCache=' + noCache});
 });
 
-function FrontPageCtrl($scope, Person, PagingTableService) {
+function FrontPageCtrl($scope, Person, Badge, PagingTableService) {
   $scope.persons = PagingTableService.create($scope, PagingTableService.defaultCallback(Person));
+  $scope.recentBadges = Badge.query();
 }
-- 
cgit v1.2.3