use of biweekly.io.text.ICalWriter in project alf.io by alfio-event.
the class EventUtil method getIcalForEvent.
public static Optional<byte[]> getIcalForEvent(Event event, TicketCategory ticketCategory, String description) {
ICalendar ical = new ICalendar();
VEvent vEvent = new VEvent();
vEvent.setSummary(event.getDisplayName());
vEvent.setDescription(description);
vEvent.setLocation(StringUtils.replacePattern(event.getLocation(), "[\n\r\t]+", " "));
ZonedDateTime begin = Optional.ofNullable(ticketCategory).map(tc -> tc.getTicketValidityStart(event.getZoneId())).orElse(event.getBegin());
ZonedDateTime end = Optional.ofNullable(ticketCategory).map(tc -> tc.getTicketValidityEnd(event.getZoneId())).orElse(event.getEnd());
vEvent.setDateStart(Date.from(begin.toInstant()));
vEvent.setDateEnd(Date.from(end.toInstant()));
vEvent.setUrl(event.getWebsiteUrl());
ical.addEvent(vEvent);
StringWriter strWriter = new StringWriter();
try (ICalWriter writer = new ICalWriter(strWriter, ICalVersion.V2_0)) {
writer.write(ical);
return Optional.of(strWriter.toString().getBytes(StandardCharsets.UTF_8));
} catch (IOException e) {
log.warn("was not able to generate iCal for event " + event.getShortName(), e);
return Optional.empty();
}
}
Aggregations