use of com.zimbra.common.calendar.ParsedDuration in project zm-mailbox by Zimbra.
the class Recurrence method main.
public static void main(String[] args) throws Exception {
ICalTimeZone pacific = new ICalTimeZone("America/Los_Angeles", -28800000, "16010101T020000", "FREQ=YEARLY;WKST=MO;INTERVAL=1;BYMONTH=11;BYDAY=1SU", "PST", -25200000, "16010101T020000", "FREQ=YEARLY;WKST=MO;INTERVAL=1;BYMONTH=3;BYDAY=2SU", "PDT");
TimeZoneMap tzmap = new TimeZoneMap(pacific);
String str = "TZID=\"" + pacific.getID() + "\":20090105T120000";
ParsedDateTime dtStart = ParsedDateTime.parse(str, tzmap);
ParsedDuration duration = ParsedDuration.parse("PT1H");
List<IRecurrence> addRules = new ArrayList<IRecurrence>();
List<IRecurrence> subRules = new ArrayList<IRecurrence>();
// weekly from 2009/01/05, for 52 weeks
ZRecur rule = new ZRecur("FREQ=WEEKLY;INTERVAL=1", tzmap);
addRules.add(new SimpleRepeatingRule(dtStart, duration, rule, null));
// add a couple of RDATES: 2009/01/06, 2009/01/07
RdateExdate rdate = new RdateExdate(ICalTok.RDATE, pacific);
str = "TZID=\"" + pacific.getID() + "\":20090106T120000";
ParsedDateTime rd1 = ParsedDateTime.parse(str, tzmap);
rdate.addValue(rd1);
str = "TZID=\"" + pacific.getID() + "\":20090107T120000";
ParsedDateTime rd2 = ParsedDateTime.parse(str, tzmap);
rdate.addValue(rd2);
addRules.add(new SingleDates(rdate, duration));
// modify instance on 2009/02/16 to start at 1pm instead of noon, for 2 hours
str = "TZID=\"" + pacific.getID() + "\":20090216T120000";
ParsedDateTime ridDtModify1 = ParsedDateTime.parse(str, tzmap);
RecurId ridModify1 = new RecurId(ridDtModify1, RecurId.RANGE_NONE);
str = "TZID=\"" + pacific.getID() + "\":20090216T130000";
ParsedDateTime dtStartModify1 = ParsedDateTime.parse(str, tzmap);
ParsedDuration durModify1 = ParsedDuration.parse("PT2H");
ExceptionRule modify1 = new ExceptionRule(ridModify1, dtStartModify1, durModify1, null);
// cancel instance on 2009/01/19
str = "TZID=\"" + pacific.getID() + "\":20090119T120000";
ParsedDateTime dtCancel1 = ParsedDateTime.parse(str, tzmap);
RecurId ridCancel1 = new RecurId(dtCancel1, RecurId.RANGE_NONE);
CancellationRule cancel1 = new CancellationRule(ridCancel1);
// EXDATE on 2009/02/09
RdateExdate exdate = new RdateExdate(ICalTok.EXDATE, pacific);
str = "TZID=\"" + pacific.getID() + "\":20090209T120000";
ParsedDateTime ex1 = ParsedDateTime.parse(str, tzmap);
exdate.addValue(ex1);
SingleDates exdateRule = new SingleDates(exdate, duration);
subRules.add(exdateRule);
RecurrenceRule recurrence = new RecurrenceRule(dtStart, duration, null, addRules, subRules);
recurrence.addException(modify1);
recurrence.addException(cancel1);
// Get all instances between 2009/01/01 and 2010/01/01.
Calendar startCal = new GregorianCalendar(pacific);
startCal.clear();
startCal.set(2009, Calendar.JANUARY, 1, 0, 0, 0);
Calendar endCal = (Calendar) startCal.clone();
endCal.add(Calendar.YEAR, 1);
// List<Instance> instances = recurrence.expandInstances(-1, startCal.getTimeInMillis(), endCal.getTimeInMillis());
List<Instance> instances = recurrence.expandInstances(-1, startCal.getTimeInMillis(), Long.MAX_VALUE);
for (Instance inst : instances) {
System.out.println(inst);
}
System.out.println("Got " + instances.size() + " instances");
}
use of com.zimbra.common.calendar.ParsedDuration in project zm-mailbox by Zimbra.
the class Period method decodeMetadata.
public static Period decodeMetadata(Metadata meta, ICalTimeZone tz, TimeZoneMap tzmap) throws ServiceException {
String start = meta.get(FN_START);
ParsedDateTime startTime;
try {
startTime = ParsedDateTime.parse(start, tzmap, tz, tzmap.getLocalTimeZone());
} catch (ParseException e) {
throw ServiceException.INVALID_REQUEST("Invalid PERIOD start time in metadata: " + meta.toString(), e);
}
String end = meta.get(FN_END, null);
if (end != null) {
ParsedDateTime endTime;
try {
endTime = ParsedDateTime.parse(end, tzmap, tz, tzmap.getLocalTimeZone());
} catch (ParseException e) {
throw ServiceException.INVALID_REQUEST("Invalid PERIOD end time in metadata: " + meta.toString(), e);
}
return new Period(startTime, endTime);
} else {
String durStr = meta.get(FN_DURATION, null);
if (durStr == null)
throw ServiceException.INVALID_REQUEST("PERIOD in metadata missing both end time and duration: " + meta.toString(), null);
ParsedDuration duration = ParsedDuration.parse(durStr);
return new Period(startTime, duration);
}
}
use of com.zimbra.common.calendar.ParsedDuration in project zm-mailbox by Zimbra.
the class Alarm method parse.
/**
* Create an Alarm from SOAP. Return value may be null.
* @param alarmElem
* @return
* @throws ServiceException
*/
public static Alarm parse(Element alarmElem) throws ServiceException {
Action action = Action.DISPLAY;
TriggerType triggerType = TriggerType.RELATIVE;
TriggerRelated triggerRelated = null;
ParsedDuration triggerRelative = null;
ParsedDateTime triggerAbsolute = null;
ParsedDuration repeatDuration = null;
int repeatCount = 0;
String description = null;
String summary = null;
Attach attach = null;
List<ZAttendee> attendees = null;
String val;
val = alarmElem.getAttribute(MailConstants.A_CAL_ALARM_ACTION);
action = Action.lookup(val);
if (action == null)
throw ServiceException.INVALID_REQUEST("Invalid " + MailConstants.A_CAL_ALARM_ACTION + " value " + val, null);
if (!actionAllowed(action))
return null;
Element triggerElem = alarmElem.getElement(MailConstants.E_CAL_ALARM_TRIGGER);
Element triggerRelativeElem = triggerElem.getOptionalElement(MailConstants.E_CAL_ALARM_RELATIVE);
if (triggerRelativeElem != null) {
triggerType = TriggerType.RELATIVE;
String related = triggerRelativeElem.getAttribute(MailConstants.A_CAL_ALARM_RELATED, null);
if (related != null) {
triggerRelated = TriggerRelated.lookup(related);
if (triggerRelated == null)
throw ServiceException.INVALID_REQUEST("Invalid " + MailConstants.A_CAL_ALARM_RELATED + " value " + val, null);
}
triggerRelative = ParsedDuration.parse(triggerRelativeElem);
} else {
triggerType = TriggerType.ABSOLUTE;
Element triggerAbsoluteElem = triggerElem.getOptionalElement(MailConstants.E_CAL_ALARM_ABSOLUTE);
if (triggerAbsoluteElem == null)
throw ServiceException.INVALID_REQUEST("<" + MailConstants.E_CAL_ALARM_TRIGGER + "> must have either <" + MailConstants.E_CAL_ALARM_RELATIVE + "> or <" + MailConstants.E_CAL_ALARM_ABSOLUTE + "> child element", null);
String datetime = triggerAbsoluteElem.getAttribute(MailConstants.A_DATE);
try {
triggerAbsolute = ParsedDateTime.parseUtcOnly(datetime);
} catch (ParseException e) {
throw ServiceException.INVALID_REQUEST("Invalid absolute trigger value " + val, e);
}
}
Element repeatElem = alarmElem.getOptionalElement(MailConstants.E_CAL_ALARM_REPEAT);
if (repeatElem != null) {
repeatDuration = ParsedDuration.parse(repeatElem);
repeatCount = (int) repeatElem.getAttributeLong(MailConstants.A_CAL_ALARM_COUNT, 0);
}
Element descElem = alarmElem.getOptionalElement(MailConstants.E_CAL_ALARM_DESCRIPTION);
if (descElem != null) {
description = descElem.getText();
}
Element summaryElem = alarmElem.getOptionalElement(MailConstants.E_CAL_ALARM_SUMMARY);
if (summaryElem != null) {
summary = summaryElem.getText();
}
Element attachElem = alarmElem.getOptionalElement(MailConstants.E_CAL_ATTACH);
if (attachElem != null)
attach = Attach.parse(attachElem);
Iterator<Element> attendeesIter = alarmElem.elementIterator(MailConstants.E_CAL_ATTENDEE);
while (attendeesIter.hasNext()) {
ZAttendee at = ZAttendee.parse(attendeesIter.next());
if (attendees == null)
attendees = new ArrayList<ZAttendee>();
attendees.add(at);
}
Alarm alarm = new Alarm(action, triggerType, triggerRelated, triggerRelative, triggerAbsolute, repeatDuration, repeatCount, description, summary, attach, attendees, CalendarUtils.parseXProps(alarmElem));
return alarm;
}
use of com.zimbra.common.calendar.ParsedDuration in project zm-mailbox by Zimbra.
the class Alarm method parse.
/**
* Create an Alarm from ZComponent. Return value may be null.
* @param comp
* @return
* @throws ServiceException
*/
public static Alarm parse(ZComponent comp) throws ServiceException {
Action action = Action.DISPLAY;
TriggerType triggerType = TriggerType.RELATIVE;
TriggerRelated triggerRelated = null;
ParsedDuration triggerRelative = null;
ParsedDateTime triggerAbsolute = null;
ParsedDuration repeatDuration = null;
int repeatCount = 0;
String description = null;
String summary = null;
Attach attach = null;
List<ZAttendee> attendees = null;
List<ZProperty> xprops = new ArrayList<ZProperty>();
Iterator<ZProperty> propIter = comp.getPropertyIterator();
while (propIter.hasNext()) {
ZProperty prop = propIter.next();
ICalTok tok = prop.getToken();
String val = prop.getValue();
if (tok == null) {
String name = prop.getName();
if (name.startsWith("X-") || name.startsWith("x-")) {
xprops.add(prop);
}
continue;
}
switch(tok) {
case ACTION:
if (val != null) {
action = Action.lookup(val);
if (action == null)
throw ServiceException.INVALID_REQUEST("Invalid ACTION value " + val, null);
if (!actionAllowed(action))
return null;
}
break;
case TRIGGER:
ZParameter valueType = prop.getParameter(ICalTok.VALUE);
if (valueType != null) {
String vt = valueType.getValue();
if (ICalTok.DATE_TIME.toString().equals(vt))
triggerType = TriggerType.ABSOLUTE;
}
if (TriggerType.RELATIVE.equals(triggerType)) {
ZParameter related = prop.getParameter(ICalTok.RELATED);
if (related != null) {
String rel = related.getValue();
if (rel != null) {
triggerRelated = TriggerRelated.lookup(rel);
if (triggerRelated == null)
throw ServiceException.INVALID_REQUEST("Invalid RELATED value " + rel, null);
}
}
triggerRelative = ParsedDuration.parse(val);
} else {
try {
if (val != null)
triggerAbsolute = ParsedDateTime.parseUtcOnly(val);
} catch (ParseException e) {
throw ServiceException.INVALID_REQUEST("Invalid TRIGGER value " + val, e);
}
}
break;
case DURATION:
if (val != null)
repeatDuration = ParsedDuration.parse(val);
break;
case REPEAT:
if (val != null) {
try {
repeatCount = Integer.parseInt(val);
} catch (NumberFormatException e) {
throw ServiceException.INVALID_REQUEST("Invalid REPEAT value " + val, e);
}
}
break;
case DESCRIPTION:
description = val;
break;
case SUMMARY:
summary = val;
break;
case ATTACH:
attach = Attach.parse(prop);
break;
case ATTENDEE:
ZAttendee attendee = new ZAttendee(prop);
if (attendees == null)
attendees = new ArrayList<ZAttendee>();
attendees.add(attendee);
break;
}
}
Alarm alarm = new Alarm(action, triggerType, triggerRelated, triggerRelative, triggerAbsolute, repeatDuration, repeatCount, description, summary, attach, attendees, xprops);
return alarm;
}
use of com.zimbra.common.calendar.ParsedDuration in project zm-mailbox by Zimbra.
the class ToXML method encodeCalendarItemRecur.
public static void encodeCalendarItemRecur(Element parent, CalendarItem calItem) {
TimeZoneMap tzmap = calItem.getTimeZoneMap();
encodeTimeZoneMap(parent, tzmap);
Invite[] invites = calItem.getInvites();
for (Invite inv : invites) {
String elemName;
if (inv.isCancel()) {
elemName = MailConstants.E_CAL_CANCEL;
} else if (inv.hasRecurId()) {
elemName = MailConstants.E_CAL_EXCEPT;
} else {
elemName = MailConstants.E_INVITE_COMPONENT;
}
Element compElem = parent.addElement(elemName);
boolean allDay = inv.isAllDayEvent();
// RECURRENCE-ID
if (inv.hasRecurId())
encodeRecurId(compElem, inv.getRecurId(), allDay);
if (!inv.isCancel()) {
// DTSTART
ParsedDateTime dtStart = inv.getStartTime();
if (dtStart != null)
encodeDtStart(compElem, dtStart, allDay, false);
// DTEND or DURATION
ParsedDateTime dtEnd = inv.getEndTime();
if (dtEnd != null) {
encodeDtEnd(compElem, dtEnd, allDay, inv.isTodo(), false);
} else {
ParsedDuration dur = inv.getDuration();
if (dur != null)
dur.toXml(compElem);
}
// recurrence definition
IRecurrence recurrence = inv.getRecurrence();
if (recurrence != null) {
Element recurElem = compElem.addElement(MailConstants.E_CAL_RECUR);
recurrence.toXml(recurElem);
}
}
}
}
Aggregations