Search in sources :

Example 16 with ICalTimeZone

use of com.zimbra.common.calendar.ICalTimeZone in project zm-mailbox by Zimbra.

the class Invite method getReferencedTZIDs.

public Set<String> getReferencedTZIDs() {
    Set<String> tzids = new HashSet<String>();
    // DTSTART
    if (mStart != null && mStart.hasTime()) {
        ICalTimeZone tz = mStart.getTimeZone();
        if (tz != null)
            tzids.add(tz.getID());
    }
    // DTEND/DUE
    if (mEnd != null && mEnd.hasTime()) {
        ICalTimeZone tz = mEnd.getTimeZone();
        if (tz != null)
            tzids.add(tz.getID());
    }
    // RECURRENCE-ID
    if (mRecurrenceId != null) {
        ParsedDateTime dt = mRecurrenceId.getDt();
        if (dt.hasTime()) {
            ICalTimeZone tz = dt.getTimeZone();
            if (tz != null)
                tzids.add(tz.getID());
        }
    }
    // RDATE/EXDATE
    IRecurrence recur = getRecurrence();
    if (recur != null)
        tzids.addAll(Recurrence.getReferencedTZIDs(recur));
    return tzids;
}
Also used : IRecurrence(com.zimbra.cs.mailbox.calendar.Recurrence.IRecurrence) ParsedDateTime(com.zimbra.common.calendar.ParsedDateTime) ICalTimeZone(com.zimbra.common.calendar.ICalTimeZone) HashSet(java.util.HashSet)

Example 17 with ICalTimeZone

use of com.zimbra.common.calendar.ICalTimeZone in project zm-mailbox by Zimbra.

the class Util method encodeAsMetadata.

public static Metadata encodeAsMetadata(TimeZoneMap tzmap) {
    Metadata meta = new Metadata();
    Map<String, Integer> /* index */
    tzIndex = new HashMap<String, Integer>();
    int nextIndex = 0;
    for (Iterator<Entry<String, ICalTimeZone>> iter = tzmap.getMap().entrySet().iterator(); iter.hasNext(); ) {
        Entry<String, ICalTimeZone> entry = iter.next();
        String tzid = entry.getKey();
        if (// ignore null/empty TZIDs (bug 25183)
        tzid == null || tzid.length() < 1)
            continue;
        ICalTimeZone zone = entry.getValue();
        String realTzid = zone.getID();
        if (!tzIndex.containsKey(realTzid)) {
            meta.put("#" + nextIndex, encodeAsMetadata(zone));
            tzIndex.put(realTzid, nextIndex);
            ++nextIndex;
        }
    }
    for (Iterator<Entry<String, String>> iter = tzmap.getAliasMap().entrySet().iterator(); iter.hasNext(); ) {
        Entry<String, String> entry = iter.next();
        String alias = entry.getKey();
        String realTzid = entry.getValue();
        if (tzIndex.containsKey(realTzid)) {
            int index = tzIndex.get(realTzid);
            meta.put(alias, index);
        }
    }
    return meta;
}
Also used : Entry(java.util.Map.Entry) HashMap(java.util.HashMap) Metadata(com.zimbra.cs.mailbox.Metadata) ICalTimeZone(com.zimbra.common.calendar.ICalTimeZone)

Example 18 with ICalTimeZone

use of com.zimbra.common.calendar.ICalTimeZone in project zm-mailbox by Zimbra.

the class Util method decodeFromMetadata.

/**
     *
     * @param meta
     * @param localTZ local time zone of user account
     * @return
     * @throws ServiceException
     */
public static TimeZoneMap decodeFromMetadata(Metadata meta, ICalTimeZone localTZ) throws ServiceException {
    Map<String, ?> map = meta.asMap();
    Map<String, String> aliasMap = new HashMap<String, String>();
    ICalTimeZone[] tzlist = new ICalTimeZone[map.size()];
    // first time, find the tz's
    for (Map.Entry<String, ?> entry : map.entrySet()) {
        String key = entry.getKey();
        if (key != null && key.length() > 0) {
            // ignore null/empty TZIDs (bug 25183)
            if (key.charAt(0) == '#') {
                int idx = Integer.parseInt(key.substring(1));
                Metadata tzMeta = (Metadata) entry.getValue();
                String tzidMeta = tzMeta.get(FN_TZID, null);
                if (tzidMeta != null) {
                    ICalTimeZone tz = decodeTimeZoneFromMetadata(tzMeta);
                    if (tz != null) {
                        String tzid = tz.getID();
                        if (!DebugConfig.disableCalendarTZMatchByID)
                            tzid = TZIDMapper.canonicalize(tzid);
                        if (!tzidMeta.equals(tzid)) {
                            aliasMap.put(tzidMeta, tzid);
                            tz = WellKnownTimeZones.getTimeZoneById(tzid);
                        }
                        tzlist[idx] = tz;
                    }
                }
            }
        }
    }
    Map<String, ICalTimeZone> tzmap = new HashMap<String, ICalTimeZone>();
    for (ICalTimeZone tz : tzlist) {
        if (tz != null)
            tzmap.put(tz.getID(), tz);
    }
    // second time, build the real map
    for (Map.Entry<String, ?> entry : map.entrySet()) {
        String tzid = entry.getKey();
        if (tzid != null && tzid.length() > 0) {
            // ignore null/empty TZIDs (bug 25183)
            if (tzid.charAt(0) != '#') {
                int idx = -1;
                try {
                    idx = Integer.parseInt(entry.getValue().toString());
                } catch (NumberFormatException e) {
                }
                if (idx >= 0 && idx < tzlist.length) {
                    ICalTimeZone tz = tzlist[idx];
                    if (tz != null) {
                        String realId = tz.getID();
                        if (!realId.equals(tzid))
                            aliasMap.put(tzid, realId);
                    }
                }
            }
        }
    }
    return new TimeZoneMap(tzmap, aliasMap, localTZ);
}
Also used : HashMap(java.util.HashMap) Metadata(com.zimbra.cs.mailbox.Metadata) TimeZoneMap(com.zimbra.common.calendar.TimeZoneMap) HashMap(java.util.HashMap) TimeZoneMap(com.zimbra.common.calendar.TimeZoneMap) Map(java.util.Map) ICalTimeZone(com.zimbra.common.calendar.ICalTimeZone)

Example 19 with ICalTimeZone

use of com.zimbra.common.calendar.ICalTimeZone 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");
}
Also used : ParsedDuration(com.zimbra.common.calendar.ParsedDuration) Instance(com.zimbra.cs.mailbox.CalendarItem.Instance) Calendar(java.util.Calendar) GregorianCalendar(java.util.GregorianCalendar) ArrayList(java.util.ArrayList) GregorianCalendar(java.util.GregorianCalendar) TimeZoneMap(com.zimbra.common.calendar.TimeZoneMap) ParsedDateTime(com.zimbra.common.calendar.ParsedDateTime) ICalTimeZone(com.zimbra.common.calendar.ICalTimeZone)

Example 20 with ICalTimeZone

use of com.zimbra.common.calendar.ICalTimeZone in project zm-mailbox by Zimbra.

the class Util method decodeTimeZoneFromMetadata.

public static ICalTimeZone decodeTimeZoneFromMetadata(Metadata m) throws ServiceException {
    String tzid;
    if (m.containsKey(FN_TZID)) {
        tzid = m.get(FN_TZID);
        boolean hasDef = m.containsKey(FN_STANDARD_OFFSET);
        if (!DebugConfig.disableCalendarTZMatchByID || !hasDef) {
            ICalTimeZone tz = WellKnownTimeZones.getTimeZoneById(tzid);
            if (tz != null) {
                return tz;
            } else if (!hasDef) {
                ZimbraLog.calendar.debug("Unknown time zone \"" + tzid + "\" in metadata; using UTC instead");
                return ICalTimeZone.getUTC().cloneWithNewTZID(tzid);
            }
        }
    } else
        tzid = "unknown time zone";
    ICalTimeZone newTz = newICalTimeZone(tzid, m);
    ICalTimeZone tz = ICalTimeZone.lookupByRule(newTz, false);
    return tz;
}
Also used : ICalTimeZone(com.zimbra.common.calendar.ICalTimeZone)

Aggregations

ICalTimeZone (com.zimbra.common.calendar.ICalTimeZone)62 ParsedDateTime (com.zimbra.common.calendar.ParsedDateTime)17 ZComponent (com.zimbra.common.calendar.ZCalendar.ZComponent)14 TimeZoneMap (com.zimbra.common.calendar.TimeZoneMap)12 Element (com.zimbra.common.soap.Element)11 ZVCalendar (com.zimbra.common.calendar.ZCalendar.ZVCalendar)10 ServiceException (com.zimbra.common.service.ServiceException)10 ArrayList (java.util.ArrayList)10 ZProperty (com.zimbra.common.calendar.ZCalendar.ZProperty)8 Account (com.zimbra.cs.account.Account)8 Invite (com.zimbra.cs.mailbox.calendar.Invite)8 Mailbox (com.zimbra.cs.mailbox.Mailbox)7 IOException (java.io.IOException)7 Metadata (com.zimbra.cs.mailbox.Metadata)6 ItemId (com.zimbra.cs.service.util.ItemId)6 ParseException (java.text.ParseException)6 OperationContext (com.zimbra.cs.mailbox.OperationContext)5 RecurId (com.zimbra.cs.mailbox.calendar.RecurId)5 ZimbraSoapContext (com.zimbra.soap.ZimbraSoapContext)5 GregorianCalendar (java.util.GregorianCalendar)5