// 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 combo extends FField {
    //
    // CONSTRUCTOR
    //
    public combo() {
        super("ff.combo");
    }
    
    // 
    // 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;
		}

    }
    




























    
    public void setValue(Object aValue)  throws Exception { 
    		String tds = (String)getValue("datasource");
		String trel = (String)getValue("relation");
		String ttype = (String)getValue("type");
		String tid = (String)getValue("id");
		Object tdatalist = getValue("datalist");
		Log.info("COMBO SETVALUE", aValue);
		Log.info("COMBO DATALIST", tdatalist);
		this.setValue("value", aValue);		
		if( aValue == null ) {
		   this.setValue("displayvalue", "");
		} else {
		   if( FObject.class.isAssignableFrom(aValue.getClass()) ) {
		       this.setValue("displayvalue", ((FObject)aValue).getDisplay() );
		   } else {
		       if( tdatalist != null ) {
		          Log.info("COMBO DATALIST", tdatalist); 
		          // @todo set displayvalue searchin from datalist
		          this.setValue("displayvalue", aValue);
		       } else {
		          this.setValue("displayvalue", aValue);
		       }
		   }
		}
		Log.info(this.id + " setValue ", aValue);
		Log.info(this.id + " display", this.getValue("displayvalue"));
		/**
		//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);
		        }
		    } 
		}
		**/

    }
    


    // 
    // 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");
			Object tvalue = getValue("value");
			// Set default value if not in form
			if( aAction.equals("onopen") ) {
			    FComponent tform = this.getForm();
			    if( tform == null ) {
			        Log.info("Setting defaultvalue on " + tid);
			        setValueWithoutTrigger("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"));
			    
			    // @todo DO IT IN FBUILDER or cache it here
			    FQuery toql = FQuery.parseOql(tds);
			    String tsql = toql.generateSql(FQuery.ON_LIST, aContext);        
			    
			    
			    //setValue("datasource_oql", tds);
			    setValue("datasource_sql", tsql);
			}
			//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);
			            //Log.info("Datalist", result);
			            if( tvalue != null && result != null ) {
			                Log.info("Trying to set displayvalue for value", tvalue);
			                //Log.info("Datalist", result);
			                for( var ii = 0; ii < result.size(); ii++) {
			                    FRow trow = result.get(ii);
			                    var i0 = 0;
			                    var tbool = false;
			                    for(String tkey: trow.keySet()) {
			                        if( i0 == 0 ) {
			                            if( LibPrim.areEquals(trow.getMember(tkey), tvalue) ) {
			                                tbool = true;
			                            }
			                        } else if( tbool ) {
			                            Log.info("setdisplayvalue", trow.getMember(tkey));
			                            setValue("displayvalue", trow.getMember(tkey));
			                            break;
			                        }
			                        i0++;
			                    }
			                }
			                
			                
			            }
			            
			            
			            
			        }
			    } 
			}

        }
    


    
        // onchangejava EVENT
        if( LibText.stringToArray("" + this.id + ".onchange").contains(aAction) ) {
        			Log.info("onchangejava combo " + this.id + " calc_dependants: " + this.getValue("calc_dependants"));
			this.calcDependants(aContext);
			Log.info("Executing onuserchange. " );
			super.event(aAction.replace("change", "userchange"), aContext);
			Log.info("Executed onuserchange. ");

        }
    


	    super.event(aAction, aContext);
	}
	
	
}