4 Replies Latest reply on Jan 10, 2009 11:29 AM by sanghakanwar

    Multiple delete not working

    sanghakanwar

      Hi,


      We have done a seam-gen on the entities and customized the generated code to provide for a multiple delete option on the List page.


      Problem is that it works fine if we have one session open on that page. But if another session(new browser) is opened it starts failing.


      1) Open list page ( displays say 3 entities (1,2,3) ) on two diff browsers
      2) Select entity 2 on second browser and delete it.
      3) Now select the same entity on the first browser and try to delete it.
      4) It deletes the 3'rd entity.


      code files:




      @Name("barringList")
      public class BarringList extends EntityQuery {
           @Out(scope=ScopeType.SESSION, required=false)
           Map<Barring, Boolean> findSelections;
           
           private List <Barring> barringList;
           Logger log = Logger.getLogger(BarringList.class);
           private List deleteList = null;
           private boolean flag = false;
           private static final String[] RESTRICTIONS = {
                     "lower(barring.profileName) like concat(lower(#{barringList.barring.profileName}),'%')",
                      };
      
           private Barring barring = new Barring();
           
           private static final String EJBQL = "select barring from Barring barring";
      
      
            public BarringList(){
                      setEjbql(EJBQL);
                      setRestrictionExpressionStrings(Arrays.asList(RESTRICTIONS));
                      //setMaxResults(25);
            }
           
           
           @In(create=true) EntityHome<Barring> barringHome;
           public void delete(){
               int i = 0;
               while(deleteList.size() > i){ 
                    log.debug("deleting the barring profile"+deleteList.get(i));
                   Barring ba = (Barring)deleteList.get(i);
                   barringHome.setInstance(ba);
                   barringHome.remove();
                   barringList.remove(deleteList.get(i));
                  findSelections.remove(deleteList.get(i));
                    i++;
               }
              super.addFacesMessage("Successfully deleted the profiles");
             
          }
            public void deleteList()
              {   
                  // System.out.println("inside deleteList");
                   deleteList = new ArrayList();
                  for (Barring item : barringList) {
                      Boolean selected = findSelections.get(item);
                      if (selected != null && selected) {
                       //   System.out.println("inside selected" +item.getProfileName());
                           deleteList.add(item);
                                          }
                  }
              delete();
              }
      
            @Override
           public List getResultList() {
                     // TODO Auto-generated method stub
                     findSelections = new HashMap<Barring, Boolean>();
                     barringList = super.getResultList();
                     return barringList;
                }
            
           
      
           @Override
           public Integer getMaxResults() {
                return ProfileUtils.PAGE_SIZE;
                
           }
      
           public Barring getBarring() {
                return barring;
           }
      




      The xhtml file -




      <!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">
                      
           <link href="stylesheet/theme.css" rel="stylesheet" type="text/css" /> 
           <script  language="javascript" src="scripts/tree.js" />
           
           <script language="javascript">
               
               function check()
               {
                    for(i=0; i&lt;document.forms["profileResultForm"].elements.length; i++)
                    {
                         elm = document.forms["profileResultForm"].elements[i];
                         if ( elm.type == 'checkbox')
                         {
                              if ( elm.checked )
                              {
                                   return confirm("Are you sure you want to delete selected profiles?");
                              }
                         }
                    }
                    alert("No Profile selected for deleting!!!");
                    return false;
               }
               
               function selectAllCB(checkVal)
               {
                             
                    for(i=0; i&lt;document.forms["profileResultForm"].elements.length; i++)
                    {
                         elm = document.forms["profileResultForm"].elements[i];
                         if ( elm.type == 'checkbox')
                              if ( elm.disabled == false )
                                   elm.checked = checkVal;
                    }
                    
               }
               
          </script>
        <body onload="loadLeftFrame()">
       
          <h:messages globalOnly="true" styleClass="message" id="globalMessages"/>
          
          <h:form id="barringSearch" styleClass="edit">
          
              <rich:simpleTogglePanel label="Barring search parameters" switchType="ajax">
              
                  <s:decorate template="layout/display.xhtml">
                      <ui:define name="label">Profile Name</ui:define>
                      <h:inputText id="profileName" value="#{barringList.barring.profileName}"/>
                  </s:decorate>
      
              </rich:simpleTogglePanel>
              
              <div class="actionButtons">
                   <h:commandButton id="search" value="Search" action="/BarringList.xhtml" styleClass="button"/>
              </div>     
                             
          </h:form>
          
          <rich:panel>
              <f:facet name="header">Barring search results</f:facet>
          <div class="results" id="barringList">
      
          <h:outputText value="The barring search returned no results." 
                     rendered="#{empty barringList.resultList}"/>
           <h:form id="profileResultForm">            
          <rich:dataTable id="barringList" 
                      var="barring"
                    value="#{barringList.resultList}"
                    rowClasses="table-row1,table-row2" 
                 rendered="#{not empty barringList.resultList}">
              <rich:column styleClass="select-all-col-class">
                           <f:facet name="header">
                                <h:panelGrid columns="2">
                                  <h:selectBooleanCheckbox id="selectAll" onclick="selectAllCB(this.checked)" disabled="#{empty barringList.resultList}"></h:selectBooleanCheckbox>
                                  <h:outputText value="SelectAll" style="font-size: 11px; font-weight: bold;"></h:outputText>
                             </h:panelGrid>                                    
                        </f:facet>     
                          <h:selectBooleanCheckbox value="#{findSelections[barring]}" disabled="#{barring.isDefaultProfile()}"/>
              </rich:column>
           
              <h:column>
                  <f:facet name="header">
                      <s:link styleClass="columnHeader"
                                   value="Id #{barringList.order=='oidIndex asc' ? messages.down : ( barringList.order=='oidIndex desc' ? messages.up : '' )}">
                          <f:param name="order" value="#{barringList.order=='oidIndex asc' ? 'oidIndex desc' : 'oidIndex asc'}"/>
                      </s:link>
                  </f:facet>
                  #{barring.oidIndex}
              </h:column>
              <h:column>
                  <f:facet name="header">
                      <s:link styleClass="columnHeader"
                                   value="Profile Name #{barringList.order=='profileName asc' ? messages.down : ( barringList.order=='profileName desc' ? messages.up : '' )}">
                          <f:param name="order" value="#{barringList.order=='profileName asc' ? 'profileName desc' : 'profileName asc'}"/>
                      </s:link>
                  </f:facet>
                  #{barring.profileName}
              </h:column>
              <h:column>
                  <f:facet name="header">
                      <s:link styleClass="columnHeader"
                                   value="Profile Xml #{barringList.order=='profileXml asc' ? messages.down : ( barringList.order=='profileXml desc' ? messages.up : '' )}">
                          <f:param name="order" value="#{barringList.order=='profileXml asc' ? 'profileXml desc' : 'profileXml asc'}"/>
                      </s:link>
                  </f:facet>
                  <rich:modalPanel id="panel" width="400" height="360">
      
              <f:facet name="header">
      
                  <h:panelGroup>
      
                      <h:outputText value="Profile Data"></h:outputText>
      
                  </h:panelGroup>
      
              </f:facet>
      
              <f:facet name="controls">
      
                  <h:panelGroup>
      
                      <h:graphicImage value="img/close.png" style="cursor:pointer" id="hidelink"/>
      
                      <rich:componentControl for="panel" attachTo="hidelink" operation="hide" event="onclick"/>
      
                  </h:panelGroup>
      
              </f:facet>
              
              <h:inputTextarea id="profileXml"
                                     cols="65"
                                     rows="20"
                                     disabled="true" 
                                    value="#{barring.profileXml}"/>        
      
          </rich:modalPanel>
      
          <h:outputLink value="#" id="link">
      
              View Profile Xml 
      
              <rich:componentControl for="panel" attachTo="link" operation="show" event="onclick"/>
      
          </h:outputLink>
                  
              </h:column>
              <h:column>
                  <f:facet name="header">
                      <s:link styleClass="columnHeader"
                                   value="Version Id #{barringList.order=='versionId asc' ? messages.down : ( barringList.order=='versionId desc' ? messages.up : '' )}">
                          <f:param name="order" value="#{barringList.order=='versionId asc' ? 'versionId desc' : 'versionId asc'}"/>
                      </s:link>
                  </f:facet>
                  #{barring.versionId}
              </h:column>
              <h:column rendered="#{s:hasRole('Admin') or s:hasRole('Operator')}">
                  <f:facet name="header">Action</f:facet>
                  <s:link view="/#{empty from ? 'Barring' : from}.xhtml" 
                         value="Edit" 
                            id="barring">
                      <f:param name="barringOidIndex" 
                              value="#{barring.oidIndex}"/>
                  </s:link>
              </h:column>
          </rich:dataTable>
              <h:commandButton action="#{barringList.deleteList}"
                                              rendered="#{s:hasRole('Admin') or s:hasRole('Operator')}"
                                              onclick="return check();" 
                                       value="Delete"
                                       styleClass="button"/>
             <h:commandButton action="#{barringList.bulkImportListener}" 
                                                rendered="#{s:hasRole('Admin') or s:hasRole('Operator')}"
                                       value="Export"
                                       styleClass="button"/>                          
        </h:form>      
          </div>
          </rich:panel>
          
          <div class="tableControl">
            
              <s:link view="/BarringList.xhtml" 
                  rendered="#{barringList.previousExists}" 
                     value="#{messages.left}#{messages.left} First Page"
                        id="firstPage">
                <f:param name="firstResult" value="0"/>
              </s:link>
              
              <s:link view="/BarringList.xhtml" 
                  rendered="#{barringList.previousExists}" 
                     value="#{messages.left} Previous Page"
                        id="previousPage">
                  <f:param name="firstResult" 
                          value="#{barringList.previousFirstResult}"/>
              </s:link>
              
              <s:link view="/BarringList.xhtml" 
                  rendered="#{barringList.nextExists}" 
                     value="Next Page #{messages.right}"
                        id="nextPage">
                  <f:param name="firstResult" 
                          value="#{barringList.nextFirstResult}"/>
              </s:link>
              
              <s:link view="/BarringList.xhtml" 
                  rendered="#{barringList.nextExists}" 
                     value="Last Page #{messages.right}#{messages.right}"
                        id="lastPage">
                  <f:param name="firstResult" 
                          value="#{barringList.lastFirstResult}"/>
              </s:link>
              
          </div>
          
          <s:div styleClass="actionButtons" rendered="#{empty from}">
              <s:button view="/BarringEdit.xhtml"
                          id="create"
                          rendered="#{s:hasRole('Admin') or s:hasRole('Operator')}" 
                       value="Create Barring"
                       styleClass="button">
                  <f:param name="barringOidIndex"/>
              </s:button>
              <s:button id="upload" 
                         value="Bulk Upload"
                         rendered="#{s:hasRole('Admin') or s:hasRole('Operator')}"
                          view="/BarringBulkUpload.xhtml"
                   propagation="none"
                   styleClass="button">
                       
                  </s:button>
          </s:div>
       </body>   
      
      </ui:composition>
      
      






        • 1. Re: Multiple delete not working
          baspet

          barringList and findSelections should be in page scope..


          V.

          • 2. Re: Multiple delete not working
            sanghakanwar

            tried all that and it does not work..issue seems to be how seam is integrated with JSF here...even if we put the whole class in page scope it does not work

            • 3. Re: Multiple delete not working
              swathis

              Hi Kanwar, I was actually looking for an example for multiple selection of rows in datatable using checkboxes and delete all the rows selected using delete button. I hope that your code helps me but Can you post your complete code for my better understanding i.e entity and entity home.


              Thanks in advance


              Swathi.

              • 4. Re: Multiple delete not working
                sanghakanwar

                @Name("forwardingList")
                public class ForwardingList extends EntityQuery {
                     
                     Logger log = Logger.getLogger(ForwardingList.class);
                     
                     private List <Forwarding> forwardingList;
                     
                     private static final String[] RESTRICTIONS = {
                               "lower(forwarding.profileName) like concat(lower(#{forwardingList.forwarding.profileName}),'%')",
                                };
                
                     private Forwarding forwarding = new Forwarding();
                     
                     
                     private static final String EJBQL = "select forwarding from Forwarding forwarding";
                     
                     private static final String SESSION_SELECTED_LIST_NAME = "selForwardingIdList";
                     
                     private String selForwardingId;
                     
                     public ForwardingList(){
                           setEjbql(EJBQL);
                           setRestrictionExpressionStrings(Arrays.asList(RESTRICTIONS));
                           //setMaxResults(25);
                     }
                     
                     
                     
                     public void delete(List<String> deleteList)
                     {
                          for (String id : deleteList) 
                          {
                               log.debug("deleting the Forwarding profile : "+id);
                               Forwarding s = null;
                                   
                               for(Forwarding item : forwardingList){
                                       if(item.getOidIndex().intValue()== new Integer(id).intValue()){
                                       s = item;     
                                       }
                                  }
                             if(s == null) 
                             {
                                  // that's fine - id doesn't exists. Already deleted.
                                  log.debug("Trying to delete Forwarding profile with id "+ id +" which is already deleted");
                                  super.addFacesMessage("Already Deleted. Record not found for profile with id : "+id, (Object[])null);
                             }
                             if ( s != null )
                             {
                                  try{
                                 
                           this.getEntityManager.remove(s);
                              forwardingList.remove(s);
                                 super.addFacesMessage("Successfully Deleted Profile : "+ s.getProfileName(), (Object[])null);
                                 //this.getEntityManager().clear();
                                  }
                                  catch(ProfileServiceException pe){
                                       log.error("Persistence Exception" + pe.getMessage(), pe);
                                       super.addFacesMessage("Persistence Exception" + pe.getMessage(), pe.getMessage());
                                       pe.printStackTrace();
                                  }
                                  catch(Exception e){
                                    log.error("Error while invoking webservice ! Exception" + e.getMessage(), e);
                                    super.addFacesMessage("Webservice Exception" + e, e);
                                    e.printStackTrace();
                                  }
                             
                             }
                }
                
                deleteList.clear();
                // refresh(); 
                }   
                       
                    
                      public void deleteList()
                      {   
                           List<String> selectedList = (List<String>) SPSUtil.getSessionAttribute(SESSION_SELECTED_LIST_NAME);
                           
                           if( selectedList != null )
                                delete(selectedList);
                      }
                
                      @Override
                     public List getResultList() {
                           
                           if (!SPSUtil.isPostBackRequest())
                             SPSUtil.removeSessionAttribute(SESSION_SELECTED_LIST_NAME);
                          forwardingList = super.getResultList();
                          return forwardingList;
                     }
                     
                
                     @Override
                     public Integer getMaxResults() {
                          return ProfileUtils.PAGE_SIZE;
                     }
                
                     }