1 Reply Latest reply on Apr 6, 2009 7:18 AM by swd847

    Editing the records of a datatable in the listpage using modalpanel

    vasavi
      Hi all, I am trying to edit the records of data table in the list page itself, using a modalpanel.. i have achieved to edit the records in the modalpane. But the problem is, when we click on the record for the first time, its showing the blanks in panel. and when we click on other record, it is showing the previous record... i.e., for example, if i first click on R1 record, its showing the blanks.and then if a click on say R2 record, its showing the R1 details....

      Here is the code:
      session bean:

      package com.infyz.toms.session;
      //import java.text.DateFormat;
      //import java.text.SimpleDateFormat;
      //import java.util.Date;
      import java.util.ArrayList;
      import java.util.List;

      import javax.ejb.Remove;
      import javax.ejb.Stateful;

      import javax.faces.application.FacesMessage;
      import javax.faces.context.FacesContext;
      import javax.faces.event.ActionEvent;

      import org.jboss.seam.annotations.Factory;
      import org.jboss.seam.annotations.Observer;

      import javax.persistence.EntityManager;

      import javax.persistence.PersistenceContext;
      import javax.persistence.PersistenceContextType;

      import com.infyz.toms.entity.*;
      import org.jboss.seam.ScopeType;
      import org.jboss.seam.annotations.Destroy;
      import org.jboss.seam.annotations.In;
      import org.jboss.seam.annotations.Name;
      import org.jboss.seam.annotations.Out;
      import org.jboss.seam.annotations.Scope;
      import org.jboss.seam.annotations.datamodel.DataModel;
      import org.jboss.seam.annotations.datamodel.DataModelSelection;
      import org.jboss.seam.framework.EntityHome;
      import org.jboss.seam.annotations.Logger;
      import org.jboss.seam.log.Log;

      @Stateful
      @Name("countryS")
      @Scope(ScopeType.SESSION)
      public class CountrySAction implements  CountryS  {
           
           
           @PersistenceContext(type=PersistenceContextType.EXTENDED)
           private EntityManager em;
           
           @In(required=false, create=true)
           @Out(required=false)
           private Region region;
           
           @Logger
           private Log log;
           
           private int param;
          
           @DataModel
           private List<Country> countries;
          
           @DataModelSelection
           @Out(value="country", required=false)
           @In(value="country", required=false)
           private Country selectedCountry;
         
           @Factory
           @Observer("countryConfirmed")
           @SuppressWarnings("unchecked")
           public void getCountries()
           {
                String qry = "select country from Country country";
                countries = em.createQuery(qry).getResultList();
           }
          
           public void selectCountry() {
                System.out.println("Selected Country.................: " + param);
                // refetch the region, loading all lazy associations
                selectedCountry = em.find(Country.class, param);
                selectedCountry.getRegion();
                System.out.println("Selected Country.................: " + selectedCountry.getName());
           }

           public Country getSelectedCountry()
           {
             return selectedCountry;
           }
          
           public String addCountry() {
                selectedCountry = new Country();
                //region = new Region();
                //selectedCountry.setRegion(region);
               return "sucess";
           }

         
           public String persist() {
                //selectedCountry = new Country();
                log.info(":Rao:------------Country Code" + selectedCountry.getCountryCode());
                log.info(":Rao:------------Region Code" + selectedCountry.getRegion().getRegionCode());
               
                /*if (region != null){
                     region = em.find(Region.class, region.getRegionId());
                     selectedCountry.setRegion(region);
                }*/
                em.persist(selectedCountry);
                log.info(":Rao:------------persisted");
           return "success";
           }
          
           public String remove(){
                selectedCountry = em.find(Country.class, selectedCountry.getCountryId());
                if(selectedCountry!=null)
                em.remove(selectedCountry);
                     return "String";
                    
                }
          
          
                public String update(){
                   selectedCountry = em.find(Country.class, selectedCountry.getCountryId());
                     if(selectedCountry!=null)
                     em.merge(selectedCountry);
                     return "String";
                }
               
                public  void selectRegion(){
                    if (region != null)
                    {
                       region = em.find(Region.class, region.getRegionId());
                    selectedCountry.setRegion(region);
                    }
                }  
           
           public int getParam() {
                return param;
           }

           public void setParam(int param) {
                this.param = param;
           }
           
           public String delete() {
                FacesContext context = FacesContext.getCurrentInstance();
                context.addMessage(null, new FacesMessage("Package deleted: " + param));
                selectedCountry = em.find(Country.class,param);
                     if(selectedCountry!=null)
                     em.remove(selectedCountry);
                          return "/CountrySList.xhtml";
           }
           
           public void edit() {
                FacesContext context = FacesContext.getCurrentInstance();
                context.addMessage(null, new FacesMessage("Package edited: " + param));
                //System.out.println("Bean.action()" + param);
           }
           
           public void neW() {
                FacesContext context = FacesContext.getCurrentInstance();
                context.addMessage(null, new FacesMessage("New Package is created: "));
                //System.out.println("Bean.action()" + param);
           }
           
           public void actionListener(ActionEvent event) {
                log.info(":Rao:------------Bean.actionListener()" + event.getComponent());
                System.out.println("Bean.actionListener()" + event.getComponent());
           }
           
           @Destroy @Remove
           public void destroy() {}
           }




      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"
          xmlns:a="http://richfaces.org/a4j"
          template="layout/template.xhtml">

      <ui:define name="body">

          <rich:panel>
              <f:facet name="header">Countries</f:facet>
               <div class="results" id="countryList">

                     <h:form id="countryListForm" styleClass="edit">
                     
                           <rich:contextMenu submitMode="server" id="m" attached="false">
                                <rich:menuItem action="#{countryS.selectCountry}"
                                     value="Edit: {param}" submitMode="ajax" immediate="true"
                                     actionListener="#{countryS.actionListener}" icon="/img/ico_edit.gif"
                                     oncomplete="javascript:Richfaces.showModalPanel('countryListForm:_panel2');">
                                     <a:actionparam assignTo="#{countryS.param}" name="param"
                                          value="{param}">
                                     </a:actionparam>
                                </rich:menuItem>
                                
                                <rich:menuSeparator id="menuSeparator1" />
                                
                                <rich:menuItem action="#{countryS.delete}"
                                     value="Delete: {param}" submitMode="ajax" immediate="true"
                                     actionListener="#{countryS.actionListener}" icon="/img/ico_delete.gif">
                                     <a:actionparam assignTo="#{countryS.param}" name="param"
                                          value="{param}">
                                     </a:actionparam>
                                </rich:menuItem>
                                
                                <rich:menuSeparator id="menuSeparator2" />
                                
                                <rich:menuItem action="#{countryS.neW}"
                                     value="New" submitMode="ajax" immediate="true"
                                     actionListener="#{countryS.actionListener}" icon="/img/ico_new.gif"
                                     oncomplete="javascript:Richfaces.showModalPanel('countryListForm:_panel2');">
                                     <a:actionparam assignTo="#{countryS.param}" name="param"
                                          value="{param}">
                                     </a:actionparam>
                                </rich:menuItem>
                          </rich:contextMenu>
                          
                         <h:outputText value="The country search returned no results."
                               rendered="countries != null and countries.rowCount==0}"/>
                          
                          <rich:extendedDataTable id="countryListTable" var="_country"
                               selectionMode="single" height="190px"
                              value="#{countries}"
                           rendered="#{countries.rowCount > 0}">
                                     
                               <rich:column id="regionCode" width="140px" sortable="true"
                                    sortBy="#{_country.region.regionCode}"
                                    filterBy="#{_country.region.regionCode}"
                                    filterEvent="onkeyup" label="Region Code">
                                  <f:facet name="header">
                                       <h:outputText styleClass="headerText" value="Region Code"/>
                                  </f:facet>
                                    <h:outputText value="#{_country.region.regionCode}"/>
                               </rich:column>
                
                               <rich:column id="countryCode" width="140px" sortable="true"
                                    sortBy="#{_country.countryCode}"
                                    filterBy="#{_country.countryCode}"
                                    filterEvent="onkeyup" label="Country Code">
                                  <f:facet name="header">
                                       <h:outputText styleClass="headerText" value="Country Code"/>
                                  </f:facet>
                                    <h:outputText value="#{_country.countryCode}"/>
                               </rich:column>
                
                               <rich:column id="countryName" width="150px" sortable="true"
                                    sortBy="#{_country.name}"
                                    filterBy="#{_country.name}"
                                    filterEvent="onkeyup" label="Country Name">
                                  <f:facet name="header">
                                       <h:outputText styleClass="headerText" value="Country Name"/>
                                  </f:facet>
                                    <h:outputText value="#{_country.name}"/>
                                    
                               </rich:column>
                               
                               <rich:column id="countryFActive" width="100px" sortable="true"
                                    sortBy="#{_country.FActive}" label="Active">
                                  <f:facet name="header">
                                       <h:outputText styleClass="headerText" value="Active"/>
                                  </f:facet>
                                    <h:outputText value="#{_country.FActive}"/>
                                    <rich:componentControl event="oncontextmenu" attachTo="regionCode,countryCode,countryName,countryFActive" for=":countryListForm:m"
                                                   operation="doShow">
                                                   <f:param name="param" value="#{_country.countryId}"></f:param>
                                    </rich:componentControl>
                               </rich:column>
                          </rich:extendedDataTable>
                
                          <rich:modalPanel height="400" top="100" width="600" id="_panel2" controlsClass="pointerCursor" zindex="500">
                               
                               <rich:panel>
                                 <f:facet name="header">Country</f:facet>
                                 <s:decorate id="countryCodeField" template="layout/edit.xhtml">
                                     <ui:define name="label">Country code</ui:define>
                                     <h:inputText id="countryCode"
                                            required="true"
                                                size="5"
                                           maxlength="5"
                                               value="#{country.countryCode}">
                                         <a:support event="onblur" reRender="countryCodeField" bypassUpdates="true" ajaxSingle="true"/>
                                     </h:inputText>
                                 </s:decorate>
           
                                    <s:decorate id="regionField" template="layout/edit.xhtml">
                                     <ui:define name="label">Region</ui:define>
                                     <h:selectOneMenu value="#{country.region}" required="true" id="region">
                                         <s:selectItems value="#{regiones}" var="region" label="#{region.name}" noSelectionLabel="Please Select..."/>
                                         <s:convertEntity />
                                    </h:selectOneMenu>
                                 </s:decorate>
                      
                                 <s:decorate id="nameField" template="layout/edit.xhtml">
                                     <ui:define name="label">Name</ui:define>
                                     <h:inputText id="name"
                                            required="true"
                                                size="50"
                                           maxlength="100"
                                               value="#{country.name}">
                                         <a:support event="onblur" reRender="nameField" bypassUpdates="true" ajaxSingle="true"/>
                                     </h:inputText>
                                 </s:decorate>
           
           
                                 <s:decorate id="FActiveField" template="layout/edit.xhtml">
                                     <ui:define name="label">F active</ui:define>
                                     <h:inputText id="FActive"
                                            required="true"
                                                size="2"
                                           maxlength="2"
                                               value="#{country.FActive}">
                                         <a:support event="onblur" reRender="FActiveField" bypassUpdates="true" ajaxSingle="true"/>
                                     </h:inputText>
                                 </s:decorate>
                                 <div style="clear:both">
                                     <span class="required">*</span>
                                     required fields
                                 </div>
                             </rich:panel>
                             
                             <div class="actionButtons">
                     
                                 <h:commandButton id="save"
                                               value="Save"
                                              action="#{countryS.persist}"/>
                     
                                 <s:button id="cancel"
                                        value="Cancel"
                                  propagation="end"/>
                     
                             </div>
                     
                          </rich:modalPanel>
                     </h:form>

               </div>
          </rich:panel>


      </ui:define>

      </ui:composition>



      Thank u in advance...