StringBuffer warningMessage = new StringBuffer();
if (mailTemplate != null) {
MailerWithTemplate mailer = MailerWithTemplate.getInstance();
HashMap identityGroupList = new HashMap();
for (Iterator iter = selectedGroups.iterator(); iter.hasNext();) {
BusinessGroup group = (BusinessGroup)iter.next();
// Build owner group, list of identities
SecurityGroup ownerGroup = group.getOwnerGroup();
List ownerIdentities = ManagerFactory.getManager().getIdentitiesOfSecurityGroup(ownerGroup);
// loop over this list and send email
for (Iterator iterator = ownerIdentities.iterator(); iterator.hasNext();) {
Identity identity = (Identity) iterator.next();
if (identityGroupList.containsKey(identity) ) {
List groupsOfIdentity = (List)identityGroupList.get(identity);
groupsOfIdentity.add(group);
} else {
List groupsOfIdentity = new ArrayList();
groupsOfIdentity.add(group);
identityGroupList.put(identity, groupsOfIdentity);
}
}
}
// loop over identity list and send email
for (Iterator iterator = identityGroupList.keySet().iterator(); iterator.hasNext();) {
Identity identity = (Identity) iterator.next();
mailTemplate.addToContext("responseTo", emailResponseTo);
if (!isTemplateChanged) {
// Email template has NOT changed => take translated version of subject and body text
Translator identityTranslator = new PackageTranslator(PACKAGE, I18nManager.getInstance().getLocaleOrDefault(identity.getUser().getPreferences().getLanguage()));
mailTemplate.setSubjectTemplate(identityTranslator.translate(keyEmailSubject));
mailTemplate.setBodyTemplate(identityTranslator.translate(keyEmailBody));
}
// loop over all repositoriesOfIdentity to build email message
StringBuilder buf = new StringBuilder();
for (Iterator groupIterator = ((List)identityGroupList.get(identity)).iterator(); groupIterator.hasNext();) {
BusinessGroup group = (BusinessGroup) groupIterator.next();
buf.append("\n ").append( group.getName() ).append(" / ").append(FilterFactory.getHtmlTagsFilter().filter(group.getDescription()));
}
mailTemplate.addToContext("groupList", buf.toString());
mailTemplate.putVariablesInMailContext(mailTemplate.getContext(), identity);
Tracing.logDebug(" Try to send Delete-email to identity=" + identity.getName() + " with email=" + identity.getUser().getProperty(UserConstants.EMAIL, null), this.getClass());
List<Identity> ccIdentities = new ArrayList<Identity>();
if(mailTemplate.getCpfrom()) {
ccIdentities.add(sender);
} else {
ccIdentities = null;
}
MailerResult mailerResult = mailer.sendMailUsingTemplateContext(identity, ccIdentities, null, mailTemplate, sender);
if (mailerResult.getReturnCode() == MailerResult.OK) {
// Email sended ok => set deleteEmailDate
for (Iterator groupIterator = ((List)identityGroupList.get(identity)).iterator(); groupIterator.hasNext();) {
BusinessGroup group = (BusinessGroup) groupIterator.next();
Tracing.logAudit("Group-Deletion: Delete-email send to identity=" + identity.getName() + " with email=" + identity.getUser().getProperty(UserConstants.EMAIL, null) + " for group=" + group, this.getClass());
markSendEmailEvent(group);
}
} else {
warningMessage.append( pT.translate("email.error.send.failed", new String[] {identity.getUser().getProperty(UserConstants.EMAIL, null), identity.getName()} ) ).append("\n");
}
}
} else {
// no template => User decides to sending no delete-email, mark only in lifecycle table 'sendEmail'
for (Iterator iter = selectedGroups.iterator(); iter.hasNext();) {
BusinessGroup group = (BusinessGroup)iter.next();
Tracing.logAudit("Group-Deletion: Move in 'Email sent' section without sending email, group=" + group, this.getClass());
markSendEmailEvent(group);
}
}
return warningMessage.toString();