001/*
002 * (C) Copyright 2016 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.io.StringWriter;
023
024import org.nuxeo.drive.adapter.FolderItem;
025import org.nuxeo.drive.adapter.ScrollFileSystemItemList;
026import org.nuxeo.drive.service.FileSystemItemManager;
027import org.nuxeo.ecm.automation.OperationContext;
028import org.nuxeo.ecm.automation.core.Constants;
029import org.nuxeo.ecm.automation.core.annotations.Context;
030import org.nuxeo.ecm.automation.core.annotations.Operation;
031import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
032import org.nuxeo.ecm.automation.core.annotations.Param;
033import org.nuxeo.ecm.core.api.Blob;
034import org.nuxeo.ecm.core.api.Blobs;
035import org.nuxeo.runtime.api.Framework;
036
037import com.fasterxml.jackson.core.JsonFactory;
038import com.fasterxml.jackson.core.JsonGenerator;
039import com.fasterxml.jackson.databind.ObjectMapper;
040
041/**
042 * Retrieves at most {@code batchSize} descendants of the {@link FolderItem} with the given {@code id} for the currently
043 * authenticated user and the given {@code scrollId}.
044 * <p>
045 * When passing a null {@code scrollId} the initial search request is executed and the first batch of results is
046 * returned along with a {@code scrollId} which should be passed to the next call in order to retrieve the next batch of
047 * results.
048 * <p>
049 * Ideally, the search context made available by the initial search request is kept alive during {@code keepAlive}
050 * milliseconds if {@code keepAlive} is positive.
051 * <p>
052 * Results are not necessarily sorted.
053 *
054 * @since 8.3
055 */
056@Operation(id = NuxeoDriveScrollDescendants.ID, category = Constants.CAT_SERVICES, label = "Nuxeo Drive: Scroll descendants")
057public class NuxeoDriveScrollDescendants {
058
059    public static final String ID = "NuxeoDrive.ScrollDescendants";
060
061    @Context
062    protected OperationContext ctx;
063
064    @Param(name = "id")
065    protected String id;
066
067    @Param(name = "scrollId", required = false)
068    protected String scrollId;
069
070    @Param(name = "batchSize")
071    protected int batchSize;
072
073    @Param(name = "keepAlive", required = false)
074    protected long keepAlive = 60000; // 1 minute
075
076    @OperationMethod
077    public Blob run() throws IOException {
078
079        FileSystemItemManager fileSystemItemManager = Framework.getService(FileSystemItemManager.class);
080        ScrollFileSystemItemList descendants = fileSystemItemManager.scrollDescendants(id, ctx.getPrincipal(),
081                scrollId, batchSize, keepAlive);
082        return writeJSONBlob(descendants);
083    }
084
085    protected Blob writeJSONBlob(ScrollFileSystemItemList scrollFSIList) throws IOException {
086        StringWriter writer = new StringWriter();
087        JsonFactory factory = new JsonFactory();
088        try (JsonGenerator jg = factory.createGenerator(writer)) {
089            jg.setCodec(new ObjectMapper());
090            jg.writeStartObject();
091            jg.writeStringField("scrollId", scrollFSIList.getScrollId());
092            jg.writeObjectField("fileSystemItems", scrollFSIList);
093            jg.writeEndObject();
094        }
095        return Blobs.createJSONBlob(writer.toString());
096    }
097
098}