All Downloads are FREE. Search and download functionalities are using the official Maven repository.

br.com.jarch.faces.converter.PickListBaseEntityJsfConverter Maven / Gradle / Ivy

package br.com.jarch.faces.converter;

import br.com.jarch.core.model.IIdentity;
import org.primefaces.component.picklist.PickList;
import org.primefaces.model.DualListModel;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;
import java.util.Iterator;

@FacesConverter("br.com.jarch.faces.converter.pickListBaseEntityConverter")
public class PickListBaseEntityJsfConverter implements Converter {

    @Override
    public Object getAsObject(FacesContext arg0, UIComponent arg1, String arg2) {
        Object ret = null;
        if (arg1 instanceof PickList) {
            Object dualList = ((PickList) arg1).getValue();

            DualListModel dl = (DualListModel) dualList;
            for (Iterator iterator = dl.getSource().iterator(); iterator.hasNext(); ) {
                Object o = iterator.next();
                String id = (new StringBuilder()).append(((IIdentity) o).getId()).toString();
                if (arg2.equals(id)) {
                    ret = o;
                    break;
                }
            }
            if (ret == null) {
                for (Iterator iterator1 = dl.getTarget().iterator(); iterator1.hasNext(); ) {
                    Object o = iterator1.next();
                    String id = (new StringBuilder()).append(((IIdentity) o).getId()).toString();
                    if (arg2.equals(id)) {
                        ret = o;
                        break;
                    }
                }
            }
        }
        return ret;
    }

    @Override
    public String getAsString(FacesContext arg0, UIComponent arg1, Object arg2) {
        if (arg2 instanceof IIdentity
                && ((IIdentity) arg2).getId() != null) {
            return ((IIdentity) arg2).getId().toString();
        }

        return "";
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy