Skip to content

Commit

Permalink
#21 - update assignee using AJAX (if available)
Browse files Browse the repository at this point in the history
  • Loading branch information
cbellone committed Dec 7, 2014
1 parent 16a2318 commit 40cf671
Show file tree
Hide file tree
Showing 17 changed files with 314 additions and 271 deletions.
2 changes: 1 addition & 1 deletion src/main/java/alfio/config/Initializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ private void configureSessionCookie(ServletContext servletContext) {

@Override
protected Class<?>[] getRootConfigClasses() {
return new Class<?>[] { DataSourceConfiguration.class, WebSecurityConfig.class };
return new Class<?>[] { SystemConfiguration.class, DataSourceConfiguration.class, WebSecurityConfig.class };
}

@Override
Expand Down
17 changes: 5 additions & 12 deletions src/main/java/alfio/config/MvcConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.fasterxml.jackson.datatype.jsr310.JSR310Module;
import com.samskivert.mustache.Mustache;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.ResourceLoaderAware;
import org.springframework.context.annotation.Bean;
Expand Down Expand Up @@ -71,6 +72,9 @@ public class MvcConfiguration extends WebMvcConfigurerAdapter implements Resourc

private ResourceLoader resourceLoader;

@Autowired
private MessageSource messageSource;

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
Expand Down Expand Up @@ -136,22 +140,11 @@ public void postHandle(HttpServletRequest request, HttpServletResponse response,
};
}

@Bean
public MessageSource messageSource() {
ResourceBundleMessageSource source = new ResourceBundleMessageSource();
source.setBasenames("alfio.i18n.application", "alfio.i18n.admin");
//since we have all the english translations in the default file, we don't need
//the fallback to the system locale.
source.setFallbackToSystemLocale(false);
source.setAlwaysUseMessageFormat(true);
return source;
}

@Bean
public LocalizationMessageInterceptor getTemplateMessagesInterceptor() {
LocalizationMessageInterceptor interceptor = new LocalizationMessageInterceptor();
interceptor.setLocaleResolver(getLocaleResolver());
interceptor.setMessageSource(messageSource());
interceptor.setMessageSource(messageSource);
return interceptor;
}

Expand Down
38 changes: 38 additions & 0 deletions src/main/java/alfio/config/SystemConfiguration.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* This file is part of alf.io.
*
* alf.io is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* alf.io is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with alf.io. If not, see <http://www.gnu.org/licenses/>.
*/
package alfio.config;

import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ResourceBundleMessageSource;

@Configuration
public class SystemConfiguration {

@Bean
public MessageSource messageSource() {
ResourceBundleMessageSource source = new ResourceBundleMessageSource();
source.setBasenames("alfio.i18n.application", "alfio.i18n.admin");
//since we have all the english translations in the default file, we don't need
//the fallback to the system locale.
source.setFallbackToSystemLocale(false);
source.setAlwaysUseMessageFormat(true);
return source;
}

}
113 changes: 102 additions & 11 deletions src/main/java/alfio/controller/ReservationController.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,35 @@

import alfio.controller.form.PaymentForm;
import alfio.controller.form.ReservationForm;
import alfio.controller.form.UpdateTicketOwnerForm;
import alfio.controller.support.TemplateManager;
import alfio.controller.support.TemplateProcessor;
import alfio.manager.EventManager;
import alfio.manager.StripeManager;
import alfio.manager.TicketReservationManager;
import alfio.manager.support.OrderSummary;
import alfio.manager.support.PDFTemplateBuilder;
import alfio.manager.support.PaymentResult;
import alfio.manager.support.TextTemplateBuilder;
import alfio.manager.system.Mailer;
import alfio.model.Event;
import alfio.model.Ticket;
import alfio.model.TicketCategory;
import alfio.model.TicketReservation;
import alfio.model.modification.TicketReservationWithOptionalCodeModification;
import alfio.model.user.Organization;
import alfio.repository.EventRepository;
import alfio.repository.TicketCategoryRepository;
import alfio.repository.TicketRepository;
import alfio.repository.user.OrganizationRepository;
import alfio.util.OptionalWrapper;
import alfio.util.ValidationResult;
import alfio.util.Validator;
import com.google.zxing.WriterException;
import org.apache.commons.lang3.Validate;
import org.apache.commons.lang3.time.DateUtils;
import org.apache.commons.lang3.tuple.Pair;
import org.apache.commons.lang3.tuple.Triple;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.MessageSourceResolvable;
Expand All @@ -48,6 +60,7 @@
import org.springframework.web.servlet.support.RequestContextUtils;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
Expand All @@ -64,19 +77,22 @@ public class ReservationController {
private final Mailer mailer;
private final TemplateManager templateManager;
private final MessageSource messageSource;
private final TicketRepository ticketRepository;
private final TicketCategoryRepository ticketCategoryRepository;
//
private final EventController eventController;

@Autowired
public ReservationController(EventRepository eventRepository,
EventManager eventManager,
TicketReservationManager ticketReservationManager,
OrganizationRepository organizationRepository,
StripeManager stripeManager,
Mailer mailer,
TemplateManager templateManager,
MessageSource messageSource,
EventController eventController) {
public ReservationController(EventRepository eventRepository,
EventManager eventManager,
TicketReservationManager ticketReservationManager,
OrganizationRepository organizationRepository,
StripeManager stripeManager,
Mailer mailer,
TemplateManager templateManager,
MessageSource messageSource,
TicketRepository ticketRepository,
TicketCategoryRepository ticketCategoryRepository, EventController eventController) {
this.eventRepository = eventRepository;
this.eventManager = eventManager;
this.ticketReservationManager = ticketReservationManager;
Expand All @@ -85,6 +101,8 @@ public ReservationController(EventRepository eventRepository,
this.mailer = mailer;
this.templateManager = templateManager;
this.messageSource = messageSource;
this.ticketRepository = ticketRepository;
this.ticketCategoryRepository = ticketCategoryRepository;
this.eventController = eventController;
}

Expand Down Expand Up @@ -162,8 +180,7 @@ public String showReservationPage(

return "/event/reservation-page";
} else if (reservation.get().getStatus() == TicketReservation.TicketReservationStatus.COMPLETE) {



model.addAttribute("reservationId", reservationId);
model.addAttribute("reservation", reservation.get());
model.addAttribute("confirmationEmailSent", confirmationEmailSent);
Expand All @@ -179,6 +196,7 @@ public String showReservationPage(
model.addAttribute("ticketsAreAllAssigned", tickets.stream().allMatch(Ticket::getAssigned));
model.addAttribute("countries", getLocalizedCountries(RequestContextUtils.getLocale(request)));
model.addAttribute("pageTitle", "reservation-page-complete.header.title");
model.asMap().putIfAbsent("validationResult", ValidationResult.success());
return "/event/reservation-page-complete";

} else { // reservation status has status IN_PAYMENT or STUCK.
Expand Down Expand Up @@ -260,6 +278,79 @@ public String reSendReservationConfirmationEmail(@PathVariable("eventName") Stri
return "redirect:/event/" + eventName + "/reservation/" + reservationId + "?confirmation-email-sent=true";
}


@RequestMapping(value = "/event/{eventName}/reservation/{reservationId}/ticket/{ticketIdentifier}/assign", method = RequestMethod.POST, headers = "X-Requested-With=XMLHttpRequest")
public String ajaxAssignTicketToPerson(@PathVariable("eventName") String eventName,
@PathVariable("reservationId") String reservationId,
@PathVariable("ticketIdentifier") String ticketIdentifier,
UpdateTicketOwnerForm updateTicketOwner,
BindingResult bindingResult,
HttpServletRequest request,
Model model) throws Exception {

assignTicket(eventName, reservationId, ticketIdentifier, updateTicketOwner, bindingResult, request, model);
return "/event/assign-ticket-result";

}

@RequestMapping(value = "/event/{eventName}/reservation/{reservationId}/ticket/{ticketIdentifier}/assign", method = RequestMethod.POST)
public String assignTicketToPerson(@PathVariable("eventName") String eventName,
@PathVariable("reservationId") String reservationId,
@PathVariable("ticketIdentifier") String ticketIdentifier,
UpdateTicketOwnerForm updateTicketOwner,
BindingResult bindingResult,
HttpServletRequest request,
Model model) throws Exception {

Optional<Triple<ValidationResult, Event, Ticket>> result = assignTicket(eventName, reservationId, ticketIdentifier, updateTicketOwner, bindingResult, request, model);
return result.map(t -> "redirect:/event/"+t.getMiddle().getShortName()+"/reservation/"+t.getRight().getTicketsReservationId()).orElse("redirect:/");
}

private Optional<Triple<ValidationResult, Event, Ticket>> assignTicket(String eventName, String reservationId, String ticketIdentifier, UpdateTicketOwnerForm updateTicketOwner, BindingResult bindingResult, HttpServletRequest request, Model model) {
Optional<Triple<ValidationResult, Event, Ticket>> triple = ticketReservationManager.fetchComplete(eventName, reservationId, ticketIdentifier)
.map(result -> {
Ticket t = result.getRight();
Validate.isTrue(!t.getLockedAssignment(), "cannot change a locked ticket");
final Event event = result.getLeft();
final TicketReservation ticketReservation = result.getMiddle();
ValidationResult validationResult = Validator.validateTicketAssignment(updateTicketOwner, bindingResult)
.ifSuccess(() -> updateTicketOwner(updateTicketOwner, request, t, event, ticketReservation));
return Triple.of(validationResult, event, ticketRepository.findByUUID(t.getUuid()));
});
triple.ifPresent(t -> {
model.addAttribute("value", t.getRight());
model.addAttribute("validationResult", t.getLeft());
model.addAttribute("countries", getLocalizedCountries(RequestContextUtils.getLocale(request)));
model.addAttribute("event", t.getMiddle());
});
return triple;
}

private void updateTicketOwner(UpdateTicketOwnerForm updateTicketOwner, HttpServletRequest request, Ticket t, Event event, TicketReservation ticketReservation) {
ticketReservationManager.updateTicketOwner(t, RequestContextUtils.getLocale(request), event, updateTicketOwner,
getConfirmationTextBuilder(request, t, event, ticketReservation),
getOwnerChangeTextBuilder(request, t, event),
preparePdfTicket(request, event, ticketReservation, t));
}

private TextTemplateBuilder getOwnerChangeTextBuilder(HttpServletRequest request, Ticket t, Event event) {
return TemplateProcessor.buildEmailForOwnerChange(t.getEmail(), event, t, organizationRepository, ticketReservationManager, templateManager, request);
}

private TextTemplateBuilder getConfirmationTextBuilder(HttpServletRequest request, Ticket t, Event event, TicketReservation ticketReservation) {
return TemplateProcessor.buildEmail(event, organizationRepository, ticketReservation, t, templateManager, request);
}

private PDFTemplateBuilder preparePdfTicket(HttpServletRequest request, Event event, TicketReservation ticketReservation, Ticket ticket) {
TicketCategory ticketCategory = ticketCategoryRepository.getById(ticket.getCategoryId(), event.getId());
Organization organization = organizationRepository.getById(event.getOrganizationId());
try {
return TemplateProcessor.buildPDFTicket(request, event, ticketReservation, ticket, ticketCategory, organization, templateManager);
} catch (WriterException | IOException e) {
throw new IllegalStateException(e);
}
}

private void sendReservationCompleteEmail(HttpServletRequest request, Event event, TicketReservation reservation) {

String reservationTxt = templateManager.render("/alfio/templates/confirmation-email-txt.ms",
Expand Down
114 changes: 0 additions & 114 deletions src/main/java/alfio/controller/api/EventApiController.java

This file was deleted.

1 change: 1 addition & 0 deletions src/main/java/alfio/manager/TicketReservationManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import org.apache.commons.lang3.tuple.Pair;
import org.apache.commons.lang3.tuple.Triple;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
Expand Down
Loading

0 comments on commit 40cf671

Please sign in to comment.