public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception
{
try {
final String dataSource = Settings.getInstance().getSiteInfo(CVUtility.getHostName(super.getServlet().getServletContext())).getDataSource();
// Collect the Template information from the Form
DynaActionForm dynaform = (DynaActionForm) form;
String ptData = (String) dynaform.get("templateData");
String subject = (String) dynaform.get("templatesubject");
String templatefrom = (String) dynaform.get("templatefrom");
String templatereplyto = (String) dynaform.get("templatereplyto");
String mergeType = (String) dynaform.get("mergetype");
// list of Individual's ID
HashMap individualIds = (HashMap) dynaform.get("toIndividuals");
ArrayList templateList = new ArrayList();
ArrayList emailList = new ArrayList();
Vector fieldList = new Vector();
fieldList.add("IndividualID");
fieldList.add("EntityID");
fieldList.add("Company");
fieldList.add("FirstName");
fieldList.add("MiddleInitial");
fieldList.add("LastName");
fieldList.add("Title");
fieldList.add("PrimaryAddress");
fieldList.add("Street1");
fieldList.add("Street2");
fieldList.add("City");
fieldList.add("State");
fieldList.add("Zip");
fieldList.add("Country");
fieldList.add("Website");
fieldList.add("ExternalID");
fieldList.add("Source");
fieldList.add("Fax");
fieldList.add("Home");
fieldList.add("Main");
fieldList.add("Mobile");
fieldList.add("Other");
fieldList.add("Pager");
fieldList.add("Work");
fieldList.add("Email");
if (mergeType.equals("EMAIL")) {
// Populating the ArrayList with the DDNameValue.
ArrayList attachments = new ArrayList();
String[] tempAttachmentMap = (String[]) dynaform.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));
}
}
}// end of for(int i = 0; i < tempAttachmentMap.length; i++)
}// end of if (tempAttachmentMap != null && tempAttachmentMap.size() >
// 0)
dynaform.set("attachmentList", attachments);
}
if (individualIds != null && individualIds.size() != 0) {
Set key = individualIds.keySet();
Iterator iterator = key.iterator();
while (iterator.hasNext()) {
String keyValue = (String) iterator.next();
String tempData = ptData;
int individualId = 0;
try {
individualId = Integer.parseInt(keyValue);
} catch (NumberFormatException e) {
logger.error("[execute] There was no valid individual Id.", e);
}
if (individualId != 0) {
// calling the IndividualValues with passing the individual it will
// populate the bean file PrintTemplateField and return the
// information.
PrintTemplateField printTemplate = IndividualValues(individualId, dataSource);
// Before doing any text manipulation, lets check if we are type
// email, and this
// Individual has no email address then we can just bail on this
// individual now.
if (mergeType.equals("EMAIL") && printTemplate.getEmail().equals("")) {
// punt on this one and try the next one.
continue;
}
// call the method from PrintTemplateField and replace all the
// occurance of fieldList from Template Body.
Class printTemplateClass = printTemplate.getClass();
for (int j = 0; j < fieldList.size(); j++) {
String methodName = fieldList.elementAt(j).toString();
String getmethod = "get" + methodName;
Method method = printTemplateClass.getMethod(getmethod, null);
Object methodValue = method.invoke(printTemplate, null);
String valueString = "";
if (valueString != null) {
valueString = (String) methodValue;
}
if (valueString != null && valueString.equals("null")) {
valueString = "";
}
String findString = "<" + methodName + "/>";
tempData = tempData.replaceAll(findString, valueString);
tempData = tempData.replaceAll("<" + methodName + "/>", valueString);
tempData = tempData.replaceAll("<" + methodName + "/>", valueString);
}// end of for (int j = 0; j < fieldList.size(); j++)
// get the Email Information for the Individual and Store it into
// emailList if necessary.
String name = "";
String address = "";
try {
if (mergeType.equals("EMAIL")) {
address = printTemplate.getEmail();
name = printTemplate.getFirstName() + " " + printTemplate.getLastName();
InternetAddress internetAddress = new InternetAddress(address, name);
emailList.add(internetAddress.toString());
}
// templateList is sample template with update information of the
// Individual.
templateList.add(tempData);
} catch (UnsupportedEncodingException e) {
// If the creation of an InternetAddress fails then skip adding
// this record in.
logger.error("[execute] invalid email address, skipping: " + name + ", " + address, e);
}
}// end of if (individualId != 0)
} // end of while (iterator.hasNext())
} // end of if (individualIds != null && individualIds.size() != 0)
dynaform.set("toPrint", templateList);
dynaform.set("emailList", emailList);
dynaform.set("templatesubject", subject);
dynaform.set("templatefrom", templatefrom);
dynaform.set("templatereplyto", templatereplyto);
FORWARD_final = FORWARD_ptpreview;
} catch (Exception e) {
logger.error("[execute] Exception thrown.", e);
FORWARD_final = GLOBAL_FORWARD_failure;
}