package flexa.ff;
import flexa.fb.*;
import java.util.Map;
import java.util.HashMap;
import javax.script.*;
public class form extends FComponent {
    //
    // CONSTRUCTOR
    //
    public form() {
        super("ff.form");
    }
    
    // 
    // Methods
    //
    
    
    public void updateFromRequest(HashMap<String, Object> aParameters)  throws Exception { 
    		  
		super.updateFromRequest(aParameters); 
		//if( aParameters.containsKey(this.getValue("name")) ) 
		//   this.setValue("value", aParameters.get(this.getValue("name")).toString()); 
		 
		// @todo update Object 
		 

    }
    


    
    public void updateFromObject(FObjectInterface aObject)  throws Exception { 
    		this.removeClonedChildren(); 
		super.updateFromObject(aObject);

    }
    


    // 
    // Events
    //
	public void event(String aAction, FContext aContext) throws Exception {
        
    
        // onopen EVENT
        if( LibText.stringToArray("onopen,start").contains(aAction) ) {
        			  
			// Instantiates object if datasource 
			if( getValue("datasource") != null && !((String)getValue("datasource")).equals("") && aContext.getContext("_request").hasMember("id") ) { 
			                    //this.engine.getBindings(ScriptContext.ENGINE_SCOPE).get("id") != null ) { 
			    // If request ID not empty 
			    String tsid = aContext.getContext("_request").get("id").toString();  //this.engine.getBindings(ScriptContext.ENGINE_SCOPE).get("id").toString(); 
			    if( tsid != null && !tsid.equals("") ) {  
			         
			        // Creates or Load Object (ORM) 
			        int tId = Integer.parseInt(tsid); // ID of object 
			        Log.getInstance().info("FORM start datasource : " + (String)getValue("datasource") +  " id " + tId);                 
			        String tModule = (String)aContext.get("_module"); //(String)this.engine.getBindings(ScriptContext.ENGINE_SCOPE).get("_module"); 
			        Log.getInstance().info("FORM ORMgetobject " + tModule + "." + (String)this.getValue("name") + " id: " + tId + " db " + aContext.db); 
			        this.fobject = ORM.getInstance().getObject( tModule + "." + (String)this.getValue("name"), tId, aContext);  
			         
			        // Expose fobject & its attributes to context 
			        aContext.addContext("obj", this.fobject); 
			         
			        // Set variables on FFields 
			         
			        this.updateFromObject(this.fobject); 
			    } 
			} 
			 

        }
    


    
        // save EVENT
        if( LibText.stringToArray("" + this.name + ".save").contains(aAction) ) {
        			  
			// Save object 
			Log.getInstance().info("Form save"); 
			if( getValue("datasource") != null && !((String)getValue("datasource")).equals("") && aContext.getContext("_request").hasMember("id") ) { // this.engine.getBindings(ScriptContext.ENGINE_SCOPE).get("id") != null ) { 
			    String tsid = aContext.getContext("_request").get("id").toString(); //(String)this.engine.getBindings(ScriptContext.ENGINE_SCOPE).get("id"); 
			    Log.getInstance().info("Form save id" + tsid); 
			    if( tsid != null && !tsid.equals("") ) { 
			        // Save object 
			        Log.getInstance().info("Saving form object"); 
			        this.fobject.save(); 
			 
			        // Expose fobject & its attributes to context 
			        //this.fobject.buildScriptBindings(this.engine.getBindings(ScriptContext.ENGINE_SCOPE)); 
			        aContext.addContext("obj", this.fobject); 
			         
			        // Set variables on FFields 
			        this.updateFromObject(this.fobject); 
			    } 
			} 
			 

        }
    


	    super.event(aAction, aContext);
	}
	
	
	
	
}