7 Replies Latest reply on Oct 20, 2008 6:26 PM by nimo stephan

    Seam 2.1 and EntityConverter.

    nimo stephan Master

      I use Seam 2.1 and need the org.jboss.seam.ui.converter.EntityConverter.


      But I cannot find it in the API..


      I need that for my rich:listShuttle..


      <rich:listShuttle ...
                  converter="org.jboss.seam.ui.converter.EntityConverter">
      <rich:column>
       <h:outputLabel value="#{entity.name}"/>
      </rich:column>
      </rich:listShuttle>




      Does anyone know: where is the EntityConverter?

        • 1. Re: Seam 2.1 and EntityConverter.
          Nicklas Karlsson Master

          Back up one step, I think it's in ui now.

          • 2. Re: Seam 2.1 and EntityConverter.
            nimo stephan Master

            I use jboss-seam-2.1.0-SNAPSHOT and searched the whole api (index)..no class with the name EntityConverter found.


            Have I something missed?


            • 3. Re: Seam 2.1 and EntityConverter.
              Ingo Jobling Master

              I think that there were some issues with entityConverter, maybe it has been pulled?


              We always write our own converters, just implement the JSF Converter interface.  There are  only two methods, getAsObject and getAsString


              getAsObject - read the object from the database using the supplied id


              getAsString - convert the supplied object's id to a string


              Add the Seam annotation @converter(for="my.package.MyClass") and you're good to go.


              All told, it is about 20 lines of code.


              If you want a sample, email me ingo.joblingATvideotron.ca
               

              • 4. Re: Seam 2.1 and EntityConverter.
                Ingo Jobling Master

                If a man speaks in the forest and there is no woman around to hear him, is he still wrong?


                Good one :-)

                • 5. Re: Seam 2.1 and EntityConverter.
                  Pete Muir Master

                  It's always had the id org.jboss.seam.ui.EntityConverter

                  • 6. Re: Seam 2.1 and EntityConverter.
                    nimo stephan Master

                    Hey Ingo,


                    I have integrated the org.jboss.seam.ui.EntityConverter in the rich:listShuttle:



                    <rich:listShuttle sourceValue="#{availablePersons}" targetValue="#{selectedPersons}"
                    var="person" converter="org.jboss.seam.ui.EntityConverter">
                               
                      <rich:column>
                          <h:outputLabel value="#{person.name}"/>
                       </rich:column>          
                               
                     </rich:listShuttle>



                    I get a exception:


                    ..has invalid value expression entity.Person@d4db874e




                    I have found out, that this exception can occur due to incorrect overriding of equals/hashCode.
                    However, my equals/hasCode in the Person-Entity is well implemented.


                    So I assume, that the seams EntityConverter does not work well with rich:listShuttle.


                    I have implemented my own converter in my sessionBean to see whats happen:



                    @Name("myBean")
                    @Scope(CONVERSATION)
                    public class MyBean() {
                    
                    @Factory("availablePersons") 
                    public void myPersonList() {
                    //this objectList is definitly not empty, 
                    //it shows all the persons in the sourceValue 
                    //of my listShuttle successfully
                    ..
                    }
                     
                    public Converter getConvert() {
                    return new Converter() {
                     
                    Person o= new Person();
                     
                    public Object getAsObject(FacesContext context, UIComponent component, String value) throws ConverterException 
                    { 
                                                  
                    if(value == null || value.equals("null")) {return o; }
                     
                    // the output works!               
                    java.lang.System.out.print("Object received: " + value + " Type: " + value.getClass().getName());
                     
                    // this works not, 
                    // as the Method myObjectList returns a null-pointer-exception     
                    for(Person p: this.myPersonList()) {
                                        
                    if (p.getName().equals(value)) {
                                             
                    o= p;
                    }
                                             
                    }
                    return o;
                    }
                     
                    // this works
                    public Object getAsString(...) 
                    {
                    if (value == null) { return null; }
                    
                    System.out.print("getAsString - String recieved was: " + value + " Type: " + value.getClass().getName());
                    Person o = (Person) value;
                    return o.getName();
                    
                    }
                    };
                     
                    }




                    The output:
                    07:41:03,176 INFO  [STDOUT] getAsString - String recieved was: entity.Person@d4db874e Type: entity.Person
                    07:41:06,721 INFO  [STDOUT] getAsObject - Object recieved was: testName Type: java.lang.String




                    The myPersonList() in the same sessionBean is definitly NOT empty. But my Converter cannot access this method..
                    due to my Conversation-Scope..inner/outer class? I have no clue.


                    Should I use the entity.Person@d4db874e and take the substring after '@' to get my entity?


                    I dont know..any ideas ??

                    • 7. Re: Seam 2.1 and EntityConverter.
                      nimo stephan Master

                      okay, the problem is solved. My Entity-Converter does not work, I dont know why.


                      But now I use Seams excellent entity-converter org.jboss.seam.ui.EntityConverter which works well!