use of alfio.manager.ExtensionManager in project alf.io by alfio-event.
the class CustomMessageManager method sendMessages.
public void sendMessages(String eventName, Optional<Integer> categoryId, List<MessageModification> input, String username) {
Event event = eventManager.getSingleEvent(eventName, username);
// dry run for checking the syntax
preview(event, input, username);
Organization organization = eventManager.loadOrganizer(event, username);
Map<String, List<MessageModification>> byLanguage = input.stream().collect(Collectors.groupingBy(m -> m.getLocale().getLanguage()));
var categoriesById = ticketCategoryRepository.findByEventIdAsMap(event.getId());
sendMessagesExecutor.execute(() -> {
var messageSource = messageSourceManager.getMessageSourceFor(event);
categoryId.map(id -> ticketRepository.findConfirmedByCategoryId(event.getId(), id)).orElseGet(() -> ticketRepository.findAllConfirmed(event.getId())).stream().filter(t -> isNotBlank(t.getFullName()) && isNotBlank(t.getEmail())).parallel().map(t -> {
Model model = new ExtendedModelMap();
model.addAttribute("eventName", eventName);
model.addAttribute("fullName", t.getFullName());
model.addAttribute("organizationName", organization.getName());
model.addAttribute("organizationEmail", organization.getEmail());
model.addAttribute("reservationURL", ticketReservationManager.reservationUrl(t.getTicketsReservationId(), event));
model.addAttribute("reservationID", ticketReservationManager.getShortReservationID(event, t.getTicketsReservationId()));
model.addAttribute("ticketURL", ticketReservationManager.ticketUpdateUrl(event, t.getUuid()));
model.addAttribute("ticketID", t.getUuid());
return Triple.of(t, t.getEmail(), model);
}).forEach(triple -> {
Ticket ticket = triple.getLeft();
MessageModification m = Optional.ofNullable(byLanguage.get(ticket.getUserLanguage())).orElseGet(() -> byLanguage.get(byLanguage.keySet().stream().findFirst().orElseThrow(IllegalStateException::new))).get(0);
Model model = triple.getRight();
String subject = renderResource(m.getSubject(), event, model, m.getLocale(), templateManager);
StringBuilder text = new StringBuilder(renderResource(m.getText(), event, model, m.getLocale(), templateManager));
List<Mailer.Attachment> attachments = new ArrayList<>();
var templateModel = new HashMap<>(model.asMap());
if (m.isAttachTicket()) {
var optionalReservation = ticketReservationManager.findById(ticket.getTicketsReservationId());
var optionalTicketCategory = ticketCategoryRepository.getByIdAndActive(ticket.getCategoryId());
if (optionalReservation.isPresent() && optionalTicketCategory.isPresent() && EventUtil.isAccessOnline(optionalTicketCategory.get(), event)) {
var onlineCheckInModel = new HashMap<>(TicketCheckInUtil.getOnlineCheckInInfo(extensionManager, eventRepository, ticketCategoryRepository, configurationManager, event, Locale.forLanguageTag(ticket.getUserLanguage()), ticket, categoriesById.get(ticket.getCategoryId()), ticketReservationManager.retrieveAttendeeAdditionalInfoForTicket(ticket)));
// add ticket model in order to be able to generate the calendar invitation
onlineCheckInModel.putAll(getModelForTicket(ticket, optionalReservation.get(), optionalTicketCategory.get(), organization));
// generate only calendar invitation, as Ticket PDF would not make sense in this case.
attachments.add(generateCalendarAttachmentForOnlineEvent(onlineCheckInModel));
// add check-in URL and prerequisites, if any
text.append(notificationManager.buildOnlineCheckInText(onlineCheckInModel, Locale.forLanguageTag(ticket.getUserLanguage()), messageSource));
templateModel.putAll(onlineCheckInModel);
} else if (optionalReservation.isPresent() && optionalTicketCategory.isPresent()) {
attachments.add(generateTicketAttachment(ticket, optionalReservation.get(), optionalTicketCategory.get(), organization));
}
}
notificationManager.sendSimpleEmail(event, ticket.getTicketsReservationId(), triple.getMiddle(), subject, () -> RenderedTemplate.plaintext(text.toString(), templateModel), attachments);
});
});
}
Aggregations