public int handleFirstTime( Account account, FolderHelper folderHelper, int folderDepth, String messageStoreBucket, Set<String> storedMessageIds, String context, StopWatch watch) throws Exception { int newMessages = 0; Date lastMessageReceivedDate = null; log.info(String.format("handling first mailcheck for %s", context)); Folder folder; while ((folder = folderHelper.next()) != null) { String folderContext = context + " ,folderName=" + folder.getFullName(); IMAPFolder imapFolder = (IMAPFolder) folder; Collection<Integer> messages = getFirstTimeMessages(imapFolder, folderContext); if (messages.size() > 0) { storedMessageIds = new HashSet<String>(); for (Integer messageNumber : messages) { Message message = imapFolder.getMessage(messageNumber); if (lastMessageReceivedDate == null || lastMessageReceivedDate.before(message.getReceivedDate())) { lastMessageReceivedDate = message.getReceivedDate(); } String messageId = getId(account, imapFolder, message); if (processMessage( account, message, messageNumber, messageId, imapFolder, storedMessageIds, messageStoreBucket, folderContext, watch, folderHelper)) { newMessages++; } } } } updateAccount(account, null, 0, folderDepth, lastMessageReceivedDate); return newMessages; }
public Collection<Integer> getFirstTimeMessages(Folder inbox, String context) throws Exception { log.debug(String.format("handling first mailcheck for %s", context)); Collection<Integer> messages = new ArrayList<Integer>(); // we need to get 5 most recent messages in this case if (inbox.getMessageCount() > 0) { if (areMessagesInReverseOrder(inbox)) { log.debug(String.format("order is reverse chronological for %s", context)); for (int i = 0; i < 5; i++) { messages.add(i + 1); } } else { log.debug(String.format("order is chronological for %s", context)); for (int messageNumber = inbox.getMessageCount(); messageNumber > 0 && messageNumber > inbox.getMessageCount() - 5; messageNumber--) { messages.add(messageNumber); } } } return messages; }