// 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 tr extends FComponent {
    //
    // CONSTRUCTOR
    //
    public tr() {
        super("ff.tr");
    }
    
    // 
    // Methods
    //
    
    
    public void buildFromFRow(FRow row)  throws Exception { 
    		td ttd = null;
		String lastkey = "";
		for (String key : row.keySet()) {
		    if( key.startsWith("_") ) continue;
		    
		    if( lastkey.endsWith("_display") ) {
		        //Log.info("lastkey " + lastkey + " without " + lastkey.substring(0, lastkey.length() - 8));
		    }
		    if( lastkey.endsWith("_display") && lastkey.substring(0, lastkey.length() - 8).equals(key) ) continue;
		    ttd = new td();
		    String tid = this.id + key;
		    ttd.setValue("id", tid);
		    ttd.setValue("name", tid);
		    Object tval = row.get(key);
		    if( tval != null && tval instanceof FObject ) {
		        tval = ((FObject)tval).getDisplay();
		    }
			ttd.setValue("content", tval);
			this.addChild(ttd);
			lastkey = key;
		}

    }
    


    
    public void buildHeaderFromFRow(FRow row, String columnstitle)  throws Exception { 
    		td ttd = null;
		String lastkey = "";
		int ii = 0;
		ArrayList<String> titles = columnstitle != null && !columnstitle.trim().equals("") ? LibText.stringToArray(columnstitle) : null;
		Log.info("Building header row with columnstitle", titles);
		for (String key : row.keySet()) {
		    if( key.startsWith("_") ) continue;
		    if( lastkey.endsWith("_display") && lastkey.substring(0, lastkey.length() - 8).equals(key) ) continue;
		    
		    ttd = new td();
		    String tid = this.id + key;
		    ttd.setValue("id", tid);
		    ttd.setValue("name", tid);
		    String keytitle = key.replace("_display", "").replace("_", " ");
		    String keyval = key.replace("_display", "").replace("_", " ");
		    //Log.info(" ii " + ii + " columnstitle: " + columnstitle);
		    if( columnstitle != null && !columnstitle.trim().equals("") )
		        keytitle = titles.get(ii);
			ttd.setValue("content", keytitle);
			ttd.setValue("value", keyval);
			this.addChild(ttd);
			lastkey = key;
			ii++;
		}

    }
    


    
    public void buildHeaderFromFClass(FClass fclass)  throws Exception { 
    		td ttd = new td();
		String tid = this.id + "id";
		ttd.setValue("id", tid);
		ttd.setValue("name", tid);
		ttd.setValue("content", "Id");
		this.addChild(ttd);
		HashMap<String, FComponent> tfields = fclass.getFields();
		for (String key : tfields.keySet()) {
		    if( key.startsWith("_") ) continue;
		    ttd = new td();
		    tid = this.id + key;
		    ttd.setValue("id", tid);
		    ttd.setValue("name", tid);
		    String tlabel = key;
		    if( tfields.get(key).getValue("label") != null ) tlabel = tfields.get(key).getValue("label").toString();
			ttd.setValue("content", key);
			this.addChild(ttd);
		}

    }
    


    
    public String toString()  { 
    		StringBuffer sb = new StringBuffer("TR: ");
		int i = 0;
		for (FComponent element : this.getChildren()) {
			sb.append(element.getId() + ":" +  element.toString());
			sb.append(" - ");
			i++;
		}		
		return sb.toString();

    }
    


    // 
    // Events
    //
	public void event(String aAction, FContext aContext) throws Exception {
        // Events
        
    
        // onopen EVENT
        if( LibText.stringToArray("" + this.name + ".select," + this.name + ".refresh,start,onopen").contains(aAction) ) {
        			if( this.status <= 0 && (hasValue("datasource_oql") || hasValue("datasource_sql"))    ) {
			    // EXECUTES SQL/OQL
			    Log.info("refresh tr " + this.id + " processing datasource");
			    //Log.info(aContext.toString());
			    FResult result = this.processDatasource(aContext);
			    int totalrows = result.size();
			    this.clonefromresult(result, aContext); // Clone me
			    
			    /**     
			    FComponent tbody = this.findChildOfType("tr", "type", "body");
			    if( tbody == null ) tbody = this.findChildOfType("tr", "type", "");         // @todo more than one row
			    FComponent theadergroup = this.findChildOfType("tr", "type", "headergroup");// @todo more than one row
			    FComponent tfootergroup = this.findChildOfType("tr", "type", "footergroup");   // @todo more than one row
			    String tgroupby = (String)getValue("groupby");
			    
			    // If no rows, build TRs & TDs from result
			    if( tbody == null ) {
			        buildRowsFromResult(result);
			        return;
			        //throw new Exception("No TR body found on table " + (String)getValue("name"));   // @todo armar filas con FResult
			    }
			    
			    // Clone TRs from result
			    int tposition = this.getChildren().indexOf(tbody) + 1;
			    FComponent torender = tbody;
			    FComponent torender2 = null;
			    String tid = (String)torender.getValue("id");
			    // Por cada registro clona elemento
			    Object tlastgroupby = null;
			    int tgroupid = 0;
			    FRow row = null;
			    FRow lastrow = null;
			    for(int i = 0; i < totalrows; i++) {
			        row = result.get(i);
			        Log.getInstance().info("TR body ds row " + i);
			        //Log.getInstance().info(row.toString());
			            
			        // Group by header/footer
			        if( tgroupby != null ) {
			            this.context = new FContext(row);
			            Object tgroupbyval = this.calcExpression(aContext, "groupby");
			            if( i == 0 ) theadergroup.context = new FContext(row);
			            if( i > 0 && !tgroupbyval.equals(tlastgroupby) ) {
			                tgroupid++;
			                // Footer group
			                // Clone footer
			                torender2 = (FComponent)tfootergroup.clone();
			                torender2.setValue("id", tfootergroup.getValue("id") + "[" + tgroupid + "]");
			                torender2.context = new FContext(lastrow);
			                this.addChild(torender2, tposition );
			                tposition++;
			                // Header group
			                Log.info("Changed group by i" + i + " " + tgroupbyval);
			                torender2 = (FComponent)theadergroup.clone();
			                //tlastgroupby
			                torender2.setValue("id", theadergroup.getValue("id") + "[" + tgroupid + "]");
			                torender2.context = new FContext(row);
			                this.addChild(torender2, tposition );
			                tposition++;
			            }
			            tlastgroupby = tgroupbyval;
			        }
			        
			        
			        if( i > 0 ) {
			            // Clone and add to father
			            //torender = (FComponent)PipedDeepCopy.copy(this);
			            torender = (FComponent)tbody.clone();
			            //torender = new FComponent(this);
			            torender.setValue("id", tid + "[" + i + "]");
			            this.addChild(torender, tposition);
			            tposition++;
			        }
			        //Log.info("Row moredata:");
			        //Log.info(row.toString());
			        //Log.info(((FObject)row.get("x")).info());
			        //torender.moreData = row;
			        torender.context = new FContext(row);
			        //Log.getInstance().info("Torender " + torender.getValue("id"));
			        lastrow = row;
			    }
			    // Footer group
			    if( totalrows > 0 && tgroupby != null ) {
			        tfootergroup.context = new FContext(row);
			    }
			    **/
			    //Log.getInstance().info("TABLE EVENT end: " + torender.info());
			    // @todo eliminar clonados demas de anteriores clonaciones
			    // @todo si no hay registros limpiar elemento y ponerlo como inactivo (status -1)
			}

        }
    


	    super.event(aAction, aContext);
	}
	
	
}