0 Replies Latest reply on Jul 22, 2011 1:25 AM by Niraj Gurjar

    Using same page for Update and Create operation of an entity

    Niraj Gurjar Newbie

      I have one staff table with usual fields - staffid, username, password, firstname, lastname. Now, i have a Create staff JSP page. Also, i want to use same page to update the staff record selected from Search Staff screen. Following is the page content

       

       

      <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
                pageEncoding="ISO-8859-1"%>
      <%@ taglib prefix="f" uri="http://java.sun.com/jsf/core"%>
      <%@ taglib prefix="h" uri="http://java.sun.com/jsf/html"%>
      <%@ taglib uri="http://richfaces.org/rich" prefix="rich"%>
      <%@ taglib uri="http://richfaces.org/a4j" prefix="a4j"%>
      
      
      <rich:tabPanel switchType="client" width="670" height="600">
                <rich:tab label="#{empty staff.staffid  ? 'Craete' : 'Update'} Staff">
                          <rich:layout> 
                                    <rich:layoutPanel position="top" width="100%">
      
      
                                    </rich:layoutPanel>
                                    <rich:layoutPanel position="left" width="20%">
      
      
                                    </rich:layoutPanel>
                                    <rich:layoutPanel position="center" width="80%">
                                              
                                              <rich:panel>
                                                        <f:facet name="header">
                                                                  <h:outputText value="Staff Details (* are mandatory fields)" />
                                                        </f:facet>
        
        
                                                        <h:panelGrid columns="4">
      
      
                                                                  <h:outputText value="Staff Id" rendered="#{! empty staff.staffid}"/>
                                                                  <h:inputText id="StaffId" value="#{staff.staffid}" rendered="#{! empty staff.staffid}" disabled="true"/>
      
      
                                                                  <h:outputText value="* Username" />
                                                                  <h:inputText id="Username" value="#{staff.username}" required="true" maxlength="30"/>
      
      
                                                                  <h:outputText value="* Password" />
                                                                  <h:inputSecret id="Password" value="#{staff.password}" required="true" maxlength="30" redisplay="true"/>
                                                        </h:panelGrid>
                                                        <rich:message for="Username" ></rich:message>
                                                        <rich:message for="Password" ></rich:message>
                                              </rich:panel>
                                              <rich:panel>
                                                        <f:facet name="header">
                                                                  <h:outputText value="Personal Details (* are mandatory fields)" />
                                                        </f:facet>
                                                        <h:panelGrid columns="4">
                                                                  <h:outputText value="* First Name" />
                                                                  <h:inputText id="FirstName" value="#{staff.firstName}" required="true"/>
      
      
                                                                  <h:outputText value="* Last Name" />
                                                                  <h:inputText id="LastName" value="#{staff.lastName}" required="true"/>
                                                        </h:panelGrid>
                                                        <rich:message for="FirstName" ></rich:message>
                                                        <rich:message for="LastName" ></rich:message>
                                              </rich:panel>
      
      
                                    </rich:layoutPanel>
                                    <rich:layoutPanel position="bottom">
                                              <h:panelGrid columns="2">
                                                        <a4j:commandButton value="Submit" action="#{staff.createOrUpdate}" reRender="pagecontent">
        
                                                        </a4j:commandButton>
                                                        <a4j:commandButton value="Cancel">
        
                                                        </a4j:commandButton>
        
                                              </h:panelGrid>
                                    </rich:layoutPanel>
                          </rich:layout>
                </rich:tab>
      </rich:tabPanel>
      
      

       

      following is the Staff backing bean

       

       

      package app.onlinetest.staff;
      
      
      import java.io.Serializable;
      import java.util.Date;
      import java.util.List;
      
      
      import org.hibernate.Session;
      
      
      import app.onlinetest.HibernateUtil;
      import app.onlinetest.MessageHandler;
      
      
      public class Staff implements Serializable{
      
      
                private String staffid;
                private String firstname;
                private String lastname;
                private String username;
                private String password;
                private Date dateofcreation = new Date();
        
                public String getStaffid() {
                          return staffid;
                }
                public void setStaffid(String staffid) {
                          this.staffid = staffid;
                }
                public String getFirstName() {
                          return firstname;
                }
                public void setFirstName(String firstname) {
                          this.firstname = firstname;
                }
                public String getLastName() {
                          return lastname;
                }
                public void setLastName(String lastname) {
                          this.lastname = lastname;
                }
        
                public String getUsername() {
                          return username;
                }
                public void setUsername(String username) {
                          this.username = username;
                }
                public String getPassword() {
                          return password;
                }
                public void setPassword(String password) {
                          this.password = password;
                }
                public Date getDateofcreation() {
                          return dateofcreation;
                }
                public void setDateofcreation(Date dateofcreation) {
                          this.dateofcreation = dateofcreation;
                }
        
                private String generateStaffId(){
                          String newStaffId = null;
        
                          String query = "select max(stf.id) from Staff stf";
        
                           Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        
                           List list = session.createQuery(query.toString()).list();
                           
                           if(!list.isEmpty()){
                                     String maxStaffId = (String)list.get(0);
                                     int i = 3;
                                     while (maxStaffId.charAt(i) == '0'){
                                               i++;
                                     }
                                     int maxNumber = Integer.parseInt(maxStaffId.substring(i));
                                     StringBuffer tmpNewStaffId = new StringBuffer("STF");
        
                                     for(i=3; i < (10-String.valueOf(maxNumber+1).length()); i++){
                                               tmpNewStaffId.append("0");
                                     }
                                     tmpNewStaffId.append(maxNumber+1);
                                     newStaffId = tmpNewStaffId.toString();
                           }
                           else
                                     newStaffId = "STF0000001";
        
                          return newStaffId;
                }
                public void createOrUpdate(){
        
                          Session session = HibernateUtil.getSessionFactory().getCurrentSession();
              session.beginTransaction();
      
      
              if(staffid == null || "".equals(staffid)){
                        this.staffid = generateStaffId();
                        session.save(this);
                        MessageHandler.getInstance().setMessageBoard(MessageHandler.STAFF_CREATE_SUCCESS);
              }else{
                        session.update(this);
                        MessageHandler.getInstance().setMessageBoard(MessageHandler.STAFF_UPDATE_SUCCESS);
              }
              session.getTransaction().commit();
                          return ;
                }
        
                @Override
                public String toString() {
                          return "Staff [staffid=" + staffid + ", firstname=" + firstname + ", username="
                                              + username + ", password=" + password + ", dateofcreation="
                                              + dateofcreation + "]";
                }
        
        
      }
      
      

       

       

       <managed-bean>
        <managed-bean-name>staff</managed-bean-name>
        <managed-bean-class>app.onlinetest.staff.Staff</managed-bean-class>
        <managed-bean-scope>request</managed-bean-scope>
       </managed-bean>
      
      

       

      Now, to use the same Create staff screen to update the staff, when the user selects a staff from search screen and clicks on 'Updat' button, i set selected Staff record into the request scope using FacesContext valule binding and forward to Create Staff page. Therefore, when on update screen i see all the fields are populated. After changin fields when user clicks 'Submit' on Create Staff page 'createOrUpdate' action method is called in which i check if staffid is not null (i.e. update case) then update the record else create the record.

       

      The problem here i am facing is that i always get 'staffid' as null whether it is update or create operation. in case of 'update' operation i get all the fields set (i.e username, password, firstname, lastname) but not staffid

       

      what could be the reason?