public boolean checkAndChangePassword(
     final User user, final String oldPassword, final String password) throws MailException {
   if (user.checkPassword(oldPassword)) {
     ((UserImpl) user).setPassword(password);
     entityManager.merge(user);
     final UserString email = user.getEmail();
     if (email != null)
       MailSender.sendPasswordChangeMail(user.getName().toString(), password, email.nonEscaped());
     return true;
   }
   return false;
 }
 private Collection<GroupData> fetchGroupDataForUser(
     final User user, final boolean restrictToSuscribed) {
   final String ifConnectedColumns =
       "max(ifnull(USER_ID=:userId, false))>0, sum(ifnull(USER_ID=:userId AND LAST_VISIT<PUBLIC_MESSAGES.`DATE`, false))";
   final Query query =
       entityManager.createNativeQuery(
           "select GROUPS.ID, name, count(DISTINCT GROUP_USER.USER_ID), "
               + (user != null ? ifConnectedColumns : " 0,0")
               + " from GROUPS left join  GROUP_USER on GROUP_ID=ID "
               + " left join PUBLIC_MESSAGES on PUBLIC_MESSAGES.GROUP_ID=GROUP_USER.GROUP_ID "
               + (restrictToSuscribed ? " where GROUP_USER.USER_ID=:userId" : "")
               + " group by GROUPS.ID order by CREATION_DATE");
   if (user != null) query.setParameter("userId", user.getId());
   final List<Object[]> list = query.getResultList();
   final Collection<GroupData> result = new ArrayList<GroupData>(list.size());
   for (final Object[] o : list)
     result.add(
         new GroupData(
             ((Number) o[0]).longValue(),
             UserStringImpl.valueOf(String.valueOf(o[1])),
             ((Number) o[2]).longValue(),
             ((Number) o[3]).intValue() != 0,
             ((Number) o[4]).intValue()));
   return result;
 }
 public void deleteMessageFor(final Long id, final User user) {
   final Query query =
       entityManager.createNativeQuery(
           "update MESSAGES SET deleted_by=:user_id where ID=:id and deleted_by IS NULL and receiver_id <> sender_id and (receiver_id=:user_id or sender_id=:user_id)");
   query.setParameter("user_id", user.getId());
   query.setParameter("id", id);
   final int updated = query.executeUpdate();
   if (updated == 0) {
     final Query query2 =
         entityManager.createNativeQuery(
             "delete from MESSAGES where ID=:id and (receiver_id=:user_id or sender_id=:user_id)");
     query2.setParameter("id", id);
     query2.setParameter("user_id", user.getId());
     query2.executeUpdate();
   }
 }
 public void partGroup(final User user, final Long groupId) {
   final Query query =
       entityManager.createNativeQuery(
           "DELETE FROM GROUP_USER WHERE GROUP_ID=:groupId AND USER_ID=:userId");
   query.setParameter("groupId", groupId);
   query.setParameter("userId", user.getId());
   query.executeUpdate();
 }
 private boolean isGroupMember(final User user, final GroupImpl group) {
   final Query query =
       entityManager.createNativeQuery(
           "select 1 from GROUP_USER where USER_ID=:userId AND GROUP_ID=:groupId");
   query.setParameter("userId", user.getId());
   query.setParameter("groupId", group.getId());
   return query.getResultList().size() > 0;
 }
 public void deletePublicMessageFor(final Long messageId, final User user) {
   final Query query =
       entityManager.createNativeQuery(
           "delete from PUBLIC_MESSAGES where ID=:id and sender_id=:user_id");
   query.setParameter("user_id", user.getId());
   query.setParameter("id", messageId);
   query.executeUpdate();
 }
 public void joinGroup(final User user, final Long groupId) {
   final Query query =
       entityManager.createNativeQuery(
           "insert GROUP_USER SET GROUP_ID=:groupId, USER_ID=:userId, JOINING_DATE=:joiningDate, LAST_VISIT=:joiningDate");
   query.setParameter("groupId", groupId);
   query.setParameter("userId", user.getId());
   query.setParameter("joiningDate", new Date());
   query.executeUpdate();
 }
 public void removeWorkoutParticipants(
     final User user, final Long workoutId, final Long... participants)
     throws AccessDeniedException {
   final WorkoutImpl workout = entityManager.find(WorkoutImpl.class, workoutId);
   if (!workout.getUser().equals(user)) throw new AccessDeniedException();
   final Set<Long> participantsWithoutSelf = new HashSet<Long>(Arrays.asList(participants));
   participantsWithoutSelf.remove(user.getId());
   final Query query = createParticipantsDeleteQuery(workoutId, participantsWithoutSelf);
   query.executeUpdate();
 }
 public void createTrack(
     final User user, final String title, final String points, final double length) {
   final Query query =
       entityManager.createNativeQuery(
           "INSERT INTO TRACKS SET OWNER_ID=:userId, TITLE=:title, POINTS=:points, LENGTH=:length");
   query.setParameter("userId", user.getId());
   query.setParameter("title", title);
   query.setParameter("points", points);
   query.setParameter("length", length);
   query.executeUpdate();
 }
 private void updateLastGroupVisit(final User user, final GroupImpl group) {
   if (group != null) {
     final Query query =
         entityManager.createNativeQuery(
             "update GROUP_USER SET LAST_VISIT=:now where GROUP_ID=:groupId and USER_ID=:userId");
     query.setParameter("now", new Date());
     query.setParameter("groupId", group.getId());
     query.setParameter("userId", user.getId());
     query.executeUpdate();
   }
 }
 public void updateMessage(
     final User user, final Long messageId, final String content, final MessageKind kind)
     throws AccessDeniedException {
   final String table = kind == MessageKind.PRIVATE ? "MESSAGES" : "PUBLIC_MESSAGES";
   final Query query =
       entityManager.createNativeQuery(
           "update " + table + " SET CONTENT=:content where ID=:id and SENDER_ID=:userId");
   query.setParameter("content", content);
   query.setParameter("id", messageId);
   query.setParameter("userId", user.getId());
   if (query.executeUpdate() != 1) throw new AccessDeniedException();
 }
 public BibPageData fetchBibPageData(
     final User currentUser,
     final Long targetUserId,
     final int workoutStartIndex,
     final int privateMessagesPageIndex)
     throws UserNotFoundException {
   final User target;
   final PaginatedCollection<PrivateMessage> privateMessages;
   if (currentUser == null) {
     target = fetchUser(targetUserId);
     privateMessages = emptyPage();
   } else {
     target = currentUser.getId().equals(targetUserId) ? currentUser : fetchUser(targetUserId);
     privateMessages =
         fetchPrivateConversation(currentUser, targetUserId, privateMessagesPageIndex);
   }
   final PaginatedCollection<Workout> workouts =
       workoutStore.getWorkouts(target, EMPTY_STRING_LIST, workoutStartIndex, 10);
   return new BibPageData(target, privateMessages, workouts);
 }
 public void updateGroup(
     final User user, final Long groupId, final String name, final String description)
     throws GroupNotFoundException, AccessDeniedException, NameClashException {
   final GroupImpl group = entityManager.find(GroupImpl.class, groupId);
   if (group == null) throw new GroupNotFoundException();
   if (group.getOwner().equals(user)) {
     final Query query =
         entityManager.createNativeQuery(
             "update GROUPS SET NAME=:name, DESCRIPTION=:description where ID=:groupId and OWNER_ID=:userId");
     query.setParameter("name", name);
     query.setParameter("description", description);
     query.setParameter("groupId", groupId);
     query.setParameter("userId", user.getId());
     try {
       query.executeUpdate();
     } catch (EntityExistsException e) {
       throw new NameClashException();
     }
   } else throw new AccessDeniedException();
 }