*/
@Override
public void save(Mailbox<Integer> mailbox) throws MailboxException {
try {
Mailbox<Integer> originalMailbox = getCachedMailbox(mailbox.getMailboxId());
MaildirFolder folder = maildirStore.createMaildirFolder(mailbox);
// equals with null check
if (originalMailbox.getName() == null ? mailbox.getName() != null : !originalMailbox.getName().equals(mailbox.getName())) {
if (folder.exists())
throw new MailboxExistsException(mailbox.getName());
MaildirFolder originalFolder = maildirStore.createMaildirFolder(originalMailbox);
// renaming the INBOX means to move its contents to the new folder
if (originalMailbox.getName().equals(MailboxConstants.INBOX)) {
try {
File inboxFolder = originalFolder.getRootFile();
File newFolder = folder.getRootFile();
if (!newFolder.mkdirs())
throw new IOException("Could not create folder " + newFolder);
if (!originalFolder.getCurFolder().renameTo(folder.getCurFolder()))
throw new IOException("Could not rename folder " + originalFolder.getCurFolder() + " to " + folder.getCurFolder());
if (!originalFolder.getNewFolder().renameTo(folder.getNewFolder()))
throw new IOException("Could not rename folder " + originalFolder.getNewFolder() + " to " + folder.getNewFolder());
if (!originalFolder.getTmpFolder().renameTo(folder.getTmpFolder()))
throw new IOException("Could not rename folder " + originalFolder.getTmpFolder() + " to " + folder.getTmpFolder());
File oldUidListFile = new File(inboxFolder, MaildirFolder.UIDLIST_FILE);
File newUidListFile = new File(newFolder, MaildirFolder.UIDLIST_FILE);
if (!oldUidListFile.renameTo(newUidListFile))
throw new IOException("Could not rename file " + oldUidListFile + " to " + newUidListFile);
File oldValidityFile = new File(inboxFolder, MaildirFolder.VALIDITY_FILE);
File newValidityFile = new File(newFolder, MaildirFolder.VALIDITY_FILE);
if (!oldValidityFile.renameTo(newValidityFile))
throw new IOException("Could not rename file " + oldValidityFile + " to " + newValidityFile);
// recreate the INBOX folders, uidvalidity and uidlist will
// automatically be recreated later
if (!originalFolder.getCurFolder().mkdir())
throw new IOException("Could not create folder " + originalFolder.getCurFolder());
if (!originalFolder.getNewFolder().mkdir())
throw new IOException("Could not create folder " + originalFolder.getNewFolder());
if (!originalFolder.getTmpFolder().mkdir())
throw new IOException("Could not create folder " + originalFolder.getTmpFolder());
} catch (IOException e) {
throw new MailboxException("Failed to save Mailbox " + mailbox, e);
}
}
else {
if (!originalFolder.getRootFile().renameTo(folder.getRootFile()))
throw new MailboxException("Failed to save Mailbox " + mailbox,
new IOException("Could not rename folder " + originalFolder));
}
}
} catch (MailboxNotFoundException e) {
// it cannot be found and is thus new
MaildirFolder folder = maildirStore.createMaildirFolder(mailbox);
if (!folder.exists()) {
boolean success = folder.getRootFile().exists();
if (!success) success = folder.getRootFile().mkdirs();
if (!success)
throw new MailboxException("Failed to save Mailbox " + mailbox);
success = folder.getCurFolder().mkdir();
success = success && folder.getNewFolder().mkdir();
success = success && folder.getTmpFolder().mkdir();
if (!success)
throw new MailboxException("Failed to save Mailbox " + mailbox, new IOException("Needed folder structure can not be created"));
}
try {
folder.setUidValidity(mailbox.getUidValidity());
} catch (IOException ioe) {
throw new MailboxException("Failed to save Mailbox " + mailbox, ioe);
}
}