Search in sources :

Example 16 with CalendarItem

use of com.zimbra.cs.mailbox.CalendarItem in project zm-mailbox by Zimbra.

the class ToXML method encodeInvitesForMessage.

private static Element encodeInvitesForMessage(Element parent, ItemIdFormatter ifmt, OperationContext octxt, Message msg, int fields, boolean neuter) throws ServiceException {
    if (fields != NOTIFY_FIELDS && !needToOutput(fields, Change.INVITE)) {
        return parent;
    }
    Element ie = parent.addNonUniqueElement(MailConstants.E_INVITE);
    Mailbox mbox = msg.getMailbox();
    for (Iterator<Message.CalendarItemInfo> iter = msg.getCalendarItemInfoIterator(); iter.hasNext(); ) {
        Message.CalendarItemInfo info = iter.next();
        CalendarItem calItem = null;
        ICalTok method = ICalTok.REQUEST;
        Invite invCi = info.getInvite();
        if (invCi != null) {
            method = Invite.lookupMethod(invCi.getMethod());
        }
        Invite invite = invCi;
        ItemId calendarItemId = info.getCalendarItemId();
        if (info.calItemCreated()) {
            try {
                calItem = mbox.getCalendarItemById(octxt, calendarItemId);
            } catch (MailServiceException.NoSuchItemException e) {
                // Calendar item has been deleted. Bug 84877 - don't include stale references to it in SOAP response
                calendarItemId = null;
            } catch (ServiceException e) {
                // eat PERM_DENIED
                if (e.getCode() != ServiceException.PERM_DENIED) {
                    throw e;
                }
                // If we can't access it.  Don't include a reference to it.
                calendarItemId = null;
            }
            // Do staleness check for invitation messages.
            if (ICalTok.REQUEST.equals(method) || ICalTok.PUBLISH.equals(method)) {
                if (calItem != null && calItem.getFolderId() != Mailbox.ID_FOLDER_TRASH) {
                    if (invCi != null) {
                        // See if the messsage's invite is outdated.
                        Invite invCurr = calItem.getInvite(invCi.getRecurId());
                        if (invCurr != null) {
                            if (invCi.getSeqNo() >= invCurr.getSeqNo()) {
                                // Invite is new or same as what's in the appointment.  Show it.
                                invite = invCi;
                            } else {
                                // Outdated.  Don't show it.
                                invite = null;
                            }
                        } else {
                            // New invite.  Show it.
                            invite = invCi;
                        }
                    } else {
                        // legacy case
                        invite = calItem.getInvite(msg.getId(), info.getComponentNo());
                    // invite == null if the invite was outdated by a newer update
                    }
                }
            }
        } else {
            // We have an invite that wasn't auto-added.
            if (invCi != null) {
                if (!Invite.isOrganizerMethod(invCi.getMethod()) || ICalTok.DECLINECOUNTER.equals(method)) {
                    invite = invCi;
                } else {
                    try {
                        calItem = mbox.getCalendarItemByUid(octxt, invCi.getUid());
                    } catch (MailServiceException.NoSuchItemException e) {
                    // ignore
                    } catch (ServiceException e) {
                        // eat PERM_DENIED
                        if (e.getCode() != ServiceException.PERM_DENIED) {
                            throw e;
                        }
                    }
                    if (calItem != null) {
                        // See if the messsage's invite is outdated.
                        Invite invCurr = calItem.getInvite(invCi.getRecurId());
                        if (invCurr != null) {
                            if (invCi.getSeqNo() >= invCurr.getSeqNo()) {
                                // Invite is new or same as what's in the appointment.  Show it.
                                invite = invCi;
                            } else {
                                // Outdated.  Don't show it.
                                invite = null;
                            }
                        } else {
                            // New invite.  Show it.
                            invite = invCi;
                        }
                    } else {
                        // Appointment doesn't exist.  The invite in the message should be displayed and the
                        // user can manually add the appointment.
                        invite = invCi;
                    }
                }
            }
        }
        if (invite != null) {
            setCalendarItemType(ie, invite.getItemType());
            encodeTimeZoneMap(ie, invite.getTimeZoneMap());
            com.zimbra.soap.mail.type.CalendarItemInfo remoteCalendarItem = null;
            if (calItem == null) {
                remoteCalendarItem = msg.getRemoteCalendarItem(invite);
                if (remoteCalendarItem != null) {
                    calendarItemId = new ItemId(remoteCalendarItem.getId(), (String) null);
                }
            }
            encodeInviteComponent(ie, ifmt, octxt, calItem, calendarItemId, invite, fields, neuter);
            ICalTok invMethod = Invite.lookupMethod(invite.getMethod());
            if (ICalTok.REQUEST.equals(invMethod) || ICalTok.PUBLISH.equals(invMethod)) {
                InviteChanges invChanges = info.getInviteChanges();
                if (invChanges != null && !invChanges.noChange()) {
                    Element comp = ie.getOptionalElement(MailConstants.E_INVITE_COMPONENT);
                    if (comp != null) {
                        comp.addAttribute(MailConstants.A_CAL_CHANGES, invChanges.toString());
                    }
                }
                if (calItem != null) {
                    boolean showAll = invite.isPublic() || allowPrivateAccess(octxt, calItem);
                    if (showAll) {
                        RecurId rid = invite.getRecurId();
                        encodeCalendarReplies(ie, calItem, invite, rid != null ? rid.getDtZ() : null);
                    }
                } else if (null != remoteCalendarItem) {
                    CalendarReply.encodeCalendarReplyList(ie, remoteCalendarItem.getCalendarReplies());
                }
            }
        }
    }
    return ie;
}
Also used : InviteChanges(com.zimbra.cs.mailbox.calendar.InviteChanges) MimeMessage(javax.mail.internet.MimeMessage) Message(com.zimbra.cs.mailbox.Message) Element(com.zimbra.common.soap.Element) RecurId(com.zimbra.cs.mailbox.calendar.RecurId) ItemId(com.zimbra.cs.service.util.ItemId) ICalTok(com.zimbra.common.calendar.ZCalendar.ICalTok) CalendarItem(com.zimbra.cs.mailbox.CalendarItem) NoSuchItemException(com.zimbra.cs.mailbox.MailServiceException.NoSuchItemException) Mailbox(com.zimbra.cs.mailbox.Mailbox) ServiceException(com.zimbra.common.service.ServiceException) MailServiceException(com.zimbra.cs.mailbox.MailServiceException) MailServiceException(com.zimbra.cs.mailbox.MailServiceException) Invite(com.zimbra.cs.mailbox.calendar.Invite)

Example 17 with CalendarItem

use of com.zimbra.cs.mailbox.CalendarItem in project zm-mailbox by Zimbra.

the class SnoozeCalendarItemAlarm method handle.

public Element handle(Element request, Map<String, Object> context) throws ServiceException {
    ZimbraSoapContext zsc = getZimbraSoapContext(context);
    Mailbox mbox = getRequestedMailbox(zsc);
    String acctId = mbox.getAccountId();
    Account authAcct = getAuthenticatedAccount(zsc);
    OperationContext octxt = getOperationContext(zsc, context);
    Element response = getResponseElement(zsc);
    for (String calItemElement : sCalItemElems) {
        for (Iterator<Element> iter = request.elementIterator(calItemElement); iter.hasNext(); ) {
            Element calItemElem = iter.next();
            ItemId iid = new ItemId(calItemElem.getAttribute(MailConstants.A_ID), zsc);
            long snoozeUntil = calItemElem.getAttributeLong(MailConstants.A_CAL_ALARM_SNOOZE_UNTIL);
            // trace logging
            ZimbraLog.calendar.info("<SnoozeCalendarItemAlarm> id=%s,until=%d", iid.toString(), snoozeUntil);
            // mailbox for this calendar item; not necessarily same as requested mailbox
            Mailbox ciMbox = null;
            String ciAcctId = iid.getAccountId();
            if (ciAcctId == null || ciAcctId.equals(acctId)) {
                ciMbox = mbox;
            } else {
                // Let's see if the account is a Desktop account.
                if (AccountUtil.isZDesktopLocalAccount(zsc.getAuthtokenAccountId()))
                    ciMbox = MailboxManager.getInstance().getMailboxByAccountId(ciAcctId, false);
                if (ciMbox == null)
                    throw MailServiceException.PERM_DENIED("cannot snooze alarms of a shared calendar");
            }
            int calItemId = iid.getId();
            ItemIdFormatter ifmt = new ItemIdFormatter(authAcct.getId(), acctId, false);
            try {
                ciMbox.snoozeCalendarItemAlarm(octxt, calItemId, snoozeUntil);
                CalendarItem calItem = ciMbox.getCalendarItemById(octxt, calItemId);
                Element calItemRespElem;
                if (calItem instanceof Appointment)
                    calItemRespElem = response.addElement(MailConstants.E_APPOINTMENT);
                else
                    calItemRespElem = response.addElement(MailConstants.E_TASK);
                calItemRespElem.addAttribute(MailConstants.A_CAL_ID, iid.toString(ifmt));
                boolean hidePrivate = !calItem.allowPrivateAccess(authAcct, zsc.isUsingAdminPrivileges());
                boolean showAll = !hidePrivate || calItem.isPublic();
                if (showAll) {
                    AlarmData alarmData = calItem.getAlarmData();
                    if (alarmData != null)
                        ToXML.encodeAlarmData(calItemRespElem, calItem, alarmData);
                }
            } catch (NoSuchItemException nsie) {
                // item must not exist in db anymore.
                // this can happen if an alarm is snoozed while a big sync is happening which deletes the item (e.g. bug 48560)
                // since item is not in db, it has effectively been snoozed; return ok and no further alarms
                Element calItemRespElem = response.addElement(calItemElement);
                calItemRespElem.addAttribute(MailConstants.A_CAL_ID, iid.toString(ifmt));
            }
        }
    }
    return response;
}
Also used : OperationContext(com.zimbra.cs.mailbox.OperationContext) Appointment(com.zimbra.cs.mailbox.Appointment) Account(com.zimbra.cs.account.Account) ItemIdFormatter(com.zimbra.cs.service.util.ItemIdFormatter) Element(com.zimbra.common.soap.Element) ItemId(com.zimbra.cs.service.util.ItemId) NoSuchItemException(com.zimbra.cs.mailbox.MailServiceException.NoSuchItemException) CalendarItem(com.zimbra.cs.mailbox.CalendarItem) Mailbox(com.zimbra.cs.mailbox.Mailbox) ZimbraSoapContext(com.zimbra.soap.ZimbraSoapContext) AlarmData(com.zimbra.cs.mailbox.CalendarItem.AlarmData)

Example 18 with CalendarItem

use of com.zimbra.cs.mailbox.CalendarItem in project zm-mailbox by Zimbra.

the class SetCalendarItem method handle.

@Override
public Element handle(Element request, Map<String, Object> context) throws ServiceException {
    ZimbraSoapContext zsc = getZimbraSoapContext(context);
    Mailbox mbox = getRequestedMailbox(zsc);
    OperationContext octxt = getOperationContext(zsc, context);
    ItemIdFormatter ifmt = new ItemIdFormatter(zsc);
    int flags = Flag.toBitmask(request.getAttribute(MailConstants.A_FLAGS, null));
    String[] tags = TagUtil.parseTags(request, mbox, octxt);
    mbox.lock.lock();
    try {
        int defaultFolder = getItemType() == MailItem.Type.TASK ? Mailbox.ID_FOLDER_TASKS : Mailbox.ID_FOLDER_CALENDAR;
        String defaultFolderStr = Integer.toString(defaultFolder);
        String folderIdStr = request.getAttribute(MailConstants.A_FOLDER, defaultFolderStr);
        ItemId iidFolder = new ItemId(folderIdStr, zsc);
        Folder folder = mbox.getFolderById(octxt, iidFolder.getId());
        SetCalendarItemParseResult parsed = parseSetAppointmentRequest(request, zsc, octxt, folder, getItemType(), false);
        CalendarItem calItem = mbox.setCalendarItem(octxt, iidFolder.getId(), flags, tags, parsed.defaultInv, parsed.exceptions, parsed.replies, parsed.nextAlarm);
        Element response = getResponseElement(zsc);
        if (parsed.defaultInv != null) {
            response.addElement(MailConstants.A_DEFAULT).addAttribute(MailConstants.A_ID, ifmt.formatItemId(parsed.defaultInv.invite.getMailItemId()));
        }
        if (parsed.exceptions != null) {
            for (SetCalendarItemData cur : parsed.exceptions) {
                Element e = response.addElement(MailConstants.E_CAL_EXCEPT);
                e.addAttribute(MailConstants.A_CAL_RECURRENCE_ID, cur.invite.getRecurId().toString());
                e.addAttribute(MailConstants.A_ID, ifmt.formatItemId(cur.invite.getMailItemId()));
            }
        }
        String itemId = ifmt.formatItemId(calItem == null ? 0 : calItem.getId());
        response.addAttribute(MailConstants.A_CAL_ID, itemId);
        try {
            Element inv = request.getElement(MailConstants.A_DEFAULT).getElement(MailConstants.E_MSG).getElement(MailConstants.E_INVITE);
            Element comp = inv.getOptionalElement(MailConstants.E_INVITE_COMPONENT);
            if (comp != null) {
                inv = comp;
            }
            String reqCalItemId = inv.getAttribute(MailConstants.A_CAL_ID);
            String uid = inv.getAttribute(MailConstants.A_UID);
            // new or same as requested, or Outlook and case-insensitive equal
            boolean uidSame = (calItem == null || (calItem.getUid() == null && uid == null) || (calItem.getUid() != null && (calItem.getUid().equals(uid) || (Invite.isOutlookUid(calItem.getUid()) && calItem.getUid().equalsIgnoreCase(uid)))));
            if (ZimbraLog.calendar.isInfoEnabled()) {
                StringBuilder logBuf = new StringBuilder();
                if (!reqCalItemId.equals(itemId)) {
                    logBuf.append("Mapped requested id ").append(reqCalItemId).append(" -> ").append(itemId);
                }
                if (!uidSame) {
                    logBuf.append(" ?? requested UID ").append(uid).append(" differs from mapped ").append(calItem.getUid());
                    ZimbraLog.calendar.warn(logBuf.toString());
                } else if (logBuf.length() > 0) {
                    ZimbraLog.calendar.info(logBuf.toString());
                }
            }
            assert (uidSame);
        } catch (ServiceException se) {
        // one of the elements we wanted to use doesn't exist; ignore; no log/assertion possible
        }
        if (!parsed.isTodo)
            // for backward compat
            response.addAttribute(MailConstants.A_APPT_ID_DEPRECATE_ME, itemId);
        return response;
    } finally {
        mbox.lock.release();
    }
}
Also used : OperationContext(com.zimbra.cs.mailbox.OperationContext) ItemIdFormatter(com.zimbra.cs.service.util.ItemIdFormatter) Element(com.zimbra.common.soap.Element) Folder(com.zimbra.cs.mailbox.Folder) ItemId(com.zimbra.cs.service.util.ItemId) SetCalendarItemData(com.zimbra.cs.mailbox.Mailbox.SetCalendarItemData) CalendarItem(com.zimbra.cs.mailbox.CalendarItem) Mailbox(com.zimbra.cs.mailbox.Mailbox) ServiceException(com.zimbra.common.service.ServiceException) ZimbraSoapContext(com.zimbra.soap.ZimbraSoapContext)

Example 19 with CalendarItem

use of com.zimbra.cs.mailbox.CalendarItem in project zm-mailbox by Zimbra.

the class SearchResponse method add.

/**
 * The encoded element OR NULL if the search params contained a calItemExpand
 * range AND the calendar item did not have any instances in the specified range.
 *
 * @return could be NULL
 */
private Element add(CalendarItemHit hit) throws ServiceException {
    CalendarItem item = hit.getCalendarItem();
    Account acct = DocumentHandler.getRequestedAccount(zsc);
    long rangeStart = params.getCalItemExpandStart();
    long rangeEnd = params.getCalItemExpandEnd();
    if (rangeStart == -1 && rangeEnd == -1 && (item instanceof Appointment)) {
        // If no time range was given, force first instance only. (bug 51267)
        rangeStart = item.getStartTime();
        rangeEnd = rangeStart + 1;
    }
    EncodeCalendarItemResult encoded = GetCalendarItemSummaries.encodeCalendarItemInstances(zsc, octxt, item, acct, rangeStart, rangeEnd, true);
    Element el = encoded.element;
    if (el != null) {
        element.addElement(el);
        ToXML.setCalendarItemFields(el, ifmt, octxt, item, PendingModifications.Change.ALL_FIELDS, false, params.getNeuterImages());
        el.addAttribute(MailConstants.A_CONTENTMATCHED, true);
    }
    return el;
}
Also used : CalendarItem(com.zimbra.cs.mailbox.CalendarItem) Appointment(com.zimbra.cs.mailbox.Appointment) Account(com.zimbra.cs.account.Account) EncodeCalendarItemResult(com.zimbra.cs.service.mail.GetCalendarItemSummaries.EncodeCalendarItemResult) Element(com.zimbra.common.soap.Element)

Example 20 with CalendarItem

use of com.zimbra.cs.mailbox.CalendarItem in project zm-mailbox by Zimbra.

the class ZimbraQueryResultsImpl method getZimbraHit.

/**
 * We've got a {@link Mailbox}, a {@link DbSearch.Result} and (optionally) a Lucene Doc...
 * that's everything we need to build a real ZimbraHit.
 *
 * @param doc - Optional, only set if this search had a Lucene part
 */
ZimbraHit getZimbraHit(Mailbox mbox, DbSearch.Result sr, Document doc, DbSearch.FetchMode fetch) {
    MailItem item = null;
    ImapMessage i4msg = null;
    int modseq = -1, parentId = 0;
    switch(fetch) {
        case MAIL_ITEM:
            item = sr.getItem();
            break;
        case IMAP_MSG:
            i4msg = sr.getImapMessage();
            break;
        case MODSEQ:
            modseq = sr.getModSeq();
            break;
        case PARENT:
            parentId = sr.getParentId();
            break;
    }
    ZimbraHit result = null;
    switch(sr.getType()) {
        case CHAT:
        case MESSAGE:
            if (doc != null) {
                result = getMessagePartHit(mbox, sr.getId(), (Message) item, doc, sr.getSortValue());
            } else {
                result = getMessageHit(mbox, sr.getId(), (Message) item, null, sr.getSortValue());
            }
            break;
        case CONTACT:
            result = getContactHit(mbox, sr.getId(), (Contact) item, sr.getSortValue());
            break;
        case NOTE:
            result = getNoteHit(mbox, sr.getId(), (Note) item, sr.getSortValue());
            break;
        case APPOINTMENT:
            result = getAppointmentHit(mbox, sr.getId(), (CalendarItem) item, sr.getSortValue());
            break;
        case TASK:
            result = getTaskHit(mbox, sr.getId(), (Task) item, sr.getSortValue());
            break;
        case DOCUMENT:
        case WIKI:
            result = getDocumentHit(mbox, sr.getId(), (com.zimbra.cs.mailbox.Document) item, doc, sr.getSortValue());
            break;
        default:
            assert (false);
    }
    if (i4msg != null) {
        result.cacheImapMessage(i4msg);
    }
    if (modseq > 0) {
        result.cacheModifiedSequence(modseq);
    }
    if (parentId != 0) {
        result.cacheParentId(parentId);
    }
    return result;
}
Also used : CalendarItem(com.zimbra.cs.mailbox.CalendarItem) MailItem(com.zimbra.cs.mailbox.MailItem) Task(com.zimbra.cs.mailbox.Task) ImapMessage(com.zimbra.cs.imap.ImapMessage) Message(com.zimbra.cs.mailbox.Message) ImapMessage(com.zimbra.cs.imap.ImapMessage) Note(com.zimbra.cs.mailbox.Note) Document(org.apache.lucene.document.Document) Contact(com.zimbra.cs.mailbox.Contact)

Aggregations

CalendarItem (com.zimbra.cs.mailbox.CalendarItem)56 Mailbox (com.zimbra.cs.mailbox.Mailbox)36 Element (com.zimbra.common.soap.Element)27 OperationContext (com.zimbra.cs.mailbox.OperationContext)24 Invite (com.zimbra.cs.mailbox.calendar.Invite)23 ItemId (com.zimbra.cs.service.util.ItemId)22 Account (com.zimbra.cs.account.Account)19 ZimbraSoapContext (com.zimbra.soap.ZimbraSoapContext)17 ServiceException (com.zimbra.common.service.ServiceException)15 MailItem (com.zimbra.cs.mailbox.MailItem)14 MailServiceException (com.zimbra.cs.mailbox.MailServiceException)13 Message (com.zimbra.cs.mailbox.Message)12 ItemIdFormatter (com.zimbra.cs.service.util.ItemIdFormatter)11 Folder (com.zimbra.cs.mailbox.Folder)9 RecurId (com.zimbra.cs.mailbox.calendar.RecurId)9 Appointment (com.zimbra.cs.mailbox.Appointment)8 ArrayList (java.util.ArrayList)8 MimeMessage (javax.mail.internet.MimeMessage)8 ParsedDateTime (com.zimbra.common.calendar.ParsedDateTime)7 TimeZoneMap (com.zimbra.common.calendar.TimeZoneMap)5