Search in sources :

Example 1 with SetCalendarItemData

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

the class CalendarItem method processNewInviteExceptions.

void processNewInviteExceptions(List<SetCalendarItemData> scidList, int folderId, long nextAlarm, boolean preserveAlarms, boolean replaceExistingInvites) throws ServiceException {
    persistBatchedChanges = false;
    for (SetCalendarItemData scid : scidList) {
        processNewInviteRequestOrCancel(scid.message, scid.invite, folderId, nextAlarm, preserveAlarms, replaceExistingInvites, true);
    }
    // now update the recurrence.
    updateRecurrence(nextAlarm);
    if (persistBatchedChanges) {
        // persist the data to the DB.
        try {
            setContent(null, null);
        } catch (IOException e) {
            throw ServiceException.FAILURE("IOException", e);
        }
    }
}
Also used : IOException(java.io.IOException) SetCalendarItemData(com.zimbra.cs.mailbox.Mailbox.SetCalendarItemData)

Example 2 with SetCalendarItemData

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

the class ArchiveFormatter method addItem.

private void addItem(UserServletContext context, Folder fldr, Map<Object, Folder> fmap, FolderDigestInfo digestInfo, Map<Integer, Integer> idMap, int[] ids, Set<MailItem.Type> types, Resolve r, ItemData id, ArchiveInputStream ais, ArchiveInputEntry aie, List<ServiceException> errs) throws ServiceException {
    try {
        Mailbox mbox = fldr.getMailbox();
        MailItem mi = MailItem.constructItem(mbox, id.ud);
        MailItem newItem = null, oldItem = null;
        OperationContext octxt = context.opContext;
        String path;
        ParsedMessage pm;
        boolean root = fldr.getId() == Mailbox.ID_FOLDER_ROOT || fldr.getId() == Mailbox.ID_FOLDER_USER_ROOT || id.path.startsWith(fldr.getPath() + '/');
        if ((ids != null && Arrays.binarySearch(ids, id.ud.id) < 0) || (types != null && !types.contains(MailItem.Type.of(id.ud.type))))
            return;
        if (id.ud.getBlobDigest() != null && aie == null) {
            addError(errs, FormatterServiceException.MISSING_BLOB(id.path));
            return;
        }
        if (root) {
            path = id.path;
        } else {
            path = fldr.getPath() + id.path;
        }
        if (path.endsWith("/") && !path.equals("/")) {
            path = path.substring(0, path.length() - 1);
        }
        if (mbox.isImmutableSystemFolder(id.ud.folderId))
            return;
        switch(mi.getType()) {
            case APPOINTMENT:
            case TASK:
                CalendarItem ci = (CalendarItem) mi;
                fldr = createPath(context, fmap, path, ci.getType() == MailItem.Type.APPOINTMENT ? MailItem.Type.APPOINTMENT : MailItem.Type.TASK);
                if (!root || r != Resolve.Reset) {
                    CalendarItem oldCI = null;
                    try {
                        oldCI = mbox.getCalendarItemByUid(octxt, ci.getUid());
                    } catch (Exception e) {
                    }
                    if (oldCI != null && r == Resolve.Replace) {
                        mbox.delete(octxt, oldCI.getId(), oldCI.getType());
                    } else {
                        oldItem = oldCI;
                    }
                }
                if (oldItem == null || r != Resolve.Skip) {
                    CalendarItem.AlarmData ad = ci.getAlarmData();
                    byte[] data = readArchiveEntry(ais, aie);
                    Map<Integer, MimeMessage> blobMimeMsgMap = data == null ? null : CalendarItem.decomposeBlob(data);
                    SetCalendarItemData defScid = new SetCalendarItemData();
                    SetCalendarItemData[] exceptionScids = null;
                    Invite[] invs = ci.getInvites();
                    MimeMessage mm;
                    if (invs != null && invs.length > 0) {
                        defScid.invite = invs[0];
                        if (blobMimeMsgMap != null && (mm = blobMimeMsgMap.get(defScid.invite.getMailItemId())) != null) {
                            defScid.message = new ParsedMessage(mm, mbox.attachmentsIndexingEnabled());
                        }
                        if (invs.length > 1) {
                            exceptionScids = new SetCalendarItemData[invs.length - 1];
                            for (int i = 1; i < invs.length; i++) {
                                SetCalendarItemData scid = new SetCalendarItemData();
                                scid.invite = invs[i];
                                if (blobMimeMsgMap != null && (mm = blobMimeMsgMap.get(defScid.invite.getMailItemId())) != null) {
                                    scid.message = new ParsedMessage(mm, mbox.attachmentsIndexingEnabled());
                                }
                                exceptionScids[i - 1] = scid;
                            }
                        }
                        newItem = mbox.setCalendarItem(octxt, oldItem != null && r == Resolve.Modify ? oldItem.getFolderId() : fldr.getId(), ci.getFlagBitmask(), ci.getTags(), defScid, exceptionScids, ci.getAllReplies(), ad == null ? CalendarItem.NEXT_ALARM_KEEP_CURRENT : ad.getNextAt());
                    }
                }
                break;
            case CHAT:
                Chat chat = (Chat) mi;
                byte[] content = readArchiveEntry(ais, aie);
                pm = new ParsedMessage(content, mi.getDate(), mbox.attachmentsIndexingEnabled());
                fldr = createPath(context, fmap, path, MailItem.Type.CHAT);
                if (root && r != Resolve.Reset) {
                    Chat oldChat = null;
                    try {
                        oldChat = mbox.getChatById(octxt, chat.getId());
                        if (oldChat.getFolderId() != fldr.getId()) {
                            oldChat = null;
                        }
                    } catch (Exception e) {
                    }
                    if (oldChat != null && chat.getSender().equals(oldChat.getSender()) && chat.getSubject().equals(oldChat.getSubject())) {
                        if (r == Resolve.Replace) {
                            mbox.delete(octxt, oldChat.getId(), oldChat.getType());
                        } else {
                            oldItem = oldChat;
                            if (r == Resolve.Modify)
                                newItem = mbox.updateChat(octxt, pm, oldItem.getId());
                        }
                    }
                }
                if (oldItem == null)
                    newItem = mbox.createChat(octxt, pm, fldr.getId(), chat.getFlagBitmask(), chat.getTags());
                break;
            case CONVERSATION:
                Conversation cv = (Conversation) mi;
                if (r != Resolve.Reset && r != Resolve.Skip) {
                    try {
                        oldItem = mbox.getConversationByHash(octxt, Mailbox.getHash(cv.getSubject()));
                    } catch (Exception e) {
                    }
                }
                break;
            case CONTACT:
                Contact ct = (Contact) mi;
                fldr = createPath(context, fmap, path, Folder.Type.CONTACT);
                if (root && r != Resolve.Reset) {
                    Contact oldContact = null;
                    oldContact = findContact(octxt, mbox, ct, fldr);
                    if (oldContact != null) {
                        String email = string(ct.get(ContactConstants.A_email));
                        String first = string(ct.get(ContactConstants.A_firstName));
                        String name = string(ct.get(ContactConstants.A_fullName));
                        String oldemail = string(oldContact.get(ContactConstants.A_email));
                        String oldfirst = string(oldContact.get(ContactConstants.A_firstName));
                        String oldname = string(oldContact.get(ContactConstants.A_fullName));
                        if (email.equals(oldemail) && first.equals(oldfirst) && name.equals(oldname)) {
                            if (r == Resolve.Replace) {
                                mbox.delete(octxt, oldContact.getId(), oldContact.getType());
                            } else {
                                oldItem = oldContact;
                                if (r == Resolve.Modify) {
                                    mbox.modifyContact(octxt, oldItem.getId(), new ParsedContact(ct.getFields(), readArchiveEntry(ais, aie)));
                                }
                            }
                        }
                    }
                }
                if (oldItem == null) {
                    newItem = mbox.createContact(octxt, new ParsedContact(ct.getFields(), readArchiveEntry(ais, aie)), fldr.getId(), ct.getTags());
                }
                break;
            case DOCUMENT:
            case WIKI:
                Document doc = (Document) mi;
                Document oldDoc = null;
                Integer oldId = idMap.get(mi.getId());
                fldr = createParent(context, fmap, path, doc.getType() == MailItem.Type.DOCUMENT ? MailItem.Type.DOCUMENT : MailItem.Type.WIKI);
                if (oldId == null) {
                    try {
                        for (Document listDoc : mbox.getDocumentList(octxt, fldr.getId())) {
                            if (doc.getName().equals(listDoc.getName())) {
                                oldDoc = listDoc;
                                idMap.put(doc.getId(), oldDoc.getId());
                                break;
                            }
                        }
                    } catch (Exception e) {
                    }
                } else {
                    oldDoc = mbox.getDocumentById(octxt, oldId);
                }
                if (oldDoc != null) {
                    if (r == Resolve.Replace && oldId == null) {
                        mbox.delete(octxt, oldDoc.getId(), oldDoc.getType());
                    } else if (doc.getVersion() < oldDoc.getVersion()) {
                        return;
                    } else {
                        oldItem = oldDoc;
                        if (doc.getVersion() > oldDoc.getVersion()) {
                            newItem = mbox.addDocumentRevision(octxt, oldDoc.getId(), doc.getCreator(), doc.getName(), doc.getDescription(), doc.isDescriptionEnabled(), ais.getInputStream());
                        }
                        if (r != Resolve.Skip) {
                            mbox.setDate(octxt, oldDoc.getId(), doc.getType(), doc.getDate());
                        }
                    }
                }
                if (oldItem == null) {
                    if (mi.getType() == MailItem.Type.DOCUMENT) {
                        newItem = mbox.createDocument(octxt, fldr.getId(), doc.getName(), doc.getContentType(), doc.getCreator(), doc.getDescription(), ais.getInputStream());
                    } else {
                        WikiItem wi = (WikiItem) mi;
                        newItem = mbox.createWiki(octxt, fldr.getId(), wi.getWikiWord(), wi.getCreator(), wi.getDescription(), ais.getInputStream());
                    }
                    mbox.setDate(octxt, newItem.getId(), doc.getType(), doc.getDate());
                    idMap.put(doc.getId(), newItem.getId());
                }
                break;
            case FLAG:
                return;
            case FOLDER:
                String aclParam = context.params.get("acl");
                boolean doACL = aclParam == null || !aclParam.equals("0");
                Folder f = (Folder) mi;
                ACL acl = f.getACL();
                Folder oldF = null;
                MailItem.Type view = f.getDefaultView();
                if (view == MailItem.Type.CONVERSATION || view == MailItem.Type.FLAG || view == MailItem.Type.TAG)
                    break;
                try {
                    oldF = mbox.getFolderByPath(octxt, path);
                } catch (Exception e) {
                }
                if (oldF != null) {
                    oldItem = oldF;
                    if (r != Resolve.Skip) {
                        if (!f.getUrl().equals(oldF.getUrl())) {
                            mbox.setFolderUrl(octxt, oldF.getId(), f.getUrl());
                        }
                        if (doACL) {
                            ACL oldACL = oldF.getACL();
                            if ((acl == null && oldACL != null) || (acl != null && (oldACL == null || !acl.equals(oldACL)))) {
                                mbox.setPermissions(octxt, oldF.getId(), acl);
                            }
                        }
                    }
                }
                if (oldItem == null) {
                    fldr = createParent(context, fmap, path, Folder.Type.UNKNOWN);
                    Folder.FolderOptions fopt = new Folder.FolderOptions();
                    fopt.setDefaultView(f.getDefaultView()).setFlags(f.getFlagBitmask()).setColor(f.getColor()).setUrl(f.getUrl());
                    newItem = fldr = mbox.createFolder(octxt, f.getName(), fldr.getId(), fopt);
                    if (doACL && acl != null) {
                        mbox.setPermissions(octxt, fldr.getId(), acl);
                    }
                    fmap.put(fldr.getId(), fldr);
                    fmap.put(fldr.getPath(), fldr);
                }
                break;
            case MESSAGE:
                Message msg = (Message) mi;
                Message oldMsg = null;
                fldr = createPath(context, fmap, path, Folder.Type.MESSAGE);
                if (root && r != Resolve.Reset) {
                    try {
                        oldMsg = mbox.getMessageById(octxt, msg.getId());
                        if (!msg.getDigest().equals(oldMsg.getDigest()) || oldMsg.getFolderId() != fldr.getId()) {
                            oldMsg = null;
                        }
                    } catch (Exception e) {
                    }
                }
                if (oldMsg == null) {
                    Integer digestId = digestInfo.getIdForDigest(fldr, mi.getDigest());
                    if (digestId != null) {
                        oldMsg = mbox.getMessageById(octxt, digestId);
                        if (!msg.getDigest().equals(oldMsg.getDigest())) {
                            oldMsg = null;
                        }
                    }
                }
                if (oldMsg != null) {
                    if (r == Resolve.Replace) {
                        ZimbraLog.misc.debug("Deleting old msg with id=%s as has same digest='%s'", oldMsg.getId(), mi.getDigest());
                        mbox.delete(octxt, oldMsg.getId(), oldMsg.getType());
                    } else {
                        oldItem = oldMsg;
                    }
                }
                if (oldItem != null) {
                    ZimbraLog.misc.debug("Message with id=%s has same digest='%s' - not re-adding", oldItem.getId(), mi.getDigest());
                } else {
                    DeliveryOptions opt = new DeliveryOptions().setFolderId(fldr.getId()).setNoICal(true).setFlags(msg.getFlagBitmask()).setTags(msg.getTags());
                    newItem = mbox.addMessage(octxt, ais.getInputStream(), (int) aie.getSize(), msg.getDate(), opt, null, id);
                }
                break;
            case MOUNTPOINT:
                Mountpoint mp = (Mountpoint) mi;
                MailItem oldMP = null;
                try {
                    oldMP = mbox.getItemByPath(octxt, path);
                    if (oldMP.getType() == mi.getType()) {
                        oldMP = null;
                    }
                } catch (Exception e) {
                }
                if (oldMP != null) {
                    if (r == Resolve.Modify || r == Resolve.Replace) {
                        mbox.delete(octxt, oldMP.getId(), oldMP.getType());
                    } else {
                        oldItem = oldMP;
                    }
                }
                if (oldItem == null) {
                    fldr = createParent(context, fmap, path, Folder.Type.UNKNOWN);
                    newItem = mbox.createMountpoint(context.opContext, fldr.getId(), mp.getName(), mp.getOwnerId(), mp.getRemoteId(), mp.getRemoteUuid(), mp.getDefaultView(), mp.getFlagBitmask(), mp.getColor(), mp.isReminderEnabled());
                }
                break;
            case NOTE:
                Note note = (Note) mi;
                Note oldNote = null;
                fldr = createPath(context, fmap, path, MailItem.Type.NOTE);
                try {
                    for (Note listNote : mbox.getNoteList(octxt, fldr.getId())) {
                        if (note.getSubject().equals(listNote.getSubject())) {
                            oldNote = listNote;
                            break;
                        }
                    }
                } catch (Exception e) {
                }
                if (oldNote != null) {
                    if (r == Resolve.Replace) {
                        mbox.delete(octxt, oldNote.getId(), oldNote.getType());
                    } else {
                        oldItem = oldNote;
                        if (r == Resolve.Modify) {
                            mbox.editNote(octxt, oldItem.getId(), new String(readArchiveEntry(ais, aie), UTF8));
                        }
                    }
                    break;
                }
                if (oldItem == null) {
                    newItem = mbox.createNote(octxt, new String(readArchiveEntry(ais, aie), UTF8), note.getBounds(), note.getColor(), fldr.getId());
                }
                break;
            case SEARCHFOLDER:
                SearchFolder sf = (SearchFolder) mi;
                MailItem oldSF = null;
                try {
                    oldSF = mbox.getItemByPath(octxt, path);
                    if (oldSF.getType() == mi.getType()) {
                        oldSF = null;
                    }
                } catch (Exception e) {
                }
                if (oldSF != null) {
                    if (r == Resolve.Modify) {
                        mbox.modifySearchFolder(octxt, oldSF.getId(), sf.getQuery(), sf.getReturnTypes(), sf.getSortField());
                    } else if (r == Resolve.Replace) {
                        mbox.delete(octxt, oldSF.getId(), oldSF.getType());
                    } else {
                        oldItem = oldSF;
                    }
                }
                if (oldItem == null) {
                    fldr = createParent(context, fmap, path, MailItem.Type.UNKNOWN);
                    newItem = mbox.createSearchFolder(octxt, fldr.getId(), sf.getName(), sf.getQuery(), sf.getReturnTypes(), sf.getSortField(), sf.getFlagBitmask(), sf.getColor());
                }
                break;
            case TAG:
                Tag tag = (Tag) mi;
                try {
                    Tag oldTag = mbox.getTagByName(octxt, tag.getName());
                    oldItem = oldTag;
                } catch (Exception e) {
                }
                if (oldItem == null) {
                    newItem = mbox.createTag(octxt, tag.getName(), tag.getColor());
                }
                break;
            case VIRTUAL_CONVERSATION:
                return;
        }
        if (newItem != null) {
            if (mi.getColor() != newItem.getColor()) {
                mbox.setColor(octxt, newItem.getId(), newItem.getType(), mi.getColor());
            }
            if (!id.flags.equals(newItem.getFlagString()) || !id.tagsEqual(newItem)) {
                mbox.setTags(octxt, newItem.getId(), newItem.getType(), Flag.toBitmask(id.flags), getTagNames(id), null);
            }
        } else if (oldItem != null && r == Resolve.Modify) {
            if (mi.getColor() != oldItem.getColor()) {
                mbox.setColor(octxt, oldItem.getId(), oldItem.getType(), mi.getColor());
            }
            if (!id.flags.equals(oldItem.getFlagString()) || !id.tagsEqual(oldItem)) {
                mbox.setTags(octxt, oldItem.getId(), oldItem.getType(), Flag.toBitmask(id.flags), getTagNames(id), null);
            }
        }
    } catch (MailServiceException e) {
        if (e.getCode() == MailServiceException.QUOTA_EXCEEDED) {
            throw e;
        } else if (r != Resolve.Skip || e.getCode() != MailServiceException.ALREADY_EXISTS) {
            addError(errs, e);
        }
    } catch (Exception e) {
        String path = id.path;
        // When importing items into, e.g. the Inbox, often path is just "/Inbox" which isn't that useful
        if ((aie != null) && !Strings.isNullOrEmpty(aie.getName())) {
            path = aie.getName();
        }
        addError(errs, FormatterServiceException.UNKNOWN_ERROR(path, e));
    }
}
Also used : MimeMessage(javax.mail.internet.MimeMessage) ParsedMessage(com.zimbra.cs.mime.ParsedMessage) Message(com.zimbra.cs.mailbox.Message) Conversation(com.zimbra.cs.mailbox.Conversation) Document(com.zimbra.cs.mailbox.Document) SearchFolder(com.zimbra.cs.mailbox.SearchFolder) Folder(com.zimbra.cs.mailbox.Folder) SetCalendarItemData(com.zimbra.cs.mailbox.Mailbox.SetCalendarItemData) CalendarItem(com.zimbra.cs.mailbox.CalendarItem) ParsedContact(com.zimbra.cs.mime.ParsedContact) Mailbox(com.zimbra.cs.mailbox.Mailbox) MimeMessage(javax.mail.internet.MimeMessage) Chat(com.zimbra.cs.mailbox.Chat) MailServiceException(com.zimbra.cs.mailbox.MailServiceException) DeliveryOptions(com.zimbra.cs.mailbox.DeliveryOptions) Mountpoint(com.zimbra.cs.mailbox.Mountpoint) OperationContext(com.zimbra.cs.mailbox.OperationContext) ParsedMessage(com.zimbra.cs.mime.ParsedMessage) SearchFolder(com.zimbra.cs.mailbox.SearchFolder) ACL(com.zimbra.cs.mailbox.ACL) NoSuchItemException(com.zimbra.cs.mailbox.MailServiceException.NoSuchItemException) ExportPeriodNotSpecifiedException(com.zimbra.cs.mailbox.MailServiceException.ExportPeriodNotSpecifiedException) ServiceException(com.zimbra.common.service.ServiceException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) MailServiceException(com.zimbra.cs.mailbox.MailServiceException) ExportPeriodTooLongException(com.zimbra.cs.mailbox.MailServiceException.ExportPeriodTooLongException) UserServletException(com.zimbra.cs.service.UserServletException) Mountpoint(com.zimbra.cs.mailbox.Mountpoint) ParsedContact(com.zimbra.cs.mime.ParsedContact) Contact(com.zimbra.cs.mailbox.Contact) MailItem(com.zimbra.cs.mailbox.MailItem) WikiItem(com.zimbra.cs.mailbox.WikiItem) Note(com.zimbra.cs.mailbox.Note) Tag(com.zimbra.cs.mailbox.Tag) Invite(com.zimbra.cs.mailbox.calendar.Invite)

Example 3 with SetCalendarItemData

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

the class SetCalendarItem method parseSetAppointmentRequest.

public static SetCalendarItemParseResult parseSetAppointmentRequest(Element request, ZimbraSoapContext zsc, OperationContext octxt, Folder folder, MailItem.Type type, boolean parseIds) throws ServiceException {
    Account acct = getRequestedAccount(zsc);
    Mailbox mbox = getRequestedMailbox(zsc);
    SetCalendarItemParseResult result = new SetCalendarItemParseResult();
    ArrayList<SetCalendarItemData> exceptions = new ArrayList<SetCalendarItemData>();
    Invite defInv = null;
    // First, the <default>
    {
        Element e = request.getOptionalElement(MailConstants.A_DEFAULT);
        if (e != null) {
            result.defaultInv = getSetCalendarItemData(zsc, octxt, acct, mbox, e, new SetCalendarItemInviteParser(false, false, folder, type));
            defInv = result.defaultInv.invite;
        }
    }
    // for each <except>
    for (Element e : request.listElements(MailConstants.E_CAL_EXCEPT)) {
        SetCalendarItemData exDat = getSetCalendarItemData(zsc, octxt, acct, mbox, e, new SetCalendarItemInviteParser(true, false, folder, type));
        exceptions.add(exDat);
        if (defInv == null) {
            defInv = exDat.invite;
        }
    }
    // for each <cancel>
    for (Element e : request.listElements(MailConstants.E_CAL_CANCEL)) {
        SetCalendarItemData exDat = getSetCalendarItemData(zsc, octxt, acct, mbox, e, new SetCalendarItemInviteParser(true, true, folder, type));
        exceptions.add(exDat);
        if (defInv == null) {
            defInv = exDat.invite;
        }
    }
    if (exceptions.size() > 0) {
        result.exceptions = new SetCalendarItemData[exceptions.size()];
        exceptions.toArray(result.exceptions);
    } else {
        if (result.defaultInv == null)
            throw ServiceException.INVALID_REQUEST("No default/except/cancel specified", null);
    }
    // <replies>
    Element repliesElem = request.getOptionalElement(MailConstants.E_CAL_REPLIES);
    if (repliesElem != null)
        result.replies = CalendarUtils.parseReplyList(repliesElem, defInv.getTimeZoneMap());
    result.isTodo = defInv != null && defInv.isTodo();
    boolean noNextAlarm = request.getAttributeBool(MailConstants.A_CAL_NO_NEXT_ALARM, false);
    if (noNextAlarm)
        result.nextAlarm = CalendarItem.NEXT_ALARM_ALL_DISMISSED;
    else
        result.nextAlarm = request.getAttributeLong(MailConstants.A_CAL_NEXT_ALARM, CalendarItem.NEXT_ALARM_KEEP_CURRENT);
    return result;
}
Also used : Account(com.zimbra.cs.account.Account) Mailbox(com.zimbra.cs.mailbox.Mailbox) Element(com.zimbra.common.soap.Element) ArrayList(java.util.ArrayList) Invite(com.zimbra.cs.mailbox.calendar.Invite) SetCalendarItemData(com.zimbra.cs.mailbox.Mailbox.SetCalendarItemData)

Example 4 with SetCalendarItemData

use of com.zimbra.cs.mailbox.Mailbox.SetCalendarItemData 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 5 with SetCalendarItemData

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

the class MetadataTest method legacyCalendarItem.

@Test
public void legacyCalendarItem() throws ServiceException, MessagingException {
    Mailbox mbox = MailboxManager.getInstance().getMailboxByAccountId(MockProvisioning.DEFAULT_ACCOUNT_ID);
    SetCalendarItemData defaultInv = new SetCalendarItemData();
    MimeMessage message = new JavaMailMimeMessage(JMSession.getSession(), new ZSharedFileInputStream("data/TestMailRaw/invite1"));
    defaultInv.message = new ParsedMessage(message, Calendar.getInstance().getTimeInMillis(), false);
    TimeZoneMap tzMap = new TimeZoneMap(WellKnownTimeZones.getTimeZoneById("EST"));
    Invite invite = new Invite("REQUEST", tzMap, false);
    invite.setUid("test-uid");
    Calendar cal = GregorianCalendar.getInstance();
    cal.set(2005, 1, 21);
    invite.setDtStart(ParsedDateTime.fromUTCTime(cal.getTimeInMillis()));
    cal.set(2005, 2, 21);
    invite.setDtEnd(ParsedDateTime.fromUTCTime(cal.getTimeInMillis()));
    defaultInv.invite = invite;
    CalendarItem calItem = mbox.setCalendarItem(null, Mailbox.ID_FOLDER_CALENDAR, 0, null, defaultInv, null, null, CalendarItem.NEXT_ALARM_KEEP_CURRENT);
    calItem.mData.dateChanged = (int) (cal.getTimeInMillis() / 1000L);
    Metadata meta = calItem.encodeMetadata();
    meta.remove(Metadata.FN_TZMAP);
    calItem.decodeMetadata(meta);
    Assert.assertEquals(0, calItem.getStartTime());
    Assert.assertEquals(0, calItem.getEndTime());
    //simulate existence of FN_TZMAP with bad content. In reality the metadata versions 4, 5, 6 had more subtle differences in invite encoding, but this provokes the exception we need
    meta.put(Metadata.FN_TZMAP, "foo");
    calItem.decodeMetadata(meta);
    Assert.assertEquals(0, calItem.getStartTime());
    Assert.assertEquals(0, calItem.getEndTime());
    cal.set(2007, 2, 21);
    calItem.mData.dateChanged = (int) (cal.getTimeInMillis() / 1000L);
    boolean caught = false;
    try {
        calItem.decodeMetadata(meta);
    } catch (ServiceException se) {
        if (se.getCode().equalsIgnoreCase(ServiceException.INVALID_REQUEST)) {
            caught = true;
        }
    }
    Assert.assertTrue("new(er) appointment with bad metadata", caught);
}
Also used : ParsedMessage(com.zimbra.cs.mime.ParsedMessage) Calendar(com.ibm.icu.util.Calendar) GregorianCalendar(com.ibm.icu.util.GregorianCalendar) JavaMailMimeMessage(com.zimbra.common.mime.shim.JavaMailMimeMessage) SetCalendarItemData(com.zimbra.cs.mailbox.Mailbox.SetCalendarItemData) ZSharedFileInputStream(com.zimbra.common.zmime.ZSharedFileInputStream) ServiceException(com.zimbra.common.service.ServiceException) MimeMessage(javax.mail.internet.MimeMessage) JavaMailMimeMessage(com.zimbra.common.mime.shim.JavaMailMimeMessage) TimeZoneMap(com.zimbra.common.calendar.TimeZoneMap) Invite(com.zimbra.cs.mailbox.calendar.Invite) Test(org.junit.Test)

Aggregations

SetCalendarItemData (com.zimbra.cs.mailbox.Mailbox.SetCalendarItemData)9 Invite (com.zimbra.cs.mailbox.calendar.Invite)7 ServiceException (com.zimbra.common.service.ServiceException)5 Mailbox (com.zimbra.cs.mailbox.Mailbox)5 Element (com.zimbra.common.soap.Element)4 CalendarItem (com.zimbra.cs.mailbox.CalendarItem)4 OperationContext (com.zimbra.cs.mailbox.OperationContext)4 ParsedMessage (com.zimbra.cs.mime.ParsedMessage)4 MimeMessage (javax.mail.internet.MimeMessage)4 MailItem (com.zimbra.cs.mailbox.MailItem)3 IOException (java.io.IOException)3 Account (com.zimbra.cs.account.Account)2 DavException (com.zimbra.cs.dav.DavException)2 Folder (com.zimbra.cs.mailbox.Folder)2 MailServiceException (com.zimbra.cs.mailbox.MailServiceException)2 NoSuchItemException (com.zimbra.cs.mailbox.MailServiceException.NoSuchItemException)2 RecurId (com.zimbra.cs.mailbox.calendar.RecurId)2 ZimbraSoapContext (com.zimbra.soap.ZimbraSoapContext)2 ArrayList (java.util.ArrayList)2 Calendar (com.ibm.icu.util.Calendar)1