4 Replies Latest reply on Mar 4, 2010 10:59 AM by Patrick Reis

    Problem retrieving values from a row on datatable to modal

    Patrick Reis Newbie
      Hi guys i am a new user from seam framework and i want to now how to retrieve data to a modal panel from the datable. I now there is some information over the web about is, buit i have try 100 thins i dosent work. so there is my bean;



      @Name("usersList")
      public class UsersList implements Serializable{
      /**
            *
            */
           private static final long serialVersionUID = -8191663213732129820L;


          @Logger private Log log;
          public Users userSelected= new Users();
          private Collection<Users> usersList = new ArrayList<Users>(0); 
          @In StatusMessages statusMessages;
           private Set <Integer>keys;
         
          public void usersList()
          {
               try {
                    
                    EntityManagerFactory emf =Persistence.createEntityManagerFactory("projectseam2");          
                    EntityManager em = emf.createEntityManager();         
                    String query = "select x from Users x where x.name like '%" + value +"%'";
                    usersList = em.createQuery(query).getResultList();
                    em.close();     
                    } catch (Exception e) {
                         // TODO Auto-generated catch block
                         statusMessages.add(getFullExceptionMessage(e));
                    }
          }

          public Collection<Users> getUserlist()
          {
                return usersList;

          }


          public void getCurrentRow(Users user) {
           
           }
          public Set<Integer> getKeys() {
                return keys;
           }

           public void setKeys(Set<Integer> keys) {
                this.keys = keys;
           }

         
          public static String getFullExceptionMessage(Exception e) 
          { 
              String message = e.getLocalizedMessage();  
               Throwable t = e.getCause(); 
               while(t != null)  
               { 
                  message += "\n" + t.getMessage();  
                  t = t.getCause(); 
               }        
               return message; 
         }

           
      }
      and my usersList.xmhtml
      <!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
          "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
      <ui:composition xmlns="http://www.w3.org/1999/xhtml"
           xmlns:s="http://jboss.com/products/seam/taglib"
           xmlns:ui="http://java.sun.com/jsf/facelets"
           xmlns:f="http://java.sun.com/jsf/core"
           xmlns:h="http://java.sun.com/jsf/html"
           xmlns:rich="http://richfaces.org/rich"
           xmlns:a="http://richfaces.org/a4j" template="layout/template.xhtml">

           <ui:define name="body">
                <h:form id="usersListForm">

                     <rich:panel>
                          <f:facet name="header">usersList</f:facet>


                          <ui:define name="label">Search:</ui:define>
                          <h:inputText id="value" value="#{usersList.value}" />
                          <h:commandButton id="usersList" value="Search"
                               action="#{usersList.usersList}" for="table" />
                     
           
      <h:outputText value="#{usersList.value}" />

                          <div style="clear: both" />

                     </rich:panel>

                     <div class="actionButtons">
                
                     <rich:dataTable
                          value="#{usersList.userlist}" var="user" rows="5" rowKeyVar="row"
                           id="table" ajaxKeys="#{usersList.keys}"
                           >
                          <f:facet name="header">
                               <h:outputText value="Utilizadores" />
                          </f:facet>
                          <rich:column>
                               <f:facet name="header">
                                    <h:outputText value="Nome" />
                               </f:facet>
                               <h:outputText value="#{user.name}" id="name" />
                          </rich:column>
                          <rich:column>
                               <f:facet name="header">
                                    <h:outputText value="Username" />
                               </f:facet>
                               <h:outputText value="#{user.username}" id="username" />
                          </rich:column>
                          <rich:column>
                               <f:facet name="header">
                                    <h:outputText value="Password" />
                               </f:facet>
                               <h:outputText value="#{user.password}" id="password" />
                          </rich:column>
                     <rich:column>
                     
                     <f:facet name="header">
                                    <h:outputText value="actions" />
                               </f:facet>
                     <a:commandLink value="editar"
                      onclick="#{rich:component('modal')}.show()">
            <f:setPropertyActionListener value="#{user}"
                                target="#{usersList.currentRow(user)}" />
           </a:commandLink>


                     </rich:column>

                          <f:facet name="footer">
                          

                               <rich:datascroller id="ds1" page="1" rerender="table" />
                          </f:facet>
                     </rich:dataTable></div>

                </h:form>
                
                <rich:modalPanel id="modal" minHeight="200" minWidth="450"
                     autosized="false" moveable="false">
                     <f:facet name="header">
                          <h:outputText value="Realmente Deseja Excluir?" />
                     </f:facet>
                     <h:panelGrid columns="2">
                     <h:outputText value="#{users.name}" />
                          <a:commandButton value="Sim"
                               actionListener="#{usersList.excluir}"
                               reRender="table"
                               oncomplete="#{rich:component('modal')}.hide();" />
                          
      <a:commandButton value="Nao" onclick="#{rich:component('modal')}.hide(); return false" />
                     </h:panelGrid>
                </rich:modalPanel>

           </ui:define>

      </ui:composition>


      and my class:

      package org.domain.projectseam2.entity;

      import javax.persistence.Column;
      import javax.persistence.Entity;
      import javax.persistence.Id;
      import javax.persistence.Table;
      import org.hibernate.validator.Length;
      import org.hibernate.validator.NotNull;
      import org.jboss.seam.annotations.Name;

      /**
      * Users generated by hbm2java
      */
      @Name("users")
      @Entity
      @Table(name = "users", catalog = "jbossformacao")
      public class Users implements java.io.Serializable {

           private int idutilzadores;
           private String username;
           private String password;
           private String name;

           public Users() {
           }

           public Users(int idutilzadores, String username, String password,
                     String name) {
                this.idutilzadores = idutilzadores;
                this.username = username;
                this.password = password;
                this.name = name;
           }

           @Id
           @Column(name = "idutilzadores", unique = true, nullable = false)
           public int getIdutilzadores() {
                return this.idutilzadores;
           }

           public void setIdutilzadores(int idutilzadores) {
                this.idutilzadores = idutilzadores;
           }

           @Column(name = "username", nullable = false, length = 45)
           @NotNull
           @Length(max = 45)
           public String getUsername() {
                return this.username;
           }

           public void setUsername(String username) {
                this.username = username;
           }

           @Column(name = "password", nullable = false, length = 45)
           @NotNull
           @Length(max = 45)
           public String getPassword() {
                return this.password;
           }

           public void setPassword(String password) {
                this.password = password;
           }

           @Column(name = "name", nullable = false, length = 45)
           @NotNull
           @Length(max = 45)
           public String getName() {
                return this.name;
           }

           public void setName(String name) {
                this.name = name;
           }

      }
      .. i dont now what is the problem because i get the data wheni call the method userlist in userlist.java and then i can execute any actions with the link on the datable. It dosent call the  usersList.currentRow on
            <f:setPropertyActionListener value="#{user}"
                                target="#{usersList.currentRow(user)}" /> .

      i try a system.out.print in this method but it never works.

      please if you have any ideia, please tell-me

      .

      Best regards

      Patrick reis
        • 1. Re: Problem retrieving values from a row on datatable to modal
          Thomas Küstermann Newbie

          I think you misunderstood the function of f:setPropertyActionListener (It's all about getter and setters).


          UsersList.java:


          @Name("usersList")
          public class UsersList implements Serializable {
          // ...
          
              private Users currentUser;
          
              public Users getCurrentUser() {
                  return this.current;
              }
          
              public void setCurrentUser(Users user) {
                  this.currentUser = user;
              }
          
          // ...
          }
          



          usersList.xhtml should be:


          <a:commandLink value="editar" reRender="modal"
            oncomplete="#{rich:component('modal')}.show()">
              <f:setPropertyActionListener value="#{user}" target="#{usersList.currentUser}" />
          </a:commandLink>




          <rich:modalPanel id="modal" ...>
              <!-- ... -->
              <h:outputText value="#{usersList.currentUser.name}" />
              <!-- ... -->
          </rich:modalPanel>
          



          If you want to call an action out of your modal panel, you should place a form into it.

          • 3. Re: Problem retrieving values from a row on datatable to modal
            Samuel Doyle Apprentice

            This is what both @DataModel and @DataModelSelection were designed for.
            See Clickable lists in Seam for a great explanation.


            • 4. Re: Problem retrieving values from a row on datatable to modal
              Patrick Reis Newbie

              Hi guys, i tried used Datamodel and DatamodelSelection but now i have some other problems, firt i cannot the new instance of the classe user, i must put the values manually, like this,

              public void selectUser(Users current){
                       
                       currentUser.setIdutilzadores(current.getIdutilzadores());
                       currentUser.setName(current.getName());
                       currentUser.setUsername(current.getUsername());
                       currentUser.setPassword(current.getPassword());
                       
                  }


              second when i try to update or remove, on the the datable it chenge me the firt row in the datable with current row selected, but if i refresh the list it becommes update or deleted.
              i put the code for have a better perpective.


              my bean:




               */
              @Name("user")
              @Entity
              @Table(name = "users", catalog = "jbossformacao")
              public class Users implements java.io.Serializable {
              
                   private int idutilzadores;
                   private String username;
                   private String password;
                   private String name;
              
                   public Users() {
                   }
              
                   public Users(int idutilzadores, String username, String password,
                             String name) {
                        this.idutilzadores = idutilzadores;
                        this.username = username;
                        this.password = password;
                        this.name = name;
                   }
              
                   @Id
                   @Column(name = "idutilzadores", unique = true, nullable = false)
                   public int getIdutilzadores() {
                        return this.idutilzadores;
                   }
              
                   public void setIdutilzadores(int idutilzadores) {
                        this.idutilzadores = idutilzadores;
                   }
              
                   @Column(name = "username", nullable = false, length = 45)
                   @NotNull
                   @Length(max = 45)
                   public String getUsername() {
                        return this.username;
                   }
              
                   public void setUsername(String username) {
                        this.username = username;
                   }
              
                   @Column(name = "password", nullable = false, length = 45)
                   @NotNull
                   @Length(max = 45)
                   public String getPassword() {
                        return this.password;
                   }
              
                   public void setPassword(String password) {
                        this.password = password;
                   }
              
                   @Column(name = "name", nullable = false, length = 45)
                   @NotNull
                   @Length(max = 45)
                   public String getName() {
                        return this.name;
                   }
              
                   public void setName(String name) {
                        this.name = name;
                   }
              }
              
              my form.java
              
              @Name("listsUsers")
              public class ListsUsers
              {
                  
                   EntityManagerFactory emf =Persistence.createEntityManagerFactory("project5");       
                   @Logger private Log log;
              
                  @In StatusMessages statusMessages;
              
                  private String value;
                  
                  @DataModel(scope = ScopeType.PAGE)
                   private List<Users> users; 
                  
                  @DataModelSelection
                  @Out(required=false)
                  
                  private Users currentUser;
                  public void listsUsers()
                  {
                      // implement your business logic here
                       try {
                             EntityManager em = emf.createEntityManager();    
                             String query = "select x from Users x where x.name like '%" + value +"%'";
                             setUsers(em.createQuery(query).getResultList());
                             em.close();     
                             emf.close();
                        } catch (Exception e) {
                             // TODO Auto-generated catch block
                             statusMessages.add(getFullExceptionMessage(e));
                        }
                  }
                  
                  public void selectUser(Users current){
                       
                       currentUser.setIdutilzadores(current.getIdutilzadores());
                       currentUser.setName(current.getName());
                       currentUser.setUsername(current.getUsername());
                       currentUser.setPassword(current.getPassword());
                       
                  }
                  
                  public void saveUser(){
                       EntityManager em = emf.createEntityManager();  
                       
                       Users existingUser =em.find(Users.class, currentUser.getIdutilzadores());
                       existingUser.setName(currentUser.getName());
                       existingUser.setUsername(currentUser.getUsername());
                       existingUser.setPassword(currentUser.getPassword());
                       em.close();
                       
              
                  }
                  
                  public void deleteUser(){
                       EntityManager em = emf.createEntityManager();  
                       Users existingUser =em.find(Users.class, currentUser.getIdutilzadores());
                       em.remove(existingUser);
                       em.close();
                       
                  }
              
                  // add additional action methods
                
                  @Length(max = 10)
                  public String getValue()
                  {
                      return value;
                  }
                
                  public void setValue(String value)
                  {
                      this.value = value;
                  }
              
                   public static String getFullExceptionMessage(Exception e)  
                   {  
                        String message = e.getLocalizedMessage();   
                        Throwable t = e.getCause();  
                        while(t != null)   
                        {  
                             message += "\n" + t.getMessage();   
                             t = t.getCause();  
                        }         
                        return message;  
                   }
              
                   public void setCurrentUser(Users currentUser) {
                        
                        this.currentUser = currentUser;
                   }
              
                   public Users getCurrentUser() {
                        return currentUser;
                   }
              
                   public void setUsers(List<Users> users) {
                        this.users = users;
                   }
              
                   public List<Users> getUsers() {
                        return users;
                   }
              }
              
              the form:
              
              <!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
                  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
              <ui:composition xmlns="http://www.w3.org/1999/xhtml"
                  xmlns:s="http://jboss.com/products/seam/taglib"
                  xmlns:ui="http://java.sun.com/jsf/facelets"
                  xmlns:f="http://java.sun.com/jsf/core"
                  xmlns:h="http://java.sun.com/jsf/html"
                  xmlns:rich="http://richfaces.org/rich"
                  xmlns:a="http://richfaces.org/a4j"
                  template="layout/template.xhtml">
              
              <ui:define name="body">
              
                  <h:form id="listsUsersForm">
              
                      <rich:panel>
                          <f:facet name="header">listsUsers</f:facet>
              
                          <s:decorate id="valueField" template="layout/edit.xhtml">
                              <ui:define name="label">Value</ui:define>
                              <h:inputText id="value" required="false"
                                           value="#{listsUsers.value}"/>
                          </s:decorate>
              
                          <div style="clear:both"/>
              
                      </rich:panel>
              
                      <div class="actionButtons">
                          <h:commandButton id="listsUsers" value="listsUsers"
                                  action="#{listsUsers.listsUsers}"/>
                      </div>
                      
                      <rich:dataTable id="tableUsers" var="tbUsers" ajaxKeys="#{editUser.keys}"
              
                                  value="#{listsUsers.users}">
                                  <h:column>
                                       <f:facet name="header">Name</f:facet>
                                       <h:outputText value="#{tbUsers.name}" />
                                  </h:column>
                                  <h:column>
                                       <f:facet name="header">Username</f:facet>
                                       <h:outputText value="#{tbUsers.username}" />
                                  </h:column>
                                  <h:column>
                                       <f:facet name="header">Password</f:facet>
                                       <h:outputText value="#{tbUsers.password}" />
                                  </h:column>
                                  <h:column>          
                                       <f:facet name="header">Name</f:facet>
                                  <a:commandLink  reRender="modal" action="#{listsUsers.selectUser(tbUsers)}"
                              oncomplete="#{rich:component('modal')}.show()">
                                  <h:graphicImage value="img/ico_edit.png" style="border: 0; heigth=25px;width : 21px; height : 18px;"/>
                                   <f:setPropertyActionListener target="#{listsUsers.currentUser}" value="#{tbUsers}" />
                          </a:commandLink>  
                                  </h:column>
                                       <h:column>          
                                       <f:facet name="header">Name</f:facet>
                                  <a:commandLink  reRender="modal" action="#{listsUsers.selectUser(tbUsers)}"
                              oncomplete="#{rich:component('modalDelete')}.show()">
                                  <h:graphicImage value="img/delete.png"/>
                                   <f:setPropertyActionListener target="#{listsUsers.currentUser}" value="#{tbUsers}" />
                          </a:commandLink>  
                                  </h:column>
                             </rich:dataTable>
              
                  </h:form>
                   <rich:modalPanel id="modal" minHeight="100" minWidth="300"
                             height="150" width="300">
                             <h:form>
                                  
                                   <table width="100%">
                              <tbody>
                                  <tr>
                                  <td>Nome:</td>
                                  <td>
                                  <h:inputText id="value2" value="#{listsUsers.currentUser.name}" required="false"/>          
                                  </td>
                                  </tr>
                                   <tr>
                                  <td>Username:</td>
                                  <td>
                                  <h:inputText id="value3" value="#{listsUsers.currentUser.username}" required="false"/>          
                                  </td>
                                  </tr>
                                   <tr>
                                  <td>Password:</td>
                                  <td>
                                  <h:inputText id="value4" value="#{listsUsers.currentUser.password}" required="false"/>          
                                  </td>
                                  </tr>
                                
                                  <tr>
                                  <td colspan="2">
                                  <h:commandButton id="btnGuardar" value="Guardar"
                                        action="#{listsUsers.saveUser}" />
              
                                  <a:commandButton value="cancelar"
                                       onclick="#{rich:component('modal')}.hide(); return false"/>
                                  </td>
                                  
                                  </tr>
                                  </tbody>
                                  </table>
                             </h:form>
                             
                        </rich:modalPanel>
                        
                             <rich:modalPanel id="modalDelete" minHeight="100" minWidth="300"
                             height="150" width="300">
                             <h:form>
                                  
                                   <table width="100%">
                              <tbody>
                                  <tr>
                                   <td colspan="2">
                                  Are you sure, do you want to delete these user?
                                  </td>
                                  </tr>
                                  
                                
                                  <tr>
                                  <td colspan="2">
                                  <h:commandButton id="btnDelete" value="Delete"
                                        action="#{listsUsers.deleteUser}" reRender="tableUsers"/>
              
                                  <a:commandButton value="cancelar"
                                       onclick="#{rich:component('modalDelete')}.hide(); return false"/>
                                  </td>
                                  
                                  </tr>
                                  </tbody>
                                  </table>
                             </h:form>
                             
                        </rich:modalPanel>
              </ui:define>
              
              </ui:composition>