Search in sources :

Example 1 with DavObject

use of com.zimbra.cs.dav.client.DavObject in project zm-mailbox by Zimbra.

the class CalDavDataImport method syncFolders.

private ArrayList<CalendarFolder> syncFolders() throws ServiceException, IOException, DavException, HttpException {
    ArrayList<CalendarFolder> ret = new ArrayList<CalendarFolder>();
    DataSource ds = getDataSource();
    OperationContext octxt = new OperationContext(mbox);
    HashMap<String, DataSourceItem> allFolders = getAllFolderMappings(ds);
    Folder rootFolder = null;
    try {
        rootFolder = mbox.getFolderById(octxt, getRootFolderId(ds));
    } catch (NoSuchItemException e) {
        // folder may be deleted. delete the datasource
        ZimbraLog.datasource.info("Folder %d was deleted.  Deleting data source %s.", getRootFolderId(ds), ds.getName());
        mbox.getAccount().deleteDataSource(ds.getId());
        // return empty array
        return new ArrayList<CalendarFolder>(0);
    }
    List<Integer> deleted = new ArrayList<Integer>();
    int lastSync = (int) rootFolder.getLastSyncDate();
    if (lastSync > 0) {
        for (int itemId : mbox.getTombstones(lastSync).getAllIds()) deleted.add(itemId);
    }
    CalDavClient client = getClient();
    Map<String, DavObject> calendars = client.getCalendars();
    for (String name : calendars.keySet()) {
        DavObject obj = calendars.get(name);
        String ctag = obj.getPropertyText(DavElements.E_GETCTAG);
        String url = obj.getHref();
        DataSourceItem f = allFolders.get(url);
        if (f == null)
            f = new DataSourceItem(0, 0, url, null);
        CalendarFolder cf = new CalendarFolder(f.itemId);
        Folder folder = null;
        if (f.itemId != 0) {
            // check if the folder was deleted
            if (deleted.contains(f.itemId)) {
                allFolders.remove(url);
                DbDataSource.deleteMapping(ds, f.itemId);
                DbDataSource.deleteAllMappingsInFolder(ds, f.itemId);
                deleteRemoteFolder(url);
                continue;
            }
            // check if the folder is valid
            try {
                folder = mbox.getFolderById(octxt, f.itemId);
            } catch (ServiceException se) {
                if (se.getCode() != MailServiceException.NO_SUCH_FOLDER) {
                    throw se;
                }
                f.itemId = 0;
            }
        }
        if (f.itemId == 0) {
            try {
                // check if we can use the folder
                folder = mbox.getFolderByName(octxt, rootFolder.getId(), name);
                if (folder.getDefaultView() != MailItem.Type.APPOINTMENT) {
                    name = name + " (" + getDataSource().getName() + ")";
                    folder = null;
                }
            } catch (MailServiceException.NoSuchItemException e) {
            }
            if (folder == null) {
                Folder.FolderOptions fopt = new Folder.FolderOptions();
                fopt.setDefaultView(MailItem.Type.APPOINTMENT).setFlags(DEFAULT_FOLDER_FLAGS).setColor(getDefaultColor());
                folder = mbox.createFolder(octxt, name, rootFolder.getId(), fopt);
            }
            f.itemId = folder.getId();
            f.folderId = folder.getFolderId();
            f.md = new Metadata();
            f.md.put(METADATA_KEY_TYPE, METADATA_TYPE_FOLDER);
            if (ctag != null) {
                f.md.put(METADATA_KEY_CTAG, ctag);
            }
            f.remoteId = url;
            cf.id = f.itemId;
            mbox.setSyncDate(octxt, folder.getId(), mbox.getLastChangeID());
            DbDataSource.addMapping(ds, f);
        } else if (f.md == null) {
            ZimbraLog.datasource.warn("syncFolders: empty metadata for item %d", f.itemId);
            f.folderId = folder.getFolderId();
            f.remoteId = url;
            f.md = new Metadata();
            f.md.put(METADATA_KEY_TYPE, METADATA_TYPE_FOLDER);
            if (ctag != null)
                f.md.put(METADATA_KEY_CTAG, ctag);
            DbDataSource.addMapping(ds, f);
        } else if (ctag != null) {
            String oldctag = f.md.get(METADATA_KEY_CTAG, null);
            if (ctag.equals(oldctag)) {
                cf.ctagMatched = true;
            } else {
                f.md.put(METADATA_KEY_CTAG, ctag);
                DbDataSource.updateMapping(ds, f);
            }
        }
        String fname = folder.getName();
        if (!fname.equals(name)) {
            ZimbraLog.datasource.warn("renaming folder %s to %s", fname, name);
            try {
                mbox.rename(octxt, f.itemId, MailItem.Type.FOLDER, name, folder.getFolderId());
            } catch (ServiceException e) {
                ZimbraLog.datasource.warn("folder rename failed", e);
            }
        }
        allFolders.remove(url);
        ret.add(cf);
    }
    if (!allFolders.isEmpty()) {
        // handle deleted folders
        ArrayList<Integer> fids = new ArrayList<Integer>();
        int[] fidArray = new int[allFolders.size()];
        int i = 0;
        for (DataSourceItem f : allFolders.values()) {
            Folder folder = mbox.getFolderById(octxt, f.itemId);
            if (folder != null && folder.getDefaultView() != MailItem.Type.APPOINTMENT && folder.getDefaultView() != MailItem.Type.TASK) {
                continue;
            }
            fids.add(f.itemId);
            fidArray[i++] = f.itemId;
            DbDataSource.deleteAllMappingsInFolder(ds, f.itemId);
        }
        if (!fids.isEmpty()) {
            DbDataSource.deleteMappings(ds, fids);
            try {
                mbox.delete(octxt, fidArray, MailItem.Type.FOLDER, null);
            } catch (ServiceException e) {
                ZimbraLog.datasource.warn("folder delete failed", e);
            }
        }
    }
    mbox.setSyncDate(octxt, rootFolder.getId(), mbox.getLastChangeID());
    return ret;
}
Also used : OperationContext(com.zimbra.cs.mailbox.OperationContext) ArrayList(java.util.ArrayList) Metadata(com.zimbra.cs.mailbox.Metadata) Folder(com.zimbra.cs.mailbox.Folder) NoSuchItemException(com.zimbra.cs.mailbox.MailServiceException.NoSuchItemException) CalDavClient(com.zimbra.cs.dav.client.CalDavClient) DbDataSource(com.zimbra.cs.db.DbDataSource) DataSource(com.zimbra.cs.account.DataSource) NoSuchItemException(com.zimbra.cs.mailbox.MailServiceException.NoSuchItemException) ServiceException(com.zimbra.common.service.ServiceException) MailServiceException(com.zimbra.cs.mailbox.MailServiceException) DataSourceItem(com.zimbra.cs.db.DbDataSource.DataSourceItem) MailServiceException(com.zimbra.cs.mailbox.MailServiceException) DavObject(com.zimbra.cs.dav.client.DavObject)

Aggregations

ServiceException (com.zimbra.common.service.ServiceException)1 DataSource (com.zimbra.cs.account.DataSource)1 CalDavClient (com.zimbra.cs.dav.client.CalDavClient)1 DavObject (com.zimbra.cs.dav.client.DavObject)1 DbDataSource (com.zimbra.cs.db.DbDataSource)1 DataSourceItem (com.zimbra.cs.db.DbDataSource.DataSourceItem)1 Folder (com.zimbra.cs.mailbox.Folder)1 MailServiceException (com.zimbra.cs.mailbox.MailServiceException)1 NoSuchItemException (com.zimbra.cs.mailbox.MailServiceException.NoSuchItemException)1 Metadata (com.zimbra.cs.mailbox.Metadata)1 OperationContext (com.zimbra.cs.mailbox.OperationContext)1 ArrayList (java.util.ArrayList)1