1 Reply Latest reply on Mar 18, 2009 12:44 PM by frer

    Serializable Converter

    frer

      Hi,


      I'm having an issue building a SerializableCOnverter.  I recently changed my ids of my application from  String to Serializable (like Hibernate).


      My application suddenly stopped working with the error message No converter for java.io.Serializable.  Therefore I created the following converter


      public class SerializableConverter implements Converter, Serializable {
          private static final long serialVersionUID = 3944270147489071787L;
      
          @Override
          public Object getAsObject(FacesContext context, UIComponent component, String asString) {
              try {
                  return new Long(Long.parseLong(asString));
              } catch (NumberFormatException e) {
                  return asString;
              }
          }
      
          @Override
          public String getAsString(FacesContext context, UIComponent component, Object asOperator) {
              if(!(asOperator instanceof Serializable)) {
                  throw new ConverterException("Wrong object type");
              }
              return asOperator.toString();
          }
      }
      



      As you can see it is not very brilliant ;)  If the object is recognized as a long it parses it, otherwise it returns a string.  I thought, most of my ids meet that rule so would it be satisfactory.


      But then I realized that some of my string ids could be parsed as longs 1 or 10, etc.


      Is there any way that I can avoid this problem?  Is there a better way to write this converter?  Is there a way to force the type in my xhtml?


      THanks for your help,


      Francois


        • 1. Re: Serializable Converter
          frer

          In case this can help someone help me: the reason I use serializable is that I have a common interface for all my entities (POJOs) which has the following methods:


              public Serializable getId();
              public void setId(Serializable id);
          



          For example, it can be implemented like this:


              private String id;
              public String getId() {
                  return this.id;
              }
              public void setId(java.io.Serializable value) {
                  this.id = (String) value;
              }
          



          Contrarily to the getter, I cannot put String in the received value setter.


          All this to say that when seam tries to serialize my entity id, it is not able to convert it because it doesn't seem to know the type.


          I don't explicitely call the id in my jsf page but obviously seam needs it.


          Here is an example of a jsf file that fails:


                      <h:form>
                          <rich:tabPanel switchType="ajax">
                              <rich:tab label="#{messages['org.mdarad.framework.resources.form.tabs.general']}" immediate="true">
                                  <table class="attributes">
                                      <tbody class="attributes-tbody">
                                          <tr class="attribute" id="loginRole-humanReadableKey">
                                              <td id="loginRole-humanReadableKey-label-container" class="label-container">
                                                  <label class="label required" id="loginRole-humanReadableKey-label">
                                                      <h:outputText value="#{messages['ca.inukshuk.system.authentication.LoginRole.attributes.humanReadableKey']}"/>
                                                  </label>
                                              </td>
                                              <td id="loginRole-humanReadableKey-edition-container" class="edition-container">
                                                                  <h:inputText value="#{formBean.humanReadableKey}" id="loginRole-humanReadableKey-value" required="true"/>
                                                                  <rich:message for="loginRole-humanReadableKey-value" styleClass="message" infoClass="success" errorClass="error" warnClass="warning"/>
                                              </td>
                                          </tr>
                                      </tbody>
                                  </table>
                              </rich:tab>
                          </rich:tabPanel>
                      </h:form>
          



          If any more detail is needed please let me know.


          Francois