package flexa.ff;

import flexa.fb.*;
import java.util.Map;
import java.util.HashMap;
import javax.script.*;



public class form extends FComponent {
    

    public form() {
        super("ff.form");
    }

	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);
	}
	
	public void event(String aAction, FContext aContext) throws Exception {
	
	    // ONOPEN/ONSTART ACTION - INSTANTIATES DATASOURCE OBJECT (NEW OR LOAD)
	    switch(aAction) {
            case "onopen":
            case "start":
                // 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);
            	    }
        	    }
                break;
	    }
	    
	    // SAVE ACTION - SAVE DATASOURCE OBJECT
	    if( aAction.equals(this.name + ".save") ) {
            // 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);
	}
	
	
	
	
}