use of net.fortuna.ical4j.model.DateTime in project zm-mailbox by Zimbra.
the class SchedulingViewOfTnef method getRecurrenceIdTime.
/**
* Candidate properties :
*
* PidLidExceptionReplaceTime specifies UTC date and time
* within a recurrence pattern that an exception will replace.
*
* @return A suitable time for the RECURRENCE-ID property for this
* calendaring item if one exists.
* @throws IOException
*/
public DateTime getRecurrenceIdTime() throws IOException {
// Note: Was assuming PidLidOldWhenStartWhole was a good candidate
// but that is used for non-recurrence related objects
// to give the previous start time of a single appointment.
DateTime recurrenceIdTime = MapiPropertyId.PidLidExceptionReplaceTime.getDateTimeAsUTC(this);
if (recurrenceIdTime != null) {
sLog.debug("RECURRENCE-ID taken from PidLidExceptionReplaceTime");
} else {
getGlobalObjectId();
if (this.globalObjectId == null) {
return null;
}
int origYear = globalObjectId.getOrigInstanceYear();
if (origYear == 0) {
return null;
}
int origMonth = globalObjectId.getOrigInstanceMonth();
int origDay = globalObjectId.getOrigInstanceDay();
// PidLidStartRecurrenceTime identifies start time of the recurrence pattern.
Integer recurTime = MapiPropertyId.PidLidStartRecurrenceTime.getIntegerValue(this);
int secs = 0;
int mins = 0;
int hrs = 0;
if (recurTime != null) {
// bottom 6 bits are seconds, next 6 bits are minutes, rest is hours
secs = recurTime & 0x0000003f;
mins = (recurTime & 0x00000fc0) >> 6;
hrs = (recurTime & 0xfffff000) >> 12;
sLog.debug("RECURRENCE-ID taken from GlobalObjectId/PidLidStartRecurrenceTime");
} else {
sLog.debug("RECURRENCE-ID taken from JUST GlobalObjectId");
}
try {
String dateString;
if (recurrenceTZinfo == null) {
dateString = String.format("%04d%02d%02dT%02d%02d%02dZ", origYear, origMonth, origDay, hrs, mins, secs);
recurrenceIdTime = new DateTime(dateString);
} else {
dateString = String.format("%04d%02d%02dT%02d%02d%02d", origYear, origMonth, origDay, hrs, mins, secs);
recurrenceIdTime = new DateTime(dateString, recurrenceTZinfo.getTimeZone());
}
} catch (ParseException e) {
sLog.debug("Problem constructing recurrence-id time", e);
}
}
return recurrenceIdTime;
}
use of net.fortuna.ical4j.model.DateTime in project zm-mailbox by Zimbra.
the class RecurrenceDefinition method icalRecurrenceProperty.
/**
* @param isAllDay
* @param isFloating
* @return The main recurrence property as ICAL - typically an RRULE but
* could theoretically be an X-MICROSOFT-RRULE
* @throws ServiceException
*/
public Property icalRecurrenceProperty(boolean isAllDay, boolean isFloating) throws ServiceException {
RRule theRule = null;
if (this.calScale.isSolarCalendar() == false) {
throw TNEFtoIcalendarServiceException.NON_SOLAR_CALENDAR();
}
// iCal4j Recur is a bit basic when it come to building things
// up from components, for instance, there currently isn't a way
// to set any BYDAY= value other than in the constructor from a String
StringBuffer recurrenceRule = new StringBuffer("FREQ=");
// According to RFC, only absolutely NEED WKST of have a
// weekly recurrence with interval greater than 1 OR using
// BYWEEKNO - however, does no harm to always include it.
String weekStartDay = firstDayOfWeek.toString();
boolean hasBYDAY = false;
boolean isYearly = false;
int interval = 1;
switch(patternType) {
case DAY:
recurrenceRule.append(Recur.DAILY);
interval = Long.valueOf(period).intValue() / 1440;
break;
case WEEK:
recurrenceRule.append(Recur.WEEKLY);
interval = Long.valueOf(period).intValue();
hasBYDAY = true;
break;
case MONTH:
interval = Long.valueOf(period).intValue();
if ((interval % 12) == 0) {
isYearly = true;
recurrenceRule.append(Recur.YEARLY);
interval = (interval / 12);
} else {
recurrenceRule.append(Recur.MONTHLY);
}
if (dayOfMonth != 0) {
recurrenceRule.append(";BYMONTHDAY=");
if (dayOfMonth == 31) {
recurrenceRule.append("-1");
} else {
recurrenceRule.append(dayOfMonth);
}
}
if (isYearly) {
java.util.TimeZone javaTZ = null;
if (tzDef != null) {
javaTZ = tzDef.getTimeZone();
} else {
javaTZ = TimeZone.getTimeZone(TimeZones.UTC_ID);
}
Date bymonthDate = IcalUtil.localMinsSince1601toDate(firstDateTime, tzDef);
Calendar bymonthCal = new GregorianCalendar(javaTZ);
bymonthCal.setTimeInMillis(bymonthDate.getTime());
String MONTH_ONLY_PATTERN = "MM";
DateFormat monthOnlyFormat = new SimpleDateFormat(MONTH_ONLY_PATTERN);
monthOnlyFormat.setCalendar(bymonthCal);
recurrenceRule.append(";BYMONTH=");
recurrenceRule.append(monthOnlyFormat.format(bymonthDate));
}
break;
case MONTH_NTH:
interval = Long.valueOf(period).intValue();
if ((interval % 12) == 0) {
isYearly = true;
recurrenceRule.append(Recur.YEARLY);
interval = (interval / 12);
} else {
recurrenceRule.append(Recur.MONTHLY);
}
hasBYDAY = true;
recurrenceRule.append(";BYSETPOS=");
if (weekDayOccurrenceNumber == 5) {
recurrenceRule.append(-1);
} else {
recurrenceRule.append(weekDayOccurrenceNumber);
}
if (isYearly) {
java.util.TimeZone javaTZ = null;
if (tzDef != null) {
javaTZ = tzDef.getTimeZone();
} else {
javaTZ = TimeZone.getTimeZone(TimeZones.UTC_ID);
}
Date bymonthDate = IcalUtil.localMinsSince1601toDate(firstDateTime, tzDef);
Calendar bymonthCal = new GregorianCalendar(javaTZ);
bymonthCal.setTimeInMillis(bymonthDate.getTime());
String MONTH_ONLY_PATTERN = "MM";
DateFormat monthOnlyFormat = new SimpleDateFormat(MONTH_ONLY_PATTERN);
monthOnlyFormat.setCalendar(bymonthCal);
recurrenceRule.append(";BYMONTH=");
recurrenceRule.append(monthOnlyFormat.format(bymonthDate));
}
break;
case MONTH_END:
case HJ_MONTH:
case HJ_MONTH_END:
case HJ_MONTH_NTH:
throw TNEFtoIcalendarServiceException.UNSUPPORTED_RECURRENCE_TYPE(patternType.name());
default:
throw TNEFtoIcalendarServiceException.UNSUPPORTED_RECURRENCE_TYPE(patternType.name());
}
if (recurrenceRule.length() > 5) /* length of "FREQ=" */
{
if (endType.equals(EndType.END_AFTER_N_OCCURRENCES)) {
recurrenceRule.append(";COUNT=");
recurrenceRule.append(occurrenceCount);
} else if (endType.equals(EndType.END_BY_DATE)) {
// MS-OXCICAL :
// set to (EndDate + startTimeOffset), converted from the
// time zone specified by PidLidTimeZoneStruct to the UTC time zone
// From RFC 5545 :
// The UNTIL rule part defines a DATE or DATE-TIME value that bounds
// the recurrence rule in an inclusive manner. If the value
// specified by UNTIL is synchronized with the specified recurrence,
// this DATE or DATE-TIME becomes the last instance of the
// recurrence. The value of the UNTIL rule part MUST have the same
// value type as the "DTSTART" property. Furthermore, if the
// "DTSTART" property is specified as a date with local time, then
// the UNTIL rule part MUST also be specified as a date with local
// time [Gren - i.e. when DTSTART is a floating time].
// If the "DTSTART" property is specified as a date with UTC
// time or a date with local time and time zone reference, then the
// UNTIL rule part MUST be specified as a date with UTC time.
long minsSince1601 = this.endMinsSince1601 + this.startTimeOffset;
DateTime untilDateTime = IcalUtil.localMinsSince1601toDate(minsSince1601, tzDef);
recurrenceRule.append(";UNTIL=");
java.util.TimeZone untilTZ = null;
if (this.tzDef != null) {
untilTZ = this.tzDef.getTimeZone();
}
if (isFloating) {
// Use localtime.
recurrenceRule.append(IcalUtil.iCalDateTimeValue(untilDateTime, untilTZ, isAllDay));
} else {
if (isAllDay) {
recurrenceRule.append(IcalUtil.iCalDateTimeValue(untilDateTime, untilTZ, isAllDay));
} else {
// MUST be UTC time
recurrenceRule.append(IcalUtil.icalUtcTime(minsSince1601, tzDef));
}
}
}
if (hasBYDAY) {
WeekDayList weekDayList = new WeekDayList();
dayOfWeekMask = getDayOfWeekMask();
for (DayOfWeek dow : dayOfWeekMask) {
weekDayList.add(dow.iCal4JWeekDay());
}
if (!weekDayList.isEmpty()) {
recurrenceRule.append(";BYDAY=");
recurrenceRule.append(weekDayList);
}
}
if (interval != 1) {
recurrenceRule.append(";INTERVAL=");
recurrenceRule.append(interval);
}
if (weekStartDay != null) {
recurrenceRule.append(";WKST=");
recurrenceRule.append(weekStartDay);
}
Recur recur;
try {
recur = new Recur(recurrenceRule.toString());
} catch (ParseException ex) {
throw TNEFtoIcalendarServiceException.RRULE_PARSING_PROBLEM(ex);
}
theRule = new RRule(recur);
}
return theRule;
}
use of net.fortuna.ical4j.model.DateTime 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);
}
// }
}
use of net.fortuna.ical4j.model.DateTime in project OpenOLAT by OpenOLAT.
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 traccar by traccar.
the class Calendar method checkMoment.
public boolean checkMoment(Date date) {
if (calendar != null) {
Period period = new Period(new DateTime(date), new Dur(0, 0, 0, 0));
Predicate<CalendarComponent> periodRule = new PeriodRule<>(period);
Filter<CalendarComponent> filter = new Filter<>(new Predicate[] { periodRule }, Filter.MATCH_ANY);
Collection<CalendarComponent> events = filter.filter(calendar.getComponents(CalendarComponent.VEVENT));
if (events != null && !events.isEmpty()) {
return true;
}
}
return false;
}
Aggregations