001    /*
002     * Copyright 2010 the original author or authors.
003     * 
004     *  Licensed under the Apache License, Version 2.0 (the "License");
005     *  you may not use this file except in compliance with the License.
006     *  You may obtain a copy of the License at
007     *
008     *      http://www.apache.org/licenses/LICENSE-2.0
009     *
010     *  Unless required by applicable law or agreed to in writing, software
011     *  distributed under the License is distributed on an "AS IS" BASIS,
012     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     *  See the License for the specific language governing permissions and
014     *  limitations under the License.
015     */
016    package com.hs.mail.imap.event;
017    
018    import java.util.Collection;
019    import java.util.List;
020    import java.util.Set;
021    import java.util.TreeSet;
022    
023    import org.apache.commons.collections.CollectionUtils;
024    
025    import com.hs.mail.imap.dao.DaoFactory;
026    
027    
028    /**
029     * 
030     * @author Won Chul Doh
031     * @since Jul 30, 2010
032     *
033     */
034    public class EventTracker implements EventListener {
035    
036            private boolean deletedMailbox = false;
037        private boolean sizeChanged = false;
038        private Set<Long> expungedUids;
039        private Set<Long> flagUpdatedUids;
040        private List<Long> cachedUids;
041    
042            public boolean isDeletedMailbox() {
043                    return deletedMailbox;
044            }
045    
046            public boolean isSizeChanged() {
047                    return sizeChanged;
048            }
049    
050            public boolean hasExpungedUids() {
051                    return CollectionUtils.isNotEmpty(expungedUids);
052            }
053            
054            public Set<Long> getExpungedUids() {
055                    return expungedUids;
056            }
057            
058            public boolean hasUpdatedFlags() {
059                    return CollectionUtils.isNotEmpty(flagUpdatedUids);
060            }
061            
062            public Set<Long> getFlagUpdatedUids() {
063                    return flagUpdatedUids;
064            }
065    
066            public List<Long> getCachedUids() {
067                    return cachedUids;
068            }
069    
070            public void event(Event event) {
071                    if (event instanceof MessageAddedEvent) {
072                            reset();
073                            sizeChanged = true;
074                    } else if (event instanceof MessageExpungedEvent) {
075                            expunged((MessageExpungedEvent) event);
076                    } else if (event instanceof FlagUpdatedEvent) {
077                            flagsUpdated((FlagUpdatedEvent) event);
078                    } else if (event instanceof MailboxDeletedEvent) {
079                            deletedMailbox = true;
080                    }
081            }
082            
083            private void expunged(MessageExpungedEvent event) {
084                    List<Long> uids = event.getExpungedUids();
085                    if (expungedUids == null) {
086                            expungedUids = new TreeSet<Long>(uids);
087                            cachedUids = DaoFactory.getMessageDao().getMessageIDList(
088                                            event.getMailboxID());
089                    } else {
090                            expungedUids.addAll(uids);
091                    }
092            }
093            
094            private void flagsUpdated(FlagUpdatedEvent event) {
095                    List<Long> uids = event.getFlagUpdatedUids();
096                    if (flagUpdatedUids == null) {
097                            flagUpdatedUids = new TreeSet<Long>(uids);
098                    } else {
099                            flagUpdatedUids.addAll(uids);
100                    }
101            }
102            
103            private void clear(Collection<Long> uids) {
104                    if (uids != null) {
105                            uids.clear();
106                    }
107            }
108            
109            public void reset() {
110                    sizeChanged = false;
111                    clear(expungedUids);
112                    clear(flagUpdatedUids);
113                    clear(cachedUids);
114                    expungedUids = null;
115            }
116    
117    }