Created
March 8, 2012 22:31
-
-
Save tomafc330/2003880 to your computer and use it in GitHub Desktop.
Getting the entity from the SelectItem without going to database
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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: