Search in sources :

Example 26 with TypedIdList

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

the class CalendarUtils method migrateAppointmentsAndTasks.

/**
 * Move appointments from TASKS type folders to Calendar folder.
 * Also, move tasks from APPOINTMENT type folders to Tasks folder.
 * @param mbox
 * @throws ServiceException
 */
public static void migrateAppointmentsAndTasks(Mailbox mbox) throws ServiceException {
    // get the list of folders.
    List<Folder> folderList = mbox.getFolderList(null, SortBy.NONE);
    for (Folder folder : folderList) {
        int targetId;
        TypedIdList idlist;
        MailItem.Type type;
        if (folder.getDefaultView() == MailItem.Type.APPOINTMENT) {
            // get tasks from this folder and move them to TASKS folder.
            idlist = mbox.listCalendarItemsForRange(null, MailItem.Type.TASK, -1, -1, folder.getId());
            targetId = Mailbox.ID_FOLDER_TASKS;
            type = MailItem.Type.TASK;
        } else if (folder.getDefaultView() == MailItem.Type.TASK) {
            // get appointments from this folder and move them to Calendar folder.
            idlist = mbox.listCalendarItemsForRange(null, MailItem.Type.APPOINTMENT, -1, -1, folder.getId());
            targetId = Mailbox.ID_FOLDER_CALENDAR;
            type = MailItem.Type.APPOINTMENT;
        } else {
            continue;
        }
        if (!idlist.isEmpty()) {
            if (type == MailItem.Type.APPOINTMENT)
                ZimbraLog.calendar.info("Migrating " + idlist.size() + " Appointment(s) from '" + folder.getName() + "' to 'Calendar' folder for mailbox " + mbox.getId());
            else
                ZimbraLog.calendar.info("Migrating " + idlist.size() + " Task(s) from '" + folder.getName() + "' to 'Tasks' folder for mailbox " + mbox.getId());
            int[] items = new int[idlist.size()];
            int i = 0;
            for (Integer id : idlist.getAllIds()) {
                items[i] = id.intValue();
                i++;
            }
            mbox.move(null, items, type, targetId, null);
        }
    }
}
Also used : MailItem(com.zimbra.cs.mailbox.MailItem) Folder(com.zimbra.cs.mailbox.Folder) TypedIdList(com.zimbra.cs.mailbox.util.TypedIdList)

Example 27 with TypedIdList

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

the class SearchActionTest method testSearchActionMove.

@Test
public void testSearchActionMove() throws Exception {
    Account acct = Provisioning.getInstance().getAccountByName("testMove@zimbra.com");
    Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(acct);
    // Add two messages to inbox, one with search match and other with no match
    DeliveryOptions dopt = new DeliveryOptions().setFolderId(Mailbox.ID_FOLDER_INBOX).setFlags(Flag.BITMASK_UNREAD | Flag.BITMASK_MUTED);
    mbox.addMessage(null, MailboxTestUtil.generateMessage("test subject"), dopt, null);
    mbox.addMessage(null, MailboxTestUtil.generateMessage("unmatched subject"), dopt, null);
    TypedIdList ids = mbox.getItemIds(null, 2);
    Assert.assertEquals(2, ids.size());
    SearchRequest sRequest = new SearchRequest();
    sRequest.setSearchTypes("conversation");
    // search with query 'test'
    sRequest.setQuery("test");
    BulkAction bAction = new BulkAction();
    // search action - move search result to 'Trash'
    bAction.setOp(BulkAction.Operation.move);
    bAction.setFolder("Trash");
    Map<String, Object> context = ServiceTestUtil.getRequestContext(acct);
    ZimbraSoapContext zsc = (ZimbraSoapContext) context.get(SoapEngine.ZIMBRA_CONTEXT);
    Element searchResponse = new Search().handle(zsc.jaxbToElement(sRequest), ServiceTestUtil.getRequestContext(acct));
    com.zimbra.soap.mail.message.SearchResponse sResponse = zsc.elementToJaxb(searchResponse);
    List<SearchHit> searchHits = sResponse.getSearchHits();
    SearchAction.performAction(bAction, sRequest, searchHits, mbox, null);
    // check inbox contains only 1 unmatched mail item after move
    List<MailItem> mailItems = mbox.getItemList(null, MailItem.Type.MESSAGE, 2, com.zimbra.cs.index.SortBy.DATE_DESC);
    Assert.assertEquals(1, mailItems.size());
    Assert.assertEquals("unmatched subject", mailItems.get(0).getSubject());
    // check trash contains mail item having 'test subject' after move
    mailItems = mbox.getItemList(null, MailItem.Type.MESSAGE, 3, com.zimbra.cs.index.SortBy.DATE_DESC);
    Assert.assertEquals(1, mailItems.size());
    Assert.assertEquals("test subject", mailItems.get(0).getSubject());
}
Also used : Account(com.zimbra.cs.account.Account) SearchRequest(com.zimbra.soap.mail.message.SearchRequest) SearchHit(com.zimbra.soap.type.SearchHit) Element(com.zimbra.common.soap.Element) BulkAction(com.zimbra.soap.mail.type.BulkAction) TypedIdList(com.zimbra.cs.mailbox.util.TypedIdList) MailItem(com.zimbra.cs.mailbox.MailItem) Mailbox(com.zimbra.cs.mailbox.Mailbox) ZimbraSoapContext(com.zimbra.soap.ZimbraSoapContext) DeliveryOptions(com.zimbra.cs.mailbox.DeliveryOptions) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 28 with TypedIdList

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

the class DbMailItem method listByFolder.

public static TypedIdList listByFolder(Folder folder, boolean descending) throws ServiceException {
    Mailbox mbox = folder.getMailbox();
    TypedIdList result = new TypedIdList();
    DbConnection conn = mbox.getOperationConnection();
    PreparedStatement stmt = null;
    ResultSet rs = null;
    try {
        stmt = conn.prepareStatement("SELECT id, type, uuid FROM " + getMailItemTableName(folder) + " WHERE " + IN_THIS_MAILBOX_AND + "folder_id = ?" + " ORDER BY date" + (descending ? " DESC" : ""));
        int pos = 1;
        pos = setMailboxId(stmt, mbox, pos);
        stmt.setInt(pos++, folder.getId());
        rs = stmt.executeQuery();
        while (rs.next()) {
            result.add(MailItem.Type.of(rs.getByte(2)), rs.getInt(1), rs.getString(3));
        }
        return result;
    } catch (SQLException e) {
        throw ServiceException.FAILURE("fetching item list for folder " + folder.getId(), e);
    } finally {
        DbPool.closeResults(rs);
        DbPool.closeStatement(stmt);
    }
}
Also used : Mailbox(com.zimbra.cs.mailbox.Mailbox) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) TypedIdList(com.zimbra.cs.mailbox.util.TypedIdList) DbConnection(com.zimbra.cs.db.DbPool.DbConnection)

Example 29 with TypedIdList

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

the class DbMailItem method readTombstones.

public static TypedIdList readTombstones(Mailbox mbox, long lastSync, boolean equalModSeq) throws ServiceException {
    TypedIdList tombstones = new TypedIdList();
    DbConnection conn = mbox.getOperationConnection();
    PreparedStatement stmt = null;
    ResultSet rs = null;
    try {
        if (equalModSeq) {
            stmt = conn.prepareStatement("SELECT type, ids, sequence FROM " + getTombstoneTableName(mbox) + " WHERE " + IN_THIS_MAILBOX_AND + "sequence >= ? AND ids IS NOT NULL" + " ORDER BY sequence");
        } else {
            stmt = conn.prepareStatement("SELECT type, ids, sequence FROM " + getTombstoneTableName(mbox) + " WHERE " + IN_THIS_MAILBOX_AND + "sequence > ? AND ids IS NOT NULL" + " ORDER BY sequence");
        }
        Db.getInstance().enableStreaming(stmt);
        int pos = 1;
        pos = setMailboxId(stmt, mbox, pos);
        stmt.setLong(pos++, lastSync);
        rs = stmt.executeQuery();
        while (rs.next()) {
            MailItem.Type type = MailItem.Type.of(rs.getByte(1));
            String row = rs.getString(2);
            int modSeq = rs.getInt(3);
            if (row == null || row.equals("")) {
                continue;
            }
            // the list of tombstones is comma-delimited
            for (String stone : row.split(",")) {
                try {
                    // a tombstone may either be ID or ID:UUID, so parse accordingly
                    int delimiter = stone.indexOf(':');
                    if (delimiter == -1) {
                        tombstones.add(type, Integer.parseInt(stone), null, modSeq);
                    } else {
                        tombstones.add(type, Integer.parseInt(stone.substring(0, delimiter)), Strings.emptyToNull(stone.substring(delimiter + 1)), modSeq);
                    }
                } catch (NumberFormatException nfe) {
                    ZimbraLog.sync.warn("unparseable TOMBSTONE entry: " + stone);
                }
            }
        }
        return tombstones;
    } catch (SQLException e) {
        throw ServiceException.FAILURE("reading tombstones since change: " + lastSync, e);
    } finally {
        DbPool.closeResults(rs);
        DbPool.closeStatement(stmt);
    }
}
Also used : MailItem(com.zimbra.cs.mailbox.MailItem) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) TypedIdList(com.zimbra.cs.mailbox.util.TypedIdList) DbConnection(com.zimbra.cs.db.DbPool.DbConnection)

Example 30 with TypedIdList

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

the class DbMailItem method listItems.

public static TypedIdList listItems(Folder folder, long messageSyncStart, MailItem.Type type, boolean descending, boolean older) throws ServiceException {
    Mailbox mbox = folder.getMailbox();
    assert Db.supports(Db.Capability.ROW_LEVEL_LOCKING) || Thread.holdsLock(mbox);
    TypedIdList result = new TypedIdList();
    DbConnection conn = mbox.getOperationConnection();
    PreparedStatement stmt = null;
    ResultSet rs = null;
    try {
        if (older) {
            stmt = conn.prepareStatement("SELECT id, type, uuid FROM " + getMailItemTableName(folder) + " WHERE " + IN_THIS_MAILBOX_AND + " type = ? AND folder_id = ? AND date < ?" + " ORDER BY date" + (descending ? " DESC" : ""));
        } else {
            stmt = conn.prepareStatement("SELECT id, type, uuid FROM " + getMailItemTableName(folder) + " WHERE " + IN_THIS_MAILBOX_AND + " type = ? AND folder_id = ? AND date >= ?" + " ORDER BY date" + (descending ? " DESC" : ""));
        }
        int pos = 1;
        pos = setMailboxId(stmt, mbox, pos);
        stmt.setByte(pos++, type.toByte());
        stmt.setInt(pos++, folder.getId());
        stmt.setLong(pos++, messageSyncStart);
        rs = stmt.executeQuery();
        while (rs.next()) {
            MailItem.Type dataType = MailItem.Type.of(rs.getByte(2));
            result.add(dataType, rs.getInt(1), rs.getString(3));
        }
        return result;
    } catch (SQLException e) {
        throw ServiceException.FAILURE("fetching item list for folder " + folder.getId(), e);
    } finally {
        DbPool.closeResults(rs);
        DbPool.closeStatement(stmt);
    }
}
Also used : MailItem(com.zimbra.cs.mailbox.MailItem) Mailbox(com.zimbra.cs.mailbox.Mailbox) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) TypedIdList(com.zimbra.cs.mailbox.util.TypedIdList) DbConnection(com.zimbra.cs.db.DbPool.DbConnection)

Aggregations

TypedIdList (com.zimbra.cs.mailbox.util.TypedIdList)33 MailItem (com.zimbra.cs.mailbox.MailItem)10 Test (org.junit.Test)8 DbConnection (com.zimbra.cs.db.DbPool.DbConnection)6 Folder (com.zimbra.cs.mailbox.Folder)6 MailServiceException (com.zimbra.cs.mailbox.MailServiceException)6 Mailbox (com.zimbra.cs.mailbox.Mailbox)6 ResultSet (java.sql.ResultSet)6 ServiceException (com.zimbra.common.service.ServiceException)5 PreparedStatement (java.sql.PreparedStatement)5 SQLException (java.sql.SQLException)5 List (java.util.List)5 ZFolder (com.zimbra.client.ZFolder)4 Element (com.zimbra.common.soap.Element)4 PagedDelete (com.zimbra.cs.mailbox.util.PagedDelete)4 ArrayList (java.util.ArrayList)4 Pair (com.zimbra.common.util.Pair)3 Account (com.zimbra.cs.account.Account)3 CustomMetadata (com.zimbra.cs.mailbox.MailItem.CustomMetadata)3 Type (com.zimbra.cs.mailbox.MailItem.Type)3