9 Replies Latest reply on Sep 15, 2006 3:44 AM by Denis Karpov

    Multiple roles and accessing the DataModel

    zzzz8 Newbie

      I'd like access a JSF DataModel from my JSP using EL. Normally, this is a very easy thing to when one doesn't use multiple roles. But in this case, I'm assigning multiple roles to a component and I can't seem to figure out the correct syntax for accessing the DataModel.

      For example, here's a snippet of my source code:

      @Name("component1")
      @Role(name="component2")
      @Stateful
      public class Test implements TestLocal {
       @DataModel
       List<Item> list;
      
       @DataModelSelection
       Item item;
      }


      What is the correct EL for accessing the DataModel list associated with the component1 role and the component2 role?

      I've tried:

      #{component1.list}
      #{component2.list}


      However, when I do this, I don't seem to be getting the DataModel, but rather the actual List itself. This is evidenced by the fact that a method in the class that uses the DataModelSelection is always stuck at pointing to the first item in the List list...

      I've also tried modifying the source code to annotate with DataModel on the getter method:

      List<Item> list;
      
      @DataModel
      public List<Item> getList() {
       return list;
      }


      Also, is there a bug with using the DataModelSelection and DataModelSelectionIndex annotations in the same class?

        • 1. Re: Multiple roles and accessing the DataModel
          zzzz8 Newbie

          I hate to move this thread up (and it's probably bad etiquette), but I would really appreciate some help here. I'm really at a lost here and I certainly don't want to explicitly wrap the List object in a DataModel object... I don't think it should take too long to respond either - please tell me if I'm doing something stupid... :) Please help! Thanks!

          • 2. Re: Multiple roles and accessing the DataModel
            Raja Master

             

            "zzzz8" wrote:

            However, when I do this, I don't seem to be getting the DataModel, but rather the actual List itself. This is evidenced by the fact that a method in the class that uses the DataModelSelection is always stuck at pointing to the first item in the List list...


            Can you elaborate on that bit, especially the "first item in the List". The way it is, your List will get wrapped into a ListDataModel before its sent out to the view and unwrapped on its way back.


            • 3. Re: Multiple roles and accessing the DataModel
              Denis Karpov Newbie

              You should use it like this #{list}, because when you use @DataModel Seam outjected wrapper into the current context with name of the property (list in your case)

              Another way you should wrap it by yourself, only then you can use it the way #{YourBean.YourModel}

              • 4. Re: Multiple roles and accessing the DataModel
                Gavin King Master

                 

                "denis-karpov" wrote:
                You should use it like this #{list}, because when you use @DataModel Seam outjected wrapper into the current context with name of the property (list in your case)


                Right.

                • 5. Re: Multiple roles and accessing the DataModel
                  zzzz8 Newbie

                  It would be nice to change the Seam spec such that one can apply the DataModel annotation to the getter method of the list so that one can use the DataModel in multiple roles without explicitly wrapping the list (in the source code) in a DataModel object. I guess I'm suggesting this because I'm quite sure I will run into this problem in the future (and I just hate explicitly wrapping the list in a DataModel).

                  Again, thanks for all the help.

                  • 6. Re: Multiple roles and accessing the DataModel
                    Gavin King Master

                    You can use @DataModel on a getter method.

                    • 7. Re: Multiple roles and accessing the DataModel
                      zzzz8 Newbie

                       

                      "gavin.king@jboss.com" wrote:
                      You can use @DataModel on a getter method.


                      Hi Gavin,

                      I understand you can use the DataModel annotation on the getter method, but if you have two instances of this component "alive" at the same time, with one component associated with the default component name and the other with another role, then it would not work - because the outjected context DataModel context variable cannot be associated with two roles at the same time - i.e. the client cannot distinguish if the list DataModel context variable belongs to the component1 role or the component2 role. This is the limitation I was referring to and something I hope can be dealt with in the future.

                      • 8. Re: Multiple roles and accessing the DataModel
                        eekboom Newbie

                        I think the only way to do this is to create a getter that returns the already wrapped field and then use the "parent" components name , something like

                         public DataModel getListModel() {
                         return new ListDataModel(list);
                        

                        then #{component1.listModel} and #{component2.listModel} should return the appropriate models.

                        And yes, there's a bug with selection and index in the same class:
                        http://jira.jboss.org/jira/browse/JBSEAM-270

                        • 9. Re: Multiple roles and accessing the DataModel
                          Denis Karpov Newbie

                          In this case you can use:

                          yourBean.getYourModel.getRowData();
                          yourBean.getYourModel.getRowIndex();
                          


                          But you should not create DataModel each time in a getter.
                          I use it something like this:
                          private DataModel dm = null;
                          
                           public DataModel getDetails() {
                           if (dm==null){
                           dm = new ListDataModel();
                           }
                           return dm;
                           }
                          
                           private void syncDetails() {
                           Client o = getObject();
                           if (o!=null){
                           getDetails().setWrappedData(o.getDocuments());
                           }
                           }
                          
                           private IdentityCard getSelectedDocument() {
                           return (IdentityCard)getDetails().getRowData();
                           }
                          

                          Frankly, I do not like
                          @DataModel
                          @DataModelSelection
                          @DataModelSelectionIndex