public StatisticsPageData fetchStatisticsPageData(final Long userId, final String discipline)
     throws UserNotFoundException {
   final User user = fetchUser(userId);
   final String fromWhereClause =
       " from WorkoutImpl w where :user MEMBER OF w.participants "
           + (discipline != null ? " and discipline = :discipline " : " ");
   final Query disciplineQuery =
       query(
           "select w.discipline, count(*) from WorkoutImpl w "
               + "WHERE :user MEMBER OF w.participants GROUP BY w.discipline ORDER BY count(*) DESC");
   disciplineQuery.setParameter("user", user);
   final Query query = query("select sum(w.distance) " + fromWhereClause);
   bindQuery(query, user, discipline);
   final Query query2 =
       query(
           "select year(w.date), sum(w.distance), sum(w.duration), sum(w.energy) "
               + fromWhereClause
               + " GROUP BY year(w.date) ORDER BY year(w.date) DESC");
   bindQuery(query2, user, discipline);
   final Query query3 =
       query(
           "select year(w.date), month(w.date), sum(w.distance), sum(w.duration), sum(w.energy) "
               + fromWhereClause
               + "GROUP BY year(w.date), month(w.date) "
               + "ORDER BY year(w.date)  DESC, month(w.date)  DESC");
   bindQuery(query3, user, discipline);
   return new StatisticsPageData(
       user,
       disciplineQuery.getResultList(),
       query.getSingleResult(),
       query2.getResultList(),
       query3.getResultList());
 }
 private Double fetchGlobalDistance(final Group group) {
   final Query query =
       query(
           "select sum(w.distance) from GroupImpl g left join g.members u left join u.workouts w where g=:group");
   query.setParameter("group", group);
   return (Double) query.getSingleResult();
 }
 public User facebookLogin(final Long facebookId) {
   final Query query = query("select u from UserImpl u where u.facebookId=:facebookId");
   query.setParameter("facebookId", facebookId);
   try {
     return (User) query.getSingleResult();
   } catch (NoResultException e) {
     return null;
   }
 }
 private Double fetchGlobalDistance(final User user) {
   final Query query =
       query(
           "select sum(w.distance) from WorkoutImpl w where 1=1"
               + (user != null ? " and :user MEMBER OF w.participants" : ""));
   if (user != null) query.setParameter("user", user);
   final Double result = (Double) query.getSingleResult();
   return result == null ? Double.valueOf(0) : result;
 }
 public User fetchUser(final String name) throws UserNotFoundException {
   final Query query = query("select u from UserImpl u where u.name=:name");
   query.setParameter("name", name);
   try {
     return (User) query.getSingleResult();
   } catch (NoResultException e) {
     throw new UserNotFoundException();
   }
 }
 public User fetchRememberedUser(final String rememberCookie) throws UserNotFoundException {
   final Query query = query("select u from UserImpl u where u.rememberToken =:rememberCookie");
   query.setParameter("rememberCookie", rememberCookie);
   try {
     return (User) query.getSingleResult();
   } catch (NoResultException e) {
     throw new UserNotFoundException();
   }
 }
 public User fetchUser(final Long id) throws UserNotFoundException {
   final Query query = query("select u from UserImpl u where u.id=:id");
   query.setParameter("id", id);
   try {
     return (User) query.getSingleResult();
   } catch (NoResultException e) {
     throw new UserNotFoundException();
   }
 }
 /** @return null if auth failed, user otherwise */
 public User authenticate(final String login, final String password, final boolean rememberMe) {
   final Query query = query("select u from UserImpl u where u.name=:user_login");
   query.setParameter("user_login", login);
   try {
     final UserImpl user = (UserImpl) query.getSingleResult();
     if (rememberMe && user.getRememberToken() == null) {
       final String token = generateToken();
       user.setRememberToken(token);
       entityManager.merge(user);
     }
     return user.checkPassword(password) ? user : null;
   } catch (NoResultException e) {
     return null;
   }
 }