package com.rjconsultores.ventaboletos.web.utilerias;

import java.util.Iterator;

import org.zkoss.zk.ui.Component;
import org.zkoss.zkplus.databind.TypeConverter;
import org.zkoss.zul.Comboitem;

public class StringToIntegerConverter implements TypeConverter {

	@SuppressWarnings("rawtypes")
	public Object coerceToUi(Object val, Component comp) {
        if (val != null) {
            for (Iterator it = comp.getChildren().iterator(); it.hasNext();) {
                final Component child = (Component) it.next();

                if (child instanceof Comboitem) {
                    String ret = null;
                    
                    if (val instanceof Integer) {
                        ret = val.toString();
                    }

                    if (ret.equals(((Comboitem) child).getValue())) {
                        return child;
                    }
                }
            }
        }
        
        return null;
    }

    public Object coerceToBean(Object val, Component cmpnt) {
        val = val != null ? ((Comboitem) val).getValue() : null;
        
        if (val instanceof String) {
            return Integer.valueOf((String) val);
        }
        
        return null;
    }
}
