public void facebookConnect(final User user, final Long facebookId) throws NameClashException { final UserImpl user1 = (UserImpl) user; try { user1.setFacebookId(facebookId); entityManager.merge(user); entityManager.flush(); } catch (EntityExistsException e) { user1.setFacebookId(null); throw new NameClashException(); } }
public void forgotPassword(final String email) throws UserNotFoundException, MailException { final Query query = entityManager.createQuery("select u from UserImpl u where u.email=:cryptedMail"); query.setParameter("cryptedMail", CipherHelper.cipher(email)); final List<UserImpl> list = query.getResultList(); if (list.isEmpty()) throw new UserNotFoundException(); for (final UserImpl user : list) { final String password = Helper.randomstring(); user.setPassword(password); MailSender.forgotPasswordMail(user.getName(), password, email); entityManager.merge(user); } }
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 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); }
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 updateTrack(final User user, final long id, final String title, final String points) throws TrackNotFoundException, AccessDeniedException { final TrackImpl track = fetchTrackForUpdate(user, id); track.setTitle(title); track.setPoints(points); entityManager.merge(track); }
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 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 void deleteWorkout(final Long id, final User user) throws WorkoutNotFoundException, AccessDeniedException { final Workout workout = fetchWorkout(id, user, true); separatePrivateMessagesFromWorkout(id); deletePublicMessageAboutWorkout(id); entityManager.remove(workout); }
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 updateBib( final User user, final String town, final String description, final String webSite) { final UserImpl userImpl = (UserImpl) user; userImpl.setTown(town); userImpl.setDescription(description); userImpl.setWebSite(webSite); entityManager.merge(user); }
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 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 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 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 List<User> fetchUsersBeginningByAndAddableToWorkout(final String prefix, final long id) { final Query query = entityManager.createQuery( "select u from UserImpl u, WorkoutImpl workout where u.name<>'googlebot' AND u.name LIKE CONCAT(:prefix, '%')" + " AND u not MEMBER OF workout.participants AND workout.id = :id "); query.setParameter("prefix", prefix); query.setParameter("id", id); return query.getResultList(); }
private Query createParticipantsDeleteQuery(final Long workoutId, final Set<Long> participants) { final SQLHelper.Predicate idPred = createInListPredicate("USER_ID", participants, "participant"); final Query query = entityManager.createNativeQuery( "delete FROM WORKOUT_USER WHERE WORKOUT_ID = :workoutId AND " + idPred); query.setParameter("workoutId", workoutId); idPred.bindVariables(query); return query; }
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 User createFacebookUser(final String login, final Long facebookId) throws NameClashException { try { final UserImpl user = new UserImpl(login, facebookId); entityManager.persist(user); return user; } catch (EntityExistsException e) { throw new NameClashException(); } }
public void updateNikePlusData( final User user, final String nikePlusEmail, final String nikePlusPassword, final String nikePlusId) { final UserImpl userImpl = (UserImpl) user; userImpl.setNikePluEmail(nikePlusEmail); userImpl.setNikePlusPassword(nikePlusPassword); userImpl.setNikePlusId(nikePlusId); entityManager.merge(userImpl); }
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 Group createGroup(final User user, final String name, final String description) throws NameClashException { final GroupImpl group = new GroupImpl(name, user, description, new Date()); try { entityManager.persist(group); joinGroup(user, group.getId()); return group; } catch (EntityExistsException e) { throw new NameClashException(); } }
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 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(); }
public User createUser(final String login, final String password, final String email) throws NameClashException, MailException { try { final UserImpl user = new UserImpl(login, password); if (email != null) user.setEmail(email); entityManager.persist(user); if (email != null) MailSender.sendSignupMail(login, password, email); return user; } catch (EntityExistsException e) { throw new NameClashException(); } }
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 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 Query createParticipantsInsertUnionQuery( final Long workoutId, final Set<Long> participants) { final SQLHelper.Predicate listPred = createInListPredicate("ID", participants, "participant"); final Query query = entityManager.createNativeQuery( "insert INTO WORKOUT_USER (USER_ID, WORKOUT_ID) SELECT ID, :workoutId FROM USERS WHERE" + " ID not in (select USER_ID from WORKOUT_USER where WORKOUT_ID = :workoutId) and " + listPred); query.setParameter("workoutId", workoutId); listPred.bindVariables(query); return query; }
private PrivateMessage createMessage( final User sender, final User receiver, final String content, final Date date, final Long workoutId) throws WorkoutNotFoundException { final Workout workout = workoutId != null ? workoutStore.fetchWorkout(workoutId) : null; final PrivateMessageImpl message = new PrivateMessageImpl(sender, receiver, date, content, workout); entityManager.persist(message); return message; }