public Group fetchGroupForUpdate(final User user, final Long groupId)
     throws GroupNotFoundException, AccessDeniedException {
   final GroupImpl group = entityManager.find(GroupImpl.class, groupId);
   if (group == null) throw new GroupNotFoundException();
   if (group.getOwner().equals(user)) return group;
   throw new AccessDeniedException();
 }
 public GroupPageData fetchGroupPageData(
     final User user,
     final Long groupId,
     final int messageStartIndex,
     final int workoutStartIndex,
     final String discipline) {
   final Collection<GroupData> result = fetchGroupDataForUser(user, false);
   final GroupImpl group;
   final WorkoutsData<DisciplineData.Count> workoutsData;
   final PaginatedCollection<User> users;
   final boolean member;
   if (groupId != null) {
     group = entityManager.find(GroupImpl.class, groupId);
     workoutsData = fetchStatisticsData(group, workoutStartIndex, discipline);
     users = fetchGroupMembers(group);
     member = user != null && isGroupMember(user, group);
   } else {
     workoutsData = null;
     group = null;
     users = emptyPage();
     member = false;
   }
   final PaginatedCollection<PublicMessage> messagePaginatedCollection =
       fetchPublicMessages(Topic.Kind.GROUP, groupId, 10, messageStartIndex);
   if (user != null && group != null) {
     updateLastGroupVisit(user, group);
   }
   return new GroupPageData(
       group, member, result, messagePaginatedCollection, workoutsData, users);
 }
 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 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 PublicMessage createPublicMessage(
     final User sender,
     final String content,
     final Date date,
     final Long topicId,
     final Topic.Kind topicKind)
     throws WorkoutNotFoundException {
   final PublicMessageImpl message;
   if (topicKind == Topic.Kind.WORKOUT) {
     final Workout workout = workoutStore.fetchWorkout(topicId);
     message = new PublicMessageImpl(sender, date, content, workout);
   } else {
     final GroupImpl group = entityManager.find(GroupImpl.class, topicId);
     message = new PublicMessageImpl(sender, date, content, group);
   }
   entityManager.persist(message);
   return message;
 }
 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();
 }
 public TrackImpl fetchTrack(final Long id) throws TrackNotFoundException {
   final TrackImpl track = entityManager.find(TrackImpl.class, id);
   if (track == null) throw new TrackNotFoundException();
   return track;
 }