HttpSession session = request.getSession();
UserObject userObject = (UserObject)session.getAttribute("userobject");
int individualID = userObject.getIndividualID(); // logged in user
ActionMessages allErrors = new ActionMessages();
String forward = "displayComposeForm";
ActionMessages messages = new ActionMessages();
// "composeMailForm", defined in struts-config-email.xml
DynaActionForm emailForm = (DynaActionForm)form;
// Populate the Attachments In-Case. If we face any problem while sending
// email.
// Populating the ArrayList with the DDNameValue.
ArrayList attachments = new ArrayList();
ArrayList attachmentFileIDs = new ArrayList();
String[] tempAttachmentMap = (String[])emailForm.get("attachments");
if (tempAttachmentMap != null && tempAttachmentMap.length > 0) {
for (int i = 0; i < tempAttachmentMap.length; i++) {
String fileKeyName = tempAttachmentMap[i];
if (fileKeyName != null) {
int indexOfHash = fileKeyName.indexOf("#");
if (indexOfHash != -1) {
int lenString = fileKeyName.length();
String fileID = fileKeyName.substring(0, indexOfHash);
String fileName = fileKeyName.substring(indexOfHash + 1, lenString);
attachments.add(new DDNameValue(fileID + "#" + fileName, fileName));
attachmentFileIDs.add(new Integer(fileID));
}
}
}
}
// Setting the Attachments to form.
emailForm.set("attachmentList", attachments);
try {
MailHome home = (MailHome)CVUtility.getHomeObject("com.centraview.mail.MailHome", "Mail");
Mail remote = home.create();
remote.setDataSource(dataSource);
// create a MailMessageVO
MailMessageVO messageVO = new MailMessageVO();
Integer messageID = new Integer(0);
messageVO.setMessageID(messageID.intValue());
Integer accountID = (Integer)emailForm.get("accountID");
messageVO.setEmailAccountID(accountID.intValue());
// IMPORTANT: Because we use this same handler to do the save "Template"
// functionality, note the logic below. Here, we're getting the folder ID
// for the folder where we are going to store the message. Instead of
// writing
// a second handler for Save Template that did almost the same exact thing
// as this handler, I created a SaveTemplateHandler that just sets one
// Boolean
// request attribute named "saveTemplate". If that attribute is set to
// true
// here, then we'll get the folderID of the Templates folder instead of
// the
// Drafts folder. That is the ONLY difference between Draft and Template.
String folderName = MailFolderVO.DRAFTS_FOLDER_NAME;
Boolean saveTemplate = (Boolean)request.getAttribute("saveTemplate");
if (saveTemplate != null && saveTemplate.booleanValue() == true) {
messageID = (Integer)emailForm.get("templateID");
folderName = MailFolderVO.TEMPLATES_FOLDER_NAME;
} else {
messageID = (Integer)emailForm.get("savedDraftID");
}
messageVO.setMessageID(messageID.intValue());
MailFolderVO draftsFolder = remote.getEmailFolderByName(accountID.intValue(), folderName);
messageVO.setEmailFolderID(draftsFolder.getFolderID());
MailAccountVO accountVO = remote.getMailAccountVO(accountID.intValue());
if (accountVO == null) {
accountVO = new MailAccountVO();
}
InternetAddress fromAddress = new InternetAddress(accountVO.getEmailAddress(), accountVO
.getAccountName());
messageVO.setFromAddress(fromAddress.toString());
messageVO.setReplyTo(accountVO.getReplyToAddress());
String subject = (String)emailForm.get("subject");
messageVO.setSubject(subject);
String body = (String)emailForm.get("body");
messageVO.setBody(body);
Boolean composeInHTML = (Boolean)emailForm.get("composeInHTML");
if (composeInHTML.booleanValue()) {
messageVO.setContentType(MailMessageVO.PLAIN_TEXT_TYPE);
} else {
messageVO.setContentType(MailMessageVO.HTML_TEXT_TYPE);
}
// To: is *NOT* a required field here
ArrayList toList = this.parseAddresses((String)emailForm.get("to"));
messageVO.setToList(toList);
ArrayList ccList = this.parseAddresses((String)emailForm.get("cc"));
messageVO.setCcList(ccList);
ArrayList bccList = this.parseAddresses((String)emailForm.get("bcc"));
messageVO.setBccList(bccList);
Date receivedDate = new Date();
messageVO.setReceivedDate(receivedDate);
// Handle attachments - the attachment handler puts a ArrayList
// This ArrayList contains the list of attached fileIDs.
// So we'll get that ArrayList, iterate through it, creating a
// CvFileVO object for each attachment, then add that file VO
// to the messageVO object which will take care of the rest in
// the EJB layer. Note that in the future, we'll want to make
// this better by not saving the attachment list on the session.
// So if you intend to modify this code, please consult the rest
// of the team to see if it makes sense to make that change now.
// Also, if you make a change here, you must make sure the
// attachment handling code in ForwardHandler reflects your changes.
if (attachmentFileIDs != null && attachmentFileIDs.size() > 0) {
CvFileFacade fileRemote = new CvFileFacade();
for (int i = 0; i < attachmentFileIDs.size(); i++) {
int fileID = ((Integer)attachmentFileIDs.get(i)).intValue();
// get the CvFileVO from the EJB layer
CvFileVO fileVO = fileRemote.getFile(individualID, fileID, dataSource);
if (fileVO != null) {
// add this attachment to the messageVO
messageVO.addAttachedFiles(fileVO);
}
} // end while (attachIter.hasNext())
} // end if (attachmentMap != null && attachmentMap.size() > 0)
remote.saveDraft(individualID, messageVO);
// **ATTENTION** Note the "tricky" logic here. We're using the action
// errors framework to send a *SUCCESS* message to the user!!!
// We're going to add a message to the error list *only* if there
// were no errors up to this point. In all cases, we're going to save
// the error messages and forward back to the Compose.do handler.
if (allErrors.isEmpty()) {
messages.add(ActionErrors.GLOBAL_MESSAGE, new ActionMessage("error.freeForm",
"Message saved in " + folderName + " folder."));
saveMessages(request, messages);
}
saveErrors(request, allErrors);