* something relevant is received
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
public void event(Event event) {
final MailboxSession session = event.getSession();
try {
if (event instanceof MessageEvent) {
if (event instanceof AddedImpl) {
AddedImpl added = (AddedImpl) event;
final Mailbox<Id> mailbox = added.getMailbox();
Iterator<Long> uids = added.getUids().iterator();
while (uids.hasNext()) {
long next = uids.next();
Iterator<Message<Id>> messages = factory.getMessageMapper(session).findInMailbox(mailbox, MessageRange.one(next), FetchType.Full, -1);
while(messages.hasNext()) {
Message<Id> message = messages.next();
try {
add(session, mailbox, message);
} catch (MailboxException e) {
session.getLog().debug("Unable to index message " + message.getUid() + " for mailbox " + mailbox, e);
}
}
}
} else if (event instanceof ExpungedImpl) {
ExpungedImpl expunged = (ExpungedImpl) event;
final Mailbox<Id> mailbox = expunged.getMailbox();
List<Long> uids = expunged.getUids();
List<MessageRange> ranges = MessageRange.toRanges(uids);
for (int i = 0; i < ranges.size(); i++) {
MessageRange range = ranges.get(i);
try {
delete(session, mailbox, range);
} catch (MailboxException e) {
session.getLog().debug("Unable to deleted range " + range.toString() + " from index for mailbox " + mailbox, e);
}
}
} else if (event instanceof FlagsUpdatedImpl) {
FlagsUpdatedImpl flagsUpdated = (FlagsUpdatedImpl) event;
final Mailbox<Id> mailbox = flagsUpdated.getMailbox();
Iterator<UpdatedFlags> flags = flagsUpdated.getUpdatedFlags().iterator();
while(flags.hasNext()) {
UpdatedFlags uFlags = flags.next();
try {
update(session, mailbox, MessageRange.one(uFlags.getUid()), uFlags.getNewFlags());
} catch (MailboxException e) {
session.getLog().debug("Unable to update flags for message " + uFlags.getUid() + " in index for mailbox " + mailbox, e);
}
}
}
} else if (event instanceof MailboxDeletionImpl) {
// delete all indexed messages for the mailbox
delete(session, ((MailboxDeletionImpl) event).getMailbox(), MessageRange.all());
}
} catch (MailboxException e) {
session.getLog().debug("Unable to update index", e);
}
}