}
@Override
public List<Long> sendInvitationsEmail(Long fromUserId, List<Long> invitationIds) {
Set<Long> invitationsSent = new HashSet<Long>();
WebRequest request = WebRequest.getInstance();
// Get all Invitations for supplied IDs
Set<Invitation> invitations = new HashSet<Invitation>();
if (invitationIds != null) {
for (Long invitationId : invitationIds) {
Invitation invitation = invitationService.get(invitationId);
if (invitation != null) {
invitations.add(invitation);
}
}
}
if (request != null && sendEmail && !CollectionUtils.isEmpty(invitations)) {
User fromUser = null;
if (fromUserId != null) {
fromUser = userService.get(fromUserId);
}
final MimeMessage mimeMessage = this.mailSender.createMimeMessage();
final MimeMessageHelper message = new MimeMessageHelper(mimeMessage, "UTF-8");
Locale locale = LocaleContextHolder.getLocale();
Context ctx = new Context(locale);
if (fromUser != null) {
ctx.setVariable("adminName", fromUser.getFullName());
}
// Convert Invitation List to Map with email addresses being keys
Map<String, List<Invitation>> emailMap = new HashMap<String, List<Invitation>>();
for (Invitation invitation : invitations) {
String key = StringUtils.trimAllWhitespace(invitation.getEmail()).toLowerCase();
if (emailMap.get(key) == null) {
emailMap.put(key, new ArrayList<Invitation>());
}
emailMap.get(key).add(invitation);
}
Organization parentDomain = null;
// Loop over each email address and send out a single invitation for each
for (String emailAddress : emailMap.keySet()) {
// Create Map separating domains for the email addresses invitations
Map<String, Set<Invitation>> invitationsMap = new HashMap<String, Set<Invitation>>();
for (Invitation invitation : emailMap.get(emailAddress)) {
if (DomainType.ORGANIZATION.equals(invitation.getDomain().getDomainType())) {
if (invitationsMap.get(DomainType.ORGANIZATION.name()) == null) {
invitationsMap.put(DomainType.ORGANIZATION.name(), new TreeSet<Invitation>(new InvitationDomainNameComparator()));
}
invitationsMap.get(DomainType.ORGANIZATION.name()).add(invitation);
parentDomain = (Organization) invitation.getDomain();
} else if (DomainType.GROUP.equals(invitation.getDomain().getDomainType())) {
if (invitationsMap.get(DomainType.GROUP.name()) == null) {
invitationsMap.put(DomainType.GROUP.name(), new TreeSet<Invitation>(new InvitationDomainNameComparator()));
}
invitationsMap.get(DomainType.GROUP.name()).add(invitation);
if(parentDomain == null) {
Group group = (Group) invitation.getDomain();
parentDomain = group.getOrganization();
}
}
}
ctx.setVariable("domainTypes", invitationsMap.keySet());
ctx.setVariable("groupInvitations", invitationsMap.get(DomainType.GROUP.name()));
String parentDomainName = parentDomain != null ? parentDomain.getName() : "";
ctx.setVariable("parentDomainName", StringUtils.trimTrailingWhitespace(parentDomainName));
User invitee = userService.getByEmail(emailAddress);
if (invitee == null) {
ctx.setVariable("existingUser", false);
NameValuePair emailParam = new BasicNameValuePair("email", emailAddress);
ctx.setVariable("url", request.generateURL("/auth/register", emailParam));
} else {
ctx.setVariable("existingUser", true);
ctx.setVariable("url", request.generateURL(""));
}
applyBranding(parentDomain, ctx);
// Create the HTML body using Thymeleaf
final String htmlContent = this.templateEngine.process("email-invitationTH", ctx);