Search in sources :

Example 1 with Type

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

the class PagedDeleteTest method testRemoveBeforeAfterCutoff.

@Test
public void testRemoveBeforeAfterCutoff() {
    TypedIdList tombstone = new TypedIdList();
    tombstone.add(Type.MESSAGE, 3, "", 100);
    tombstone.add(Type.MESSAGE, 4, "", 101);
    tombstone.add(Type.APPOINTMENT, 1, "", 101);
    tombstone.add(Type.APPOINTMENT, 5, "", 100);
    tombstone.add(Type.APPOINTMENT, 9, "", 103);
    tombstone.add(Type.MESSAGE, 2, "", 99);
    tombstone.add(Type.MESSAGE, 22, "", 105);
    tombstone.add(Type.MESSAGE, 24, "", 103);
    tombstone.add(Type.MESSAGE, 28, "", 106);
    PagedDelete pgDelete = new PagedDelete(tombstone, true);
    pgDelete.removeBeforeCutoff(4, 101);
    Collection<Integer> ids = pgDelete.getAllIds();
    Assert.assertEquals(5, ids.size());
    pgDelete.trimDeletesTillPageLimit(5);
    ids = pgDelete.getAllIds();
    Assert.assertEquals(5, ids.size());
    pgDelete.removeAfterCutoff(105);
    ids = pgDelete.getAllIds();
    Assert.assertEquals(4, ids.size());
    Assert.assertTrue(ids.contains(4));
    Assert.assertTrue(ids.contains(9));
    Assert.assertTrue(ids.contains(24));
    Assert.assertTrue(ids.contains(22));
    Assert.assertTrue(pgDelete.isDeleteOverFlow());
    Assert.assertEquals(pgDelete.getCutOffModsequnce(), 106);
    Assert.assertEquals(pgDelete.getLastItemId(), 28);
    Multimap<Type, Integer> ids2Type = pgDelete.getTypedItemIds();
    Assert.assertEquals(4, ids2Type.size());
    Assert.assertTrue(ids2Type.containsEntry(Type.MESSAGE, 4));
    Assert.assertTrue(ids2Type.containsEntry(Type.APPOINTMENT, 9));
    Assert.assertTrue(ids2Type.containsEntry(Type.MESSAGE, 24));
    Assert.assertTrue(ids2Type.containsEntry(Type.MESSAGE, 22));
}
Also used : Type(com.zimbra.cs.mailbox.MailItem.Type) PagedDelete(com.zimbra.cs.mailbox.util.PagedDelete) TypedIdList(com.zimbra.cs.mailbox.util.TypedIdList) Test(org.junit.Test)

Example 2 with Type

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

the class MailItemResource method patchProperties.

/* Modifies the set of dead properties saved for this resource.
     * Properties in the parameter 'set' are added to the existing properties.
     * Properties in 'remove' are removed.
     */
@Override
public void patchProperties(DavContext ctxt, java.util.Collection<Element> set, java.util.Collection<QName> remove) throws DavException, IOException {
    List<QName> reqProps = new ArrayList<QName>();
    for (QName n : remove) {
        mDeadProps.remove(n);
        reqProps.add(n);
    }
    for (Element e : set) {
        QName name = e.getQName();
        if (name.equals(DavElements.E_DISPLAYNAME) && (type == MailItem.Type.FOLDER || type == MailItem.Type.MOUNTPOINT)) {
            // rename folder
            try {
                String val = e.getText();
                String uri = getUri();
                Mailbox mbox = getMailbox(ctxt);
                mbox.rename(ctxt.getOperationContext(), mId, type, val, mFolderId);
                setProperty(DavElements.P_DISPLAYNAME, val);
                UrlNamespace.addToRenamedResource(getOwner(), uri, this);
                UrlNamespace.addToRenamedResource(getOwner(), uri.substring(0, uri.length() - 1), this);
            } catch (ServiceException se) {
                ctxt.getResponseProp().addPropError(DavElements.E_DISPLAYNAME, new DavException(se.getMessage(), DavProtocol.STATUS_FAILED_DEPENDENCY));
            }
            mDeadProps.remove(name);
            continue;
        } else if (name.equals(DavElements.E_CALENDAR_COLOR) && (type == MailItem.Type.FOLDER || type == MailItem.Type.MOUNTPOINT)) {
            // change color
            String colorStr = e.getText();
            Color color = new Color(colorStr.substring(0, 7));
            byte col = (byte) COLOR_LIST.indexOf(colorStr);
            if (col >= 0)
                color.setColor(col);
            try {
                Mailbox mbox = getMailbox(ctxt);
                mbox.setColor(ctxt.getOperationContext(), new int[] { mId }, type, color);
            } catch (ServiceException se) {
                ctxt.getResponseProp().addPropError(DavElements.E_CALENDAR_COLOR, new DavException(se.getMessage(), DavProtocol.STATUS_FAILED_DEPENDENCY));
            }
            mDeadProps.remove(name);
            continue;
        } else if (name.equals(DavElements.E_SUPPORTED_CALENDAR_COMPONENT_SET)) {
            // change default view
            @SuppressWarnings("unchecked") List<Element> elements = e.elements(DavElements.E_COMP);
            boolean isTodo = false;
            boolean isEvent = false;
            for (Element element : elements) {
                Attribute attr = element.attribute(DavElements.P_NAME);
                if (attr != null && CalComponent.VTODO.name().equals(attr.getValue())) {
                    isTodo = true;
                } else if (attr != null && CalComponent.VEVENT.name().equals(attr.getValue())) {
                    isEvent = true;
                }
            }
            if (isEvent ^ isTodo) {
                // we support a calendar collection of type event or todo, not both or none.
                Type type = (isEvent) ? Type.APPOINTMENT : Type.TASK;
                try {
                    Mailbox mbox = getMailbox(ctxt);
                    mbox.setFolderDefaultView(ctxt.getOperationContext(), mId, type);
                    // See UrlNamespace.addToRenamedResource()
                    if (this instanceof Collection) {
                        ((Collection) this).view = type;
                    }
                } catch (ServiceException se) {
                    ctxt.getResponseProp().addPropError(name, new DavException(se.getMessage(), DavProtocol.STATUS_FAILED_DEPENDENCY));
                }
            } else {
                ctxt.getResponseProp().addPropError(name, new DavException.CannotModifyProtectedProperty(name));
            }
            continue;
        }
        mDeadProps.put(name, e);
        reqProps.add(name);
    }
    String configVal = "";
    if (mDeadProps.size() > 0) {
        org.dom4j.Document doc = org.dom4j.DocumentHelper.createDocument();
        Element top = doc.addElement(CONFIG_KEY);
        for (Map.Entry<QName, Element> entry : mDeadProps.entrySet()) top.add(entry.getValue().detach());
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        OutputFormat format = OutputFormat.createCompactFormat();
        XMLWriter writer = new XMLWriter(out, format);
        writer.write(doc);
        configVal = new String(out.toByteArray(), "UTF-8");
        if (configVal.length() > PROP_LENGTH_LIMIT)
            for (Map.Entry<QName, Element> entry : mDeadProps.entrySet()) ctxt.getResponseProp().addPropError(entry.getKey(), new DavException("prop length exceeded", DavProtocol.STATUS_INSUFFICIENT_STORAGE));
    }
    Mailbox mbox = null;
    try {
        mbox = getMailbox(ctxt);
        mbox.lock.lock();
        Metadata data = mbox.getConfig(ctxt.getOperationContext(), CONFIG_KEY);
        if (data == null) {
            data = new Metadata();
        }
        data.put(Integer.toString(mId), configVal);
        mbox.setConfig(ctxt.getOperationContext(), CONFIG_KEY, data);
    } catch (ServiceException se) {
        for (QName qname : reqProps) ctxt.getResponseProp().addPropError(qname, new DavException(se.getMessage(), HttpServletResponse.SC_FORBIDDEN));
    } finally {
        if (mbox != null)
            mbox.lock.release();
    }
}
Also used : Attribute(org.dom4j.Attribute) Element(org.dom4j.Element) ArrayList(java.util.ArrayList) Metadata(com.zimbra.cs.mailbox.Metadata) XMLWriter(org.dom4j.io.XMLWriter) Mailbox(com.zimbra.cs.mailbox.Mailbox) ZMailbox(com.zimbra.client.ZMailbox) DavException(com.zimbra.cs.dav.DavException) QName(org.dom4j.QName) Color(com.zimbra.common.mailbox.Color) OutputFormat(org.dom4j.io.OutputFormat) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Type(com.zimbra.cs.mailbox.MailItem.Type) ServiceException(com.zimbra.common.service.ServiceException) MailServiceException(com.zimbra.cs.mailbox.MailServiceException) Map(java.util.Map) HashMap(java.util.HashMap)

Example 3 with Type

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

the class PendingModifications method deserialize.

@SuppressWarnings("unchecked")
public static PendingModifications deserialize(Mailbox mbox, byte[] data) throws IOException, ClassNotFoundException, ServiceException {
    ByteArrayInputStream bis = new ByteArrayInputStream(data);
    ObjectInputStream ois = new ObjectInputStream(bis);
    PendingModifications pms = new PendingModifications();
    pms.changedTypes = (Set<Type>) ois.readObject();
    LinkedHashMap<ModificationKeyMeta, String> metaCreated = (LinkedHashMap<ModificationKeyMeta, String>) ois.readObject();
    if (metaCreated != null) {
        pms.created = new LinkedHashMap<ModificationKey, MailItem>();
        Iterator<Entry<ModificationKeyMeta, String>> iter = metaCreated.entrySet().iterator();
        while (iter.hasNext()) {
            Entry<ModificationKeyMeta, String> entry = iter.next();
            Metadata meta = new Metadata(entry.getValue());
            MailItem.UnderlyingData ud = new MailItem.UnderlyingData();
            ud.deserialize(meta);
            MailItem item = MailItem.constructItem(mbox, ud, true);
            if (item instanceof Folder) {
                Folder folder = ((Folder) item);
                folder.setParent(mbox.getFolderById(null, folder.getFolderId()));
            }
            ModificationKey key = new ModificationKey(entry.getKey().accountId, entry.getKey().itemId);
            pms.created.put(key, item);
        }
    }
    Map<ModificationKeyMeta, ChangeMeta> metaModified = (Map<ModificationKeyMeta, ChangeMeta>) ois.readObject();
    pms.modified = getOriginal(mbox, metaModified);
    Map<ModificationKeyMeta, ChangeMeta> metaDeleted = (Map<ModificationKeyMeta, ChangeMeta>) ois.readObject();
    pms.deleted = getOriginal(mbox, metaDeleted);
    return pms;
}
Also used : Metadata(com.zimbra.cs.mailbox.Metadata) Folder(com.zimbra.cs.mailbox.Folder) LinkedHashMap(java.util.LinkedHashMap) Type(com.zimbra.cs.mailbox.MailItem.Type) MailItem(com.zimbra.cs.mailbox.MailItem) Entry(java.util.Map.Entry) ByteArrayInputStream(java.io.ByteArrayInputStream) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) ObjectInputStream(java.io.ObjectInputStream)

Example 4 with Type

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

the class PagedDeleteTest method testRemoveBeforeCutoff.

@Test
public void testRemoveBeforeCutoff() {
    TypedIdList tombstone = new TypedIdList();
    tombstone.add(Type.MESSAGE, 3, "", 100);
    tombstone.add(Type.MESSAGE, 4, "", 101);
    tombstone.add(Type.APPOINTMENT, 1, "", 101);
    tombstone.add(Type.APPOINTMENT, 5, "", 100);
    tombstone.add(Type.APPOINTMENT, 9, "", 103);
    tombstone.add(Type.MESSAGE, 2, "", 99);
    tombstone.add(Type.MESSAGE, 22, "", 105);
    tombstone.add(Type.MESSAGE, 24, "", 103);
    PagedDelete pgDelete = new PagedDelete(tombstone, true);
    pgDelete.removeBeforeCutoff(4, 101);
    Collection<Integer> ids = pgDelete.getAllIds();
    Assert.assertEquals(4, ids.size());
    pgDelete.trimDeletesTillPageLimit(3);
    ids = pgDelete.getAllIds();
    Assert.assertEquals(3, ids.size());
    Assert.assertTrue(ids.contains(4));
    Assert.assertTrue(ids.contains(9));
    Assert.assertTrue(ids.contains(24));
    Assert.assertTrue(pgDelete.isDeleteOverFlow());
    Assert.assertEquals(pgDelete.getCutOffModsequnce(), 105);
    Assert.assertEquals(pgDelete.getLastItemId(), 22);
    Multimap<Type, Integer> ids2Type = pgDelete.getTypedItemIds();
    Assert.assertEquals(3, ids2Type.size());
    Assert.assertTrue(ids2Type.containsEntry(Type.MESSAGE, 4));
    Assert.assertTrue(ids2Type.containsEntry(Type.APPOINTMENT, 9));
    Assert.assertTrue(ids2Type.containsEntry(Type.MESSAGE, 24));
}
Also used : Type(com.zimbra.cs.mailbox.MailItem.Type) PagedDelete(com.zimbra.cs.mailbox.util.PagedDelete) TypedIdList(com.zimbra.cs.mailbox.util.TypedIdList) Test(org.junit.Test)

Example 5 with Type

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

the class PagedDeleteTest method testTypedDeletesTillPageLimit.

@Test
public void testTypedDeletesTillPageLimit() {
    TypedIdList tombstone = new TypedIdList();
    tombstone.add(Type.MESSAGE, 3, "", 100);
    tombstone.add(Type.MESSAGE, 4, "", 101);
    tombstone.add(Type.APPOINTMENT, 1, "", 101);
    tombstone.add(Type.APPOINTMENT, 5, "", 100);
    tombstone.add(Type.MESSAGE, 9, "", 103);
    PagedDelete pgDelete = new PagedDelete(tombstone, true);
    pgDelete.trimDeletesTillPageLimit(3);
    Collection<Integer> ids = pgDelete.getAllIds();
    Assert.assertEquals(3, ids.size());
    Assert.assertTrue(ids.contains(3));
    Assert.assertTrue(ids.contains(5));
    Assert.assertTrue(ids.contains(1));
    Assert.assertTrue(pgDelete.isDeleteOverFlow());
    Assert.assertTrue(pgDelete.getCutOffModsequnce() == 101);
    Assert.assertTrue(pgDelete.getLastItemId() == 4);
    Multimap<Type, Integer> ids2Type = pgDelete.getTypedItemIds();
    Assert.assertEquals(3, ids2Type.size());
    Assert.assertTrue(ids2Type.containsEntry(Type.MESSAGE, 3));
    Assert.assertTrue(ids2Type.containsEntry(Type.APPOINTMENT, 5));
    Assert.assertTrue(ids2Type.containsEntry(Type.APPOINTMENT, 1));
}
Also used : Type(com.zimbra.cs.mailbox.MailItem.Type) PagedDelete(com.zimbra.cs.mailbox.util.PagedDelete) TypedIdList(com.zimbra.cs.mailbox.util.TypedIdList) Test(org.junit.Test)

Aggregations

Type (com.zimbra.cs.mailbox.MailItem.Type)7 MailItem (com.zimbra.cs.mailbox.MailItem)3 PagedDelete (com.zimbra.cs.mailbox.util.PagedDelete)3 TypedIdList (com.zimbra.cs.mailbox.util.TypedIdList)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 Test (org.junit.Test)3 ServiceException (com.zimbra.common.service.ServiceException)2 Folder (com.zimbra.cs.mailbox.Folder)2 Metadata (com.zimbra.cs.mailbox.Metadata)2 ZFolder (com.zimbra.client.ZFolder)1 ZMailbox (com.zimbra.client.ZMailbox)1 Color (com.zimbra.common.mailbox.Color)1 MemcachedMap (com.zimbra.common.util.memcached.MemcachedMap)1 DavException (com.zimbra.cs.dav.DavException)1 MailServiceException (com.zimbra.cs.mailbox.MailServiceException)1 Mailbox (com.zimbra.cs.mailbox.Mailbox)1 Message (com.zimbra.cs.mailbox.Message)1 Change (com.zimbra.cs.session.PendingModifications.Change)1 ModificationKey (com.zimbra.cs.session.PendingModifications.ModificationKey)1