001/*
002 * (C) Copyright 2006-2011 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 *    Mariana Cedica
018 */
019package org.nuxeo.ecm.automation.core.operations.services;
020
021import org.nuxeo.ecm.automation.AutomationService;
022import org.nuxeo.ecm.automation.OperationContext;
023import org.nuxeo.ecm.automation.OperationException;
024import org.nuxeo.ecm.automation.core.Constants;
025import org.nuxeo.ecm.automation.core.annotations.Context;
026import org.nuxeo.ecm.automation.core.annotations.Operation;
027import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
028import org.nuxeo.ecm.automation.core.annotations.Param;
029import org.nuxeo.ecm.automation.jaxrs.io.documents.PaginableDocumentModelListImpl;
030import org.nuxeo.ecm.core.api.DocumentModel;
031import org.nuxeo.ecm.platform.query.api.PageProvider;
032
033/**
034 * Run an embedded operation chain for each page of the pageProvider given as input. The output is undefined (Void)
035 *
036 * @since 5.6
037 */
038@Operation(id = RunOperationOnProvider.ID, category = Constants.CAT_SUBCHAIN_EXECUTION, label = "Run For Each Page", description = "Run an operation for each page of the provider defined by the provider name, the operation input is the curent page ", aliases = {
039        "Context.RunOperationOnProvider" })
040public class RunOperationOnProvider {
041    public static final String ID = "RunOperationOnProvider";
042
043    @Context
044    protected OperationContext ctx;
045
046    @Context
047    protected AutomationService service;
048
049    @Param(name = "id")
050    protected String chainId;
051
052    @Param(name = "isolate", required = false, values = { "true" })
053    protected boolean isolate = true;
054
055    @OperationMethod
056    public void run(PaginableDocumentModelListImpl paginableList) throws OperationException {
057        PageProvider<DocumentModel> pageProvider = paginableList.getProvider();
058        try (OperationContext subctx = ctx.getSubContext(isolate)) {
059            long initialRC = pageProvider.getResultsCount();
060            long initialNoPages = pageProvider.getNumberOfPages();
061
062            PaginableDocumentModelListImpl input = new PaginableDocumentModelListImpl(pageProvider);
063            while (pageProvider.getCurrentPageIndex() < initialNoPages) {
064                subctx.setInput(input);
065                service.run(subctx, chainId);
066                if (!pageProvider.isNextPageAvailable()) {
067                    break;
068                }
069                // check if the chain run is "consuming" docs returned by the
070                // pageProvider or not
071                pageProvider.refresh();
072                input = new PaginableDocumentModelListImpl(pageProvider);
073
074                if (pageProvider.getResultsCount() == initialRC) {
075                    pageProvider.nextPage();
076                    input = new PaginableDocumentModelListImpl(pageProvider);
077                }
078            }
079        }
080
081    }
082}