001/*
002 * (C) Copyright 2012 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Antoine Taillefer <ataillefer@nuxeo.com>
016 */
017package org.nuxeo.drive.operations;
018
019import java.io.IOException;
020import java.util.Map;
021import java.util.Set;
022
023import org.nuxeo.drive.service.FileSystemChangeSummary;
024import org.nuxeo.drive.service.NuxeoDriveManager;
025import org.nuxeo.drive.service.impl.RootDefinitionsHelper;
026import org.nuxeo.ecm.automation.OperationContext;
027import org.nuxeo.ecm.automation.core.Constants;
028import org.nuxeo.ecm.automation.core.annotations.Context;
029import org.nuxeo.ecm.automation.core.annotations.Operation;
030import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
031import org.nuxeo.ecm.automation.core.annotations.Param;
032import org.nuxeo.ecm.core.api.Blob;
033import org.nuxeo.ecm.core.api.IdRef;
034import org.nuxeo.runtime.api.Framework;
035
036/**
037 * Gets a summary of document changes in the synchronization roots of the currently authenticated user.
038 *
039 * @author Antoine Taillefer
040 */
041@Operation(id = NuxeoDriveGetChangeSummary.ID, category = Constants.CAT_SERVICES, label = "Nuxeo Drive: Get change summary")
042public class NuxeoDriveGetChangeSummary {
043
044    public static final String ID = "NuxeoDrive.GetChangeSummary";
045
046    @Context
047    protected OperationContext ctx;
048
049    @Param(name = "lastSyncDate", required = false)
050    protected Long lastSyncDate = -1L;
051
052    @Param(name = "lowerBound", required = false)
053    protected Long lowerBound = -1L;
054
055    // Expect a String structure with form:
056    // repo-1:root-ref-1,repo-1:root-ref-2,repo-2:root-ref-3
057    @Param(name = "lastSyncActiveRootDefinitions", required = false)
058    protected String lastSyncActiveRootDefinitions;
059
060    @OperationMethod
061    public Blob run() throws IOException {
062        NuxeoDriveManager driveManager = Framework.getLocalService(NuxeoDriveManager.class);
063        Map<String, Set<IdRef>> lastActiveRootRefs = RootDefinitionsHelper.parseRootDefinitions(lastSyncActiveRootDefinitions);
064        FileSystemChangeSummary docChangeSummary;
065        if (lastSyncDate >= 0) {
066            docChangeSummary = driveManager.getChangeSummary(ctx.getPrincipal(), lastActiveRootRefs, lastSyncDate);
067        } else {
068            docChangeSummary = driveManager.getChangeSummaryIntegerBounds(ctx.getPrincipal(), lastActiveRootRefs,
069                    lowerBound);
070        }
071        return NuxeoDriveOperationHelper.asJSONBlob(docChangeSummary);
072    }
073
074}