ActionErrors allErrors = new ActionErrors();
String forward = "displayComposeForm";
String errorForward = "errorOccurred";
// "composeMailForm", defined in struts-config-email.xml
DynaActionForm emailForm = (DynaActionForm) form;
MailHome home = (MailHome) CVUtility.getHomeObject("com.centraview.mail.MailHome", "Mail");
try {
// get the message ID from the form bean
Integer messageID = (Integer) emailForm.get("messageID");
// now, check the message ID on the form...
if (messageID == null || messageID.intValue() <= 0) {
// if Message ID is not set on the form, then there is
// no point in continuing forward. Show user the door. :-)
allErrors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("error.general.requiredField", "Message ID"));
return (mapping.findForward(errorForward));
}
Mail remote = home.create();
remote.setDataSource(dataSource);
MailMessageVO messageVO = remote.getEmailMessageVO(individualID, messageID.intValue());
// set the subject on the form bean. Don't forget to prepend "Re: "
String prevSubject = messageVO.getSubject();
StringBuffer newSubject = new StringBuffer("");
if (prevSubject != null && prevSubject.length() > 0) {
if ((prevSubject.toLowerCase()).startsWith("re: ")) {
// If the subject already has "Re:" pre-pended,
// do not add another "Re:"
newSubject.append(prevSubject);
} else {
newSubject.append("Re: " + prevSubject);
}
} else {
newSubject.append("Re: your message");
}
emailForm.set("subject", newSubject.toString());
String linebreak = "\n";
UserPrefererences userPref = userObject.getUserPref();
if ((userPref.getContentType()).equals("HTML")) {
linebreak = "<br>\n";
}
StringBuffer body = new StringBuffer();
body.append(linebreak + linebreak + "---- Original Message: ----" + linebreak);
body.append("From: " + messageVO.getFromAddress() + linebreak);
body.append("Sent: " + messageVO.getReceivedDate() + linebreak);
body.append("Subject: " + messageVO.getSubject() + linebreak + linebreak);
// if the original message is an HTML message, we can only quote
// it if the user is using IE (with HTMLArea). Otherwise, we need
// to attach the original message as an attachment to the new message
boolean saveOrigBodyAsAttachment = false;
String originalBody = messageVO.getBody();
originalBody = originalBody.replaceAll("\r", "");
originalBody = originalBody.replaceAll("\n", linebreak);
body.append(originalBody);
String messageBody = body.toString();
emailForm.set("body", messageBody);
// we always reply with the From: address as the new To: address
// keep in mind that we should use ReplyTo: if it was supplied
String from = messageVO.getFromAddress();
String replyTo = messageVO.getReplyTo();
if (replyTo != null && replyTo.length() > 0) {
emailForm.set("to", replyTo);
} else {
emailForm.set("to", from);
}
Boolean replyToAll = (Boolean) emailForm.get("replyToAll");
if (replyToAll != null && replyToAll.booleanValue()) {
// if we're replying to all recipients, we put all
// the previous To: addresses and Cc: addresses in
// the new Cc: field
StringBuffer cc = new StringBuffer();
ArrayList toList = (ArrayList) messageVO.getToList();
cc.append(this.appendCcAddresses(toList));
// make sure to add a , between the first set of addresses,
// and the second set, if the first set was not empty
if (cc.length() > 0) {
cc.append(", ");
}
ArrayList ccList = (ArrayList) messageVO.getCcList();
cc.append(this.appendCcAddresses(ccList));
emailForm.set("cc", cc.toString());
} // end if (replyToAll != null && replyToAll.booleanValue())
ArrayList attachmentMap = new ArrayList();
if (saveOrigBodyAsAttachment) {
CvFolderVO attachmentFolderVO = remote.getAttachmentFolder(individualID);
int attachmentFolderID = attachmentFolderVO.getFolderId();
int newFileID = -1;
// somehow create a file using CvFileFacade, and get a CvFileVO from
// that.
CvFileVO fileVO = new CvFileVO();
SimpleDateFormat df = new SimpleDateFormat("MMMM_dd_yyyy_hh_mm_ss_S");
String prependDate = df.format(new Date());
fileVO.setName("OriginalMessage_#" + messageID.intValue() + "_" + prependDate + ".html");
fileVO.setTitle("OriginalMessage_#" + messageID.intValue());
fileVO.setDescription("");
fileVO.setFileSize(0.0f); // float
fileVO.setVersion("1.0");
fileVO.setStatus("PUBLISHED");
fileVO.setVisibility(CvFileVO.FV_PRIVATE);
fileVO.setPhysical(CvFileVO.FP_PHYSICAL);
fileVO.setPhysicalFolder(attachmentFolderID);
fileVO.setAuthorId(individualID);
fileVO.setIsTemporary(CvFileVO.FIT_YES);
ByteArrayInputStream inputStream = new ByteArrayInputStream(originalBody.getBytes());
try {
CvFileFacade fileFacade = new CvFileFacade();
newFileID = fileFacade.addFile(individualID, attachmentFolderID, fileVO, inputStream, dataSource);
} catch (CvFileException cfe) {
// I guess do nothing
}
if (newFileID > 0) {
// Add the CvFileVO to attachmentList, and the following line for the
// attachmentMap
fileVO.setFileId(newFileID);
String strFileID = String.valueOf(newFileID);
String strFileName = "OriginalMessage_" + messageID.intValue();
attachmentMap.add(new DDNameValue(strFileID + "#" + strFileName, strFileName));
}
} // end if (saveOrigBodyAsAttachment)
if (!attachmentMap.isEmpty()) {
emailForm.set("attachmentList", attachmentMap);
}
} catch (Exception e) {
logger.error("[Exception] ReplyHandler.Execute Handler ", e);
}