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 com.hs.mail.imap.mailbox.Mailbox;
022    import com.hs.mail.imap.mailbox.MailboxManager;
023    import com.hs.mail.imap.mailbox.MailboxQuery;
024    
025    /**
026     * 
027     * @author Won Chul Doh
028     * @since Feb 1, 2010
029     *
030     */
031    public class LsubProcessor extends AbstractListProcessor {
032    
033            @Override
034            protected List<Mailbox> listMailbox(long userID, long ownerID,
035                            String mailboxName, MailboxQuery query) {
036                    MailboxManager manager = getMailboxManager();
037                    List<Mailbox> children = manager.getChildren(userID, ownerID,
038                                    mailboxName, true);
039                    List<Mailbox> results = new ArrayList<Mailbox>();
040                    for (Mailbox child : children) {
041                            addSubscription(manager, children, query, child, false, results);
042                    }
043                    return results;
044            }
045    
046            @Override
047            protected Mailbox getMailbox(long ownerID, String mailboxName) {
048                    MailboxManager manager = getMailboxManager();
049                    Mailbox result = manager.getMailbox(ownerID, mailboxName);
050                    if (result != null
051                                    && manager.isSubscribed(ownerID, result.getName())) {
052                            result.setHasChildren(manager.hasChildren(result));
053                    }
054                    return result;
055            }
056            
057            private void addSubscription(MailboxManager manager,
058                            List<Mailbox> subscriptions, MailboxQuery query, Mailbox mailbox,
059                            boolean noSelect, List<Mailbox> results) {
060                    if (query.match(mailbox.getName())) {
061                            if (!results.contains(mailbox)) {
062                                    mailbox.setNoSelect(noSelect);
063                                    mailbox.setHasChildren(manager.hasChildren(mailbox));
064                                    results.add(mailbox);
065                            }
066                    } else {
067                            String parentName = Mailbox.getParent(mailbox.getName());
068                            if (!"".equals(parentName) && !results.contains(parentName)) {
069                                    addSubscription(manager, subscriptions, query, new Mailbox(
070                                                    parentName), true, results);
071                            }
072                    }
073            }
074    
075    }