Search in sources :

Example 51 with ICalTimeZone

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

the class GetMiniCal method handle.

@Override
public Element handle(Element request, Map<String, Object> context) throws ServiceException {
    ZimbraSoapContext zsc = getZimbraSoapContext(context);
    Mailbox mbox = getRequestedMailbox(zsc);
    Account authAcct = getAuthenticatedAccount(zsc);
    OperationContext octxt = getOperationContext(zsc, context);
    Element response = getResponseElement(zsc);
    long rangeStart = request.getAttributeLong(MailConstants.A_CAL_START_TIME);
    long rangeEnd = request.getAttributeLong(MailConstants.A_CAL_END_TIME);
    List<ItemId> folderIids = new ArrayList<ItemId>();
    for (Iterator<Element> foldersIter = request.elementIterator(MailConstants.E_FOLDER); foldersIter.hasNext(); ) {
        Element fElem = foldersIter.next();
        ItemId iidFolder = new ItemId(fElem.getAttribute(MailConstants.A_ID), zsc);
        folderIids.add(iidFolder);
    }
    ICalTimeZone tz = parseTimeZone(request);
    if (tz == null)
        // requestor's time zone, not mailbox owner's
        tz = Util.getAccountTimeZone(authAcct);
    TreeSet<String> busyDates = new TreeSet<String>();
    Provisioning prov = Provisioning.getInstance();
    MailboxManager mboxMgr = MailboxManager.getInstance();
    Server localServer = prov.getLocalServer();
    ItemIdFormatter ifmt = new ItemIdFormatter(zsc);
    Map<ItemId, Resolved> resolved = resolveMountpoints(octxt, mbox, folderIids);
    Map<ItemId, ItemId> /* requested iid */
    reverseMap = new HashMap<ItemId, ItemId>();
    for (Map.Entry<ItemId, Resolved> entry : resolved.entrySet()) {
        ItemId requestedIid = entry.getKey();
        Resolved res = entry.getValue();
        if (res.error == null) {
            reverseMap.put(res.iid, requestedIid);
        } else {
            addError(response, ifmt.formatItemId(requestedIid), res.error.getCode(), res.error.getMessage());
        }
    }
    Map<Server, Map<String, List<Integer>>> /* folder ids */
    groupedByServer = Search.groupByServer(groupFoldersByAccount(resolved));
    // Look up in calendar cache first.
    if (LC.calendar_cache_enabled.booleanValue()) {
        CalSummaryCache calCache = CalendarCacheManager.getInstance().getSummaryCache();
        Calendar cal = new GregorianCalendar(tz);
        for (Iterator<Map.Entry<Server, Map<String, List<Integer>>>> serverIter = groupedByServer.entrySet().iterator(); serverIter.hasNext(); ) {
            Map.Entry<Server, Map<String, List<Integer>>> serverMapEntry = serverIter.next();
            Map<String, List<Integer>> accountFolders = serverMapEntry.getValue();
            // for each account
            for (Iterator<Map.Entry<String, List<Integer>>> acctIter = accountFolders.entrySet().iterator(); acctIter.hasNext(); ) {
                Map.Entry<String, List<Integer>> acctEntry = acctIter.next();
                String acctId = acctEntry.getKey();
                List<Integer> folderIds = acctEntry.getValue();
                // for each folder
                for (Iterator<Integer> iterFolderId = folderIds.iterator(); iterFolderId.hasNext(); ) {
                    int folderId = iterFolderId.next();
                    try {
                        CalendarDataResult result = calCache.getCalendarSummary(octxt, acctId, folderId, MailItem.Type.APPOINTMENT, rangeStart, rangeEnd, true);
                        if (result != null) {
                            // Found data in cache.
                            iterFolderId.remove();
                            addBusyDates(cal, result.data, rangeStart, rangeEnd, busyDates);
                        }
                    } catch (ServiceException e) {
                        iterFolderId.remove();
                        ItemId iid = new ItemId(acctId, folderId);
                        // Error must mention folder id requested by client.
                        ItemId reqIid = reverseMap.get(iid);
                        if (reqIid != null) {
                            ZimbraLog.calendar.warn("Error accessing calendar folder " + ifmt.formatItemId(reqIid), e);
                            addError(response, ifmt.formatItemId(reqIid), e.getCode(), e.getMessage());
                        } else {
                            ZimbraLog.calendar.warn("Error accessing calendar folder; resolved id=" + ifmt.formatItemId(iid) + " (missing reverse mapping)", e);
                            addError(response, ifmt.formatItemId(iid), e.getCode(), e.getMessage());
                        }
                    }
                }
                if (folderIds.isEmpty())
                    acctIter.remove();
            }
            if (accountFolders.isEmpty())
                serverIter.remove();
        }
    }
    // For any remaining calendars, we have to get the data the hard way.
    for (Map.Entry<Server, Map<String, List<Integer>>> serverMapEntry : groupedByServer.entrySet()) {
        Server server = serverMapEntry.getKey();
        Map<String, List<Integer>> accountFolders = serverMapEntry.getValue();
        if (server.equals(localServer)) {
            // local server
            for (Map.Entry<String, List<Integer>> entry : accountFolders.entrySet()) {
                String acctId = entry.getKey();
                List<Integer> folderIds = entry.getValue();
                Account targetAcct = prov.get(AccountBy.id, acctId);
                if (targetAcct == null) {
                    ZimbraLog.calendar.warn("Skipping unknown account " + acctId + " during minical search");
                    continue;
                }
                Mailbox targetMbox = mboxMgr.getMailboxByAccount(targetAcct);
                for (int folderId : folderIds) {
                    try {
                        doLocalFolder(octxt, tz, targetMbox, folderId, rangeStart, rangeEnd, busyDates);
                    } catch (ServiceException e) {
                        ItemId iid = new ItemId(acctId, folderId);
                        // Error must mention folder id requested by client.
                        ItemId reqIid = reverseMap.get(iid);
                        if (reqIid != null) {
                            ZimbraLog.calendar.warn("Error accessing calendar folder " + ifmt.formatItemId(reqIid), e);
                            addError(response, ifmt.formatItemId(reqIid), e.getCode(), e.getMessage());
                        } else {
                            ZimbraLog.calendar.warn("Error accessing calendar folder; resolved id=" + ifmt.formatItemId(iid) + " (missing reverse mapping)", e);
                            addError(response, ifmt.formatItemId(iid), e.getCode(), e.getMessage());
                        }
                    }
                }
            }
        } else {
            // remote server
            // mail service soap requests want to see a target account
            String nominalTargetAcctId = null;
            List<String> folderList = new ArrayList<String>();
            for (Map.Entry<String, List<Integer>> entry : accountFolders.entrySet()) {
                String acctId = entry.getKey();
                if (nominalTargetAcctId == null)
                    nominalTargetAcctId = acctId;
                ItemIdFormatter ifmtRemote = new ItemIdFormatter(authAcct.getId(), acctId, false);
                List<Integer> folderIds = entry.getValue();
                for (int folderId : folderIds) {
                    folderList.add(ifmtRemote.formatItemId(folderId));
                }
            }
            doRemoteFolders(zsc, nominalTargetAcctId, folderList, rangeStart, rangeEnd, busyDates, response, reverseMap, ifmt);
        }
    }
    for (String datestamp : busyDates) {
        Element dateElem = response.addElement(MailConstants.E_CAL_MINICAL_DATE);
        dateElem.setText(datestamp);
    }
    return response;
}
Also used : Account(com.zimbra.cs.account.Account) Server(com.zimbra.cs.account.Server) ItemIdFormatter(com.zimbra.cs.service.util.ItemIdFormatter) HashMap(java.util.HashMap) Element(com.zimbra.common.soap.Element) ArrayList(java.util.ArrayList) CalendarDataResult(com.zimbra.cs.mailbox.calendar.cache.CalSummaryCache.CalendarDataResult) ItemId(com.zimbra.cs.service.util.ItemId) Provisioning(com.zimbra.cs.account.Provisioning) Mailbox(com.zimbra.cs.mailbox.Mailbox) ZMailbox(com.zimbra.client.ZMailbox) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) List(java.util.List) OperationContext(com.zimbra.cs.mailbox.OperationContext) Calendar(java.util.Calendar) GregorianCalendar(java.util.GregorianCalendar) GregorianCalendar(java.util.GregorianCalendar) Mountpoint(com.zimbra.cs.mailbox.Mountpoint) MailboxManager(com.zimbra.cs.mailbox.MailboxManager) AccountServiceException(com.zimbra.cs.account.AccountServiceException) ServiceException(com.zimbra.common.service.ServiceException) MailServiceException(com.zimbra.cs.mailbox.MailServiceException) ZimbraSoapContext(com.zimbra.soap.ZimbraSoapContext) HashMap(java.util.HashMap) Map(java.util.Map) IcalXmlStrMap(com.zimbra.cs.mailbox.calendar.IcalXmlStrMap) ICalTimeZone(com.zimbra.common.calendar.ICalTimeZone) CalSummaryCache(com.zimbra.cs.mailbox.calendar.cache.CalSummaryCache)

Example 52 with ICalTimeZone

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

the class GetMiniCal method parseTimeZone.

private static ICalTimeZone parseTimeZone(Element request) throws ServiceException {
    Element tzElem = request.getOptionalElement(MailConstants.E_CAL_TZ);
    if (tzElem != null) {
        String tzid = tzElem.getAttribute(MailConstants.A_ID, null);
        if (tzid != null) {
            ICalTimeZone knownTZ = WellKnownTimeZones.getTimeZoneById(tzid);
            if (knownTZ != null)
                return knownTZ;
        }
        // custom timezone
        String stdOffset = tzElem.getAttribute(MailConstants.A_CAL_TZ_STDOFFSET, null);
        if (stdOffset == null)
            throw ServiceException.INVALID_REQUEST("Unknown TZ: \"" + tzid + "\" and no " + MailConstants.A_CAL_TZ_STDOFFSET + " specified", null);
        return CalendarUtils.parseTzElement(tzElem);
    } else {
        return null;
    }
}
Also used : Element(com.zimbra.common.soap.Element) ICalTimeZone(com.zimbra.common.calendar.ICalTimeZone)

Example 53 with ICalTimeZone

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

the class Invite method newToICalendar.

public ZVCalendar newToICalendar(boolean useOutlookCompatMode, boolean includePrivateData) throws ServiceException {
    ZVCalendar vcal = new ZVCalendar();
    vcal.addVersionAndProdId();
    vcal.addProperty(new ZProperty(ICalTok.METHOD, mMethod.toString()));
    // timezones
    if (!isAllDayEvent() || useOutlookCompatMode) {
        // Don't write any VTIMEZONE for all-day appointments.
        for (Iterator<ICalTimeZone> iter = mTzMap.tzIterator(); iter.hasNext(); ) {
            ICalTimeZone cur = iter.next();
            vcal.addComponent(cur.newToVTimeZone());
        }
    }
    vcal.addComponent(newToVComponent(useOutlookCompatMode, includePrivateData));
    return vcal;
}
Also used : ZVCalendar(com.zimbra.common.calendar.ZCalendar.ZVCalendar) ZProperty(com.zimbra.common.calendar.ZCalendar.ZProperty) ICalTimeZone(com.zimbra.common.calendar.ICalTimeZone)

Example 54 with ICalTimeZone

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

the class Recurrence method getReferencedTZIDs.

// Get the set of TZIDs referenced in a recurrence.  RDATE and EXDATE can use TZID parameter.
public static Set<String> getReferencedTZIDs(IRecurrence recur) {
    Set<String> tzids = new HashSet<String>();
    // RDATE
    for (Iterator iter = recur.addRulesIterator(); iter != null && iter.hasNext(); ) {
        IRecurrence cur = (IRecurrence) iter.next();
        if (cur.getType() == Recurrence.TYPE_SINGLE_DATES) {
            Recurrence.SingleDates sd = (Recurrence.SingleDates) cur;
            RdateExdate rdate = sd.getRdateExdate();
            ICalTimeZone tz = rdate.getTimeZone();
            if (tz != null)
                tzids.add(tz.getID());
        }
    }
    // EXDATE
    for (Iterator iter = recur.subRulesIterator(); iter != null && iter.hasNext(); ) {
        IRecurrence cur = (IRecurrence) iter.next();
        if (cur.getType() == Recurrence.TYPE_SINGLE_DATES) {
            Recurrence.SingleDates sd = (Recurrence.SingleDates) cur;
            RdateExdate exdate = sd.getRdateExdate();
            ICalTimeZone tz = exdate.getTimeZone();
            if (tz != null)
                tzids.add(tz.getID());
        }
    }
    ParsedDateTime recurStart = recur.getStartTime();
    if (recurStart != null) {
        ICalTimeZone tz = recurStart.getTimeZone();
        if (tz != null)
            tzids.add(tz.getID());
    }
    return tzids;
}
Also used : Iterator(java.util.Iterator) ParsedDateTime(com.zimbra.common.calendar.ParsedDateTime) ICalTimeZone(com.zimbra.common.calendar.ICalTimeZone) HashSet(java.util.HashSet)

Example 55 with ICalTimeZone

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

the class Util method getAccountTimeZone.

/**
     * Returns the time zone for the given account.
     */
public static ICalTimeZone getAccountTimeZone(Account account) {
    String tzid = account.getAttr(Provisioning.A_zimbraPrefTimeZoneId);
    tzid = TZIDMapper.canonicalize(tzid);
    ICalTimeZone timeZone = WellKnownTimeZones.getTimeZoneById(tzid);
    if (timeZone == null) {
        return ICalTimeZone.getUTC();
    }
    return timeZone;
}
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