ArrayList problemFolders = new ArrayList();
// process each folder in the "folders" array, and *possibly* the List "reprocessFolders"
for (int i=0,k=0; (i < folders.length) || (k != 0) ; i++) {
// get the folder for this iteration...
IMAPFolder currentFolder = null;
if ((k != 0) && (i >= folders.length)) {
// in this case, we have finished processing the "folders" array,
// and we are now processing the "reprocessFolders" List. Get the
// currentFolder from the IMAP server ("store").
k--;
String newFolderPath = (String)reprocessFolders.get(k);
currentFolder = (IMAPFolder)store.getFolder(newFolderPath);
}else{
// in this case, we're looping through the "folders" array for the
// first time, so get the currentFolder from the "folders" array.
currentFolder = folders[i];
}
String currentFolderName = currentFolder.getName();
if (! currentFolder.isSubscribed() && (! currentFolderName.equals("INBOX"))) {
continue;
}
// folderFullPath should contain the PATH to the folder, *EXcluding*
// current folder name. (if the current folder is /Inbox/Personal/Jokes,
// then folderFullPath should be /Inbox/Personal). This will be used
// to get the parent ID of the local folder so that we can create the
// new folder locally.
String folderFullPath = "/root/";
String remoteFullName = currentFolder.getFullName();
folderFullPath += remoteFullName;
folderFullPath = folderFullPath.replaceAll(currentFolderName+"$", "");
folderFullPath = folderFullPath.replaceAll("/$", "");
String folderFullName = remoteFullName.replaceAll("/$", "");
if (k != 0) {
// If we are finished processing the "folders" array, and are
// now currently processing the "reprocessFolders" ArrayList,
// we must remove the object from reprocessFolders, so that we
// won't process that folder yet again
if (reprocessFolders.contains(folderFullName)) {
reprocessFolders.remove(folderFullName);
k--;
}
}
try {
currentFolder.open(Folder.READ_WRITE);
}catch(MessagingException me){
System.out.println("[MailEJB][Exception] A messaging exception was caught while opening [" + folderFullName + "].");
System.out.println("[MailEJB][Exception] skipping this folder, and continuing to process other folders.");
me.printStackTrace();
continue;
}
int folderID = -1;
MailFolderVO currentFolderVO = this.getEmailFolderByName(mailAccountVO.getAccountID(), currentFolder.getName(), cvdal);
// Get the remote folders, check if they exist locally, and CRUD them if necessary
// TODO: when checking if IMAP folder exists locally, we need to pass full path, not just folder name
if (this.emailFolderExists(mailAccountVO.getAccountID(), currentFolder.getName(), cvdal)) {
// folder exists locally, just get the ID
folderID = currentFolderVO.getFolderID();
}else{
// folder does not exist locally, create a new one and get the new ID
// THIS CODE NEEDS TO BE ABSTRACTED OUT INTO A NEW METHOD, BECAUSE IT
// IS IDENTICAL TO SOME CODE DOWN BELOW AFTER THE FOLDERS LOOP.
int parentFolderID = this.getFolderIDFromFullPath(folderFullPath, mailAccountVO.getAccountID(), cvdal);
if (parentFolderID > 0) {
MailFolderVO newFolderVO = new MailFolderVO();
newFolderVO.setFolderName(currentFolder.getName());
newFolderVO.setFolderType(MailFolderVO.USER_FOLDER_TYPE);
newFolderVO.setEmailAccountID(mailAccountVO.getAccountID());
newFolderVO.setParentID(parentFolderID);
folderID = this.addEmailFolder(newFolderVO, false, cvdal);
}else{
// if the parent folder did not exist locally, then we need
// to add this folder to the ArrayList problemFolders. After
// we are finished processing the "folders" array once, we'll
// then process the folders in "problemFolders" to try and
// make sure we successfully sync all folders.
HashMap probFolder = new HashMap();
probFolder.put("fullName", remoteFullName);
probFolder.put("name", currentFolder.getName());
problemFolders.add(probFolder);
continue;
}
}
// Now get the messages from the server and store them locally
Message[] messageArray = currentFolder.getMessages();
FetchProfile profile = new FetchProfile();
// These make us supa fast!
profile.add(FetchProfile.Item.FLAGS);
profile.add(UIDFolder.FetchProfileItem.UID);
//profile.add(FetchProfile.Item.ENVELOPE);
//profile.add(FetchProfile.Item.HEADERS);
currentFolder.fetch(messageArray, profile);
ArrayList transport = new ArrayList();
transport.add(reprocessFolders);
transport.add(validIDs);
transport.add(messageIDs);
// k += this.handleIMAPMessageArray(messageArray, currentFolder, folderID, mailAccountVO, transport, store, cvdal);
reprocessFolders = (ArrayList)transport.get(0);
validIDs = (ArrayList)transport.get(1);
messageIDs = (ArrayList)transport.get(2);
}
// perhaps get the list of folders that couldn't be created,
// and try creating them again? THIS CODE NEEDS TO BE ABSTRACTED
// OUT INTO A METHOD, BECAUSE IT IS IDENTICAL TO SOME CODE AT
// THE TOP OF THE ABOVE LOOP.
Iterator problemIter = problemFolders.iterator();
while (problemIter.hasNext()) {
HashMap probFolder = (HashMap)problemIter.next();
String folderFullPath = "/root/";
folderFullPath += (String)probFolder.get("fullName");
folderFullPath = folderFullPath.replaceAll((String)probFolder.get("name")+"$", "");
folderFullPath = folderFullPath.replaceAll("/$", "");
IMAPFolder newFolder = (IMAPFolder)store.getFolder(folderFullPath);
int parentFolderID = this.getFolderIDFromFullPath(folderFullPath, mailAccountVO.getAccountID(), cvdal);
if (parentFolderID > 0) {
MailFolderVO newFolderVO = new MailFolderVO();