Search in sources :

Example 1 with ExtensionManager

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);
        });
    });
}
Also used : java.util(java.util) TicketCategoryRepository(alfio.repository.TicketCategoryRepository) MessageModification(alfio.model.modification.MessageModification) MessageSourceManager(alfio.manager.i18n.MessageSourceManager) ConfigurationManager(alfio.manager.system.ConfigurationManager) Mailer(alfio.manager.system.Mailer) TicketCheckInUtil(alfio.util.checkin.TicketCheckInUtil) RenderedTemplate(alfio.util.RenderedTemplate) Json(alfio.util.Json) Model(org.springframework.ui.Model) Triple(org.apache.commons.lang3.tuple.Triple) TicketRepository(alfio.repository.TicketRepository) Executor(java.util.concurrent.Executor) Organization(alfio.model.user.Organization) TemplateManager(alfio.util.TemplateManager) EventRepository(alfio.repository.EventRepository) Collectors(java.util.stream.Collectors) Executors(java.util.concurrent.Executors) TicketReservationManager(alfio.manager.TicketReservationManager) Component(org.springframework.stereotype.Component) EventManager(alfio.manager.EventManager) StringUtils.isNotBlank(org.apache.commons.lang3.StringUtils.isNotBlank) alfio.model(alfio.model) NotificationManager(alfio.manager.NotificationManager) CALENDAR_ICS(alfio.manager.system.Mailer.AttachmentIdentifier.CALENDAR_ICS) Log4j2(lombok.extern.log4j.Log4j2) EventUtil(alfio.util.EventUtil) ExtensionManager(alfio.manager.ExtensionManager) AllArgsConstructor(lombok.AllArgsConstructor) ExtendedModelMap(org.springframework.ui.ExtendedModelMap) Organization(alfio.model.user.Organization) ExtendedModelMap(org.springframework.ui.ExtendedModelMap) MessageModification(alfio.model.modification.MessageModification) Model(org.springframework.ui.Model)

Aggregations

EventManager (alfio.manager.EventManager)1 ExtensionManager (alfio.manager.ExtensionManager)1 NotificationManager (alfio.manager.NotificationManager)1 TicketReservationManager (alfio.manager.TicketReservationManager)1 MessageSourceManager (alfio.manager.i18n.MessageSourceManager)1 ConfigurationManager (alfio.manager.system.ConfigurationManager)1 Mailer (alfio.manager.system.Mailer)1 CALENDAR_ICS (alfio.manager.system.Mailer.AttachmentIdentifier.CALENDAR_ICS)1 alfio.model (alfio.model)1 MessageModification (alfio.model.modification.MessageModification)1 Organization (alfio.model.user.Organization)1 EventRepository (alfio.repository.EventRepository)1 TicketCategoryRepository (alfio.repository.TicketCategoryRepository)1 TicketRepository (alfio.repository.TicketRepository)1 EventUtil (alfio.util.EventUtil)1 Json (alfio.util.Json)1 RenderedTemplate (alfio.util.RenderedTemplate)1 TemplateManager (alfio.util.TemplateManager)1 TicketCheckInUtil (alfio.util.checkin.TicketCheckInUtil)1 java.util (java.util)1