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.Blobs;
036import org.nuxeo.ecm.core.api.IdRef;
037import org.nuxeo.runtime.api.Framework;
038
039/**
040 * Gets a summary of document changes in the synchronization roots of the currently authenticated user.
041 *
042 * @author Antoine Taillefer
043 */
044@Operation(id = NuxeoDriveGetChangeSummary.ID, category = Constants.CAT_SERVICES, label = "Nuxeo Drive: Get change summary")
045public class NuxeoDriveGetChangeSummary {
046
047    public static final String ID = "NuxeoDrive.GetChangeSummary";
048
049    @Context
050    protected OperationContext ctx;
051
052    @Param(name = "lastSyncDate", required = false)
053    protected Long lastSyncDate = -1L;
054
055    @Param(name = "lowerBound", required = false)
056    protected Long lowerBound = -1L;
057
058    // Expect a String structure with form:
059    // repo-1:root-ref-1,repo-1:root-ref-2,repo-2:root-ref-3
060    @Param(name = "lastSyncActiveRootDefinitions", required = false)
061    protected String lastSyncActiveRootDefinitions;
062
063    @OperationMethod
064    public Blob run() throws IOException {
065        NuxeoDriveManager driveManager = Framework.getLocalService(NuxeoDriveManager.class);
066        Map<String, Set<IdRef>> lastActiveRootRefs = RootDefinitionsHelper.parseRootDefinitions(lastSyncActiveRootDefinitions);
067        FileSystemChangeSummary docChangeSummary;
068        if (lastSyncDate >= 0) {
069            docChangeSummary = driveManager.getChangeSummary(ctx.getPrincipal(), lastActiveRootRefs, lastSyncDate);
070        } else {
071            docChangeSummary = driveManager.getChangeSummaryIntegerBounds(ctx.getPrincipal(), lastActiveRootRefs,
072                    lowerBound);
073        }
074        return Blobs.createJSONBlobFromValueJackson1(docChangeSummary);
075    }
076
077}