5 Replies Latest reply on Nov 21, 2011 7:45 PM by Tim Evers

    Injecting one of many objects to a variable

    Kannan Gonka Newbie

      Hi Everyone, I have a complex scenario (which might be easy in Seam).  I am new to Seam.  Need your help.


      I have around 400 different Items in my application.  Each Item has different attributes.  I will have to design a JSF page for each of the items.  Hence I will have around 400 JSF pages. 


      However beyond the page and possibly validations, other layers are common and do not change as I am planning to store these as XML in an RDBMS. 


      So, I define 400 Entities and name them as item1, item2, item3.... item400 using the @Name annotation given by Seam.


      The user has the option to choose N items and create them using my application.  He will however create those N items one by one.


      My question is this: How do I Inject the user selected / data entered item into my action or session bean.  The 400 items do not have a super class (of course apart from Object which all Java classes extend.  Also, each of the 400 entities have different names.  So, how do I inject the particular user entered item into one session bean instance variable.


      For example,


      @In
      public Object item


      will not work as I need to be able to inject any one of the 400 objects into this particular Session Bean instance variable.


      Any suggestions or inputs would be really appreciated.


      Thanks.


      Kannan

        • 1. Re: Injecting one of many objects to a variable
          omid pourhadi Apprentice

          Hi,


          if i understand correctly in this case u need create instance dynamically for everyItem 


          instead of using @In use Component.getInstance(class, true);


          public void persist100Entity(){
          Item1 item1 = Component.getInstance(Item1.class, true);
          Item1 item2 = Component.getInstance(Item2.class, true);
          
          persist(item1);
          persist(item2);
          
          
          }



          • 2. Re: Injecting one of many objects to a variable
            Kannan Gonka Newbie
            Thanks Omid.  In this case, I hope that as far as the JSF page goes, I can still use {#item1.att1} etc where item1 is the @Name of the Item1 class and att1 is an attribute in that.

            So, the flow goes like this:
            1.  From the JSF page, Seam creates an instance of Item1 at the specified context and stores the screen data
            2.  The session bean or entity bean can use Component.getInstance(Item1.class, true) so that seam injects the screen data into the session / entity bean for further processing.

            Please confirm if my understanding is correct.

            • 3. Re: Injecting one of many objects to a variable
              Kannan Gonka Newbie
              Hi Omid,

              One problem I will face with the approach you have mentioned is that, I need to know the correct class to instantiate.  Out of the 400 items, the user might have just created 1 or 2 items.  I should only instantiate those two items.  Is there a way Seam can help make this job easier? 

              For example, can I have @Name for all these 400 items to be the same (Eg. @Name="Item") because the user anyway will work on one item at a time.  Will having the same name for all items create an issue?

              What are the other options we have?

              Thanks.

              <blockquote>
              _omid pourhadi wrote on Nov 09, 2011 01:41:_<br/>

              Hi,

              if i understand correctly in this case u need create instance dynamically for everyItem 

              instead of using @In use Component.getInstance(class, true);

              `public void persist100Entity(){
              Item1 item1 = Component.getInstance(Item1.class, true);
              Item1 item2 = Component.getInstance(Item2.class, true);

              persist(item1);
              persist(item2);


              }`


              </blockquote>

              Click HELP for text formatting instructions. Then edit this text and check the preview.
              • 4. Re: Injecting one of many objects to a variable
                Daniel Chaves Newbie

                Hi,


                Have you try de @Role annotation ?

                • 5. Re: Injecting one of many objects to a variable
                  Tim Evers Master

                  Seems like a rather odd scenario you have there and I'm really not sure I have any idea what you are trying to describe. But...something like this?


                  If the user selects that they want to create items X, Z, AA, CC you store these types (classes) in a list on a session bean.


                  Then your process will just loop through the list and utilize the Component.getInstance(list.get(index), true) to instantiate one of the appropriate entities.