View Javadoc

1   /*
2    * Copyright (c) 2005-2007 Creative Sphere Limited.
3    * All rights reserved. This program and the accompanying materials
4    * are made available under the terms of the Eclipse Public License v1.0
5    * which accompanies this distribution, and is available at
6    * http://www.eclipse.org/legal/epl-v10.html
7    *
8    * Contributors:
9    *
10   *   Creative Sphere - initial API and implementation
11   *
12   */
13  package org.abstracthorizon.mercury.maildir.uid;
14  
15  import java.util.ArrayList;
16  import java.util.HashMap;
17  import java.util.Map;
18  
19  import javax.mail.Message;
20  import javax.mail.MessagingException;
21  import javax.mail.UIDFolder;
22  import javax.mail.internet.MimeMessage;
23  
24  import org.abstracthorizon.mercury.maildir.MaildirFolder;
25  import org.abstracthorizon.mercury.maildir.MaildirFolderData;
26  import org.abstracthorizon.mercury.maildir.util.MessageWrapper;
27  
28  
29  /**
30   * This class implements UID maildir folder.
31   *
32   * @author Daniel Sendula
33   */
34  public class UIDMaildirFolder extends MaildirFolder implements UIDFolder {
35  
36      /** Map that maps uids to messages */
37      protected Map<UID, UIDMessageWrapper> uids;
38  
39      /**
40       * Constructor.
41       * @param store store
42       * @param folderData folder data
43       */
44      protected UIDMaildirFolder(UIDMaildirStore store, MaildirFolderData folderData) {
45          super(store, folderData);
46      }
47  
48      /**
49       * This implementation creates uids map and calls super class' open method.
50       * @param mode mode
51       * @throws MessagingException
52       */
53      public void open(int mode) throws MessagingException {
54          uids = new HashMap<UID, UIDMessageWrapper>();
55          super.open(mode);
56      }
57  
58      /**
59       * This implementation releases uids map and calls superclass' close method.
60       * @param expunge
61       * @throws MessagingException
62       */
63      public void close(boolean expunge) throws MessagingException {
64          super.close(expunge);
65          uids = null;
66      }
67  
68      /**
69       * Appends messages to this folder.
70       * @param messages messages to be appended.
71       * @throws MessagingException
72       */
73      public void appendMessages(Message[] messages) throws MessagingException {
74          // TODO optimise this by allocating more UID at once... and then adding them to messages...
75          super.appendMessages(messages);
76      }
77  
78      /**
79       * Adds message to folder's internal storage. This method wraps message as well.
80       * @param msg folder data message
81       * @param num message number
82       * @return wrapped message
83       * @throws MessagingException
84       */
85      protected MessageWrapper addMessage(MimeMessage msg, int num) throws MessagingException {
86          UIDMessageWrapper wrapper = new UIDMessageWrapper(this, (UIDMaildirMessage)msg, num);
87          UID uid = wrapper.getUID();
88          // if (uid.getUID() > maxUid) {
89          //     maxUid = uid.getUID();
90          // }
91          uids.put(uid, wrapper);
92          map.put(msg, wrapper);
93          return wrapper;
94      }
95  
96      /**
97       * This medhod removes message. Argument could be folder's message or folder data's message.
98       * @param msg message to be removed
99       * @return wrapped message that is removed
100      * @throws MessagingException
101      */
102     protected MessageWrapper removeMessage(MimeMessage msg) throws MessagingException {
103         UIDMessageWrapper wrapper = (UIDMessageWrapper)super.removeMessage(msg);
104         if (wrapper != null) {
105             uids.remove(wrapper.getUID());
106         }
107         return wrapper;
108     }
109 
110     /**
111      * This method obtains message by given uid number.
112      * @param uid uid number
113      * @return message
114      * @throws MessagingException
115      */
116     public Message getMessageByUID(long uid) throws MessagingException {
117         if (!opened) {
118             throw new IllegalStateException("Folder is not opened; "+getFullName());
119         }
120         UID uidx = new UID(uid);
121         return (Message)uids.get(uidx);
122     }
123 
124     /**
125      * This method obtains message by given uid numbers.
126      * @param uids uid numbers array
127      * @return messages
128      * @throws MessagingException
129      */
130     public Message[] getMessagesByUID(long[] uids) throws MessagingException {
131         if (!opened) {
132             throw new IllegalStateException("Folder is not opened; "+getFullName());
133         }
134         long maxUid = ((UIDMaildirFolderData)folderData).getMaxUID();
135         ArrayList<Message> list = new ArrayList<Message>();
136         for (int i = 0; i < uids.length; i++) {
137             if (uids[i] <= maxUid) {
138                 Message msg = getMessageByUID(uids[i]);
139                 if (msg != null) {
140                     list.add(msg);
141                 }
142             }
143         }
144         Message[] res = new Message[list.size()];
145         return (Message[])list.toArray(res);
146     }
147 
148     /**
149      * This method obtains message from given uid range.
150      * @param start start uid
151      * @param end end uid
152      * @return messages
153      * @throws MessagingException
154      */
155     public Message[] getMessagesByUID(long start, long end) throws MessagingException {
156         if (!opened) {
157             throw new IllegalStateException("Folder is not opened; "+getFullName());
158         }
159         long maxUid = ((UIDMaildirFolderData)folderData).getMaxUID();
160         if ((maxUid < 0) && (messages != null) && (messages.size() > 0)) {
161             UIDMessage uid = (UIDMessage)messages.get(messages.size()-1);
162             maxUid = uid.getUID().getUID();
163         }
164         if (end > maxUid) {
165             end = maxUid;
166         }
167         ArrayList<Message> list = new ArrayList<Message>();
168         for (long i = start; i <= end; i++) {
169             Message msg = getMessageByUID(i);
170             if (msg != null) {
171                 list.add(msg);
172             }
173         }
174         Message[] res = new Message[list.size()];
175         return (Message[])list.toArray(res);
176     }
177 
178     /**
179      * Thid method obtains uid from the given message
180      * @param message message
181      * @return uid
182      * @throws MessagingException
183      */
184     public long getUID(Message message) throws MessagingException {
185         if (!opened) {
186             throw new IllegalStateException("Folder is not opened; "+getFullName());
187         }
188         UIDMessage msg = (UIDMessage)message;
189         return msg.getUID().getUID();
190     }
191 
192     /**
193      * Returns UID validity for the folder
194      * @return UID validity for the folder
195      */
196     public long getUIDValidity() {
197         return ((UIDMaildirFolderData)folderData).getUIDValidity();
198     }
199 }
200 
201