}
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()) {