Search in sources :

Example 1 with DateIterator

use of biweekly.util.com.google.ical.compat.javautil.DateIterator 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)

Aggregations

ICalendar (biweekly.ICalendar)1 VEvent (biweekly.component.VEvent)1 DateIterator (biweekly.util.com.google.ical.compat.javautil.DateIterator)1 Pattern (java.util.regex.Pattern)1 AdditionalInfo (me.vertretungsplan.objects.AdditionalInfo)1 DateTime (org.joda.time.DateTime)1 Duration (org.joda.time.Duration)1 DateTimeFormatter (org.joda.time.format.DateTimeFormatter)1