public List<GeneralMessage> getUnreadMessages(String userId, String tagId, UserTags tags) { Query<UnreaddenMessage> q = ds.createQuery(UnreaddenMessage.class).field("userId").equal(userId.toString()); String order = tags.getOrder(); if (!tagId.equals(UserTags.ALL_TAGS)) { SocioTag tag = tags.getTag(tagId); order = tag.getOrder(); List<String> tagsIds = new ArrayList<String>(); List<SocioTag> leaves = tag.getLeaves(); for (SocioTag leaf : leaves) { tagsIds.add(leaf.getUniqueId()); } q.field("tagId").hasAnyOf(tagsIds); } if (order.equals(SocioTag.ASCENDING_ORDER)) { q.order("-messageDate"); } else { q.order("messageDate"); } q.limit(tags.getRange()); List<GeneralMessage> messagesList = new ArrayList<GeneralMessage>(); Iterable<UnreaddenMessage> messages = q.fetch(); for (UnreaddenMessage unreaddenMessage : messages) { messagesList.add(unreaddenMessage.getMessage()); } tags.setSelectedTag(tagId); ds.save(tags); return messagesList; }
@Override public void setMessagesReadden(String userId, String tagId, UserTags tags) { Query<UnreaddenMessage> q = ds.createQuery(UnreaddenMessage.class).field("userId").equal(userId); if (!tagId.equals(UserTags.ALL_TAGS)) { SocioTag tag = tags.getTag(tagId); List<String> tagsIds = new ArrayList<String>(); List<SocioTag> leaves = tag.getLeaves(); for (SocioTag leaf : leaves) { tagsIds.add(leaf.getUniqueId()); } q.field("tagId").hasAnyOf(tagsIds); } List<UnreaddenMessage> unreadMessages = q.asList(); List<GeneralMessage> messages = new ArrayList<GeneralMessage>(); List<ReaddenMessage> readdenMessages = new ArrayList<ReaddenMessage>(); for (UnreaddenMessage unreaddenMessage : unreadMessages) { GeneralMessage message = unreaddenMessage.getMessage(); messages.add(message); ReaddenMessage readdenMessage = new ReaddenMessage(); readdenMessage.setUserId(userId); readdenMessage.setMessageUniqueId(message.getUniqueFieldValue().toString()); readdenMessages.add(readdenMessage); } ds.save(readdenMessages); ds.delete(q); for (GeneralMessage message : messages) { // TODO Next lines are just to save space in unpaid mongoDB on CloudBees Query<UnreaddenMessage> isMore = ds.createQuery(UnreaddenMessage.class).field("message").equal(message); if (isMore.countAll() <= 0) { ds.delete(message); } } }