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(); }
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(); }
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 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(); } }
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; }
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(); }