Search in sources :

Example 1 with ICalWriter

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();
    }
}
Also used : VEvent(biweekly.component.VEvent) ICalendar(biweekly.ICalendar) UriComponentsBuilder(org.springframework.web.util.UriComponentsBuilder) DateTimeFormatterBuilder(java.time.format.DateTimeFormatterBuilder) ChronoField(java.time.temporal.ChronoField) java.util(java.util) ICalWriter(biweekly.io.text.ICalWriter) Predicate(java.util.function.Predicate) StringWriter(java.io.StringWriter) ZonedDateTime(java.time.ZonedDateTime) MapSqlParameterSource(org.springframework.jdbc.core.namedparam.MapSqlParameterSource) IOException(java.io.IOException) SaleableTicketCategory(alfio.controller.decorator.SaleableTicketCategory) ConfigurationManager(alfio.manager.system.ConfigurationManager) VEvent(biweekly.component.VEvent) ICalVersion(biweekly.ICalVersion) StringUtils(org.apache.commons.lang3.StringUtils) StandardCharsets(java.nio.charset.StandardCharsets) UtilityClass(lombok.experimental.UtilityClass) Stream(java.util.stream.Stream) alfio.model(alfio.model) DateTimeFormatter(java.time.format.DateTimeFormatter) Configuration(alfio.model.system.Configuration) Log4j2(lombok.extern.log4j.Log4j2) ConfigurationKeys(alfio.model.system.ConfigurationKeys) ICalWriter(biweekly.io.text.ICalWriter) StringWriter(java.io.StringWriter) ZonedDateTime(java.time.ZonedDateTime) ICalendar(biweekly.ICalendar) IOException(java.io.IOException)

Aggregations

SaleableTicketCategory (alfio.controller.decorator.SaleableTicketCategory)1 ConfigurationManager (alfio.manager.system.ConfigurationManager)1 alfio.model (alfio.model)1 Configuration (alfio.model.system.Configuration)1 ConfigurationKeys (alfio.model.system.ConfigurationKeys)1 ICalVersion (biweekly.ICalVersion)1 ICalendar (biweekly.ICalendar)1 VEvent (biweekly.component.VEvent)1 ICalWriter (biweekly.io.text.ICalWriter)1 IOException (java.io.IOException)1 StringWriter (java.io.StringWriter)1 StandardCharsets (java.nio.charset.StandardCharsets)1 ZonedDateTime (java.time.ZonedDateTime)1 DateTimeFormatter (java.time.format.DateTimeFormatter)1 DateTimeFormatterBuilder (java.time.format.DateTimeFormatterBuilder)1 ChronoField (java.time.temporal.ChronoField)1 java.util (java.util)1 Predicate (java.util.function.Predicate)1 Stream (java.util.stream.Stream)1 UtilityClass (lombok.experimental.UtilityClass)1