Show
Ignore:
Timestamp:
08/01/08 10:18:16 PM (5 months ago)
Author:
octorian
Message:

Mailbox node serialization

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/LogicMail/src/org/logicprobe/LogicMail/model/AccountNode.java

    r256 r259  
    3131package org.logicprobe.LogicMail.model; 
    3232 
     33import java.util.Enumeration; 
    3334import java.util.Hashtable; 
    3435import java.util.Vector; 
     
    5152import org.logicprobe.LogicMail.message.Message; 
    5253import org.logicprobe.LogicMail.message.MessageFlags; 
     54import org.logicprobe.LogicMail.util.DataStore; 
     55import org.logicprobe.LogicMail.util.DataStoreFactory; 
    5356import org.logicprobe.LogicMail.util.EventListenerList; 
    5457 
     
    7477        private Vector outboundNewMessages = new Vector(); 
    7578        private Hashtable outboundMessageReplies = new Hashtable();  
    76          
     79        private DataStore accountDataStore; 
     80 
    7781        public final static int STATUS_LOCAL   = 0; 
    7882        public final static int STATUS_OFFLINE = 1; 
     
    132136                        // Create the fake INBOX node for non-folder-capable mail stores 
    133137                        this.rootMailbox = new MailboxNode(new FolderTreeItem("", "", ""), -1); 
     138                        this.rootMailbox.setParentAccount(this); 
    134139                        MailboxNode inboxNode = new MailboxNode(new FolderTreeItem("INBOX", "INBOX", ""), MailboxNode.TYPE_INBOX); 
    135140                        inboxNode.setParentAccount(this); 
     
    137142                        pathMailboxMap.put("INBOX", inboxNode); 
    138143                } 
     144                 
     145                // Load any saved tree data 
     146                load(); 
    139147        } 
    140148         
     
    333341         */ 
    334342        public void refreshMailboxStatus() { 
    335                 MailboxNode mailbox; 
    336                 synchronized(rootMailboxLock) { 
    337                         mailbox = this.rootMailbox; 
    338                 } 
    339                 mailStore.requestFolderStatus(mailbox.getFolderTreeItem()); 
     343                int size = pathMailboxMap.size(); 
     344                FolderTreeItem[] folders = new FolderTreeItem[size]; 
     345                Enumeration e = pathMailboxMap.keys(); 
     346                for(int i=0; i<size; i++) { 
     347                        folders[i] = ((MailboxNode)pathMailboxMap.get(e.nextElement())).getFolderTreeItem(); 
     348                } 
     349                mailStore.requestFolderStatus(folders); 
    340350        } 
    341351 
     
    408418                } 
    409419                 
     420                save(); 
    410421                fireAccountStatusChanged(AccountNodeEvent.TYPE_MAILBOX_TREE); 
    411422        } 
     
    431442                } 
    432443        } 
    433          
     444 
    434445        private void populateMailboxNodes(FolderTreeItem folderTreeItem, MailboxNode currentMailbox, Hashtable remainingMailboxMap) { 
    435446                pathMailboxMap.put(folderTreeItem.getPath(), currentMailbox); 
     
    681692        } 
    682693    } 
     694     
     695    /** 
     696     * Saves the mailbox tree to persistent storage. 
     697     */ 
     698        private void save() { 
     699                if(accountConfig == null) { 
     700                        return; 
     701                } 
     702                if(accountDataStore == null) { 
     703                        long accountId = accountConfig.getUniqueId(); 
     704                        accountDataStore = DataStoreFactory.getConnectionCacheStore(accountId); 
     705                        accountDataStore.load(); 
     706                } 
     707                 
     708                accountDataStore.putNamedObject("ROOT_MAILBOX", rootMailbox); 
     709                accountDataStore.save(); 
     710        } 
     711         
     712        /** 
     713         * Loads the mailbox tree from persistent storage. 
     714         */ 
     715        private void load() { 
     716                if(accountConfig == null) { 
     717                        return; 
     718                } 
     719                if(accountDataStore == null) { 
     720                        long accountId = accountConfig.getUniqueId(); 
     721                        accountDataStore = DataStoreFactory.getConnectionCacheStore(accountId); 
     722                        accountDataStore.load(); 
     723                } 
     724 
     725                Object loadedObject = accountDataStore.getNamedObject("ROOT_MAILBOX"); 
     726                if(loadedObject instanceof MailboxNode) { 
     727                        synchronized(rootMailboxLock) { 
     728                                this.rootMailbox = (MailboxNode)loadedObject; 
     729                                this.rootMailbox.setParentAccount(this); 
     730                                prepareDeserializedMailboxNode(rootMailbox); 
     731                        } 
     732                } 
     733        } 
     734         
     735        //TODO: Handle deleted account nodes 
     736         
     737        /** 
     738         * Traverses the deserialized mailbox nodes, populates any necessary 
     739         * data structures in the account node, and sets the mailbox parent 
     740         * account references.  
     741         * 
     742         * @param mailboxNode The mailbox node. 
     743         */ 
     744        private void prepareDeserializedMailboxNode(MailboxNode mailboxNode) { 
     745                mailboxNode.setParentAccount(this); 
     746                FolderTreeItem item = mailboxNode.getFolderTreeItem(); 
     747                if(item != null && item.getPath().length() > 0) { 
     748                        this.pathMailboxMap.put(item.getPath(), mailboxNode); 
     749                } 
     750                MailboxNode[] children = mailboxNode.getMailboxes(); 
     751                for(int i=0; i<children.length; i++) { 
     752                        prepareDeserializedMailboxNode(children[i]); 
     753                } 
     754        } 
    683755}