Capture array values to display in a panelGrid (PrimeFaces)

3

I do a project using Serverfaces and Primefaces, and I am faced with this situation. I am printing the searches of my page (of hotels) in a dataGrid. In turn, if I select any hotel, it prints me some information I want in a dialog, which contains a panelGrid (both components are from Primefaces). This is my class where I post the information:

    package Clases;

public class ResultElement {
    private String nombre, imagen, localidad, id;
    private String tipoHabitacion[], alojamiento[], imagenCuarto[];
    private int personas[], disponible[];
    private String precio[];
    public ResultElement(String nombre, String imagen, String localidad,String id,
                         String tipoHabitacion[], String alojamiento[], String imagenCuarto[],
                         int personas[], int disponible[], String precio[]){
        this.nombre=nombre;
        this.imagen=imagen;
        this.localidad=localidad;
        this.id = id;
        this.tipoHabitacion = tipoHabitacion;
        this.alojamiento = alojamiento;
        this.imagenCuarto = imagenCuarto;
        this.personas = personas;
        this.disponible = disponible;
        this.precio = precio;
    }
    /**
     * @return the nombre
     */
    public String getNombre() {
        return nombre;
    }

    /**
     * @param nombre the nombre to set
     */
    public void setNombre(String nombre) {
        this.nombre = nombre;
    }

    /**
     * @return the imagen
     */
    public String getImagen() {
        return imagen;
    }

    /**
     * @param imagen the imagen to set
     */
    public void setImagen(String imagen) {
        this.imagen = imagen;
    }

    /**
     * @return the localidad
     */
    public String getLocalidad() {
        return localidad;
    }

    /**
     * @param localidad the localidad to set
     */
    public void setLocalidad(String localidad) {
        this.localidad = localidad;
    }

    /**
     * @return the id
     */
    public String getId() {
        return id;
    }

    /**
     * @param id the id to set
     */
    public void setId(String id) {
        this.id = id;
    }

    /**
     * @return the tipoHabitacion
     */
    public String[] getTipoHabitacion() {
        return tipoHabitacion;
    }

    /**
     * @param tipoHabitacion the tipoHabitacion to set
     */
    public void setTipoHabitacion(String[] tipoHabitacion) {
        this.tipoHabitacion = tipoHabitacion;
    }

    /**
     * @return the alojamiento
     */
    public String[] getAlojamiento() {
        return alojamiento;
    }

    /**
     * @param alojamiento the alojamiento to set
     */
    public void setAlojamiento(String[] alojamiento) {
        this.alojamiento = alojamiento;
    }

    /**
     * @return the imagenCuarto
     */
    public String[] getImagenCuarto() {
        return imagenCuarto;
    }

    /**
     * @param imagenCuarto the imagenCuarto to set
     */
    public void setImagenCuarto(String[] imagenCuarto) {
        this.imagenCuarto = imagenCuarto;
    }

    /**
     * @return the personas
     */
    public int[] getPersonas() {
        return personas;
    }

    /**
     * @param personas the personas to set
     */
    public void setPersonas(int personas[]) {
        this.personas = personas;
    }

    /**
     * @return the disponible
     */
    public int[] getDisponible() {
        return disponible;
    }

    /**
     * @param disponible the disponible to set
     */
    public void setDisponible(int disponible[]) {
        this.disponible = disponible;
    }

    /**
     * @return the precio
     */
    public String[] getPrecio() {
        return precio;
    }

    /**
     * @param precio the precio to set
     */
    public void setPrecio(String precio[]) {
        this.precio = precio;
    }

    public void pruebaCuarto(){
        for(int i=0;i<3; i++){
            System.out.println(imagenCuarto[i]);
        }
    }
}

Note the last method. Just print the image strings by console now. This will be key so they can understand my problem. Each object has 6 arrays as an attribute, and all will always have 3 as a total index.

Well, I want to print now that data in the panelGrid, but the following happens:

The information of the details, is always repeated for all hotels. It's as if I'm loading the details of the last hotel (the one on the right) for all the other hotels, and the weirdest thing is that if I check by console, the information is loading well:

Highlight the last part in red because it is the only information that my page (the one from the last hotel) wants to show me. Well, the code where I charge everything is this:

   public void cargarPortada(){   
        try{
            int i = 0;
            String habitacion[] = new String[3];
            String alojamiento[] = new String[3];
            String imagen[] = new String[3];
            int prsonas[] = new int[3];
            int disponible[] = new int[3];
            String precio[] = new String[3];
            ResultSet rs = GestorDB.getConsulta("SELECT HOTELES.IDHOTELES, HOTELES.NOMBRE AS A, HOTELES.IMGHOTEL, LOCALIDADES.NOMBRE AS B FROM HOTELES INNER JOIN LOCALIDADES"
                + " ON LOCALIDADES.IDLOCALIDADES = HOTELES.LOCALIDADES_IDLOCALIDADES");
            ResultSet rp;
            while(rs.next()){
               i = 0;
               rp = GestorDB.getConsulta("SELECT TIPO_HABITACION, ALOJAMIENTO, IMGHAB, PERSONAS, N_DISPONIBLE, PRECIO_NOCHE"
                       + " FROM TARIFAS WHERE HOTELES_IDHOTELES='" + rs.getString("IDHOTELES") + "'");
                   while(rp.next()){
                         habitacion[i] = rp.getString("TIPO_HABITACION");
                         alojamiento[i] = rp.getString("ALOJAMIENTO");
                         imagen[i] = rp.getString("IMGHAB");
                         prsonas[i] = rp.getInt("PERSONAS");
                         disponible[i] = rp.getInt("N_DISPONIBLE");
                         precio[i] = rp.getString("PRECIO_NOCHE");
                         i++;
                    }
               ResultElement res = new ResultElement(rs.getString("A"), rs.getString("IMGHOTEL"),rs.getString("B"),rs.getString("IDHOTELES"),habitacion,
               alojamiento,imagen,prsonas,disponible,precio);
               res.pruebaCuarto(); //AQUI COMPRUEBO SI SE ESTA CARGANDO BIEN
               displayList.add(res);              
               for(int c = 0; i < 2; c++){
                   habitacion[c] = null; alojamiento[c] = null; imagen[c] = null;
                   prsonas[c] = 0; precio[c] = ""; disponible[c] = 0;
               }
            }
        } catch (SQLException ex) {
            Logger.getLogger(Buscador.class.getName()).log(Level.SEVERE, null, ex);
        }       
    }

I read somewhere that I could call the position of an array by PrimeFaces simply by using the brackets "[]", and that's what I did ... it's the way I'm doing calling everything:

    <?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
      xmlns:h="http://xmlns.jcp.org/jsf/html"
      xmlns:f="http://xmlns.jcp.org/jsf/core"
      xmlns:p="http://primefaces.org/ui"
      xmlns:c="http://xmlns.jcp.org/jsp/jstl/core">
    <f:metadata>
        <f:viewAction action="#{buscador.cargarPortada()}"/>
    </f:metadata>
    <body>

        <ui:composition template="./templates/base.xhtml">

            <ui:define name="content">
                <h:form id="form2">
                    <p:dataGrid var="hotel" value="#{buscador.display}" columns="3" rows="9" paginator="true" id="hoteles" paginatorTemplate="{CurrentPageReport}  {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink}">
                        <p:panel header="#{hotel.nombre}" style="text-align:center;width: 230px;height:139px;">
                            <h:panelGrid columns="1" style="width:100%;">
                                <p:commandLink update=":form2:detalles" oncomplete="PF('hotDialog').show()" title="Ver detalles">
                                     <p:graphicImage name="images/#{hotel.imagen}" style="width: 160px; height: 70px;"/>
                                     <f:setPropertyActionListener value="#{hotel}" target="#{buscador.selectedResult}"/>
                                </p:commandLink>
                                <h:outputText value="#{hotel.localidad}"/>
                            </h:panelGrid>
                        </p:panel>
                    </p:dataGrid>

                    <p:dialog header="Detalles del Hotel" widgetVar="hotDialog" modal="true" showEffect="fade" hideEffect="explode" resizable="false">
                       <p:outputPanel id="detalles" style="text-align: center;">
                          <p:panelGrid columns="4" rendered="#{not empty buscador.selectedResult}" columnClasses="label,value">
                            <f:facet name="header">
                                <p:graphicImage name="images/#{buscador.selectedResult.imagen}" width="350" height="350"/>
                             </f:facet>
                             <h:outputText value="Nombre del Hotel: " style="font-weight: bold; border-bottom: 1px solid maroon;"/>
                             <h:outputText value=" "/><h:outputText value="#{buscador.selectedResult.nombre}"/><h:outputText value=" "/>
                             <h:outputText value="Localidad: " style="font-weight: bold; border-bottom: 1px solid maroon;"/>
                             <h:outputText value=" "/><h:outputText value="#{buscador.selectedResult.localidad}"/><h:outputText value=" "/>
                             <h:outputText value="Habitaciones: " style="font-weight: bold; border-bottom: 1px solid maroon;"/>
                             <p:graphicImage name="images/#{buscador.selectedResult.imagenCuarto[0]}" width="80" height="50" style="border:1px solid black;"/>
                             <p:graphicImage name="images/#{buscador.selectedResult.imagenCuarto[1]}" width="80" height="50" style="border:1px solid black;"/>
                             <p:graphicImage name="images/#{buscador.selectedResult.imagenCuarto[2]}" width="80" height="50" style="border:1px solid black;"/>
                             <h:outputText value="Tipo de Habitacion: " style="font-weight: bold; border-bottom: 1px solid maroon;"/>
                             <h:outputText value="#{buscador.selectedResult.tipoHabitacion[0]}"/>
                             <h:outputText value="#{buscador.selectedResult.tipoHabitacion[1]}"/>
                             <h:outputText value="#{buscador.selectedResult.tipoHabitacion[2]}"/>
                             <h:outputText value="Alojamiento: " style="font-weight: bold; border-bottom: 1px solid maroon;"/>
                             <h:outputText value="#{buscador.selectedResult.alojamiento[0]}"/>
                             <h:outputText value="#{buscador.selectedResult.alojamiento[1]}"/>
                             <h:outputText value="#{buscador.selectedResult.alojamiento[2]}"/>
                             <h:outputText value="Disponibles: " style="font-weight: bold; border-bottom: 1px solid maroon;"/>
                             <h:outputText value="#{buscador.selectedResult.disponible[0]}"/>
                             <h:outputText value="#{buscador.selectedResult.disponible[1]}"/>
                             <h:outputText value="#{buscador.selectedResult.disponible[2]}"/>
                             <h:outputText value="Precio por noche: " style="font-weight: bold; border-bottom: 1px solid maroon;"/>
                             <h:outputText value="#{buscador.selectedResult.precio[0]}$"/>
                             <h:outputText value="#{buscador.selectedResult.precio[1]}$"/>
                             <h:outputText value="#{buscador.selectedResult.precio[2]}$"/>
                          </p:panelGrid>
                       </p:outputPanel>
                    </p:dialog>
                </h:form>
            </ui:define>                      
        </ui:composition>

    </body>
</html>

What could it be ...?

UPDATE: complete controller code "search engine":

package Beans;

import Clases.ResultElement;
import javax.inject.Named;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import java.sql.ResultSet;
import Database.GestorDB;
import java.sql.SQLException;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
 *
 * @author SEMINARIO
 */
@Named(value = "portada")
@ViewScoped
public class Buscador {
    private String fechaE, fechaS, localidad;
    private int personas;
    private List<ResultElement> displayList; 
    private ResultElement selectedResult;
    private GestorDB gestor;
    public Buscador() {displayList = new ArrayList<>();gestor = new GestorDB();}

    public String getFechaE() {return fechaE;}
    public void setFechaE(String fechaE) {this.fechaE = fechaE;}
    public String getFechaS() {return fechaS;}
    public void setFechaS(String fechaS) {this.fechaS = fechaS;}
    public String getLocalidad() {return localidad;}
    public void setLocalidad(String localidad) {this.localidad = localidad;}
    public int getPersonas() {return personas;}
    public void setPersonas(int personas) {this.personas = personas;}
    public List<ResultElement> getDisplay() {return displayList;}
    public void setDisplay(ArrayList<ResultElement> listaResultado) {displayList = listaResultado;}  
    public ResultElement getSelectedResult(){return selectedResult;}
    public void setSelectedResult(ResultElement r){selectedResult = r;}
    public void agregarResultado(ResultElement resultado){displayList.add(resultado);}   
    public void limpiarResultados(){displayList.clear();}  

    public void cargarPortada(){   
        try{
            int i = 0;
            String habitacion[] = new String[3];
            String alojamiento[] = new String[3];
            String imagen[] = new String[3];
            int prsonas[] = new int[3];
            int disponible[] = new int[3];
            String precio[] = new String[3];
            ResultSet rs = gestor.getConsulta("SELECT HOTELES.IDHOTELES, HOTELES.NOMBRE AS A, HOTELES.IMGHOTEL, LOCALIDADES.NOMBRE AS B FROM HOTELES INNER JOIN LOCALIDADES"
                + " ON LOCALIDADES.IDLOCALIDADES = HOTELES.LOCALIDADES_IDLOCALIDADES");
            ResultSet rp;
            while(rs.next()){
               i = 0;
               rp = gestor.getConsulta("SELECT TIPO_HABITACION, ALOJAMIENTO, IMGHAB, PERSONAS, N_DISPONIBLE, PRECIO_NOCHE"
                       + " FROM TARIFAS WHERE HOTELES_IDHOTELES='" + rs.getString("IDHOTELES") + "'");
                   while(rp.next()){
                         habitacion[i] = rp.getString("TIPO_HABITACION");
                         alojamiento[i] = rp.getString("ALOJAMIENTO");
                         imagen[i] = rp.getString("IMGHAB");
                         prsonas[i] = rp.getInt("PERSONAS");
                         disponible[i] = rp.getInt("N_DISPONIBLE");
                         precio[i] = rp.getString("PRECIO_NOCHE");
                         i++;
                    }
               ResultElement res = new ResultElement(rs.getString("A"), rs.getString("IMGHOTEL"),rs.getString("B"),rs.getString("IDHOTELES"),habitacion,
               alojamiento,imagen,prsonas,disponible,precio);
               res.pruebaCuarto();
               displayList.add(res);              
               for(int c = 0; i < 2; c++){
                   habitacion[c] = null; alojamiento[c] = null; imagen[c] = null;
                   prsonas[c] = 0; precio[c] = ""; disponible[c] = 0;
               }
            }
        } catch (SQLException ex) {
            Logger.getLogger(Buscador.class.getName()).log(Level.SEVERE, null, ex);
        }       
    }
}
    
asked by TwoDent 27.07.2017 в 23:10
source

1 answer

0

I would like to use the Dialog Framework, to abstract the visualization functionality of the room detail and to do the search based on the data that passes as a parameter to the RequestContext, favoring more the organization and refresh the view of the dialogue be force, which I imagine should be the problem, the dialogue you have is possibly not refreshing its content and stays with the first search, the Dialog Framework use example is this:

link

If you definitely want to continue using the setPropertyActionListener, maybe this post will help you.

link

    
answered by 01.08.2017 в 22:38