14 Replies Latest reply on Feb 6, 2007 7:35 PM by smokingapipe

    SelectItems with objects?

    smokingapipe

      Simple problem: I want to have a selectonemenu where the labels are strings (as usual) and the values are objects. For example, let's say that the user can go to a checkout page and select a shipping address: home, business, or other. Those have labels, but the value is an Address object.

      What's a good way to do this?

      One easy way is to make the value be the Address objects' identifier, and then have the SBSB handle it. But is there a way I can avoid using the SFSB for this conversion?

      I hope my question here makes some sense.

        • 1. Re: SelectItems with objects?
          pmuir

          You should take a look either at SeamSelectItems on the wiki or s:selectItems in 1.1.5 (which is based on SeamSelectItems). The wiki one comes with an EntityConverter (I assume from what you are saying the objects are entities) which should work ootb. The one in 1.1.5 doesn't, but I will package up the entityconverter separate to work with it some point soon.

          • 2. Re: SelectItems with objects?
            smokingapipe

            COOL! That's exactly what I'm talking about. A select menu where the values are entities. This is a pretty common usage but it's not something which is handled well in any of the existing systems (whether JSP, JSF, or even PHP or whatever). If there's a good Seam solution that would be cool.

            I know I should upgrade from 1.1.0 to 1.1.5 but I'm a bit scared to. Is it likely to break anything in mysterious ways?

            Anyway, what is the entityConverter needed for in the s:selectItems?

            I should try all this.

            • 3. Re: SelectItems with objects?
              pmuir

              Wel, the wiki version should work fine with 1.1.0 I think. The entityconverter is there so that you can reference a list of objects, which have @Id fields/methods and uses that as the value to write to the page - take a look at the wiki - it explains it all.

              • 4. Re: SelectItems with objects?
                smokingapipe

                Ok I may go ahead and try that.

                Btw here's the link:

                http://wiki.jboss.org/wiki/Wiki.jsp?page=SeamSelectItemsNewDesign

                • 5. Re: SelectItems with objects?
                  smokingapipe

                  Quick question: I'm using SMPC but my EntityManager is not named entityManager. What do I put in my components.xml to fix that?

                  I'm eager to try this now because it looks like exactly the right way to do this. I guess I will switch to 1.1.5 in the near future so I'll try that also.

                  • 6. Re: SelectItems with objects?
                    pmuir

                    <selectitems:config entity-manager="#{entityManager}" /> - you'll also need to add the namespace for the selectitems prefix.

                    • 7. Re: SelectItems with objects?
                      smokingapipe

                      I tried it like this:

                      <selectitems:config entity-manager="#{smpc}" />
                      
                       <component name="smpc"
                       class="org.jboss.seam.core.ManagedPersistenceContext">
                       <property name="persistenceUnitJndiName">java:/EntityManagerFactories/smpcData</property>
                       </component>
                      


                      and then I got:

                      java.lang.IllegalStateException: entityManager is null
                       org.jboss.seam.framework.EntityQuery.validate(EntityQuery.java:27)
                       org.jboss.seam.framework.EntityQuery$$FastClassByCGLIB$$225925e6.invoke(<generated>)
                       net.sf.cglib.proxy.MethodProxy.invoke(MethodProxy.java:149)
                       org.jboss.seam.intercept.RootInvocationContext.proceed(RootInvocationContext.java:45)
                       org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:55)
                       org.jboss.seam.interceptors.OutcomeInterceptor.interceptOutcome(OutcomeInterceptor.java:23)


                      Any ideas? I tried changing order of things in components.xml etc but I still get this same exception.


                      • 8. Re: SelectItems with objects?
                        smokingapipe

                        Ok, I had a few problems, which I sorted out (I still have a lot of problems but at least these are now sorted).

                        First, if I'm using an unusual name for the entityManager, I need to do this:

                        <framework:entity-query entity-manager="#{smpc}" name="queryStoreList" ejbql="from Store s" />


                        and then I need to have a factory that uses that. Stupid mistakes.

                        Now everything is working as far as displaying the page. What isn't working is that every time I submit the form it fails validation. Not sure why that is. It says: "Value is not a valid option."

                        Any ideas would be appreciated.


                        • 9. Re: SelectItems with objects?
                          smokingapipe

                          I looked at the HTML output and I can see that the option values are being correctly set as the entity IDs. So, now I really don't understand this. Hmm.

                          • 10. Re: SelectItems with objects?
                            pmuir

                            Are you sure you're in a conversation? (You need to be). Either that or try using id-equality (override equals() to check equality on the id).

                            • 11. Re: SelectItems with objects?
                              smokingapipe

                              Yes, that's exactly the problem. I don't know why there was scope="PAGE" in the example line, but I took that out (didn't specify any scope) and now it converts correctly. Now one of my other fields is not converting correctly, but that should be easy to fix, and then hopefully this thing will work end to end. This is the coolest way to do select menus I have ever seen, because it's exactly the way a web developer would like to think about it. I hope the whole thing becomes an integrated part of Seam in a near-future release, so I don't need these extra jars to set up. Ideally the select component could also get its EntityManager from the seam configuration too.

                              • 12. Re: SelectItems with objects?
                                pmuir

                                If you think about it there is no way to automagically guess the entity manager. Seam allows you to specify more than one entity manager (this is necessary for example if you are wanting to do some sort of audit logging). The best we can do is make assumptions, and keep them consistent. So the Application Framework assumes your entitymanager is called entityManager, so does selectitems.

                                I'm still working on the best way to bring it into core. Shortly there will be a version of the converter packaged to work with the s:selectItems - but I've been very busy over the last few days.

                                • 13. Re: SelectItems with objects?
                                  smokingapipe

                                  And it worked. Cool! Except it stored the object in my entity as a serialized object instead of a reference. I think I need to put in a @ManyToOne or whatever because that has nothing to do with the select menu.

                                  • 14. Re: SelectItems with objects?
                                    smokingapipe

                                    And it worked completely. What a cool feature. I'll say again, please integrate this whole thing into Seam in a future release!