package flexa.ff;

import flexa.fb.*;
import java.util.Map;
import java.util.HashMap;
import java.util.ArrayList;
import javax.script.*;



public class subformrow extends FComponent {
    
    public subformrow() {
        super("ff.subformrow");
    }

	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 {
		String tName = (String)this.getValue("name");
		Log.getInstance().info("updateFromObject subform " + tName);
		if( tName != null && !tName.equals("") ) {
			if( aObject.hasMember(tName) ) {
			    FCollection fcol = (FCollection)aObject.getMember(tName);
			    this.collection = fcol; 
			    //@todo this.removeClonedChildren();
			    ArrayList<FComponent> tlist = this.clonefromcollection(fcol);
			    for(var i = 0; i < tlist.size(); i++) {
			        ((subform)tlist.get(i)).collection = fcol;
			    }
			}
		}
	}
	**/
	
	
	public void event(String aAction, FContext aContext) throws Exception {
        
         if( aAction.equals(this.name + ".delete") ) {
	        // Remove myself
	        if( this.fobject != null ) {
	            ((subform)this.container).collection.remove(this.fobject);
	        }
	        this.removeMyself();
	    }
	    
	    
	    /**
	    // 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);
	}
}