/*
 * Copyright (c) 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     bstefanescu
 */
package org.nuxeo.ecm.automation.core.operations.execution;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import org.nuxeo.ecm.automation.AutomationService;
import org.nuxeo.ecm.automation.OperationContext;
import org.nuxeo.ecm.automation.core.Constants;
import org.nuxeo.ecm.automation.core.annotations.Context;
import org.nuxeo.ecm.automation.core.annotations.Operation;
import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
import org.nuxeo.ecm.automation.core.annotations.Param;

/**
 * Run an embedded operation chain using the current input. The output is
 * undefined (Void)
 *
 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
 * @since 5.5
 */
@Operation(id = RunOperationOnList.ID, category = Constants.CAT_SUBCHAIN_EXECUTION, label = "Run For Each", description = "Run an operation for each element from the list defined by the 'list' paramter. The 'list' parameter is pointing to context variable that represent the list which will be iterated. The 'item' parameter represent the name of the context varible which will point to the current element in the list at each iteration. You can use the 'isolate' parameter to specify whether or not the evalution context is the same as the parent context or a copy of it. If the isolate is 'true' then a copy of the current contetx is used and so that modifications in this context will not affect the parent context. Any input is accepted. The input is returned back as output when operation terminate.")
public class RunOperationOnList {

    public static final String ID = "Context.RunOperationOnList";

    @Context
    protected OperationContext ctx;

    @Context
    protected AutomationService service;

    @Param(name = "id")
    protected String chainId;

    @Param(name = "list")
    protected String listName;

    @Param(name = "item", required = false, values = "item")
    protected String itemName = "item";

    @Param(name = "isolate", required = false, values = "true")
    protected boolean isolate = true;

    @OperationMethod
    public void run() throws Exception {
        Map<String, Object> vars = isolate ? new HashMap<String, Object>(
                ctx.getVars()) : ctx.getVars();
        OperationContext subctx = new OperationContext(ctx.getCoreSession(),
                vars);
        subctx.setInput(ctx.getInput());
        for (Object value : (Collection<?>) ctx.get(listName)) {
            subctx.put(itemName, value);
            service.run(subctx, chainId);
        }
    }

}

@@ -417,16 +417,41 @@
 ution;%0A%0A
+import java.util.Arrays;%0A
 import j
@@ -2749,24 +2749,25 @@
 put());%0A
+%0A
         
 for (Obj
@@ -2762,61 +2762,460 @@
     
-for (Object value : (Collection%3C?%3E) ctx.get(listName)
+Collection%3C?%3E list = null;%0A        if (ctx.get(listName) instanceof Object%5B%5D) %7B%0A            list = Arrays.asList((Object%5B%5D) ctx.get(listName));%0A        %7D else if (ctx.get(listName) instanceof Collection%3C?%3E) %7B%0A            list = (Collection%3C?%3E) ctx.get(listName);%0A        %7D else %7B%0A            throw new UnsupportedOperationException(%0A                    ctx.get(listName).getClass() + %22 is not a Collection%22);%0A        %7D%0A%0A        for (Object value : list
 ) %7B%0A

support arrays for RunOperationOnList operation