Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Getting the entity from the SelectItem without going to database
package com.makedemo.makedemo.domain.converter;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.FacesConverter;
import com.makedemo.makedemo.domain.PersonName;
import com.SelectItemsBaseConverter;
@FacesConverter(value="personNameConverter")
public class PersonNameConverter extends SelectItemsBaseConverter {
@Override
public String getAsString(FacesContext context, UIComponent component,
Object value) {
if (value == null || !(value instanceof PersonName)) {
return null;
}
return ((PersonName) value).getId().toString();
}
}
package com;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
public abstract class SelectItemsBaseConverter implements Converter {
@Override
public Object getAsObject(FacesContext context, UIComponent component,
String value) {
return SelectItemsUtils.findValueByStringConversion(context, component,
value, this);
}
}
package com;
import java.util.Iterator;
import java.util.NoSuchElementException;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.model.SelectItem;
import javax.faces.model.SelectItemGroup;
import org.primefaces.util.ComponentUtils;
public final class SelectItemsUtils {
private SelectItemsUtils() {
}
public static Object findValueByStringConversion(FacesContext context, UIComponent component, String value, Converter converter) {
Iterator<SelectItem> items = ComponentUtils.createSelectItems(component).iterator();
return findValueByStringConversion(context, component, items, value, converter);
}
private static Object findValueByStringConversion(FacesContext context, UIComponent component, Iterator<SelectItem> items, String value, Converter converter) {
while (items.hasNext()) {
SelectItem item = items.next();
if (item instanceof SelectItemGroup) {
SelectItem subitems[] = ((SelectItemGroup) item).getSelectItems();
if (!isEmpty(subitems)) {
Object object = findValueByStringConversion(context, component, new ArrayIterator(subitems), value, converter);
if (object != null) {
return object;
}
}
} else if (!item.isNoSelectionOption() && value.equals(converter.getAsString(context, component, item.getValue()))) {
return item.getValue();
}
}
return null;
}
public static boolean isEmpty(Object[] array) {
return array == null || array.length == 0;
}
/**
* This class is based on Mojarra version
*/
static class ArrayIterator implements Iterator<SelectItem> {
public ArrayIterator(SelectItem items[]) {
this.items = items;
}
private SelectItem items[];
private int index = 0;
public boolean hasNext() {
return (index < items.length);
}
public SelectItem next() {
try {
return (items[index++]);
}
catch (IndexOutOfBoundsException e) {
throw new NoSuchElementException();
}
}
public void remove() {
throw new UnsupportedOperationException();
}
}
}
@tomafc330
Copy link
Author

tomafc330 commented Mar 8, 2012

Note ComponentUtils.createSelectItems(component).iterator();

This gets the SelectItems that are in memory of the JSF framework

Taken from the PF 3.1.1 code:

public static List<SelectItem> createSelectItems(UIComponent component) {
    List<SelectItem> items = new ArrayList<SelectItem>();
    Iterator<UIComponent> children = component.getChildren().iterator();

    while(children.hasNext()) {
        UIComponent child = children.next();

        if(child instanceof UISelectItem) {
            UISelectItem selectItem = (UISelectItem) child;

            items.add(new SelectItem(selectItem.getItemValue(), selectItem.getItemLabel()));
        } else if(child instanceof UISelectItems) {
            Object selectItems = ((UISelectItems) child).getValue();

            if(selectItems instanceof SelectItem[]) {
                SelectItem[] itemsArray = (SelectItem[]) selectItems;

                for(SelectItem item : itemsArray)
                    items.add(new SelectItem(item.getValue(), item.getLabel()));

            } else if(selectItems instanceof Collection) {
                Collection<SelectItem> collection = (Collection<SelectItem>) selectItems;

                for(SelectItem item : collection)
                    items.add(new SelectItem(item.getValue(), item.getLabel()));
            }
        }
    }

    return items;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment