// 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 input extends FField {
    //
    // CONSTRUCTOR
    //
    public input() {
        super("ff.input");
    }
    
    // 
    // Methods
    //
    
    
    public void updateFromRequest(Map<String, Object> aParameters)  throws Exception { 
    		String iId = (String)this.getValue("id");
		String tType = (String)this.getValue("type");
		Object oVal = aParameters.get(iId);
		Object tActual = this.getValue("value");
		if( aParameters.containsKey(iId)  && LibPrim._comparision(oVal, tActual) != 0 ) {
		    Log.info("updateFromRequest --> " + iId + " " + tType,  oVal, tActual);
		    String tVal = (String)aParameters.get(this.getValue("id"));
		    Log.getInstance().info("updateFromRequest " + iId + " type " + tType + " = " + tVal);
			switch(tType) {
			    case "integer":
			        this.setValue("value", (tVal == null || tVal.trim().equals("")) ? 0 : Long.parseLong(tVal));
			        this.cleanChanged("value");
			        break;
			    case "decimal":
			    case "money":
			    case "date":
			    case "datetime":
			    case "timestamp":
			    case "time":
			    case "text":
			    case "password":
			    case "email":
			    case "url":
			    case "tel":
			    case "checkbox":
		        default:
		            this.setValue("value", tVal);
		            this.cleanChanged("value");
			}
		}

    }
    


    
    public Object getDefaultValue()  throws Exception { 
    		String tName = (String)this.getValue("id");
		String tType = (String)this.getValue("type");
		Object tDefault = this.getValue("defaultvalue");
		if( tDefault != null && tDefault.toString().trim().equals("") ) tDefault = null;
		if( tDefault != null ) 
		    Log.info("Defaultvalue " + tName + " default " + tDefault.getClass().getName(), tDefault);
		//else
		//    Log.info("Defaultvalue " + tName + " null type " + tType);
		return LibPrim.convert_to(tDefault, tType);

    }
    






    
    public void setValuexxx(Object aValue)  throws Exception { 
    		String tType = (String)this.getValue("type");
		switch( tType ) {
		    case "date":
		        if( LibDate.isDate(aValue) ) {
		            super.setValue("value", aValue); //LibText.format(aValue, "dd/mm/yyyy"));
		        } else {
		            super.setValue("value", aValue);
		        }
		        break;
		    default:
		        super.setValue("value", aValue);      
		}
		//this.setValue("value", LibText.format(aValue));
		//

    }
    


    
    public String validate(FContext aContext)  throws Exception { 
    		//text,password,integer,decimal,money,date,datetime,timestamp,time,email,url,tel,checkbox
		if( aContext.validateUntil.equals("none") ) return "";
		StringBuffer sb = new StringBuffer();
		// Validate
		String tmessage = this.executeValid(aContext);
		if( !tmessage.equals("") ) {
			if( sb.length() > 0 ) sb.append("\n");
			sb.append(tmessage);
		}
		// Si el componentne tiene REQUIRED evalua
		boolean trequired = false;
		aContext.log("VALIDATE INPUT " + this.id);
		if( this.getValue("required") != null && this.getValue("required").toString().equals("yes") ) trequired = true;
		// Si el componentne tiene REQUIREDIF evalua
		if( this.hasExpression(aContext, "requiredif") ) {
			Object calcrequiredif = this.calcExpression(aContext, "requiredif");
			Log.info("Requiredif " + this.getId(), calcrequiredif);
			aContext.log("Requiredif " + this.getId(), calcrequiredif);
			trequired = LibPrim.to_bool(calcrequiredif);
		}
		// Si debe evaluar , evalue
		if( trequired ) {
		    trequired = false;
		    switch(this.getValue("type").toString()) {
		        case "integer":
		        case "decimal":
		        case "money":
		            if( LibNum._comparision(this.getValue("value"), 0) == 0 ) trequired = true;
		            break;
		        default:
		            if( LibPrim.isEmpty(this.getValue("value")) )   trequired = true;
		    }
		}
		// Si evaluó y required, da error
		if( trequired ) {
			if( sb.length() > 0 ) sb.append("\n");
			sb.append("Valor requerido en " +this.getValue("label"));
		}				
		return sb.toString();

    }
    




    // 
    // Events
    //
	public void event(String aAction, FContext aContext) throws Exception {
        // Events
        
    
        // onchangejava EVENT
        if( LibText.stringToArray("" + this.id + ".onchange").contains(aAction) ) {
        			Log.info("onchangejava " + 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. ");

        }
    


    
        // onopen EVENT
        if( LibText.stringToArray("onopen").contains(aAction) ) {
        			// Set default value if not in form
			FComponent tform = this.getForm();
			if( tform == null ) {
			    String tid = (String)this.getValue("id");
			    Log.info("Setting defaultvalue on " + tid);
			    setValueWithoutTrigger("value", getDefaultValue());
			}

        }
    


	    super.event(aAction, aContext);
	}
	
	
}