Search in sources :

Example 1 with ICalendar

use of biweekly.ICalendar 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)

Example 2 with ICalendar

use of biweekly.ICalendar in project substitution-schedule-parser by vertretungsplanme.

the class BaseIcalParser method getAdditionalInfo.

@Override
public AdditionalInfo getAdditionalInfo() throws IOException {
    AdditionalInfo info = new AdditionalInfo();
    info.setTitle(getTitle());
    String rawdata = httpGet(getIcalUrl(), "UTF-8");
    if (shouldStripTimezoneInfo()) {
        Pattern pattern = Pattern.compile("BEGIN:VTIMEZONE.*END:VTIMEZONE", Pattern.DOTALL);
        rawdata = pattern.matcher(rawdata).replaceAll("");
    }
    DateTime now = DateTime.now().withTimeAtStartOfDay();
    List<ICalendar> icals = Biweekly.parse(rawdata).all();
    List<Event> events = new ArrayList<>();
    for (ICalendar ical : icals) {
        for (VEvent event : ical.getEvents()) {
            Event item = new Event();
            TimeZone timezoneStart = getTimeZoneStart(ical, event);
            if (event.getDescription() != null) {
                item.description = event.getDescription().getValue();
            }
            if (event.getSummary() != null) {
                item.summary = event.getSummary().getValue();
            }
            if (event.getDateStart() != null) {
                item.startDate = new DateTime(event.getDateStart().getValue());
                item.startHasTime = event.getDateStart().getValue().hasTime();
            } else {
                continue;
            }
            if (event.getDateEnd() != null) {
                item.endDate = new DateTime(event.getDateEnd().getValue());
                item.endHasTime = event.getDateEnd().getValue().hasTime();
            }
            if (event.getLocation() != null) {
                item.location = event.getLocation().getValue();
            }
            if (event.getUrl() != null) {
                item.url = event.getUrl().getValue();
            }
            if (event.getRecurrenceRule() == null && item.endDate != null && (item.endDate.compareTo(now) < 0)) {
                continue;
            } else if (event.getRecurrenceRule() == null && (item.startDate.compareTo(now) < 0)) {
                continue;
            }
            if (event.getRecurrenceRule() != null && event.getRecurrenceRule().getValue().getUntil() != null && event.getRecurrenceRule().getValue().getUntil().compareTo(now.toDate()) < 0) {
                continue;
            }
            if (event.getRecurrenceRule() != null) {
                Duration duration = null;
                if (event.getDateEnd() != null) {
                    duration = new Duration(new DateTime(event.getDateStart().getValue()), new DateTime(event.getDateEnd().getValue()));
                }
                DateIterator iterator = event.getDateIterator(timezoneStart);
                while (iterator.hasNext()) {
                    Date date = iterator.next();
                    Event reccitem = item.clone();
                    reccitem.startDate = new DateTime(date);
                    reccitem.endDate = reccitem.startDate.plus(duration);
                    if (item.startDate.equals(reccitem.startDate))
                        continue;
                    if (item.endDate != null && (item.endDate.compareTo(now) < 0)) {
                        continue;
                    } else if (item.endDate == null && (item.startDate.compareTo(now) < 0)) {
                        continue;
                    }
                    events.add(reccitem);
                }
            }
            if (item.endDate != null && (item.endDate.compareTo(now) < 0)) {
                continue;
            } else if (item.endDate == null && (item.startDate.compareTo(now) < 0)) {
                continue;
            }
            events.add(item);
        }
    }
    Collections.sort(events, new Comparator<Event>() {

        @Override
        public int compare(Event o1, Event o2) {
            return o1.startDate.compareTo(o2.startDate);
        }
    });
    StringBuilder content = new StringBuilder();
    int count = 0;
    DateTimeFormatter fmtDt = DateTimeFormat.shortDateTime().withLocale(Locale.GERMANY);
    DateTimeFormatter fmtD = DateTimeFormat.shortDate().withLocale(Locale.GERMANY);
    for (Event item : events) {
        if (count >= getMaxItemsCount()) {
            break;
        } else if (count != 0) {
            content.append("<br><br>\n\n");
        }
        DateTime start = item.startDate;
        if (item.endDate != null) {
            DateTime end = item.endDate;
            if (!item.endHasTime) {
                end = end.minusDays(1);
            }
            content.append((item.startHasTime ? fmtDt : fmtD).print(start));
            if (!end.equals(start)) {
                content.append(" - ");
                content.append((item.endHasTime ? fmtDt : fmtD).print(end));
            }
        } else {
            content.append(fmtDt.print(start));
        }
        content.append("<br>\n");
        content.append("<b>");
        content.append(item.summary);
        content.append("</b>");
        count++;
    }
    info.setText(content.toString());
    return info;
}
Also used : VEvent(biweekly.component.VEvent) AdditionalInfo(me.vertretungsplan.objects.AdditionalInfo) Pattern(java.util.regex.Pattern) DateIterator(biweekly.util.com.google.ical.compat.javautil.DateIterator) Duration(org.joda.time.Duration) DateTime(org.joda.time.DateTime) VEvent(biweekly.component.VEvent) ICalendar(biweekly.ICalendar) DateTimeFormatter(org.joda.time.format.DateTimeFormatter)

Example 3 with ICalendar

use of biweekly.ICalendar in project drbookings by DrBookings.

the class ICalBookingFactory method build.

@Override
public Collection<BookingBeanSer> build() throws IOException {
    final Collection<BookingBeanSer> result = new ArrayList<>();
    final List<ICalendar> icals = Biweekly.parse(file).all();
    for (final ICalendar ical : icals) {
        for (final VEvent e : ical.getEvents()) {
            try {
                result.add(processEvent(e));
            } catch (final Exception ex) {
                if (logger.isInfoEnabled()) {
                    logger.info("Failed to process event ", e);
                }
            // ex.printStackTrace();
            }
        }
    }
    return result;
}
Also used : VEvent(biweekly.component.VEvent) ArrayList(java.util.ArrayList) ICalendar(biweekly.ICalendar) BookingBeanSer(com.github.drbookings.model.ser.BookingBeanSer) IOException(java.io.IOException)

Example 4 with ICalendar

use of biweekly.ICalendar in project common by zenlunatics.

the class EventProvider method writeICS.

// --------------------------------------------------------------------------
@AdminTask
public synchronized void writeICS(Request request) throws IOException {
    ICalendar ical = new ICalendar();
    String string = request.site.getSettings().getString("time zone");
    TimeZone time_zone = TimeZone.getTimeZone(string);
    TimezoneAssignment tza = TimezoneAssignment.download(time_zone, false);
    ical.getTimezoneInfo().setDefaultTimezone(tza);
    ArrayList<Event> events = new ArrayList<Event>();
    addAllEvents(events, request);
    for (Event event : events) {
        VEvent vevent = event.newVEvent(request);
        if (vevent != null)
            ical.addEvent(vevent);
    }
    FilePathStringBuilder fpsb = request.site.getBaseFilePath().append("calendars");
    new File(fpsb.toString()).mkdirs();
    String file_path = fpsb.append(m_name).toString();
    Biweekly.write(ical).go(new File(file_path));
    new File(file_path).renameTo(new File(file_path + ".ics"));
}
Also used : VEvent(biweekly.component.VEvent) TimeZone(java.util.TimeZone) TimezoneAssignment(biweekly.io.TimezoneAssignment) ArrayList(java.util.ArrayList) VEvent(biweekly.component.VEvent) ICalendar(biweekly.ICalendar) FilePathStringBuilder(web.FilePathStringBuilder) File(java.io.File) AdminTask(web.AdminTask)

Aggregations

ICalendar (biweekly.ICalendar)4 VEvent (biweekly.component.VEvent)4 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 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 TimezoneAssignment (biweekly.io.TimezoneAssignment)1 ICalWriter (biweekly.io.text.ICalWriter)1 DateIterator (biweekly.util.com.google.ical.compat.javautil.DateIterator)1 BookingBeanSer (com.github.drbookings.model.ser.BookingBeanSer)1 File (java.io.File)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