1 Reply Latest reply on Sep 19, 2008 3:47 PM by Jony Santos

    rich:suggestion

    Thiago Pereira Newbie

      Hello people, I'm new in Seam...


      Well, I'm trying to do an example of suggestiobbox through a mysql database, but I can't


      I got a good result working with Array, so I have added some contacts and it works fine. When I tried to work with a database, I got some problems...


      Someone has an example of suggestionbox with database?


      Below follows my code.




      ContatosList.xhtml



      <!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"
                      template="layout/template.xhtml">
                             
      <ui:define name="body">
          
          <h:messages globalOnly="true" styleClass="message" id="globalMessages"/>
          
          <h:form id="contatosSearch" styleClass="edit">
          
              <rich:simpleTogglePanel label="Search Contacts" switchType="ajax">
              
                  <s:decorate template="layout/display.xhtml">
                      <ui:define name="label">nome</ui:define>
                      <h:inputText id="text" value="#{contatosList.contatos.nome}" size="50"/>
                      <rich:suggestionbox id="suggestionBox"
                                                              for="text"
                                                              fetchValue="#{contatosList.contatos.nome}"
                                                              suggestionAction="#{contatosList.autocomplete}"
                                                              height="250"
                                                              width="250"
                                                              var="contato">
                      <h:column>
                              <h:graphicImage value="#{contatosList.contatos.foto}" />
                      </h:column>
                      <h:column>
                              <h:outputText value="#{contatosList.contatos.nome}" />
                      </h:column>
                      <h:column>
                              <h:outputText value="#{contatosList.contatos.email}" />
                      </h:column>
                      
                      </rich:suggestionbox>
                  </s:decorate>
              
              </rich:simpleTogglePanel>
              
              <div class="actionButtons">
                  <h:commandButton id="search" value="Search" action="/ContatosList.xhtml"/>
              </div>
              
          </h:form>
          
          <rich:panel>
              <f:facet name="header">Contatos search results</f:facet>
          <div class="results" id="contatosList">
      
          <h:outputText value="The contatos search returned no results." 
                     rendered="#{empty contatosList.resultList}"/>
                     
          <rich:dataTable id="contatosList" 
                      var="contatos"
                    value="#{contatosList.resultList}" 
                 rendered="#{not empty contatosList.resultList}">
              <h:column>
                  <f:facet name="header">
                      <s:link styleClass="columnHeader"
                                   value="id #{contatosList.order=='id asc' ? messages.down : ( contatosList.order=='id desc' ? messages.up : '' )}">
                          <f:param name="order" value="#{contatosList.order=='id asc' ? 'id desc' : 'id asc'}"/>
                      </s:link>
                  </f:facet>
                  #{contatos.id}
              </h:column>
              <h:column>
                  <f:facet name="header">
                      <s:link styleClass="columnHeader"
                                   value="nome #{contatosList.order=='nome asc' ? messages.down : ( contatosList.order=='nome desc' ? messages.up : '' )}">
                          <f:param name="order" value="#{contatosList.order=='nome asc' ? 'nome desc' : 'nome asc'}"/>
                      </s:link>
                  </f:facet>
                  #{contatos.nome}
              </h:column>
              <h:column>
                  <f:facet name="header">
                      <s:link styleClass="columnHeader"
                                   value="email #{contatosList.order=='email asc' ? messages.down : ( contatosList.order=='email desc' ? messages.up : '' )}">
                          <f:param name="order" value="#{contatosList.order=='email asc' ? 'email desc' : 'email asc'}"/>
                      </s:link>
                  </f:facet>
                  #{contatos.email}
              </h:column>
              <h:column>
                  <f:facet name="header">action</f:facet>
                  <s:link view="/#{empty from ? 'Contatos' : from}.xhtml" 
                         value="Select" 
                            id="contatos">
                      <f:param name="contatosId" 
                              value="#{contatos.id}"/>
                  </s:link>
              </h:column>
          </rich:dataTable>
      
          </div>
          </rich:panel>
          
          <div class="tableControl">
            
              <s:link view="/ContatosList.xhtml" 
                  rendered="#{contatosList.previousExists}" 
                     value="#{messages.left}#{messages.left} First Page"
                        id="firstPage">
                <f:param name="firstResult" value="0"/>
              </s:link>
              
              <s:link view="/ContatosList.xhtml" 
                  rendered="#{contatosList.previousExists}" 
                     value="#{messages.left} Previous Page"
                        id="previousPage">
                  <f:param name="firstResult" 
                          value="#{contatosList.previousFirstResult}"/>
              </s:link>
              
              <s:link view="/ContatosList.xhtml" 
                  rendered="#{contatosList.nextExists}" 
                     value="Next Page #{messages.right}"
                        id="nextPage">
                  <f:param name="firstResult" 
                          value="#{contatosList.nextFirstResult}"/>
              </s:link>
              
              <s:link view="/ContatosList.xhtml" 
                  rendered="#{contatosList.nextExists}" 
                     value="Last Page #{messages.right}#{messages.right}"
                        id="lastPage">
                  <f:param name="firstResult" 
                          value="#{contatosList.lastFirstResult}"/>
              </s:link>
              
          </div>
          
          <s:div styleClass="actionButtons" rendered="#{empty from}">
              <s:button view="/ContatosEdit.xhtml"
                          id="create" 
                       value="Create contatos">
                  <f:param name="contatosId"/>
              </s:button>
          </s:div>
          
      </ui:define>
      
      </ui:composition>
      




      Contatos.java




      package org.domain.AutoCompleteDB.entity;
      
      import java.util.ArrayList;
      import java.util.List;
      
      import javax.persistence.Column;
      import javax.persistence.Entity;
      import javax.persistence.GeneratedValue;
      import static javax.persistence.GenerationType.IDENTITY;
      import javax.persistence.Id;
      import javax.persistence.Table;
      
      
      @Entity
      @Table(name = "contatos", catalog = "thiago")
      public class Contatos implements java.io.Serializable {
      
              /**
               * 
               */
              private static final long serialVersionUID = 1L;
              private Long id;
              private String nome;
              private String email;
              private String foto;
      
              public Contatos() {
              }
      
              public Contatos(String nome, String email) {
                      this.nome = nome;
                      this.email = email;
              }
      
              @Id
              @GeneratedValue(strategy = IDENTITY)
              @Column(name = "id", unique = true, nullable = false)
              public Long getId() {
                      return this.id;
              }
      
              public void setId(Long id) {
                      this.id = id;
              }
      
              @Column(name = "nome")
              public String getNome() {
                      return this.nome;
              }
      
              public void setNome(String nome) {
                      this.nome = nome;
              }
      
              @Column(name = "email")
              public String getEmail() {
                      return this.email;
              }
      
              public void setEmail(String email) {
                      this.email = email;
              }
              
              public String getFoto() {
                      return foto;
              }
              public void setFoto(String foto) {
                      this.foto = foto;
              }
      
              public static List procurarContatos(String nome) {
                      ArrayList contatosArray = new ArrayList();
      
                      if (nome.toUpperCase().startsWith("T")) {
                              Contatos a = new Contatos();
                              a.getNome();
                              a.getEmail();
                              a.setFoto("http://tbn0.google.com/images?q=tbn:g8tttVQJ0x5oaM:http://www.portalregional.rj.gov.br/flagra/imagens/festas/325_festival_queijos_e_vinhos_nfcc/65x65/foto325_22.jpg");
                              contatosArray.add(a);
      
                              Contatos a2 = new Contatos();
                              a2.getNome();
                              a2.getEmail();
                              a2.setFoto("http://tbn0.google.com/images?q=tbn:cQ46BkIb3ObU9M:http://www.portalregional.rj.gov.br/flagra/imagens/festas/402_planeta_pop_rock_farwest/65x65/foto402_10.jpg");
                              contatosArray.add(a2);
                      }
                      return contatosArray;
              }
      }
      




      ContatosList.java



      package org.domain.AutoCompleteDB.session;
      
      import org.domain.AutoCompleteDB.entity.*;
      import org.jboss.seam.annotations.In;
      import org.jboss.seam.annotations.Name;
      import org.jboss.seam.framework.EntityQuery;
      
      import java.util.ArrayList;
      import java.util.List;
      import java.util.Arrays;
      
      @Name("contatosList")
      public class ContatosList extends EntityQuery {
      
              private static final String[] RESTRICTIONS = {
                              "lower(contatos.nome) like concat(lower(#{contatosList.contatos.nome}),'%')",
                              "lower(contatos.email) like concat(lower(#{contatosList.contatos.email}),'%')", };
      
              private Contatos contatos = new Contatos();
      
              @Override
              public String getEjbql() {
                      return "select contatos from Contatos contatos";
              }
      
              @Override
              public Integer getMaxResults() {
                      return 25;
              }
      
              public Contatos getContatos() {
                      return contatos;
              }
      
              @Override
              public List<String> getRestrictions() {
                      return Arrays.asList(RESTRICTIONS);
              }
              
              @In(required=false) Contatos contato;
              List alunos;
      
              public ContatosList(){
                      this.alunos = new ArrayList();
              }
      
              public List autocomplete(Object o){
                return Contatos.procurarContatos(o.toString());
              }
              
              public Contatos getContato() {
                      return contato;
              }
      
              public void setAluno(Contatos contato) {
                      this.contato = contato;
              }
      
      }
      



      Thanks