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 java.util.HashMap; 022import java.util.Map; 023 024import org.nuxeo.ecm.automation.AutomationService; 025import org.nuxeo.ecm.automation.InvalidChainException; 026import org.nuxeo.ecm.automation.OperationContext; 027import org.nuxeo.ecm.automation.OperationException; 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.automation.jaxrs.io.documents.PaginableDocumentModelListImpl; 034import org.nuxeo.ecm.core.api.DocumentModel; 035import org.nuxeo.ecm.platform.query.api.PageProvider; 036 037/** 038 * Run an embedded operation chain for each page of the pageProvider given as input. The output is undefined (Void) 039 * 040 * @since 5.6 041 */ 042@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 = { "Context.RunOperationOnProvider" }) 043public class RunOperationOnProvider { 044 public static final String ID = "RunOperationOnProvider"; 045 046 @Context 047 protected OperationContext ctx; 048 049 @Context 050 protected AutomationService service; 051 052 @Param(name = "id") 053 protected String chainId; 054 055 @Param(name = "isolate", required = false, values = { "true" }) 056 protected boolean isolate = true; 057 058 @OperationMethod 059 public void run(PaginableDocumentModelListImpl paginableList) 060 061 throws InvalidChainException, OperationException, Exception { 062 PageProvider<DocumentModel> pageProvider = paginableList.getProvider(); 063 Map<String, Object> vars = isolate ? new HashMap<String, Object>(ctx.getVars()) : ctx.getVars(); 064 OperationContext subctx = new OperationContext(ctx.getCoreSession(), vars); 065 final long initialRC = pageProvider.getResultsCount(); 066 final long initialNoPages = pageProvider.getNumberOfPages(); 067 068 PaginableDocumentModelListImpl input = new PaginableDocumentModelListImpl(pageProvider); 069 while (pageProvider.getCurrentPageIndex() < initialNoPages) { 070 subctx.setInput(input); 071 service.run(subctx, chainId); 072 if (!pageProvider.isNextPageAvailable()) { 073 break; 074 } 075 // check if the chain run is "consuming" docs returned by the 076 // pageProvider or not 077 pageProvider.refresh(); 078 input = new PaginableDocumentModelListImpl(pageProvider); 079 080 if (pageProvider.getResultsCount() == initialRC) { 081 pageProvider.nextPage(); 082 input = new PaginableDocumentModelListImpl(pageProvider); 083 } 084 } 085 086 } 087}