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; }
public void addSourceToUser(String userId, Source source) throws Exception { try { saveObject(source); } catch (DuplicateMySocioObjectException e) { // Cool!!! we already have this source no need to save it, let's see if user already has it. UserTags userTags = getUserTags(userId); if (userTags.getTag(source.getUrl()) != null) { logger.debug("User trying to add existing source."); return; } } source.createProcessor(userId); }
@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); } } }
public SocioUser createUser(Account account, Locale locale) throws Exception { String userId; String userName = account.getUserName(); logger.debug("Creating user" + userName + " for " + account.getAccountType()); SocioUser user = new SocioUser(); user.setName(userName); user.setLocale(locale.getLanguage()); saveObject(user); userId = user.getId().toString(); account.setUser(user); saveObject(account); user.setMainAccount(account); saveObject(user); UserTags userTags = new UserTags(); userTags.setUserId(user.getId().toString()); addAccountToUser(account, userId, userTags); TempProcessor processor = new TempProcessor(); processor.setUserId(userId); processor.setCreationDate(System.currentTimeMillis()); saveObject(processor); logger.debug("User created"); return user; }