* @return The number of messages moved (should always be 1 or 0).
*/
public int moveMessageToFolder(int individualID, int messageID, int newFolderID)
{
int numberMoved = 0;
CVDal cvdal = new CVDal(this.dataSource);
try {
// get old folder ID and the server type of the email message
MailMessageVO messageVO = this.getEmailMessageVO(individualID, messageID);
MailAccountVO accountVO = this.getMailAccountVO(messageVO.getEmailAccountID());
int oldFolderID = messageVO.getEmailFolderID();
String serverType = accountVO.getAccountType();
if (serverType.equalsIgnoreCase(MailAccountVO.IMAP_TYPE)) {
try {
Session session = null;
IMAPStore store = this.setupIMAPConnection(accountVO, session);
MailFolderVO toFolderVO = getEmailFolder(newFolderID);
MailFolderVO fromFolderVO = getEmailFolder(oldFolderID);
String name = toFolderVO.getFolderFullName();
if (name == null) {
name = toFolderVO.getFolderName();
}
IMAPFolder toFolder = (IMAPFolder)store.getFolder(name);
name = fromFolderVO.getFolderFullName();
if (name == null) {
name = fromFolderVO.getFolderName();
}
IMAPFolder fromFolder = (IMAPFolder)store.getFolder(name);
fromFolder.open(IMAPFolder.READ_WRITE);
Message msg = fromFolder.getMessageByUID(Long.parseLong(messageVO.getMessageUID()));
fromFolder.copyMessages(new Message[] {msg}, toFolder);
msg.setFlag(Flags.Flag.DELETED, true); //??
fromFolder.close(true);
} catch(Exception e) {
System.out.println("Exception thrown in moveMessageToFolder()");
e.printStackTrace();
return(0);
}
}
String insertQuery = "INSERT INTO emailmessagefolder (MessageID, FolderID) VALUES (?, ?)";
cvdal.setSqlQuery(insertQuery);
cvdal.setInt(1, messageID);
cvdal.setInt(2, newFolderID);
numberMoved = cvdal.executeUpdate();
cvdal.setSqlQueryToNull();
String deleteQuery = "DELETE FROM emailmessagefolder WHERE MessageID = ? AND FolderID = ?";
cvdal.setSqlQuery(deleteQuery);
cvdal.setInt(1, messageID);
cvdal.setInt(2, oldFolderID);
cvdal.executeUpdate();
cvdal.setSqlQueryToNull();
}catch(Exception e){
System.out.println("[Exception][MailEJB] Exception thrown in moveMessageToFolder(): " + e);
e.printStackTrace();
}finally{
cvdal.destroy();
cvdal = null;
}
return numberMoved;
}