[seam-issues] [JBoss JIRA] Updated: (SEAMFACES-141) Create a converter for dealing with lists
Brian Leathem (JIRA)
jira-events at lists.jboss.org
Wed Apr 13 17:39:33 EDT 2011
[ https://issues.jboss.org/browse/SEAMFACES-141?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]
Brian Leathem updated SEAMFACES-141:
------------------------------------
Description:
A converter is required to serialize an object into the query string for use as a view param. If that object is a list, it would be helpful to make use of an existing converter that converts each individual entry, and has the cumulative result serialized as a string.
The standard CGI syntax represents arrays as multiple values, each with the same key. This may already be supported in JSF, but at the time of writing this, I couldn't find it.
Possible implementation (not conformant with the above mentioned CDI standard syntax:
{code}
abstract public class ListConverter<T> implements Converter {
private final Converter converter;
private final String SEPERATOR_CHAR=":";
public ListConverter(Converter converter) {
this.converter = converter;
}
@Override
public Object getAsObject(FacesContext context, UIComponent component, String value) {
List<T> newList = new ArrayList<T>();
if (value == null || value.isEmpty()) {
return newList;
}
String[] chunkArray;
if (value.startsWith("[")) {
String chunks = value.substring(1, value.length()-1);
chunkArray = chunks.split(SEPERATOR_CHAR);
} else {
chunkArray = new String[] {value};
}
for (String chunk : chunkArray) {
try {
Object object = converter.getAsObject(context, component, chunk);
newList.add((T) object);
} catch (IllegalArgumentException e) {
FacesMessage facesMessage = new FacesMessage(FacesMessage.SEVERITY_ERROR, String.format("%s is not a valid search term",value), null);
context.addMessage(null, facesMessage);
}
}
return newList;
}
@Override
public String getAsString(FacesContext context, UIComponent component, Object value) {
if (value instanceof List) {
List<T> conversionList = (List<T>) value;
if (conversionList.isEmpty()) {
return "";
}
StringBuilder sb = new StringBuilder();
sb.append("[");
for (int i=0; i < conversionList.size(); i++ ) {
String string = converter.getAsString(context, component, conversionList.get(i));
sb.append(string);
if (i < conversionList.size() -1) {
sb.append(SEPERATOR_CHAR);
}
}
sb.append("]");
return sb.toString();
} else {
throw new IllegalArgumentException(String.format("Cannot convert object of class %s", value.getClass().getName()));
}
}
}
{code}
was:
A converter is required to serialize an object into the query string for use as a view param. If that object is a list, it would be helpful to make use of an existing converter that converts each individual entry, and has the cumulative result serialized as a string.
The standard CGI syntax represents arrays as multiple values, each with the same key. This may already be supported in JSF, but at the time of writing this, I couldn't find it.
> Create a converter for dealing with lists
> -----------------------------------------
>
> Key: SEAMFACES-141
> URL: https://issues.jboss.org/browse/SEAMFACES-141
> Project: Seam Faces
> Issue Type: Feature Request
> Components: Validation & Conversion
> Reporter: Brian Leathem
>
> A converter is required to serialize an object into the query string for use as a view param. If that object is a list, it would be helpful to make use of an existing converter that converts each individual entry, and has the cumulative result serialized as a string.
> The standard CGI syntax represents arrays as multiple values, each with the same key. This may already be supported in JSF, but at the time of writing this, I couldn't find it.
> Possible implementation (not conformant with the above mentioned CDI standard syntax:
> {code}
> abstract public class ListConverter<T> implements Converter {
> private final Converter converter;
> private final String SEPERATOR_CHAR=":";
> public ListConverter(Converter converter) {
> this.converter = converter;
> }
>
> @Override
> public Object getAsObject(FacesContext context, UIComponent component, String value) {
> List<T> newList = new ArrayList<T>();
> if (value == null || value.isEmpty()) {
> return newList;
> }
> String[] chunkArray;
> if (value.startsWith("[")) {
> String chunks = value.substring(1, value.length()-1);
> chunkArray = chunks.split(SEPERATOR_CHAR);
> } else {
> chunkArray = new String[] {value};
> }
> for (String chunk : chunkArray) {
> try {
> Object object = converter.getAsObject(context, component, chunk);
> newList.add((T) object);
> } catch (IllegalArgumentException e) {
> FacesMessage facesMessage = new FacesMessage(FacesMessage.SEVERITY_ERROR, String.format("%s is not a valid search term",value), null);
> context.addMessage(null, facesMessage);
> }
> }
> return newList;
> }
>
> @Override
> public String getAsString(FacesContext context, UIComponent component, Object value) {
> if (value instanceof List) {
> List<T> conversionList = (List<T>) value;
> if (conversionList.isEmpty()) {
> return "";
> }
> StringBuilder sb = new StringBuilder();
> sb.append("[");
> for (int i=0; i < conversionList.size(); i++ ) {
> String string = converter.getAsString(context, component, conversionList.get(i));
> sb.append(string);
> if (i < conversionList.size() -1) {
> sb.append(SEPERATOR_CHAR);
> }
> }
> sb.append("]");
> return sb.toString();
> } else {
> throw new IllegalArgumentException(String.format("Cannot convert object of class %s", value.getClass().getName()));
> }
> }
> }
> {code}
--
This message is automatically generated by JIRA.
For more information on JIRA, see: http://www.atlassian.com/software/jira
More information about the seam-issues
mailing list