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.processor;
017    
018    import java.util.ArrayList;
019    import java.util.List;
020    
021    import org.jboss.netty.channel.Channel;
022    
023    import com.hs.mail.imap.ImapSession;
024    import com.hs.mail.imap.mailbox.MailboxManager;
025    import com.hs.mail.imap.mailbox.SelectedMailbox;
026    import com.hs.mail.imap.mailbox.UidToMsnMapper;
027    import com.hs.mail.imap.message.SequenceRange;
028    import com.hs.mail.imap.message.request.ImapRequest;
029    import com.hs.mail.imap.message.request.StoreRequest;
030    import com.hs.mail.imap.message.responder.Responder;
031    import com.hs.mail.imap.message.responder.StoreResponder;
032    import com.hs.mail.imap.message.response.StoreResponse;
033    
034    /**
035     * 
036     * @author Won Chul Doh
037     * @since Feb 1, 2010
038     *
039     */
040    public class StoreProcessor extends AbstractImapProcessor {
041    
042            @Override
043            protected void doProcess(ImapSession session, ImapRequest message,
044                            Responder responder) {
045                    doProcess(session, (StoreRequest) message, (StoreResponder) responder);
046            }
047            
048            protected void doProcess(ImapSession session, StoreRequest request,
049                            StoreResponder responder) {
050                    MailboxManager manager = getMailboxManager();
051                    SelectedMailbox selected = session.getSelectedMailbox();
052                    UidToMsnMapper map = new UidToMsnMapper(selected, request.isUseUID());
053                    SequenceRange[] sequenceSet = request.getSequenceSet();
054                    List<Long> flagUpdatedUids = new ArrayList<Long>();
055                    for (int i = 0; i < sequenceSet.length; i++) {
056                            long min = map.getMinMessageNumber(sequenceSet[i].getMin());
057                            long max = map.getMaxMessageNumber(sequenceSet[i].getMax());
058                            for (long j = min; j <= max && j >= 0; j++) {
059                                    long uid = map.getUID((int) j);
060                                    if (uid != -1) {
061                                            manager.setFlags(uid, request.getFlags(), request
062                                                            .isReplace(), request.isPlus());
063                                            flagUpdatedUids.add(new Long(uid));
064                                            if (!request.isSilent()) {
065                                                    responder.respond(new StoreResponse(j, manager
066                                                                    .getFlags(uid)));
067                                            }
068                                    } else {
069                                            break; // Out of index
070                                    }
071                            }
072                    }
073                    flagsUpdated(session, flagUpdatedUids);
074                    responder.okCompleted(request);
075            }
076            
077            private void flagsUpdated(ImapSession session, List<Long> flagUpdatedUids) {
078                    getMailboxManager().getEventDispatcher().flagsUpdated(
079                                    session.getSessionID(),
080                                    session.getSelectedMailbox().getMailboxID(), flagUpdatedUids);
081            }
082    
083            @Override
084            protected Responder createResponder(Channel channel, ImapRequest request) {
085                    return new StoreResponder(channel, request);
086            }
087            
088    }