MailHome mailHome = (MailHome) CVUtility.getHomeObject("com.centraview.mail.MailHome", "Mail");
try {
HttpSession session = request.getSession();
UserObject userObject = (UserObject) session.getAttribute("userobject");
int individualID = userObject.getIndividualID();
DynaActionForm dynaform = (DynaActionForm) form;
String mergeType = (String) dynaform.get("mergetype");
String actionType = null;
if (request.getParameter("actionType") != null) {
actionType = request.getParameter("actionType");
}
int categoryId = this.getCategoryIdFromType(mergeType);
PrintTemplate PTRemote = PTHome.create();
PTRemote.setDataSource(dataSource);
AdvancedSearch remoteAdvancedSearch = advancedSearchHome.create();
remoteAdvancedSearch.setDataSource(dataSource);
String savedsearch1 = "";
String specificentity = "";
String entitysavedsearch = "";
if (mergeType.equals("EMAIL")) {
// collect the Account List and set it to the dynaActionForm.
Mail mailRemote = mailHome.create();
mailRemote.setDataSource(dataSource);
ArrayList accountIDList = mailRemote.getUserAccountList(individualID);
// also adding delegated accounts
accountIDList.addAll(mailRemote.getDelegatedAccountList(individualID));
ArrayList accountList = new ArrayList(); // this, we're sending to the
// form
if (accountIDList.size() > 0) {
// get the details of each account
Iterator iter = accountIDList.iterator();
while (iter.hasNext()) {
Number accountID = (Number) iter.next();
MailAccountVO accountVO = mailRemote.getMailAccountVO(accountID.intValue());
InternetAddress address = new InternetAddress(accountVO.getEmailAddress(), accountVO.getAccountName());
LabelValueBean accountDetails = new LabelValueBean(address.toString(), accountID.toString());
accountList.add(accountDetails);
}
}
dynaform.set("accountList", accountList);
} // if (mergeType.equals("EMAIL"))
entitysavedsearch = (String) dynaform.get("entitysavedsearch");
if (entitysavedsearch.equals("ENTITY")) {
savedsearch1 = (String) dynaform.get("savedsearch1");
}
if (entitysavedsearch.equals("SPECIFICENTITY")) {
specificentity = (String) dynaform.get("specificentity");
}
// So if this is an email or print merge basically we need to gather up
// a whole buttload of information to figure out which individuals will
// be getting the message
if (specificentity.equals("SPECIFICPRIMARY") && entitysavedsearch.equals("SPECIFICENTITY")) {
// get the primary contact for this entity, they are the target
// recipient
String entityid = (String) dynaform.get("selectedEntityId");
ArrayList entityIdCollection = new ArrayList();
entityIdCollection.add(entityid);
Collection contactID = PTRemote.getContactsForEntity(entityIdCollection, true);
Iterator contactIdIterator = contactID.iterator();
HashMap individualIds = new HashMap();
if (contactIdIterator.hasNext()) {
Number individualId = (Number) contactIdIterator.next();
individualIds.put(individualId.toString(), individualId.toString());
}
dynaform.set("toIndividuals", individualIds);
} else if (specificentity.equals("SPECIFICALL") && entitysavedsearch.equals("SPECIFICENTITY")) {
// All individuals associated with the selected entity are
// to be recipients
String entityid = (String) dynaform.get("selectedEntityId");
ArrayList entityIdCollection = new ArrayList();
entityIdCollection.add(entityid);
Collection contactID = PTRemote.getContactsForEntity(entityIdCollection, false);
Iterator contactIdIterator = contactID.iterator();
HashMap individualIds = new HashMap();
while (contactIdIterator.hasNext()) {
Number individualId = (Number) contactIdIterator.next();
individualIds.put(individualId.toString(), individualId.toString());
}
dynaform.set("toIndividuals", individualIds);
} else if (specificentity.equals("SPECIFICCONTACT") && entitysavedsearch.equals("SPECIFICENTITY")) {
// specific
String individualid = (String) dynaform.get("individualId");
HashMap individualIds = new HashMap();
individualIds.put(individualid, individualid);
dynaform.set("toIndividuals", individualIds);
} else if (entitysavedsearch.equals("INDIVIDUAL")) {
String individualSearchId = (String) dynaform.get("individualSearchId");
ArrayList results = new ArrayList();
results.addAll(remoteAdvancedSearch.performSearch(individualID, Integer.parseInt(individualSearchId), "ADVANCE", null));
HashMap individualIds = new HashMap();
for (int i = 0; i < results.size(); i++) {
Number resultId = (Number) results.get(i);
// I stuck the key and value in to this hashmap as the same string
// as that is how the rest of print templates is written and I
// didn't have time to make it more reasonable.
individualIds.put(resultId.toString(), resultId.toString());
}
dynaform.set("toIndividuals", individualIds);
} else if (entitysavedsearch.equals("ENTITY") && savedsearch1.equals("PRIMARY")) {
// this is when an entity saved search is selected, and we need to
// get the individualid's of the primary contacts for the found set
// of entities.
String entitySearchId = (String) dynaform.get("entityId");
ArrayList results = new ArrayList();
results.addAll(remoteAdvancedSearch.performSearch(individualID, Integer.parseInt(entitySearchId), "ADVANCE", null));
// results contains our collection of entity ids.
Collection contactID = PTRemote.getContactsForEntity(results, true);
Iterator contactIdIterator = contactID.iterator();
HashMap individualIds = new HashMap();
while (contactIdIterator.hasNext()) {
Number individualId = (Number) contactIdIterator.next();
individualIds.put(individualId.toString(), individualId.toString());
}
dynaform.set("toIndividuals", individualIds);
} else if (entitysavedsearch.equals("ENTITY") && savedsearch1.equals("ALL")) {
// This is exactly the same as above, except we are getting all
// individuals instead of just primary contacts.
String entitySearchId = (String) dynaform.get("entityId");
ArrayList results = new ArrayList();
results.addAll(remoteAdvancedSearch.performSearch(individualID, Integer.parseInt(entitySearchId), "ADVANCE", null));
// results contains our collection of entity ids.
Collection contactID = PTRemote.getContactsForEntity(results, false);
Iterator contactIdIterator = contactID.iterator();
HashMap individualIds = new HashMap();
while (contactIdIterator.hasNext()) {
Number individualId = (Number) contactIdIterator.next();
individualIds.put(individualId.toString(), individualId.toString());
}
dynaform.set("toIndividuals", individualIds);
} // end else if (entitysavedsearch.equals("ENTITY") &&
request.setAttribute("body", "new");
// now we finished getting the buttload of info.
// So get the templates.
Collection templateList = PTRemote.getallPrintTemplate(individualID, categoryId);
session.setAttribute("mergeType", mergeType);
dynaform.set("templateList", templateList);
PrintTemplateVO ptVO = new PrintTemplateVO();
String id = (String) dynaform.get("id");
Integer templateId = null;
// If the id is set on the form bean get the selected template.
// Otherwise get the default.
if (id != null && !id.equals("")) {
try {
templateId = Integer.valueOf(id);
} catch (Exception e) {}
}
// anyway somehow we got the "Default" print template. So put it in the
// bean
if (templateId == null) {
try {
ptVO = PTRemote.getDefaultPrintTemplate(individualID, categoryId);
} catch (Exception e) {
logger.error("[Exception] PTListHandler.Execute Handler ", e);
throw new ServletException(e);
}
} else {
try {
ptVO = PTRemote.getPrintTemplate(templateId.intValue());
} catch (Exception e) {
logger.error("[execute] Exception thrown.", e);
throw new ServletException(e);
}
}
dynaform.set("artifactname", ptVO.getArtifactName());
dynaform.set("artifactid", String.valueOf(ptVO.getArtifactId()));
if (actionType == null) {
dynaform.set("templateData", ptVO.getPtData());
}
dynaform.set("templateName", ptVO.getPtname());
dynaform.set("id", String.valueOf(ptVO.getPtdetailId()));
dynaform.set("categoryId", String.valueOf(ptVO.getPtcategoryId()));
if (ptVO.getPtsubject() != null) {
dynaform.set("templatesubject", ptVO.getPtsubject());
}
FORWARD_final = FORWARD_newtemplate;
} catch (Exception e) {
logger.error("[Exception] PTListHandler.Execute Handler ", e);
FORWARD_final = GLOBAL_FORWARD_failure;