2 Replies Latest reply on Feb 10, 2010 5:13 PM by minateeseam

    DataModelSelection related problems

    minateeseam

      I am new to seam and used @DataModel and @DataModelSelection for first time.
      I want to share a couple of my findings about @DataModelSelection as mentioned bellow,


      1.I have a data table in my xhtml as follows,




      <rich:dataTable value=#{deviceList} var=”device”……>
              <rich:column>
                      <f:facet …>Serial Number</f:facet>
      <a4j:commandLink action=”deviceActionBean.populateDeviceDetails” value=”#{device.serialNr}” />
              </rich:column>
              <rich:column>
                      <f:facet …>Model Name</f:facet>
                      ……
              </rich:column>
              <rich:column>
                      <f:facet …>Part Number</f:facet>
                      <h:outputText value=”#{deviceActionBean.partNumber}” />
              </rich:column>
      
      </rich:dataTable>
      
      <h:outputText value=”#{ deviceActionBean.selectedDevice }” />
      
      



      And my corresponding Java class is,




      @Name(“deviceActionBean”)
      @Scope(PAGE)
      Public class DeviceActionBean {
              
      @DataModel
              private List<Device> deviceList;
      
              @DataModelSelection
              private Device selectedDevice;
      
              public void populateDeviceDetails() {
                      System.out.println(selectedDevice.serialNr);
              }
              
              public String getPartNumber() {
                      …. Business logic ….
                      return partNumber;
              }
      
              public Device getSelectedDevice () {
                      System.out.println(selectedDevice.serialNr);
                      return  selectedDevice;
              }
      }
      



      When I click on the commandlink of a particular row then the action method populateDeviceDetails is being invoked. In the action method I am getting the correct value of the selectedDevice which is annotated with @DataModelSelection. But after that when control comes to the getter method of selectedDevice then the value of selectedDevice is always coming the value of the last row of the dataTable.
      This problem ate my head a lot. After one day struggle I found the actual problem. It is like this, I am calling another method as getPartNumber to display part number for every device in the dataTable.



      <rich:column>
                      <f:facet …>Part Number</f:facet>
                      <h:outputText value=”#{deviceActionBean.partNumber}” />
      </rich:column>
      


      So, BijectionInterceptor again does injection and outjection for the method getPartNumber. At this time the value of selectedDevice is changed as the selectedIndex of DataModel changes. And this method getPartNumber is called for every row, consequently the final value of selectedDevice is always last row of the dataTable.


      So, this scenario forces me to conclude that if we are using @DataModelSelection then we should not suppose to use any other method in the dataTable. In other words, no method should be called, after the action method of the link or button in that dataTable has been invoked. But if you want to invoke some other methods then you have to use @BypassInterceptor annotation for those methods otherwise you will loose the selected value. But use of @BypassInterceptor annotation will not be the convenient approach if your method really requires injected values to execute the business logic :(


      2.@DataModelSelection does not work if the link doesn’t have any action method as BijectionInterceptor is not being invoked which does the injection of data model selection.


      So, I didn’t get any such advantage of using @DataModelSelection. Rather I changed my code like this,



      <rich:dataTable value=#{deviceList} var=”device”……>
              <rich:column>
                      <f:facet …>Serial Number</f:facet>
      <a4j:commandLink action=”deviceActionBean.populateDeviceDetails(device)” value=”#{device.serialNr}” />
              </rich:column>
      ......
      
      @Name(“deviceActionBean”)
      @Scope(PAGE)
      Public class DeviceActionBean {
              
      @DataModel
              private List<Device> deviceList;
      
              private Device selectedDevice;
      
              public void populateDeviceDetails(Device selectedDevice) {
                      selectedDevice= selectedDevice;
              }
      
      


      I passed the device object to my action method as parameter.


      Any suggestions about my findings for @DataModelSelection would be appreciated. Please correct me if I am wrong.


      Thanks in advance :)


      Janaki.

        • 1. Re: DataModelSelection related problems

          Hi


          I've got the same problem. I use entityquery as my base class, it implements @DataModel and @DataModelSelection, but it doesn't work. DataModelSelection always returs first row of the table. I found that someones knows the solution here:
          http://yourmitra.wordpress.com/2008/03/27/does-seam-datamodelselection-always-returns-the-first-row-from-the-datamodel/
          But i don't understand what he says :)


          Anyway your solution does not work for me. Maybe i make some mistakes. Could you share hole source ?

          • 2. Re: DataModelSelection related problems
            minateeseam

            Hi,


            First of all very sorry for late reply. I had gone through the link provided by you,


            http://yourmitra.wordpress.com/2008/03/27/does-seam-datamodelselection-always-returns-the-first-row-from-the-datamodel/


            It says that access the DataModel through its name in xhtml not as an attribute. For example, in my above code,


            @Name(“deviceActionBean”)
            @Scope(PAGE)
            Public class DeviceActionBean {
                    
            @DataModel
                    private List<Device> deviceList;
            
                    @DataModelSelection
                    private Device selectedDevice;
            
                    public void populateDeviceDetails() {
                            System.out.println(selectedDevice.serialNr);
                    }
                    
                    public String getPartNumber() {
                            …. Business logic ….
                            return partNumber;
                    }
            
                    public Device getSelectedDevice () {
                            System.out.println(selectedDevice.serialNr);
                            return  selectedDevice;
                    }
            }
            


            I dont have any getter method for the dataModel and
            I am accessing the data model in the xhtml through its name like this


            <rich:dataTable value=#{deviceList} var=”device”……>
                    ..................
            
            </rich:dataTable>



            Not as bean attribute


            #{deviceActionBean.deviceList}



            Because @DataModel annotation automatically outject the list to the corresponding context so we can access it through its name.


            If your problem has not yet solved then you can share your code and I will try my best to solve your problem.


            Thanks
            Janaki