Search in sources :

Example 1 with SynchReportItem

use of org.bedework.calsvci.SynchReportItem in project bw-calendar-engine by Bedework.

the class CalSvc method getSynchReport.

/* (non-Javadoc)
   * @see org.bedework.calsvci.CalSvcI#getSynchReport(java.lang.String, java.lang.String, int, boolean)
   */
@Override
public SynchReport getSynchReport(final String path, final String token, final int limit, final boolean recurse) throws CalFacadeException {
    final BwCalendar col = getCalendarsHandler().get(path);
    if (col == null) {
        return null;
    }
    Set<SynchReportItem> items = new TreeSet<SynchReportItem>();
    String resToken = getSynchItems(col, path, token, items, recurse);
    final SynchReport res = new SynchReport(items, resToken);
    if ((limit > 0) && (res.size() >= limit)) {
        if (res.size() == limit) {
            return res;
        }
        items = new TreeSet<>();
        resToken = "";
        for (final SynchReportItem item : res.getItems()) {
            if (item.getToken().compareTo(resToken) > 0) {
                resToken = item.getToken();
            }
            items.add(item);
            if (items.size() == limit) {
                break;
            }
        }
    }
    if (resToken.length() == 0) {
        resToken = Util.icalUTCTimestamp() + "-0000";
    }
    return new SynchReport(items, resToken);
}
Also used : SynchReport(org.bedework.calsvci.SynchReport) TreeSet(java.util.TreeSet) BwCalendar(org.bedework.calfacade.BwCalendar) BwString(org.bedework.calfacade.BwString) SynchReportItem(org.bedework.calsvci.SynchReportItem)

Example 2 with SynchReportItem

use of org.bedework.calsvci.SynchReportItem in project bw-calendar-engine by Bedework.

the class BwSysIntfImpl method getSyncReport.

@Override
public SynchReportData getSyncReport(final String path, final String token, final int limit, final boolean recurse) throws WebdavException {
    try {
        String syncToken = null;
        if (token != null) {
            if (!token.startsWith("data:,")) {
                throw new WebdavForbidden(WebdavTags.validSyncToken, token);
            }
            syncToken = token.substring(6);
        }
        if ((syncToken != null) && (syncToken.length() == 16)) {
            // Force a full reload
            syncToken = null;
        }
        final SynchReport sr = getSvci().getSynchReport(path, syncToken, limit, recurse);
        if (sr == null) {
            return null;
        }
        final SynchReportData srd = new SynchReportData();
        srd.items = new ArrayList<>();
        srd.token = "data:," + sr.getToken();
        srd.truncated = sr.getTruncated();
        for (final SynchReportItem sri : sr.getItems()) {
            final SynchReportDataItem srdi;
            if (sri.getEvent() != null) {
                srdi = new SynchReportDataItem(sri.getVpath(), new BwCalDAVEvent(this, sri.getEvent()), sri.getToken());
            } else if (sri.getResource() != null) {
                srdi = new SynchReportDataItem(sri.getVpath(), new BwCalDAVResource(this, sri.getResource()), sri.getToken());
            } else if (sri.getCol() != null) {
                srdi = new SynchReportDataItem(sri.getVpath(), new BwCalDAVCollection(this, sri.getCol()), sri.getToken(), sri.getCanSync());
            } else {
                throw new RuntimeException("Unhandled sync report item type");
            }
            srd.items.add(srdi);
        }
        return srd;
    } catch (final CalFacadeAccessException cfae) {
        throw new WebdavForbidden();
    } catch (final CalFacadeInvalidSynctoken cist) {
        throw new WebdavBadRequest(WebdavTags.validSyncToken);
    } catch (final CalFacadeException cfe) {
        throw new WebdavException(cfe);
    } catch (final WebdavException we) {
        throw we;
    } catch (final Throwable t) {
        throw new WebdavException(t);
    }
}
Also used : WebdavBadRequest(org.bedework.webdav.servlet.shared.WebdavBadRequest) WebdavForbidden(org.bedework.webdav.servlet.shared.WebdavForbidden) WebdavException(org.bedework.webdav.servlet.shared.WebdavException) CalFacadeAccessException(org.bedework.calfacade.exc.CalFacadeAccessException) CalFacadeException(org.bedework.calfacade.exc.CalFacadeException) SynchReport(org.bedework.calsvci.SynchReport) SynchReportDataItem(org.bedework.caldav.server.sysinterface.SysIntf.SynchReportData.SynchReportDataItem) CalFacadeInvalidSynctoken(org.bedework.calfacade.exc.CalFacadeInvalidSynctoken) SynchReportItem(org.bedework.calsvci.SynchReportItem)

Example 3 with SynchReportItem

use of org.bedework.calsvci.SynchReportItem in project bw-calendar-engine by Bedework.

the class CalSvc method getSynchItems.

private String getSynchItems(final BwCalendar col, final String vpath, final String token, final Set<SynchReportItem> items, final boolean recurse) throws CalFacadeException {
    final Events eventsH = (Events) getEventsHandler();
    final ResourcesImpl resourcesH = (ResourcesImpl) getResourcesHandler();
    final Calendars colsH = (Calendars) getCalendarsHandler();
    String newToken = "";
    BwCalendar resolvedCol = col;
    if (debug) {
        trace("sync token: " + token + " col: " + resolvedCol.getPath());
    }
    if (col.getTombstoned()) {
        return token;
    }
    if (col.getInternalAlias()) {
        resolvedCol = getCalendarsHandler().resolveAlias(col, true, false);
    }
    if (resolvedCol.getTombstoned()) {
        return token;
    }
    /* Each collection could be:
     *    a. A calendar collection or special - like Inbox -
     *           only need to look for events.
     *    b. Other collections. Need to look for events, resources and collections.
     */
    final boolean eventsOnly = resolvedCol.getCollectionInfo().onlyCalEntities;
    final Set<EventInfo> evs = eventsH.getSynchEvents(resolvedCol.getPath(), token);
    for (final EventInfo ei : evs) {
        if (!ei.getEvent().getTombstoned() && !eventsH.isVisible(col, ei.getEvent().getName())) {
            continue;
        }
        final SynchReportItem sri = new SynchReportItem(vpath, ei);
        items.add(sri);
        if (sri.getToken().compareTo(newToken) > 0) {
            newToken = sri.getToken();
        }
    }
    if (!eventsOnly) {
        // Look for resources
        final List<BwResource> ress = resourcesH.getSynchResources(resolvedCol.getPath(), token);
        for (final BwResource r : ress) {
            final SynchReportItem sri = new SynchReportItem(vpath, r);
            items.add(sri);
            if (sri.getToken().compareTo(newToken) > 0) {
                newToken = sri.getToken();
            }
        }
    }
    final Set<SynchReportItem> colItems = new TreeSet<>();
    final Set<BwCalendar> cols = colsH.getSynchCols(resolvedCol.getPath(), token);
    final List<BwCalendar> aliases = new ArrayList<>();
    for (final BwCalendar c : cols) {
        final int calType = c.getCalType();
        if (calType == BwCalendar.calTypePendingInbox) {
            continue;
        }
        if ((token != null) && (calType == BwCalendar.calTypeAlias)) {
            aliases.add(c);
            continue;
        }
        final SynchReportItem sri = new SynchReportItem(vpath, c, canSync(c));
        colItems.add(sri);
        items.add(sri);
        if (sri.getToken().compareTo(newToken) > 0) {
            newToken = sri.getToken();
        }
        if (debug) {
            trace("     token=" + sri.getToken() + " for " + c.getPath());
        }
    }
    if (!Util.isEmpty(aliases)) {
        /* Resolve each one and see if the target is a candidate
       */
        for (final BwCalendar c : aliases) {
            final BwCalendar resolved = getCalendarsHandler().resolveAlias(c, true, false);
            if (resolved == null) {
                continue;
            }
            if (c.getTombstoned() && !getCal().testSynchCol(c, token)) {
                continue;
            }
            if (!getCal().testSynchCol(resolved, token)) {
                continue;
            }
            final SynchReportItem sri = new SynchReportItem(vpath, c, canSync(c), resolved.getLastmod().getTagValue());
            colItems.add(sri);
            items.add(sri);
            if (sri.getToken().compareTo(newToken) > 0) {
                newToken = sri.getToken();
            }
        }
    }
    if (!recurse) {
        return newToken;
    }
    if (Util.isEmpty(colItems)) {
        return newToken;
    }
    for (final SynchReportItem sri : colItems) {
        if (!sri.getCanSync()) {
            continue;
        }
        final BwCalendar sricol = sri.getCol();
        final String t = getSynchItems(sricol, Util.buildPath(true, vpath, "/", sricol.getName()), token, items, true);
        if (t.compareTo(newToken) > 0) {
            newToken = t;
        }
    }
    return newToken;
}
Also used : EventInfo(org.bedework.calfacade.svc.EventInfo) BwResource(org.bedework.calfacade.BwResource) ArrayList(java.util.ArrayList) BwString(org.bedework.calfacade.BwString) BwCalendar(org.bedework.calfacade.BwCalendar) TreeSet(java.util.TreeSet) SynchReportItem(org.bedework.calsvci.SynchReportItem)

Aggregations

SynchReportItem (org.bedework.calsvci.SynchReportItem)3 TreeSet (java.util.TreeSet)2 BwCalendar (org.bedework.calfacade.BwCalendar)2 BwString (org.bedework.calfacade.BwString)2 SynchReport (org.bedework.calsvci.SynchReport)2 ArrayList (java.util.ArrayList)1 SynchReportDataItem (org.bedework.caldav.server.sysinterface.SysIntf.SynchReportData.SynchReportDataItem)1 BwResource (org.bedework.calfacade.BwResource)1 CalFacadeAccessException (org.bedework.calfacade.exc.CalFacadeAccessException)1 CalFacadeException (org.bedework.calfacade.exc.CalFacadeException)1 CalFacadeInvalidSynctoken (org.bedework.calfacade.exc.CalFacadeInvalidSynctoken)1 EventInfo (org.bedework.calfacade.svc.EventInfo)1 WebdavBadRequest (org.bedework.webdav.servlet.shared.WebdavBadRequest)1 WebdavException (org.bedework.webdav.servlet.shared.WebdavException)1 WebdavForbidden (org.bedework.webdav.servlet.shared.WebdavForbidden)1