use of net.fortuna.ical4j.model.Recur in project openolat by klemens.
the class ICalFileCalendarManager method getRecurringsInPeriod.
private final DateList getRecurringsInPeriod(Date periodStart, Date periodEnd, KalendarEvent kEvent) {
DateList recurDates = null;
String recurrenceRule = kEvent.getRecurrenceRule();
if (StringHelper.containsNonWhitespace(recurrenceRule)) {
try {
Recur recur = new Recur(recurrenceRule);
net.fortuna.ical4j.model.Date periodStartDate = CalendarUtils.createDate(periodStart);
net.fortuna.ical4j.model.Date periodEndDate = CalendarUtils.createDate(periodEnd);
net.fortuna.ical4j.model.Date eventStartDate = CalendarUtils.createDate(kEvent.getBegin());
recurDates = recur.getDates(eventStartDate, periodStartDate, periodEndDate, Value.DATE);
} catch (ParseException e) {
log.error("cannot restore recurrence rule: " + recurrenceRule, e);
}
String recurrenceExc = kEvent.getRecurrenceExc();
if (recurrenceExc != null && !recurrenceExc.equals("")) {
try {
ExDate exdate = new ExDate();
// see OLAT-5645
if (recurrenceExc.length() > 8) {
exdate.setValue(recurrenceExc);
} else {
exdate.getParameters().replace(Value.DATE);
exdate.setValue(recurrenceExc);
}
for (Object date : exdate.getDates()) {
if (recurDates.contains(date))
recurDates.remove(date);
}
} catch (ParseException e) {
log.error("cannot restore excluded dates for this recurrence: " + recurrenceExc, e);
}
}
}
return recurDates;
}
use of net.fortuna.ical4j.model.Recur 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.Recur 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.Recur in project openolat by klemens.
the class CalendarUtils method getRecurrence.
public static String getRecurrence(String rule) {
if (rule != null) {
try {
Recur recur = new Recur(rule);
String frequency = recur.getFrequency();
WeekDayList wdl = recur.getDayList();
Integer interval = recur.getInterval();
if ((wdl != null && wdl.size() > 0)) {
// we only support one rule with daylist
return KalendarEvent.WORKDAILY;
} else if (interval != null && interval == 2) {
// we only support one rule with interval
return KalendarEvent.BIWEEKLY;
} else {
// native supportet rule
return frequency;
}
} catch (ParseException e) {
log.error("cannot restore recurrence rule", e);
}
}
return null;
}
use of net.fortuna.ical4j.model.Recur in project OpenOLAT by OpenOLAT.
the class ICalFileCalendarManager method removeFutureOfEvent.
@Override
public boolean removeFutureOfEvent(Kalendar cal, KalendarRecurEvent kalendarEvent) {
OLATResourceable calOres = getOresHelperFor(cal);
Boolean removeSuccessful = CoordinatorManager.getInstance().getCoordinator().getSyncer().doInSync(calOres, new SyncerCallback<Boolean>() {
@Override
public Boolean execute() {
boolean successfullyPersist = false;
try {
String uid = kalendarEvent.getID();
Date occurenceDate = kalendarEvent.getOccurenceDate();
Kalendar loadedCal = getCalendarFromCache(cal.getType(), cal.getCalendarID());
KalendarEvent rootEvent = loadedCal.getEvent(kalendarEvent.getID(), null);
String rRule = rootEvent.getRecurrenceRule();
Recur recur = new Recur(rRule);
recur.setUntil(CalendarUtils.createDate(occurenceDate));
RRule rrule = new RRule(recur);
rootEvent.setRecurrenceRule(rrule.getValue());
for (KalendarEvent kEvent : loadedCal.getEvents()) {
if (uid.equals(kEvent.getID()) && StringHelper.containsNonWhitespace(kEvent.getRecurrenceID()) && occurenceDate.before(kEvent.getBegin())) {
loadedCal.removeEvent(kEvent);
}
}
successfullyPersist = persistCalendar(loadedCal);
} catch (ParseException e) {
log.error("", e);
}
return new Boolean(successfullyPersist);
}
});
// inform all controller about calendar change for reload
CoordinatorManager.getInstance().getCoordinator().getEventBus().fireEventToListenersOf(new CalendarGUIModifiedEvent(cal), OresHelper.lookupType(CalendarManager.class));
return removeSuccessful.booleanValue();
}
Aggregations