use of net.fortuna.ical4j.model.DateTime in project openolat by klemens.
the class ICalFileCalendarManager method getVEvent.
private VEvent getVEvent(KalendarEvent kEvent) {
VEvent vEvent = new VEvent();
if (!kEvent.isAllDayEvent()) {
// regular VEvent
DateTime dtBegin = new DateTime(kEvent.getBegin());
if (tz != null) {
dtBegin.setTimeZone(tz);
}
Date kEventEnd = kEvent.getEnd();
if (kEventEnd == null) {
vEvent = new VEvent(dtBegin, kEvent.getSubject());
} else {
DateTime dtEnd = new DateTime(kEventEnd);
if (tz != null) {
dtEnd.setTimeZone(tz);
}
vEvent = new VEvent(dtBegin, dtEnd, kEvent.getSubject());
}
} else {
// AllDay VEvent
net.fortuna.ical4j.model.Date dtBegin = CalendarUtils.createDate(kEvent.getBegin());
// adjust end date: ICal end dates for all day events are on the next day
Date adjustedEndDate = new Date(kEvent.getEnd().getTime() + (1000 * 60 * 60 * 24));
net.fortuna.ical4j.model.Date dtEnd = CalendarUtils.createDate(adjustedEndDate);
vEvent = new VEvent(dtBegin, dtEnd, kEvent.getSubject());
}
if (kEvent.getCreated() > 0) {
Created created = new Created(new DateTime(kEvent.getCreated()));
vEvent.getProperties().add(created);
}
if ((kEvent.getCreatedBy() != null) && !kEvent.getCreatedBy().trim().isEmpty()) {
Contact contact = new Contact();
contact.setValue(kEvent.getCreatedBy());
vEvent.getProperties().add(contact);
}
if (kEvent.getLastModified() > 0) {
LastModified lastMod = new LastModified(new DateTime(kEvent.getLastModified()));
vEvent.getProperties().add(lastMod);
}
// Uid
PropertyList vEventProperties = vEvent.getProperties();
vEventProperties.add(new Uid(kEvent.getID()));
// clazz
switch(kEvent.getClassification()) {
case KalendarEvent.CLASS_PRIVATE:
vEventProperties.add(ICAL_CLASS_PRIVATE);
break;
case KalendarEvent.CLASS_PUBLIC:
vEventProperties.add(ICAL_CLASS_PUBLIC);
break;
case KalendarEvent.CLASS_X_FREEBUSY:
vEventProperties.add(ICAL_CLASS_X_FREEBUSY);
break;
default:
vEventProperties.add(ICAL_CLASS_PRIVATE);
break;
}
// location
if (kEvent.getLocation() != null) {
vEventProperties.add(new Location(kEvent.getLocation()));
}
if (kEvent.getDescription() != null) {
vEventProperties.add(new Description(kEvent.getDescription()));
}
// event links
Url urlOnce = null;
List<KalendarEventLink> kalendarEventLinks = kEvent.getKalendarEventLinks();
if ((kalendarEventLinks != null) && !kalendarEventLinks.isEmpty()) {
for (Iterator<KalendarEventLink> iter = kalendarEventLinks.iterator(); iter.hasNext(); ) {
KalendarEventLink link = iter.next();
StringBuilder linkEncoded = new StringBuilder(200);
linkEncoded.append(link.getProvider());
linkEncoded.append("§");
linkEncoded.append(link.getId());
linkEncoded.append("§");
linkEncoded.append(link.getDisplayName());
linkEncoded.append("§");
linkEncoded.append(link.getURI());
linkEncoded.append("§");
linkEncoded.append(link.getIconCssClass());
XProperty linkProperty = new XProperty(ICAL_X_OLAT_LINK, linkEncoded.toString());
vEventProperties.add(linkProperty);
if (urlOnce == null) {
try {
Url url = new Url();
url.setValue(link.getURI());
urlOnce = url;
} catch (URISyntaxException e) {
log.error("Invalid URL:" + link.getURI());
}
}
}
}
if (urlOnce != null) {
vEventProperties.add(urlOnce);
}
if (kEvent.getComment() != null) {
vEventProperties.add(new XProperty(ICAL_X_OLAT_COMMENT, kEvent.getComment()));
}
if (kEvent.getNumParticipants() != null) {
vEventProperties.add(new XProperty(ICAL_X_OLAT_NUMPARTICIPANTS, Integer.toString(kEvent.getNumParticipants())));
}
if (kEvent.getParticipants() != null) {
StringBuilder strBuf = new StringBuilder();
String[] participants = kEvent.getParticipants();
for (String participant : participants) {
strBuf.append(participant);
strBuf.append("§");
}
vEventProperties.add(new XProperty(ICAL_X_OLAT_PARTICIPANTS, strBuf.toString()));
}
if (kEvent.getSourceNodeId() != null) {
vEventProperties.add(new XProperty(ICAL_X_OLAT_SOURCENODEID, kEvent.getSourceNodeId()));
}
if (kEvent.getManagedFlags() != null) {
String val = CalendarManagedFlag.toString(kEvent.getManagedFlags());
vEventProperties.add(new XProperty(ICAL_X_OLAT_MANAGED, val));
}
if (StringHelper.containsNonWhitespace(kEvent.getExternalId())) {
vEventProperties.add(new XProperty(ICAL_X_OLAT_EXTERNAL_ID, kEvent.getExternalId()));
}
if (StringHelper.containsNonWhitespace(kEvent.getExternalSource())) {
vEventProperties.add(new XProperty(ICAL_X_OLAT_EXTERNAL_SOURCE, kEvent.getExternalSource()));
}
String recurenceId = kEvent.getRecurrenceID();
if (StringHelper.containsNonWhitespace(recurenceId)) {
try {
RecurrenceId recurId = new RecurrenceId(tz);
// VALUE=DATE recurrence id need to be specially saved
if (recurenceId.length() < 9) {
recurId = new RecurrenceId(tz);
recurId.setDate(CalendarUtils.createDate(new net.fortuna.ical4j.model.Date(recurenceId)));
} else {
recurId = new RecurrenceId(recurenceId, tz);
}
vEventProperties.add(recurId);
} catch (ParseException e) {
log.error("cannot create recurrence ID: " + recurenceId, e);
}
}
// recurrence
String recurrence = kEvent.getRecurrenceRule();
if (recurrence != null && !recurrence.equals("")) {
try {
Recur recur = new Recur(recurrence);
RRule rrule = new RRule(recur);
vEventProperties.add(rrule);
} catch (ParseException e) {
log.error("cannot create recurrence rule: " + recurrence.toString(), e);
}
}
// recurrence exclusions
String recurrenceExc = kEvent.getRecurrenceExc();
if (recurrenceExc != null && !recurrenceExc.equals("")) {
ExDate exdate = new ExDate();
try {
exdate.setValue(recurrenceExc);
vEventProperties.add(exdate);
} catch (ParseException e) {
log.error("", e);
}
}
return vEvent;
}
use of net.fortuna.ical4j.model.DateTime in project openolat by klemens.
the class ICalFileCalendarManager method getRecurrenceRule.
/**
* Build iCalendar-compliant recurrence rule
* @param recurrence
* @param recurrenceEnd
* @return rrule
*/
@Override
public String getRecurrenceRule(String recurrence, Date recurrenceEnd) {
if (recurrence != null) {
// recurrence available
// create recurrence rule
StringBuilder sb = new StringBuilder();
sb.append("FREQ=");
if (recurrence.equals(KalendarEvent.WORKDAILY)) {
// build rule for monday to friday
sb.append(KalendarEvent.DAILY).append(";").append("BYDAY=MO,TU,WE,TH,FR");
} else if (recurrence.equals(KalendarEvent.BIWEEKLY)) {
// build rule for biweekly
sb.append(KalendarEvent.WEEKLY).append(";").append("INTERVAL=2");
} else {
// normal supported recurrence
sb.append(recurrence);
}
if (recurrenceEnd != null) {
java.util.Calendar recurEndCal = java.util.Calendar.getInstance();
recurEndCal.setTimeZone(tz);
recurEndCal.setTime(recurrenceEnd);
recurEndCal = CalendarUtils.getEndOfDay(recurEndCal);
long recTime = recurEndCal.getTimeInMillis() - tz.getOffset(recurEndCal.getTimeInMillis());
DateTime recurEndDT = new DateTime(recTime);
if (tz != null) {
recurEndDT.setTimeZone(tz);
}
sb.append(";").append(KalendarEvent.UNTIL).append("=").append(recurEndDT.toString());
}
try {
Recur recur = new Recur(sb.toString());
RRule rrule = new RRule(recur);
return rrule.getValue();
} catch (ParseException e) {
log.error("cannot create recurrence rule: " + recurrence.toString(), e);
}
}
return null;
}
use of net.fortuna.ical4j.model.DateTime in project OpenOLAT by OpenOLAT.
the class ICalFileCalendarManager method getVEvent.
private VEvent getVEvent(KalendarEvent kEvent) {
VEvent vEvent = new VEvent();
if (!kEvent.isAllDayEvent()) {
// regular VEvent
DateTime dtBegin = new DateTime(kEvent.getBegin());
if (tz != null) {
dtBegin.setTimeZone(tz);
}
Date kEventEnd = kEvent.getEnd();
if (kEventEnd == null) {
vEvent = new VEvent(dtBegin, kEvent.getSubject());
} else {
DateTime dtEnd = new DateTime(kEventEnd);
if (tz != null) {
dtEnd.setTimeZone(tz);
}
vEvent = new VEvent(dtBegin, dtEnd, kEvent.getSubject());
}
} else {
// AllDay VEvent
net.fortuna.ical4j.model.Date dtBegin = CalendarUtils.createDate(kEvent.getBegin());
// adjust end date: ICal end dates for all day events are on the next day
Date adjustedEndDate = new Date(kEvent.getEnd().getTime() + (1000 * 60 * 60 * 24));
net.fortuna.ical4j.model.Date dtEnd = CalendarUtils.createDate(adjustedEndDate);
vEvent = new VEvent(dtBegin, dtEnd, kEvent.getSubject());
}
if (kEvent.getCreated() > 0) {
Created created = new Created(new DateTime(kEvent.getCreated()));
vEvent.getProperties().add(created);
}
if ((kEvent.getCreatedBy() != null) && !kEvent.getCreatedBy().trim().isEmpty()) {
Contact contact = new Contact();
contact.setValue(kEvent.getCreatedBy());
vEvent.getProperties().add(contact);
}
if (kEvent.getLastModified() > 0) {
LastModified lastMod = new LastModified(new DateTime(kEvent.getLastModified()));
vEvent.getProperties().add(lastMod);
}
// Uid
PropertyList vEventProperties = vEvent.getProperties();
vEventProperties.add(new Uid(kEvent.getID()));
// clazz
switch(kEvent.getClassification()) {
case KalendarEvent.CLASS_PRIVATE:
vEventProperties.add(ICAL_CLASS_PRIVATE);
break;
case KalendarEvent.CLASS_PUBLIC:
vEventProperties.add(ICAL_CLASS_PUBLIC);
break;
case KalendarEvent.CLASS_X_FREEBUSY:
vEventProperties.add(ICAL_CLASS_X_FREEBUSY);
break;
default:
vEventProperties.add(ICAL_CLASS_PRIVATE);
break;
}
// location
if (kEvent.getLocation() != null) {
vEventProperties.add(new Location(kEvent.getLocation()));
}
if (kEvent.getDescription() != null) {
vEventProperties.add(new Description(kEvent.getDescription()));
}
// event links
Url urlOnce = null;
List<KalendarEventLink> kalendarEventLinks = kEvent.getKalendarEventLinks();
if ((kalendarEventLinks != null) && !kalendarEventLinks.isEmpty()) {
for (Iterator<KalendarEventLink> iter = kalendarEventLinks.iterator(); iter.hasNext(); ) {
KalendarEventLink link = iter.next();
StringBuilder linkEncoded = new StringBuilder(200);
linkEncoded.append(link.getProvider());
linkEncoded.append("§");
linkEncoded.append(link.getId());
linkEncoded.append("§");
linkEncoded.append(link.getDisplayName());
linkEncoded.append("§");
linkEncoded.append(link.getURI());
linkEncoded.append("§");
linkEncoded.append(link.getIconCssClass());
XProperty linkProperty = new XProperty(ICAL_X_OLAT_LINK, linkEncoded.toString());
vEventProperties.add(linkProperty);
if (urlOnce == null) {
try {
Url url = new Url();
url.setValue(link.getURI());
urlOnce = url;
} catch (URISyntaxException e) {
log.error("Invalid URL:" + link.getURI());
}
}
}
}
if (urlOnce != null) {
vEventProperties.add(urlOnce);
}
if (kEvent.getComment() != null) {
vEventProperties.add(new XProperty(ICAL_X_OLAT_COMMENT, kEvent.getComment()));
}
if (kEvent.getNumParticipants() != null) {
vEventProperties.add(new XProperty(ICAL_X_OLAT_NUMPARTICIPANTS, Integer.toString(kEvent.getNumParticipants())));
}
if (kEvent.getParticipants() != null) {
StringBuilder strBuf = new StringBuilder();
String[] participants = kEvent.getParticipants();
for (String participant : participants) {
strBuf.append(participant);
strBuf.append("§");
}
vEventProperties.add(new XProperty(ICAL_X_OLAT_PARTICIPANTS, strBuf.toString()));
}
if (kEvent.getSourceNodeId() != null) {
vEventProperties.add(new XProperty(ICAL_X_OLAT_SOURCENODEID, kEvent.getSourceNodeId()));
}
if (kEvent.getManagedFlags() != null) {
String val = CalendarManagedFlag.toString(kEvent.getManagedFlags());
vEventProperties.add(new XProperty(ICAL_X_OLAT_MANAGED, val));
}
if (StringHelper.containsNonWhitespace(kEvent.getExternalId())) {
vEventProperties.add(new XProperty(ICAL_X_OLAT_EXTERNAL_ID, kEvent.getExternalId()));
}
if (StringHelper.containsNonWhitespace(kEvent.getExternalSource())) {
vEventProperties.add(new XProperty(ICAL_X_OLAT_EXTERNAL_SOURCE, kEvent.getExternalSource()));
}
String recurenceId = kEvent.getRecurrenceID();
if (StringHelper.containsNonWhitespace(recurenceId)) {
try {
RecurrenceId recurId = new RecurrenceId(tz);
// VALUE=DATE recurrence id need to be specially saved
if (recurenceId.length() < 9) {
recurId = new RecurrenceId(tz);
recurId.setDate(CalendarUtils.createDate(new net.fortuna.ical4j.model.Date(recurenceId)));
} else {
recurId = new RecurrenceId(recurenceId, tz);
}
vEventProperties.add(recurId);
} catch (ParseException e) {
log.error("cannot create recurrence ID: " + recurenceId, e);
}
}
// recurrence
String recurrence = kEvent.getRecurrenceRule();
if (recurrence != null && !recurrence.equals("")) {
try {
Recur recur = new Recur(recurrence);
RRule rrule = new RRule(recur);
vEventProperties.add(rrule);
} catch (ParseException e) {
log.error("cannot create recurrence rule: " + recurrence.toString(), e);
}
}
// recurrence exclusions
String recurrenceExc = kEvent.getRecurrenceExc();
if (recurrenceExc != null && !recurrenceExc.equals("")) {
ExDate exdate = new ExDate();
try {
exdate.setValue(recurrenceExc);
vEventProperties.add(exdate);
} catch (ParseException e) {
log.error("", e);
}
}
return vEvent;
}
use of net.fortuna.ical4j.model.DateTime in project OpenOLAT by OpenOLAT.
the class ICalFileCalendarManager method getRecurrenceEndDate.
/**
* @param rule
* @return date of recurrence end
*/
@Override
public Date getRecurrenceEndDate(String rule) {
if (rule != null) {
try {
TimeZone ltz = calendarModule.getDefaultTimeZone();
Recur recur = new Recur(rule);
Date dUntil = recur.getUntil();
DateTime dtUntil = dUntil == null ? null : new DateTime(dUntil.getTime());
if (dtUntil != null) {
if (ltz != null) {
dtUntil.setTimeZone(ltz);
}
return dtUntil;
}
} catch (ParseException e) {
log.error("cannot restore recurrence rule", e);
}
}
return null;
}
use of net.fortuna.ical4j.model.DateTime in project OpenOLAT by OpenOLAT.
the class ICalFileCalendarManager method getRecurringEventsInPeriod.
private final List<KalendarRecurEvent> getRecurringEventsInPeriod(KalendarEvent kEvent, Date periodStart, Date periodEnd, TimeZone userTz) {
VEvent vEvent = getVEvent(kEvent);
if (vEvent.getEndDate() == null || vEvent.getStartDate().getDate().after(vEvent.getEndDate().getDate())) {
return Collections.emptyList();
}
// calculate the events in the specified period
Period recurringPeriod = new Period(new DateTime(periodStart), new DateTime(periodEnd));
PeriodList periodList = vEvent.calculateRecurrenceSet(recurringPeriod);
List<KalendarRecurEvent> recurringEvents = new ArrayList<>(periodList.size());
for (Object obj : periodList) {
Period period = (Period) obj;
Date date = period.getStart();
java.util.Calendar eventStartCal = java.util.Calendar.getInstance();
eventStartCal.clear();
eventStartCal.setTime(kEvent.getBegin());
java.util.Calendar eventEndCal = java.util.Calendar.getInstance();
eventEndCal.clear();
eventEndCal.setTime(kEvent.getEnd());
java.util.Calendar recurStartCal = java.util.Calendar.getInstance();
recurStartCal.clear();
if (userTz == null) {
recurStartCal.setTimeInMillis(date.getTime());
} else {
recurStartCal.setTimeInMillis(date.getTime() - userTz.getOffset(date.getTime()));
}
long duration = kEvent.getEnd().getTime() - kEvent.getBegin().getTime();
java.util.Calendar beginCal = java.util.Calendar.getInstance();
beginCal.clear();
beginCal.set(recurStartCal.get(java.util.Calendar.YEAR), recurStartCal.get(java.util.Calendar.MONTH), recurStartCal.get(java.util.Calendar.DATE), eventStartCal.get(java.util.Calendar.HOUR_OF_DAY), eventStartCal.get(java.util.Calendar.MINUTE), eventStartCal.get(java.util.Calendar.SECOND));
java.util.Calendar endCal = java.util.Calendar.getInstance();
endCal.clear();
endCal.setTimeInMillis(beginCal.getTimeInMillis() + duration);
boolean original = false;
if (kEvent.getBegin().compareTo(beginCal.getTime()) == 0) {
// prevent doubled events
original = true;
}
Date recurrenceEnd = getRecurrenceEndDate(kEvent.getRecurrenceRule());
if (kEvent.isAllDayEvent() && recurrenceEnd != null && recurStartCal.getTime().after(recurrenceEnd)) {
// workaround for ical4j-bug in all day events
continue;
}
KalendarRecurEvent recurEvent = new KalendarRecurEvent(kEvent.getID(), original, kEvent.getSubject(), beginCal.getTime(), endCal.getTime());
recurEvent.setOccurenceDate(beginCal.getTime());
recurEvent.setSourceEvent(kEvent);
recurringEvents.add(recurEvent);
}
return recurringEvents;
}
Aggregations