0 Replies Latest reply on Apr 23, 2009 7:43 AM by madhu reddy

    problem with unique key validation with action Listener in modelpanel

    madhu reddy Newbie
      hi,
           I have a problem regarding unique key validation with actionListener in model panel.

      here is my code:


      <s:decorate id="countryCodeField" template="../layout/edit.xhtml">
                       <ui:define name="label">Country_code</ui:define>
      <h:inputText id="countryCode" size="20" required="true"
                            value="#{country.countryCode}">
             <a4j:support id="countryCodeCheck" event="onblur"                reRender="countryCodeField" ajaxSingle="true"                      actionListener="#{countryS.verifyCarrierCodeAvailable}"/>
               </h:inputText>
        </s:decorate>
                
                                
              <s:decorate id="name" template="../layout/edit.xhtml">
                  <ui:define name="label">Name</ui:define>
                  <h:inputText value="#{country.name}"/>
              </s:decorate>



      session bean:



      package com.infyz.toms.master.hg.session;


      import java.util.List;
      import java.sql.Date;
      import javax.ejb.Remove;
      import javax.ejb.Stateful;
      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.Factory;
      import org.jboss.seam.annotations.In;
      import org.jboss.seam.annotations.*;
      import org.jboss.seam.annotations.Observer;
      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.annotations.Logger;
      import org.jboss.seam.faces.FacesMessages;
      import org.jboss.seam.log.Log;

      @Stateful
      @Name("countryS")
      @Scope(ScopeType.SESSION)
      public class CountrySSAction implements  CountrySS  {
           
           @In
           private FacesMessages facesMessages;
           
           @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;
         
          
           public void find(){
                getCountries();
           }
          
           @Factory
           @Observer("countryConfirmed")
           @SuppressWarnings("unchecked")
           @Begin
           public void getCountries()
           {
                String qry = "select country from Country country";
                countries = em.createQuery(qry).getResultList();
           }
          
           public void selectCountry() {
                selectedCountry = em.find(Country.class,  selectedCountry.getCountryId());
                selectedCountry.getRegion();
               
               
            }

           public Country getSelectedCountry()
           {
             return selectedCountry;
           }
          
           public String addCountry() {
                selectedCountry = new Country();
                return "sucess";
           }

          @End
           public String persist() {
                selectedCountry.setCreatedDate(getCurrentDate());
                 selectedCountry.setModifiedDate(getCurrentDate());
                 em.persist(selectedCountry);
                log.info(":Rao:------------persisted");
           return "/hg/CountryS.xhtml";
           }
          @End
           public String remove(){
                selectedCountry = em.find(Country.class,  selectedCountry.getCountryId());
                if(selectedCountry!=null)
                em.remove(selectedCountry);
                     return "/hg/CountryS.xhtml";
                }
          @End
                public String update(){
                   selectedCountry = em.find(Country.class, selectedCountry.getCountryId());
                     if(selectedCountry!=null){
                        selectedCountry.setCreatedDate(getCurrentDate());
                       selectedCountry.setModifiedDate(getCurrentDate());
                     em.merge(selectedCountry);
                   log.info("------------updated");
                     }
                  
                     return "/hg/CountryS.xhtml";
                }
               
                public  void selectRegion(){
                    if (region != null)
                    {
                       region = em.find(Region.class, region.getRegionId());
                    selectedCountry.setRegion(region);
                    }
                }  
           

              @Destroy @Remove
           public void destroy() {}
           public Date getCurrentDate()
              {
                 return new java.sql.Date( System.currentTimeMillis() );
              }
           @End
           public String verifyCarrierCodeAvailable() {
               if (getSelectedCountry().getCountryCode() != null &&
                   !isActTypeCodeAvailable(getSelectedCountry().getCountryCode())) {
                   facesMessages.addToControl("countryCode", " #{country.countryCode} #{messages['toms.application.messages.duplicatekey']}");
               }
               return "/hg/CountryS.xhtml";
           }
                
                /**
                  * used to retrieve the data from the database
                  */
           public boolean isActTypeCodeAvailable(String countryCode) {
                System.out.println("company code"+countryCode);
               return em.createQuery("from Country as country where country.countryCode = :countryCode")
                                    .setParameter("countryCode", countryCode)
                                    .getResultList().size()==0;
             
           }
          
           }





      the main problem here i faced is when i edit the countryCode and changed,at back end the value is updating in the database when i pressed the next tab.The update statement is executing in the server.

      can any one help me i strucked with these code for last ten days.

      Thanks & Regards