/*******************************************************************************
 * (C) Copyright 2016 Jerome Comte and Dorian Cransac
 *  
 * This file is part of STEP
 *  
 * STEP is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *  
 * STEP is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *  
 * You should have received a copy of the GNU Affero General Public License
 * along with STEP.  If not, see <http://www.gnu.org/licenses/>.
 *******************************************************************************/
package step.artefacts.handlers;

import step.core.artefacts.AbstractArtefact;
import step.core.artefacts.handlers.ArtefactHandler;
import step.core.artefacts.reports.ReportNode;
import step.core.artefacts.reports.ReportNodeStatus;
import step.core.execution.ExecutionContext;
import step.core.variables.UndefinedVariableException;

public class SequentialArtefactScheduler extends ArtefactHandler<AbstractArtefact, ReportNode>{
	
	public SequentialArtefactScheduler(ExecutionContext context) {
		super();
		init(context);
	}

	public void createReportSkeleton_(ReportNode node, AbstractArtefact testArtefact) {		
		for(AbstractArtefact child:ArtefactHandler.getChildren(testArtefact, context)) {
			ArtefactHandler.delegateCreateReportSkeleton(context, child, node);
		}
	}
	
	public void execute_(ReportNode node, AbstractArtefact testArtefact) {
		execute_(node, testArtefact, null);
	}
	
	public void execute_(ReportNode node, AbstractArtefact testArtefact, Boolean continueOnError) {
		ReportNodeStatus resultStatus = null; 
		try {			
			for(AbstractArtefact child:ArtefactHandler.getChildren(testArtefact, context)) {
				if(context.isInterrupted()) {
					break;
				}
				ReportNode resultNode = ArtefactHandler.delegateExecute(context, child, node);
				
				if(resultStatus==null || resultNode.getStatus().ordinal()<resultStatus.ordinal()) {
					resultStatus = resultNode.getStatus();
				}
				
				Boolean continueOnce = null;
				try {
					continueOnce = context.getVariablesManager().getVariableAsBoolean(ArtefactHandler.CONTINUE_EXECUTION_ONCE);
				} catch (UndefinedVariableException e) {} finally {
					context.getVariablesManager().removeVariable(node, ArtefactHandler.CONTINUE_EXECUTION_ONCE);												
				}
				
				if(resultNode.getStatus()==ReportNodeStatus.TECHNICAL_ERROR || resultNode.getStatus()==ReportNodeStatus.FAILED) {
					if(!context.isSimulation()) {
						if(continueOnce!=null) {
							if(!continueOnce) {
								break;
							}
						} else {
							if(continueOnError!=null) {
								if(!continueOnError) {
									break;
								}
							} else {
								if (!context.getVariablesManager().getVariableAsBoolean(ArtefactHandler.CONTINUE_EXECUTION, false)) {
									break;
								}								
							}
						}
					}
				}
			}
		} catch (Exception e) {
			failWithException(node, e);
		} finally {
			Object forcedStatus = context.getVariablesManager().getVariable("tec.forceparentstatus");
			if(forcedStatus!=null) {
				node.setStatus(ReportNodeStatus.valueOf(forcedStatus.toString()));
			} else {
				if(context.isInterrupted()) {
					node.setStatus(ReportNodeStatus.INTERRUPTED);
				} else {
					// Setting the result node status to PASSED when no children nodes have been executed
					if(resultStatus == null) {
						resultStatus = ReportNodeStatus.PASSED;
					}
					node.setStatus(resultStatus);						
				}
			}
		}
	}

	@Override
	public ReportNode createReportNode_(ReportNode parentNode, AbstractArtefact testArtefact) {
		return null;
	}

}

@@ -1949,17 +1949,23 @@
 eStatus 
-r
+parentR
 esultSta
@@ -1971,19 +1971,31 @@
 atus = n
-ull
+ode.getStatus()
 ; %0D%0A%09%09tr
@@ -2235,25 +2235,31 @@
 %09%09%09%0D%0A%09%09%09%09if(
-r
+parentR
 esultStatus=
@@ -2300,17 +2300,23 @@
 dinal()%3C
-r
+parentR
 esultSta
@@ -2334,25 +2334,31 @@
 ()) %7B%0D%0A%09%09%09%09%09
-r
+parentR
 esultStatus 
@@ -3686,204 +3686,30 @@
 %09%09%09%09
-// Setting the result node status to PASSED when no children nodes have been executed%0D%0A%09%09%09%09%09if(resultStatus == null) %7B%0D%0A%09%09%09%09%09%09resultStatus = ReportNodeStatus.PASSED;%0D%0A%09%09%09%09%09%7D%0D%0A%09%09%09%09%09node.setStatus(r
+node.setStatus(parentR
 esul
@@ -3718,25 +3718,24 @@
 tatus);%09%09%09%09%09
-%09
 %0D%0A%09%09%09%09%7D%0D%0A%09%09%09

Revert "SequentialArtefactScheduler: Setting status to PASSED "