package org.openntf.xrest.xsp.script;

import javax.faces.context.FacesContext;
import javax.faces.el.EvaluationException;
import javax.faces.el.PropertyNotFoundException;

import com.ibm.xsp.binding.ValueBindingEx;
import com.ibm.xsp.exception.EvaluationExceptionEx;

public class GroovyValueBinding extends ValueBindingEx {

	private String formula;

	public GroovyValueBinding() {
	}

	public GroovyValueBinding(String formula) {
		this.formula = formula;
	}

	@Override
	public Class getType(FacesContext facesContext) throws EvaluationException, PropertyNotFoundException {
		return super.getExpectedType();
	}

	@Override
	public Object getValue(FacesContext facesContext) throws EvaluationException, PropertyNotFoundException {
		
		//TODO Execute DSLBuilder
		// Force the type based on getExpectedType
		// Converters is used after.
		Object result = null;
		return convertToExpectedType(facesContext, result);
	}

	@Override
	public boolean isReadOnly(FacesContext facesContext) throws EvaluationException, PropertyNotFoundException {
		return true;
	}

	@Override
	public void setValue(FacesContext facesContext, Object arg1) throws EvaluationException, PropertyNotFoundException {
		throw new EvaluationExceptionEx("Not implemented", this);
	}

	@Override
	public Object saveState(FacesContext arg0) {
		Object[] state = new Object[1];
		state[0] = super.saveState(arg0);
		state[1] = formula;
		return state;
	}

	@Override
	public void restoreState(FacesContext facesContext, Object state) {
		Object[] stateObject = (Object[]) state;
		super.restoreState(facesContext, stateObject[0]);
		formula = (String) stateObject[1];
	}

	@Override
	public String getExpressionString() {
		return "#{groovy:" + formula + "}";
	}
}

@@ -408,16 +408,22 @@
 Binding(
+final 
 String f
@@ -495,16 +495,22 @@
 getType(
+final 
 FacesCon
@@ -649,32 +649,38 @@
 Object getValue(
+final 
 FacesContext fac
@@ -750,15 +750,14 @@
 n %7B%0A
-%09%09
 %0A%09%09//
+ 
 TODO
@@ -972,16 +972,22 @@
 eadOnly(
+final 
 FacesCon
@@ -1113,16 +1113,22 @@
 etValue(
+final 
 FacesCon
@@ -1137,32 +1137,38 @@
 xt facesContext,
+ final
  Object arg1) th
@@ -1317,24 +1317,30 @@
 t saveState(
+final 
 FacesContext
@@ -1374,25 +1374,25 @@
  new Object%5B
-1
+2
 %5D;%0A%09%09state%5B0
@@ -1497,16 +1497,22 @@
 reState(
+final 
 FacesCon
@@ -1529,16 +1529,22 @@
 Context,
+ final
  Object 

fix index out of bounds