5 Replies Latest reply on Oct 6, 2008 2:01 AM by Val Sw

    Clearing form fields to add new form field data

    Val Sw Expert

      Scenario:


      a) User fills form say Pname, Color, etc. fields(these belongs to reagent entity fields) from a form say study.xhtml and clicks Save, values are persisted properly.


      b) After above event, user clicks a link(Add More Reagents) and passes some id to a function in reagentHome. On clicking link  it loads a particular reagent.xhtml page, with same fields as Pname, Color, etc.


      Issue is, it holds the values as submitted at point (a), for Pname, Color, etc. in reagent.xhtml and I cannot create a new Reagent (it will be updated always) because that reagent instance is still there.


      I think I need to check for creating new instance or clear instance somehow or override any function of entity home, etc... please suggest...
      What is the best way so that form fields are cleared and we should be able to persist new entry.


      Please suggest


      study.xhtml


      <h:commandLink target="_new" id="name" action="#{reagentHome.getQuantId}" immediate="true">
           <h:outputText value="Add More Reagents"/>
           <f:param name="qid" value="#{quantExperimentHome.instance.hjid}"/>
      </h:commandLink>
      




      reagent.xhtml


      <s:decorate id="pnameDecoration" template="layout/edit.xhtml">
      <ui:define name="label">PName</ui:define>
      <h:inputText id="name" required="true" immediate="true"
                value="#{reagentHome.instance.pname}">
       </h:inputText>
      </s:decorate>  
      <s:decorate id="colorDecoration" template="layout/edit.xhtml">
      <ui:define name="label">Color</ui:define>
      <h:inputText id="name" required="true"
                value="#{reagentHome.instance.color}"/>
      </s:decorate>




      reagentHome.java


      @Name("reagentHome")
      public class ReagentHome extends EntityHome<Reagent>
      {
      @RequestParameter 
      Long reagentId;
      
      @In(required=false)
      private Long hjid;
      
      @Factory("reagent")
      public Reagent initReagent() { return getInstance(); }
      
      @Override
      public Object getId() 
      { 
           if (reagentId==null)
           {
                return super.getId();
           }
           else
           {
                return reagentId;
           }
      }
      
      @Override @Begin(join=true)
      public void create() {
           super.create();
      } 
      
      //JUST for Testing
      public String getQuantId(){
           HttpServletRequest req = (HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();
           String qid = req.getParameter("name");
           return "reagent";
      }
      


        • 2. Re: Clearing form fields to add new form field data
          Andy Gibson Novice

          The problem is probably that you are carrying bean instances across pages because they are using the same conversation. If the reagentHome bean already exists from a previous page then Seam will use that instance for the new page.


          You have two choices, the first is to just use a separate conversation, and the second is to manually clear out the reagentHome instance, or at least, clear out the instance and Id values from that instance.


          Cheers,


          Andy



          • 3. Re: Clearing form fields to add new form field data
            Val Sw Expert

            Thanks for replying Andy.


            Choice 2 will be fine as of now, where in reagentHome (code provided above) I can clear out the reagentHome instance, or at least, clear out the instance and Id values from that instance.


            Other thing, whenever seam uses persist (probably like in reagentHome...), where does the control go after transaction is committed.
            e.g. suppose reagent have a field pname, once reagent is persisted and pname is in database, how can I get this stored value pname in database in reagentHome it self and pass it to some other external purpose.

            • 4. Re: Clearing form fields to add new form field data
              Val Sw Expert
              More details Andy, for passing value to external method....

              Here is the code I tried calling "passReagentId()" method in separate bean, but issue is
              value of reagent id, is not committed or transaction is not committed at the time of calling "passReagentId"

              @Name("reagentHome")
              public class ReagentHome extends EntityHome<Reagent>
              {
                  @RequestParameter
                  Long reagentId;
                 
                  @In(required=false)
                  private Long hjid;
                 
                  @Factory("reagent")
                  public Reagent initReagent() { return getInstance(); }
                 
                  @Override
                  public Object getId()
                  {
                      if (reagentId==null)
                      {
                          return super.getId();
                      }
                      else
                      {
                          return reagentId;
                      }
                  }
                 
                  @Override @Begin(join=true)
                  public void create() {         
                      super.create();
                  }
                 
                  @Override
                  public String persist(){
                 
                       getReagent();      
                      String persistResult = super.persist();
                      //getEntityManager().setFlushMode(FlushModeType.COMMIT);
                      entityManager.refresh(getInstance());
                      //passReagentId();//<--- CALLING IT
                      return persistResult;
                  }  

                  public List<Reagent> getReagent() {                      
                       ......
                       return reagentList;
                  }
                   
                 //PASS REAGENT ID to external method in other bean, once reagent is committed     
                 public void passReagentId(){
                   Utils u = new Utils();
                   try {
                        // But this should be called after persist() method
                        u.passReagent(instance.getHjid());
                   } catch (SQLException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                   }
                        
              • 5. Re: Clearing form fields to add new form field data
                Val Sw Expert

                Any suggestions... this is very important for us...