Details

      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):

      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()));
              }
          }
      }
      

        Gliffy Diagrams

          Attachments

            Activity

              People

              • Assignee:
                Unassigned
                Reporter:
                bleathem Brian Leathem
              • Votes:
                0 Vote for this issue
                Watchers:
                0 Start watching this issue

                Dates

                • Created:
                  Updated: