private Collection<ConversationSummary> fetchCorrespondents(final User user) { final Map<String, ConversationSummary> correspondants = new HashMap<String, ConversationSummary>(); { final Query query = query( "select m.sender.name, m.receiver.name, count(m), m.read from PrivateMessageImpl m where (m.receiver=:user OR " + "(m.sender=:user)) AND(m.deleter IS NULL OR m.deleter <> :user) " + "group by m.sender.name, m.receiver.name, m.read"); query.setParameter("user", user); for (final Object[] row : (List<Object[]>) query.getResultList()) { final boolean sent = row[0].equals(((UserImpl) user).getBareName()); final String name = (String) (sent ? row[1] : row[0]); final ConversationSummary previous = correspondants.get(name); final long count = ((Number) row[2]).longValue(); final long newCount; if (row[3].equals(Boolean.FALSE) && !sent) newCount = count; else newCount = 0; if (previous != null) { correspondants.put( name, new ConversationSummary( UserStringImpl.valueOf(name), count + previous.messageCount, newCount + previous.newMessageCount)); } else correspondants.put( name, new ConversationSummary(UserStringImpl.valueOf(name), count, newCount)); } } return new TreeSet<ConversationSummary>(correspondants.values()); }
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 ConversationData fetchConvertationData( final User sender, final String receiver, final Long aboutWorkoutId, final int startIndex) throws WorkoutNotFoundException { final Workout aboutWorkout = aboutWorkoutId == null ? null : workoutStore.fetchWorkout(aboutWorkoutId); final ConversationData conversationData = new ConversationData( fetchConversation(sender, receiver, startIndex), aboutWorkout, UserStringImpl.valueOf(receiver)); for (final PrivateMessage privateMessage : conversationData.privateMessages) { final PrivateMessageImpl message1 = (PrivateMessageImpl) privateMessage; if (!message1.isRead() && message1.getReceiver().equals(sender)) { message1.setRead(true); message1.setNew(true); entityManager.merge(privateMessage); } } return conversationData; }