2 Replies Latest reply on Dec 2, 2012 7:11 PM by pepelara

    Map<Integer, Boolean> for selectBooleanCeckbox

    pepelara

      I  have a xhtml file and a managed bean where I have defined Map<Integer, Boolean>

      to use it in the view layer to support a selectBooleanCeckbox nested inside a dataTable.

       

      This Map is common for all the displayed Cliente and what I really need is a

      Map<Cliente, Map<Integer, Boolean>> where every Cliente has its corresponding

      Map<Integer, Boolean>.

       

      The actual problem is the Map throws a NullPointerException when I move to another page

      in the dataTable.

       

      I do not know the way to iterate a Map<Cliente, Map<Integer, Boolean>> in the view layer.

       

      Here is the xhtml file with the simple Map,

       

      <c:forEach items="#{adminCarritoBean.clientes}" var="map">
         <c:set value="#{0}" var="cont"/>
         <c:forEach items="#{map.keySet}" var="cliente">
            <rich:dataTable value="#{map.get(cliente)}" var="carrito"            iterationStatusVar="it" rows="5">
               <h:column>
                  <f:facet name="header">
                     <h:outputText value="Select" />
                 </f:facet>
                 <h:selectBooleanCheckbox value="#{adminCarritoBean.selectedIds[carrito.id]}" />
               </h:column>
               ------------------
               -----------------
               <f:facet name="footer">
                  <rich:dataScroller page="#{adminCarritoBean.page[cont]}" />
               </f:facet>
            </rich:dataTable>
            <c:set value="#{cont + 1}" var="cont"/>
         </c:forEach>
      </c:forEach>
      

       

      and here is the bean code,

       

      private Map<Integer, Boolean> selectedIds;
      private List<Carrito> selectedDataList;    
      private List<Map<Cliente, List<Carrito>>> clientes;
      
      public void eliminar() {
              selectedDataList = new ArrayList<Carrito>();
              
              for (Map<Cliente, List<Carrito>> cliente: clientes) {
                  Set<Cliente> keys = cliente.keySet();
                  for(Iterator<Cliente> it = keys.iterator(); it.hasNext();) {
                      Cliente clte = it.next();
                      List<Carrito> carritos  = cliente.get(clte);
                      for(Carrito dataItem: carritos) {
                          if(selectedIds.get(dataItem.getId()).booleanValue()) {
                              selectedDataList.add(dataItem);
                              selectedIds.remove(dataItem.getId()); // Reset.
                          }
                      }
                  }
              }
              
              for(Carrito crto: selectedDataList) {
                  carritoDAO.eliminar(crto);
              }
              
              selectedIds = new HashMap<Integer, Boolean>();
              loadListClientes();
      }
      

       

      The line selectedIds.get(dataItem.getId()).booleanValue() throws that exception.

       

      How can I do it?

      Any idea?

       

      Best regards,

      Jose

        • 1. Re: Map<Integer, Boolean> for selectBooleanCeckbox
          pepelara

          Here is my code,

           

          private Map<Cliente, Map<Long, Boolean>> selectedIds;
          private List<Carrito> selectedDataList;
          
          public void eliminar() {
                  selectedDataList = new ArrayList<Carrito>();
                  
                  Set<Cliente> clientesKeys = clientes.keySet();
                  for(Iterator<Cliente> it = clientesKeys.iterator(); it.hasNext();) {
                      Cliente clte = it.next();
                      Map<Long, Boolean> map = selectedIds.get(clte);
                      List<Carrito> carritos  = clientes.get(clte);
                      for(Carrito carrito: carritos) {
                          Boolean itemChecked = map.get(carrito.getId());
                          if((itemChecked != null) && itemChecked.booleanValue()) {
                              selectedDataList.add(carrito);
                              map.remove(carrito.getId()); // Reset.
                          }
                      }
                  }
                  
                  if(selectedDataList.size() > 0) {
                      for(Carrito crto: selectedDataList) {
                          carritoDAO.eliminar(crto);
                      }
                  }
                  
                  selectedIds = new HashMap<Cliente, Map<Long, Boolean>>();
                  loadListClientes();
          }
          

           

          I have added the line Boolean itemChecked = map.get(carrito.getId()) that let me check a null pointer case

          and changed the code to add the Map<Cliente, Map<Long, Boolean>> as selectedIds that was what I wanted.

           

          Here is the view,

           

          <ui:repeat value="#{adminCarritoBean.clientes.entrySet().toArray()}" var="cliente"
               varStatus="current" id="repeat">     
               <c:set value="#{adminCarritoBean.selectedIds.get(cliente.key)}" var="mapIds"/>
               <c:set value="#{0}" var="cont"/>
               <rich:dataTable value="#{cliente.value}" var="crto"
                       iterationStatusVar="it" rows="5">            
                       <rich:column>
                                <f:facet name="header">Referencia</f:facet>
                                <h:outputText value="#{crto.referencia}" />
                       </rich:column>
                       ---------------------
                       ---------------------
                       <rich:column>
                               <f:facet name="header">Select</f:facet>
                               <h:selectBooleanCheckbox value="#{mapIds[crto.id]}" />
                       </rich:column>
                       <f:facet name="footer">
                               <rich:dataScroller page="#{adminCarritoBean.page[cont]}" />
                       </f:facet>
               </rich:dataTable>
               <c:set value="#{cont + 1}" var="cont"/>
          </ui:repeat>
          

           

          This works but exists an issue with the pagination of the table that even working it throws an exception,

           

          ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/richfaces-electrodomesticos]]

          (http-localhost-127.0.0.1-8081-6) facturasPendientesForm:repeat:0:j_idt74:j_idt102: Error while proccessing

          the requested information: java.lang.ClassCastException

           

          Any suggestion should be really appreciated.

           

          Regards,

          Jose

          1 of 1 people found this helpful
          • 2. Re: Map<Integer, Boolean> for selectBooleanCeckbox
            pepelara

            Is it possible that exists a bug in rich:dataScroller when you use

            more than one rich:dataTable in the same jsf page?

             

            I was trying to deploy two tables in one jsf page with a parameterized

            page attribute in the dataScroller and I was getting ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/richfaces-electrodomesticos]]

            (http-localhost-127.0.0.1-8081-6) facturasPendientesForm:repeat:0:j_idt74:j_idt102: Error while processing

            the requested information: java.lang.ClassCastException.

             

            At the same time the pagination of the tables did not work in some cases.

             

            Excuse me if I am wrong.

            Jose

             

            El mensaje fue editado por: Jose Alvarez de Lara