private void separatePrivateMessagesFromWorkout(final Long id) {
   final Query query =
       entityManager.createNativeQuery(
           "UPDATE MESSAGES SET WORKOUT_ID=NULL WHERE WORKOUT_ID=:id AND RECEIVER_ID IS NOT NULL");
   query.setParameter("id", id);
   query.executeUpdate();
 }
 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();
 }
 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 addWorkoutParticipants(
     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> participantsSet = new HashSet<Long>(Arrays.asList(participants));
   final Query query = createParticipantsInsertUnionQuery(workoutId, participantsSet);
   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 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();
 }
 private void deletePublicMessageAboutWorkout(final Long id) {
   final Query query =
       entityManager.createNativeQuery("DELETE FROM PUBLIC_MESSAGES WHERE WORKOUT_ID=:id");
   query.setParameter("id", id);
   query.executeUpdate();
 }