// generated by FBuilder
package flexa.ff;
import flexa.fb.*;
import java.util.Map;
import java.util.HashMap;
import javax.script.*;
import java.util.ArrayList;
public class select2 extends FField {
    //
    // CONSTRUCTOR
    //
    public select2() {
        super("ff.select2");
    }
    
    // 
    // Methods
    //
    
    
    public String getCompleteName()  { 
    		if( getValue("relation") != null && !(((String)getValue("relation")).trim().equals("")) ) {
		    return ((String)getValue("relation")).trim();
		} else {
		    return "";
		    //return this.module + "." + this.name;
		}

    }
    





    // 
    // Events
    //
	public void event(String aAction, FContext aContext) throws Exception {
        // Events
        
    
        // onopen EVENT
        if( LibText.stringToArray("" + this.id + ".refresh," + this.id + ".select,start,onopen,refresh").contains(aAction) ) {
        			String tds = (String)getValue("datasource");
			String trel = (String)getValue("relation");
			String ttype = (String)getValue("type");
			String tid = (String)getValue("id");
			// Set default value if not in form
			if( aAction.equals("onopen") ) {
			    FComponent tform = this.getForm();
			    if( tform == null ) {
			        Log.info("Setting defaultvalue on " + tid);
			        setValue("value", getDefaultValue());
			    }
			}
			// Generates SQL from relation 
			if( tds == null && trel != null && getValue("datasource_oql") == null ) {
			    tds = "select x from " + trel;
			    if( getValue("relationfilter") != null )
			        tds += " where " + ((String)getValue("relationfilter"));
			    setValue("datasource_oql", tds);
			}
			Log.info("COMBO " + ttype + " #" + (String)getValue("id") + " name "  + (String)getValue("name") + " event " + aAction + " ds " + tds + " rel " + trel );
			// Combo autocomplete set datasource for URL
			if( ttype != null && ttype.indexOf("autocomplete") >= 0 && !aAction.endsWith(".select") ) {
			    setValue("datasource", "/flexa/c?_c=" + aContext.mainComponent.getModule() + "/" + aContext.mainComponent.getName() +  "&_x=" + (String)getValue("id") + ".select");
			    Log.info("COMBO autocomplete dont fill with event " + aAction);
			} else {
			    // Executes datasource...
			    if( tds != null ) {
			        Log.getInstance().info("COMBO event " + aAction + " datasource : " + tds );
			        FResult result = this.processDatasource(aContext);
			        //FResult result = Util.sqltoarrayddtotal(aContext, tds);
			        
			        // ... return results if requested
			        if( aAction.endsWith(".select") ) {
			            aContext.hash.put( ((String)getValue("id")) + ".select", result); 
			            
			        // ...or set result in datalist component attribute
			        } else {
			            //String jsonmatrix = result.toJsonMatrix();
			            //Log.info("result process ds: " + jsonmatrix);
			            //setValue("datalist", jsonmatrix);
			            Log.info(tid + " setting datalist", result);
			            //if( result.size() > 0 )
			            //    Log.info(tid + " first element of datalist", result.get(0));
			            setValue("datalist", result);
			        }
			    } 
			}

        }
    


	    super.event(aAction, aContext);
	}
	
	
}