Package com.evasion.client.controler.admin

Source Code of com.evasion.client.controler.admin.CorpoConverter

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.evasion.client.controler.admin;

import com.evasion.entity.Corporation;
import java.util.Iterator;
import java.util.List;
import javax.faces.component.UIComponent;
import javax.faces.component.UISelectItems;
import javax.faces.component.html.HtmlSelectOneMenu;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;

/**
*
* @author sebastien.glon
*/
@FacesConverter(forClass = Corporation.class, value = "corpoConverter")
public class CorpoConverter implements Converter {

    @Override
    public Object getAsObject(FacesContext facesContext, UIComponent uiComponent, String rawValue) {
        Corporation result = null;
        if (uiComponent instanceof HtmlSelectOneMenu) {
            HtmlSelectOneMenu menu = (HtmlSelectOneMenu) uiComponent;
            UISelectItems items = (UISelectItems) menu.getChildren().get(0);
            List obj = (List) items.getValue();
            for (Iterator it = obj.iterator(); it.hasNext();) {
                Corporation corpo = (Corporation) it.next();
                if (corpo.getId().equals(Long.valueOf(rawValue))) {
                    result = corpo;
                    break;
                }
            }
        }
        return result;
    }

    @Override
    public String getAsString(FacesContext facesContext, UIComponent uiComponent, Object value) {

        // We are friendly and return at least an empty String
        String stringValue = "";

        if (value != null) {
            // Default components check if the value is a String and return it as it is
            if (value instanceof String) {
                stringValue = (String) value;
            } else if (value instanceof Corporation) {
                Corporation corpoValue = (Corporation) value;
                stringValue = corpoValue.getId().toString();
            }
            //throw new ConverterException(C.getMessage(facesContext,"phoneconverter-bad-phone"));
        }
        return stringValue;

    }
}
TOP

Related Classes of com.evasion.client.controler.admin.CorpoConverter

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.