001/*
002 * (C) Copyright 2012 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Antoine Taillefer <ataillefer@nuxeo.com>
018 */
019package org.nuxeo.drive.operations;
020
021import java.io.IOException;
022import java.util.Map;
023import java.util.Set;
024
025import org.nuxeo.drive.service.FileSystemChangeSummary;
026import org.nuxeo.drive.service.NuxeoDriveManager;
027import org.nuxeo.drive.service.impl.RootDefinitionsHelper;
028import org.nuxeo.ecm.automation.OperationContext;
029import org.nuxeo.ecm.automation.core.Constants;
030import org.nuxeo.ecm.automation.core.annotations.Context;
031import org.nuxeo.ecm.automation.core.annotations.Operation;
032import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
033import org.nuxeo.ecm.automation.core.annotations.Param;
034import org.nuxeo.ecm.core.api.Blob;
035import org.nuxeo.ecm.core.api.IdRef;
036import org.nuxeo.runtime.api.Framework;
037
038/**
039 * Gets a summary of document changes in the synchronization roots of the currently authenticated user.
040 *
041 * @author Antoine Taillefer
042 */
043@Operation(id = NuxeoDriveGetChangeSummary.ID, category = Constants.CAT_SERVICES, label = "Nuxeo Drive: Get change summary")
044public class NuxeoDriveGetChangeSummary {
045
046    public static final String ID = "NuxeoDrive.GetChangeSummary";
047
048    @Context
049    protected OperationContext ctx;
050
051    @Param(name = "lastSyncDate", required = false)
052    protected Long lastSyncDate = -1L;
053
054    @Param(name = "lowerBound", required = false)
055    protected Long lowerBound = -1L;
056
057    // Expect a String structure with form:
058    // repo-1:root-ref-1,repo-1:root-ref-2,repo-2:root-ref-3
059    @Param(name = "lastSyncActiveRootDefinitions", required = false)
060    protected String lastSyncActiveRootDefinitions;
061
062    @OperationMethod
063    public Blob run() throws IOException {
064        NuxeoDriveManager driveManager = Framework.getLocalService(NuxeoDriveManager.class);
065        Map<String, Set<IdRef>> lastActiveRootRefs = RootDefinitionsHelper.parseRootDefinitions(lastSyncActiveRootDefinitions);
066        FileSystemChangeSummary docChangeSummary;
067        if (lastSyncDate >= 0) {
068            docChangeSummary = driveManager.getChangeSummary(ctx.getPrincipal(), lastActiveRootRefs, lastSyncDate);
069        } else {
070            docChangeSummary = driveManager.getChangeSummaryIntegerBounds(ctx.getPrincipal(), lastActiveRootRefs,
071                    lowerBound);
072        }
073        return NuxeoDriveOperationHelper.asJSONBlob(docChangeSummary);
074    }
075
076}