/*
 * Copyright 2007 Andrew Wills
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.danann.cernunnos.flow;

import org.danann.cernunnos.AbstractContainerTask;
import org.danann.cernunnos.EntityConfig;
import org.danann.cernunnos.Formula;
import org.danann.cernunnos.Phrase;
import org.danann.cernunnos.Reagent;
import org.danann.cernunnos.ReagentType;
import org.danann.cernunnos.SimpleFormula;
import org.danann.cernunnos.SimpleReagent;
import org.danann.cernunnos.TaskRequest;
import org.danann.cernunnos.TaskResponse;

public final class IfTask extends AbstractContainerTask {

	// Instance Members.
	private Phrase test;

	/*
	 * Public API.
	 */

	public static final Reagent TEST = new SimpleReagent("TEST", "@test", ReagentType.PHRASE, String.class, 
				"Boolean value indicating whether SUBTASKS should be executed.");

	public Formula getFormula() {
		Reagent[] reagents = new Reagent[] {TEST, AbstractContainerTask.SUBTASKS};
		final Formula rslt = new SimpleFormula(IfTask.class, reagents);
		return rslt;
	}

	public void init(EntityConfig config) {

		super.init(config);		

		// Instance Members.
		this.test = (Phrase) config.getValue(TEST);
		
	}

	public void perform(TaskRequest req, TaskResponse res) {
		
		Boolean b = (Boolean) test.evaluate(req, res);
		
		if (b.equals(Boolean.TRUE)) {
			super.performSubtasks(req, res);
		}
		
	}
	
	public boolean isApplicable(TaskRequest req, TaskResponse res) {
		
		return ((Boolean) test.evaluate(req, res)).booleanValue();
				
	}
	
}
@@ -1303,22 +1303,22 @@
 SE, 
-String
+Boolean
 .class,
- 
 %0D%0A%09%09
@@ -1652,18 +1652,16 @@
 config);
-%09%09
 %0D%0A%0D%0A%09%09//
@@ -1727,20 +1727,18 @@
 TEST);%0D%0A
-%09%09
 %0D%0A
+
 %09%7D%0D%0A%0D%0A%09p
@@ -1794,20 +1794,18 @@
 res) %7B%0D%0A
-%09%09
 %0D%0A
+
 %09%09Boolea
@@ -1846,20 +1846,18 @@
  res);%0D%0A
-%09%09
 %0D%0A
+
 %09%09if (b.
@@ -1923,27 +1923,24 @@
 ;%0D%0A%09%09%7D%0D%0A
-%09%09
 %0D%0A
+
 %09%7D%0D%0A
-%09
 %0D%0A
+
 %09public 
@@ -1998,20 +1998,18 @@
 res) %7B%0D%0A
-%09%09
 %0D%0A
+
 %09%09return
@@ -2066,18 +2066,13 @@
 );%0D%0A
-%09%09%09%09
 %0D%0A
+
 %09%7D%0D%0A
-%09
 %0D%0A%7D

Corrected expected type of TEST reagent.