001/****************************************************************
002 * Licensed to the Apache Software Foundation (ASF) under one   *
003 * or more contributor license agreements.  See the NOTICE file *
004 * distributed with this work for additional information        *
005 * regarding copyright ownership.  The ASF licenses this file   *
006 * to you under the Apache License, Version 2.0 (the            *
007 * "License"); you may not use this file except in compliance   *
008 * with the License.  You may obtain a copy of the License at   *
009 *                                                              *
010 *   http://www.apache.org/licenses/LICENSE-2.0                 *
011 *                                                              *
012 * Unless required by applicable law or agreed to in writing,   *
013 * software distributed under the License is distributed on an  *
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
015 * KIND, either express or implied.  See the License for the    *
016 * specific language governing permissions and limitations      *
017 * under the License.                                           *
018 ****************************************************************/
019
020package org.apache.james.mailbox.indexer.registrations;
021
022import java.util.List;
023
024import org.apache.james.mailbox.MailboxListener;
025import org.apache.james.mailbox.MessageUid;
026import org.apache.james.mailbox.indexer.events.FlagsMessageEvent;
027import org.apache.james.mailbox.indexer.events.ImpactingMessageEvent;
028import org.apache.james.mailbox.indexer.events.MessageDeletedEvent;
029import org.apache.james.mailbox.model.MailboxPath;
030import org.apache.james.mailbox.model.UpdatedFlags;
031
032import com.google.common.collect.ArrayListMultimap;
033import com.google.common.collect.ImmutableList;
034import com.google.common.collect.Multimap;
035import com.google.common.collect.Multimaps;
036
037public class MailboxRegistration implements MailboxListener {
038
039    private final Multimap<MessageUid, ImpactingMessageEvent> impactingMessageEvents;
040    private final MailboxPath mailboxPath;
041
042    public MailboxRegistration(MailboxPath mailboxPath) {
043        this.impactingMessageEvents = Multimaps.synchronizedMultimap(ArrayListMultimap.<MessageUid, ImpactingMessageEvent>create());
044        this.mailboxPath = mailboxPath;
045    }
046
047    @Override
048    public ListenerType getType() {
049        return ListenerType.MAILBOX;
050    }
051
052    @Override
053    public ExecutionMode getExecutionMode() {
054        return ExecutionMode.SYNCHRONOUS;
055    }
056
057    public List<ImpactingMessageEvent> getImpactingEvents(MessageUid uid) {
058        return ImmutableList.copyOf(impactingMessageEvents.get(uid));
059    }
060
061    @Override
062    public void event(Event event) {
063        if (event instanceof FlagsUpdated) {
064            for (UpdatedFlags updatedFlags : ((FlagsUpdated) event).getUpdatedFlags()) {
065                impactingMessageEvents.put(updatedFlags.getUid(), new FlagsMessageEvent(mailboxPath, updatedFlags.getUid(), updatedFlags.getNewFlags()));
066            }
067        } else if (event instanceof Expunged) {
068            for (MessageUid uid: ((Expunged) event).getUids()) {
069                impactingMessageEvents.put(uid, new MessageDeletedEvent(mailboxPath, uid));
070            }
071        }
072    }
073
074}