// Check if any of the provided emails have existing invitations for the organization
List<Long> organizationIds = new ArrayList<Long>();
organizationIds.add(user.getActiveOrganization().getId());
List<Invitation> invitations = invitationService.getAllForEmailsAndDomains(new ArrayList<String>(emails), organizationIds);
for (Invitation invitation : invitations) {
Contact invalidContact = null;
for (Contact contact : validContacts) {
// If invitation exists create error for contact, add contact to invalidContacts list and remove from validContacts list
if (contact.getEmail().equalsIgnoreCase(invitation.getEmail())) {
errors.rejectValue(String.format("contacts[%s]", form.getContacts().indexOf(contact)), "batchInvitationValidator.organization.invitationExists", new Object[]{contact.getEmail(), user.getActiveOrganization().getName()}, "");
invalidContacts.add(contact);
invalidContact = contact;
break;
}
}
// If invalidContact is not null then remove from validContacts list
if (invalidContact != null) {
validContacts.remove(invalidContact);
}
}
// Check if user already belongs to organization
emails = getContactEmails(validContacts);
List<Long> organizationId = new ArrayList<Long>();
organizationId.add(user.getActiveOrganization().getId());
List<UserDomain> userDomains = userDomainService.getUserDomainsForEmailsAndDomains(new ArrayList<String>(emails), organizationId);
for (UserDomain userDomain : userDomains) {
Contact invalidContact = null;
for (Contact contact : validContacts) {
// If userDomain exists for contact
if (contact.getEmail().equalsIgnoreCase(userDomain.getUser().getEmail())) {
errors.rejectValue(String.format("contacts[%s]", form.getContacts().indexOf(contact)), "batchInvitationValidator.organization.userExists", new Object[]{contact.getEmail(), user.getActiveOrganization().getName()}, "");