use of net.fortuna.ical4j.model.component.CalendarComponent in project openhab1-addons by openhab.
the class EventReloaderJob method loadEvents.
public void loadEvents(String filename, org.joda.time.DateTime lastResourceChangeFS, final InputStream inputStream, final CalDavConfig config, final List<String> oldEventIds, boolean readFromFile) throws IOException, ParserException {
CalendarBuilder builder = new CalendarBuilder();
InputStreamReader is = new InputStreamReader(inputStream, config.getCharset());
BufferedReader in = new BufferedReader(is, 50);
final UnfoldingReader uin = new UnfoldingReader(in, 50, true);
Calendar calendar = builder.build(uin);
uin.close();
// log.trace("calendar: {}", calendar);
EventContainer eventContainer = new EventContainer(config.getKey());
eventContainer.setFilename(filename);
eventContainer.setLastChanged(lastResourceChangeFS);
org.joda.time.DateTime loadFrom = org.joda.time.DateTime.now().minusMinutes(config.getHistoricLoadMinutes());
org.joda.time.DateTime loadTo = org.joda.time.DateTime.now().plusMinutes(config.getPreloadMinutes());
final ComponentList<CalendarComponent> vEventComponents = calendar.getComponents(Component.VEVENT);
if (vEventComponents.size() == 0) {
log.debug("could not find a VEVENT from calendar build, based on file {}", filename);
// no events inside
if (!readFromFile) {
Util.storeToDisk(config.getKey(), filename, calendar);
}
return;
}
org.joda.time.DateTime lastModifedVEventOverAll = null;
for (CalendarComponent comp : vEventComponents) {
VEvent vEvent = (VEvent) comp;
log.trace("loading event: " + vEvent.getUid().getValue() + ":" + vEvent.getSummary().getValue());
// fallback, because 'LastModified' in VEvent is optional
org.joda.time.DateTime lastModifedVEvent = lastResourceChangeFS;
if (vEvent.getLastModified() != null) {
lastModifedVEvent = new org.joda.time.DateTime(vEvent.getLastModified().getDateTime());
log.trace("overriding lastmodified from file FS ({}) with event's last-modified property ({})", lastResourceChangeFS, lastModifedVEvent);
}
if (!config.isLastModifiedFileTimeStampValid()) {
if (lastModifedVEventOverAll == null || lastModifedVEvent.isAfter(lastModifedVEventOverAll)) {
lastModifedVEventOverAll = lastModifedVEvent;
}
if (eventContainer != null && !lastModifedVEvent.isBefore(eventContainer.getLastChanged())) {
// to be created
if (eventContainer.getCalculatedUntil() != null && vEventComponents.size() == 1 && eventContainer.getCalculatedUntil().isAfter(org.joda.time.DateTime.now().plusMinutes(config.getReloadMinutes()))) {
// the event is calculated as long as the next reload
// interval can handle this
log.trace("skipping resource processing {}, not changed", filename);
continue;
}
if (eventContainer.isHistoricEvent()) {
// no more upcoming events, do nothing
log.trace("skipping resource processing {}, not changed", filename);
continue;
}
}
}
Period period = new Period(new DateTime(loadFrom.toDate()), new DateTime(loadTo.toDate()));
PeriodList periods = vEvent.calculateRecurrenceSet(period);
periods = periods.normalise();
String eventId = vEvent.getUid().getValue();
final String eventName = vEvent.getSummary().getValue();
// no more upcoming events
if (periods.size() > 0) {
if (vEvent.getConsumedTime(new net.fortuna.ical4j.model.Date(), new net.fortuna.ical4j.model.Date(org.joda.time.DateTime.now().plusYears(10).getMillis())).size() == 0) {
log.trace("event will never be occur (historic): {}", eventName);
eventContainer.setHistoricEvent(true);
}
}
// expecting this is for every vEvent inside a calendar equals
eventContainer.setEventId(eventId);
eventContainer.setCalculatedUntil(loadTo);
for (Period p : periods) {
org.joda.time.DateTime start = getDateTime("start", p.getStart(), p.getRangeStart());
org.joda.time.DateTime end = getDateTime("end", p.getEnd(), p.getRangeEnd());
CalDavEvent event = new CalDavEvent(eventName, vEvent.getUid().getValue(), config.getKey(), start, end);
event.setLastChanged(lastModifedVEvent);
if (vEvent.getLocation() != null) {
event.setLocation(vEvent.getLocation().getValue());
}
if (vEvent.getDescription() != null) {
event.setContent(vEvent.getDescription().getValue());
}
event.getCategoryList().addAll(readCategory(vEvent));
event.setFilename(filename);
log.trace("adding event: " + event.getShortName());
eventContainer.getEventList().add(event);
}
}
if (lastModifedVEventOverAll != null && !config.isLastModifiedFileTimeStampValid()) {
eventContainer.setLastChanged(lastModifedVEventOverAll);
log.debug("changing eventcontainer last modified to {}", lastModifedVEventOverAll);
}
// if (!eventContainer.getEventList().isEmpty()) {
CalDavLoaderImpl.instance.addEventToMap(eventContainer, true);
if (!readFromFile) {
Util.storeToDisk(config.getKey(), filename, calendar);
}
// }
}
Aggregations