3 Replies Latest reply on Dec 9, 2011 4:54 PM by cdelosrios

    Problem with dynamic columns...

    cdelosrios

      hi, before to continue, let me tell you that I'm a newbie developer using richfaces and I'm facing a problem that I can't solve...so please I need your help!

       

      well, I'll try to be the more specific I can...

       

      this is my result in my db:

       

      result.JPG

       

      What I need is to pass the columns given by dates in columns....something like this (solved using ireport)

       

      report.JPG

      But my problem is trying to put it in a datatable, this is my code with jsp

       

      <rich:dataTable value="#{auditoriaController.beanListAplicacion}" var="item">
                                                        <rich:column>
                                                                  <f:facet name="header">
                                                                            <h:outputText value="Opción" />
                                                                  </f:facet>
                                                                  <h:outputText value="#{item.strTransaccion}"></h:outputText>
                                                        </rich:column>
        
                                                        <rich:columns value="#{auditoriaController.beanListAplicacion}" var="columns" index="ind">
                                          <f:facet name="header">
                                              <h:outputText value="#{columns.daFecRegistro}" />
                                          </f:facet>
                                          <h:outputText value="#{item[ind].intCntAcceso}"/>
                                      </rich:columns>
        
                                                        <rich:column>
                                                                  <f:facet name="header">
                                                                            <h:outputText value="APP" />
                                                                  </f:facet>
                                                                  <h:outputText value="----"></h:outputText>
                                                        </rich:column>
                                      </rich:dataTable>
      
      

       

      And this is my method that populates the grid:

       

       

      public void listarAplicacion(ActionEvent event){
                          log.info("--------------------Debugging AuditoriaController.listarAplicacion-----------------------");
                    setService(auditoriaService);
                    log.info("Se ha seteado el Service");
        
                          SimpleDateFormat sdf2 = new SimpleDateFormat("dd/MM/yyyy");
                          Date daFecIni = getDaFechaIni();
                    String daFechaIni = (daFecIni == null ? "" : sdf2.format(daFecIni));
                    Date daFecFin = getDaFechaFin();
                    String daFechaFin = (daFecFin == null ? "" : sdf2.format(daFecFin));
        
                          HashMap prmtBusq = new HashMap();
                          prmtBusq.put("pDaFecIni",                     daFechaIni);
                          prmtBusq.put("pDaFecFin",                     daFechaFin);
                    
                    ArrayList arrayAplicacion = new ArrayList();
                    ArrayList listaAplicacion = new ArrayList();
                    
                    try {
                              arrayAplicacion = getService().listarAplicacion(prmtBusq);
                          } catch (DaoException e) {
                                    log.info("ERROR  getService().listarAplicacion() " + e.getMessage());
                                    // TODO Auto-generated catch block
                                    e.printStackTrace();
                          }
        
                          log.info("arrayAplicacion.size(): "+arrayAplicacion.size());
                          for(int i=0; i<arrayAplicacion.size(); i++){
                                    Auditoria audit = (Auditoria) arrayAplicacion.get(i);
                                    log.info("ID_EMPRESA = "+audit.getIntIdEmpresa());
                                    log.info("ID_PERSONA = "+audit.getIntIdPersona());
                                    log.info("ID_TRANSACCION = "+audit.getStrIdTransaccion());
                                    log.info("VC_TRANSACCION = "+audit.getStrTransaccion());
                                    log.info("D_FECREGISTRO = "+audit.getDaFecRegistro());
                                    log.info("CNT_ACCESO = "+audit.getIntCntAcceso());
        
                                    listaAplicacion.add(audit);
                                    //List<FechasAuditoria> beanListColumns = new ArrayList<FechasAuditoria>();
                                    /*beanListColumns.add(new FechasAuditoria(audit.getDaFecRegistro()));
                                    listaTran.add(beanListColumns);*/
                          }
                          log.info("listaAplicacion.size(): "+listaAplicacion.size());
                          log.info("listaTran.size(): "+listaTran.size());
                          setBeanListAplicacion(listaAplicacion);
                }
      
      

       

      and finally this is my bean

       

      public class Auditoria {
                private Integer                                                  intIdEmpresa;
                private Integer                                                   intIdPersona;
                private String                                                  strIdTransaccion;
                private String                                                  strTransaccion;
                private String                                                           daFecRegistro;
                private Integer                                                  intCntAcceso;
                private List                                                  lstFechas;
      
      //Getters and Setters
      .
      .
      .
      }
      

       

      I was trying to use use the rich:columns example that exist in the demos but I couldn't do it.

      I can't have a result like I did in my report...I think that I'm making something wrong or maybe everything is wrong. Please help me with this problem...I have many hours trying to solve it and I can't do it.

       

      Sorry for my english.

       

      Thanks.

        • 1. Re: Problem with dynamic columns...
          mcmurdosound

          I'm not sure, if I understand you and your problem completely (using an unknown language as variablenames doesn't make it easier ;-) )....

           

          It's possible to build a datatable with a variable number of columns. I did something similar sometime ago: for each column from a given startdate till an enddate I had to put some date specific values into the cells. (Offtimecalendar)

           

          I used a TreeMap to keep the correct order.

           

          I used c:forEach as an iterator to create the rich:columns:

           

           

          <rich:dataTable value="#{bean.data.keyset().toArray()}" var="user">

           

          <c:forEach items="#{bean.dateList}" var="date">

               <rich:column>

                      #{bean.data.get(user).get(date)}

              ...

          ..

           

          Datamodel:

          bean.data: TreeMap<User, TreeMap<Date, MyData>>

          = "User1" : {01.12.2011 : 100, 02.12.2011: 0, ... , 31.12.2011: 123} ...

           

          bean.dateList = List<Date> {01.12.2011, 02.12.2011, ... 31.12.2011}

           

           

           

          oh, you used "rich:columns". I've never used this one before. I'll have to try them tomorrow at work!

           

          You can replace <c:forEach and <rich:column with <rich:columns I think.

          • 2. Re: Problem with dynamic columns...
            cdelosrios

            I see that is the same functionality and I think that my problem is in my backing bean. I've been trying to addapt the code watched in other posts but the result is not correct.

             

            Help! please!

            • 3. Re: Problem with dynamic columns...
              cdelosrios

              Ok. Finally after many times  trying to solve this f***...problem, it could be solved. It was a little bit more complex than I expected but I could do it and this is my result. I'm posting because maybe somebody could use it or it could be useful hor him/her.

               

              This is my jsp code:

               

              <rich:dataTable value="#{auditoriaController.beanListAplicacion}" var="item">

                        <rich:column>

                                            <f:facet name="header">

                                                                <h:outputText value="Opción" />

                                            </f:facet>

                                            <h:outputText value="#{item.strTransaccion}"></h:outputText>

                        </rich:column>

                        <rich:columns value="#{auditoriaController.beanListColumns}" var="columns"  index="ind">

                        <f:facet name="header">

                                  <h:outputText value="#{columns.strCabecera}" />

                        </f:facet>

                        <h:outputText value="#{item.lstFechas[ind]}"/>

                        </rich:columns>

                                  <rich:column>

                                  <f:facet name="header">

                                                      <h:outputText value="APP" />

                                  </f:facet>

                                  <h:outputText value="----"></h:outputText>

                        </rich:column>

              </rich:dataTable>

              And this is my method that populates the grid:

               

               

              public class AuditoriaController extends GenericController {
              
                        
              
                   
                        private List beanListAplicacion = new  ArrayList(); //List for Rows
                        private List<FechasAuditoria> beanListColumns = new ArrayList<FechasAuditoria>(); //List for Columns
                   .
                   .
                   .
              
              
              
                   public void listarAplicacion(ActionEvent event){
              
                          log.info("--------------------Debugging AuditoriaController.listarAplicacion-----------------------");
              
                            setService(auditoriaService);
              
                            log.info("Se ha seteado el Service");
              
                
                                  SimpleDateFormat sdf2 = new SimpleDateFormat("dd/MM/yyyy");
                                  Date daFecIni = getDaFechaIni();
                            String daFechaIni = (daFecIni == null ? "" : sdf2.format(daFecIni));
                            Date daFecFin = getDaFechaFin();
                            String daFechaFin = (daFecFin == null ? "" : sdf2.format(daFecFin));
                
                                  HashMap prmtBusq = new HashMap();
              
                                  prmtBusq.put("pDaFecIni",                     daFechaIni);
                                  prmtBusq.put("pDaFecFin",                     daFechaFin);
                            
                            ArrayList arrayAplicacion = new ArrayList();
                            ArrayList listaAplicacion = new ArrayList();
                            
                            ArrayList arrayCols = new ArrayList();
                            ArrayList listaCols = new ArrayList();
                            
                            try {
                                      arrayAplicacion = getService().listarAplicacion(prmtBusq);//List of Rows
                                      arrayCols                     = getService().listarCols(prmtBusq);//List of Columns
                                  } catch (DaoException e) {
                                            log.info("ERROR  getService().listarAplicacion() " + e.getMessage());
                                            log.info("ERROR  getService().listarCols() " + e.getMessage());
                                            // TODO Auto-generated catch block
                                            e.printStackTrace();
                                  }
                
                                  ArrayList<String[]> arrayTransacciones = new ArrayList<String[]>();
                                  String strIds = "";
                                  log.info("arrayAplicacion.size(): "+arrayAplicacion.size());
                                  for(int i=0; i<arrayAplicacion.size(); i++){
                                            Auditoria aud = new Auditoria();
                                            aud = (Auditoria)arrayAplicacion.get(i);
                                            String[] strTran = new String[2];
                                            strTran[0] = aud.getStrIdTransaccion();
                                            strTran[1] = aud.getStrTransaccion();
                                            log.info("arrayTransacciones.size(): "+arrayTransacciones.size());
                                            for(int j=0; j<arrayTransacciones.size(); j++){
                                                      String[] strAux = new String[2];
                                                      strAux = (String[]) arrayTransacciones.get(j);
                                                      strIds = strIds + strAux[0];
                                            }
                                            if(!strIds.contains(aud.getStrIdTransaccion())){
                                                      arrayTransacciones.add(strTran);
                                            }
                                  }
                                  log.info("arrayTransacciones.size(): "+arrayTransacciones.size());
                
                                  ArrayList<Auditoria> lsCruce = new ArrayList<Auditoria>();
                                  for(int i=0; i<arrayTransacciones.size(); i++){
                                            String[] strTran = new String[2];
                                            strTran = arrayTransacciones.get(i);
                                            String[] arrayFechas = new String[arrayCols.size()];
                
                                            for(int k=0; k<arrayCols.size(); k++){
                                                      FechasAuditoria fecha = new FechasAuditoria();
                                                      fecha = (FechasAuditoria) arrayCols.get(k);
                                                      arrayFechas[k]="";
                
                                                      log.info("arrayFechas.length: "+arrayFechas.length);
                
                                                      for(int j=0; j<arrayAplicacion.size(); j++){
                                                                Auditoria aud = new Auditoria();
                                                                aud = (Auditoria) arrayAplicacion.get(j);
                
                                                                log.info("strTran[0]: "+strTran[0]);
                                                                log.info("aud.getStrIdTransaccion(): "+aud.getStrIdTransaccion());
                                                                if(strTran[0].equals(aud.getStrIdTransaccion())){
                
                                                                          log.info("aud.getDaFecRegistro(): "+aud.getDaFecRegistro());
                                                                          log.info("fecha.getStrCabecera(): "+fecha.getStrCabecera());
                                                                          if(aud.getDaFecRegistro().equals(fecha.getStrCabecera())){
                                                                                    arrayFechas[k] = ""+aud.getIntCntAcceso();
                                                                          }
                                                                }
                                                      }
                
                                                      log.info("arrayFechas["+k+"]: "+arrayFechas[k]);
                                            }
                
                                            log.info("arrayFechas.length: "+arrayFechas.length);
                                            Auditoria audi = new Auditoria();
                                            audi.setStrTransaccion(strTran[1]);
                                            audi.setLstFechas(arrayFechas);
                                            lsCruce.add(audi);
                                  }
                                  setBeanListAplicacion(lsCruce);
                                  setBeanListColumns(arrayCols);
                        }
              
              
              
              
              
              }
              
              

              and finally this is my bean



              public class Auditoria {
                        private Integer                                                  intIdEmpresa;
                        private Integer                                                   intIdPersona;
                        private String                                                  strIdTransaccion;
                        private String                                                  strTransaccion;
                        private String                                                           daFecRegistro;
                        private Integer                                                  intCntAcceso;
                        private List                                                  lstFechas;
               
              //Getters and Setters
              .
              .
              .
              }
              
              
              
              //Bean for Columns
              
              public class FechasAuditoria {
                        private String                     strCabecera;
              
                        public String getStrCabecera() {
                                  return strCabecera;
                        }
                        public void setStrCabecera(String strCabecera) {
                                  this.strCabecera = strCabecera;
                        }
              }
              
              

               

                 resultado.JPG

               

              I wish this would be helpful for u...thanks.

               

              And again, sorry for my english