modifyTickets(con, daos, event.isDraft());
return null;
}
private void modifyTickets(PartakeConnection con, IPartakeDAOs daos, boolean forDraft) throws DAOException, PartakeException {
IEventTicketAccess dao = daos.getEventTicketAccess();
List<EventTicket> originalTickets = dao.findEventTicketsByEventId(con, eventId);
boolean[] processed = new boolean[originalTickets.size()];
// |tickets| should contain all the original ticket.
for (EventTicket ticket : tickets) {
EventTicket originalTicket = null;
for (int i = 0; i < originalTickets.size(); ++i) {
if (!originalTickets.get(i).getId().equals(ticket.getId()))
continue;
if (processed[i])
throw new PartakeException(UserErrorCode.INVALID_TICKET_DUPLICATE_ID);
// Found the original ticket.
processed[i] = true;
originalTicket = originalTickets.get(i);
break;
}
if (originalTicket == null) {
// If new ticket has id, it's strange. Otherwise, add a new ticket id.
if (ticket.getId() != null)
throw new PartakeException(UserErrorCode.INVALID_PARAMETERS);
ticket.setId(daos.getEventTicketAccess().getFreshId(con));
}
}
// If the event has already been published, all ticket should be preserved.
// However, it's OK to remove the ticket that no participants.
if (!forDraft) {
for (int i = 0; i < processed.length; ++i) {
UUID eventTicketId = originalTickets.get(i).getId();
if (!processed[i] && daos.getEnrollmentAccess().countByTicketId(con, eventTicketId) > 0)
throw new PartakeException(UserErrorCode.INVALID_TICKET_REMOVAL_ENROLLED);
}
}
// OK. let's save the tickets.
dao.removeByEventId(con, eventId);
for (EventTicket ticket : tickets)
dao.put(con, ticket);
}