jsfprimefacesdatatableroweditor

onRowEdit(RowEditEvent event) in prime faces is not getting me updated value


I am trying to update the row using the jsf with primefaces here is my code for each files but when I debug I am not getting updated value it gives me an old value every-time and persist it same as an old one.

user-type.xhtml

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:p="http://primefaces.org/ui">
<h:head>
</h:head>
<h:body style="align:center;">

    <div style="width: 100%">
        <h:form id="form">
            <div style="float: left; width: 54%;">
                <p:growl id="msgs" showDetail="true" />
                <p:dataTable id="userTypesTbl" var="varUserType"
                    value="#{userTypeTabMenuBean.userTypeMasters}" editable="true"
                    style="margin-bottom:20px" rowIndexVar="rowIndex">
                    <f:facet name="header">User Type Managging</f:facet>

                    <p:ajax event="rowEdit" listener="#{userTypeTabMenuBean.onRowEdit}"
                        update=":form:msgs" />
                    <p:ajax event="rowEditCancel"
                        listener="#{userTypeTabMenuBean.onRowCancel}" update=":form:msgs" />

                    <p:column headerText="Sr.">
                        #{rowIndex+1}.
                    </p:column>

                    <p:column headerText="Type">
                        <p:cellEditor>
                            <f:facet name="output">
                                <h:outputText value="#{varUserType.type}" />
                            </f:facet>
                            <f:facet name="input">
                                <p:inputText value="#{varUserType.type}" style="width:100%"
                                    label="type" />
                            </f:facet>
                        </p:cellEditor>
                    </p:column>
                    <p:column>
                        <f:facet name="header">View</f:facet>
                        <p:commandButton update=":form" oncomplete="userTypeDialog.show()"
                            icon="ui-icon-search" title="View">
                            <f:setPropertyActionListener value="#{userType}"
                                var="#{selectedUserType}"
                                target="#{userTypeTabMenuBean.selectedUserType}" />
                        </p:commandButton>
                    </p:column>

                    <p:column style="width:32px;">
                        <f:facet name="header">Edit</f:facet>
                        <p:rowEditor />
                    </p:column>

                    <p:column style="width:32px;align-items: center;">
                        <f:facet name="header">Delete</f:facet>
                        <h:commandLink
                            action="#{userTypeTabMenuBean.deleteAction(varUserType)}">
                            <h:graphicImage library="images" name="delete.png" height="45%"
                                width="50%" />
                        </h:commandLink>
                    </p:column>
                </p:dataTable>
                <ui:debug hotkey="x" />

                <p:dialog header="User type detail" widgetVar="userTypeDialog"
                    resizable="false" width="330" showEffect="explode"
                    hideEffect="explode">
                    <h:panelGrid id="display" columns="2" cellpadding="4">
                        <h:outputText value="ID : " />
                        <h:outputText value="#{userTypeTabMenuBean.selectedUserType.id}" />
                        <h:outputText value="TYPE: " />
                        <h:outputText value="#{userTypeTabMenuBean.selectedUserType.type}" />
                    </h:panelGrid>
                </p:dialog>
            </div>
            <div style="float: right; width: 44%;">
                <p:panel menuTitle="Add User Type." header="Add User Type.">
                    <h:outputText value="User type *:" />
                    <p:inputText value="#{userTypeTabMenuBean.userType}"
                        title="Enter User Type.." id="myUserType"
                        style="margin:0px 10px 0px 10px;"></p:inputText>
                    <p:commandButton value="Add Type"
                        action="#{userTypeTabMenuBean.saveAction}" ajax="false">
                    </p:commandButton>
                </p:panel>
            </div>
        </h:form>
    </div>
</h:body>
</html>

UserTypeTabMenuBean.java

package com.convoy.gpack.managedbean;

import java.io.Serializable;
import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

import org.apache.log4j.Logger;
import org.primefaces.event.RowEditEvent;

import com.convoy.gpack.dao.UserTypeMasterDAO;
import com.convoy.gpack.pack.models.UserTypeMaster;

@ManagedBean(name = "userTypeTabMenuBean")
@SessionScoped
public class UserTypeTabMenuBean implements Serializable {
private static final long serialVersionUID = 1467465633405172689L;
private static final Logger logger = Logger
        .getLogger(UserTypeTabMenuBean.class);
private List<UserTypeMaster> userTypeMasters;
private UserTypeMasterDAO userTypeMasterDAO = new UserTypeMasterDAO();
private UserTypeMaster selectedUserType;
private String userType;

public List<UserTypeMaster> getUserTypeMasters() {
    userTypeMasters = userTypeMasterDAO.getAllUserTypes();
    logger.info("getUserTypeMasters=" + userTypeMasters);
    return userTypeMasters;
}

public String deleteAction(UserTypeMaster userTypeMaster) {
    logger.info("Deleting the object with id = " + userTypeMaster.getId());
    UserTypeMasterDAO userTypeMasterDAO = new UserTypeMasterDAO();
    int result = userTypeMasterDAO.deleteUserType(userTypeMaster);
    userTypeMasters.remove(userTypeMaster);
    if (result == 1) {
        FacesMessage msg = new FacesMessage(
                "User type deleted successfuly.");
        FacesContext.getCurrentInstance().addMessage(null, msg);
    } else {
        FacesMessage msg = new FacesMessage("Failed to delete user types.");
        FacesContext.getCurrentInstance().addMessage(null, msg);
    }
    return null;
}

public void onRowEdit(RowEditEvent event) {
    UserTypeMaster userTypeMaster = (UserTypeMaster) event.getObject();
    logger.info("UPDATING THE USER TYPE MASTER : "
            + userTypeMaster.getType());
    UserTypeMasterDAO userTypeMasterDAO = new UserTypeMasterDAO();
    userTypeMasterDAO.saveOrUpdateUserTypeMaster(userTypeMaster);
    logger.info("UPDATING THE USER TYPE MASTER : "
            + userTypeMaster.getType());

    FacesMessage msg = new FacesMessage("Edited type ",
            ((UserTypeMaster) event.getObject()).getType() + "");
    FacesContext.getCurrentInstance().addMessage(null, msg);
}

public void onRowCancel(RowEditEvent event) {
    FacesMessage msg = new FacesMessage("Editing Cancelled for ",
            ((UserTypeMaster) event.getObject()).getType() + "");
    FacesContext.getCurrentInstance().addMessage(null, msg);
}

public UserTypeMaster getSelectedUserType() {
    return selectedUserType;
}

public void setSelectedUserType(UserTypeMaster selectedUserType) {
    this.selectedUserType = selectedUserType;
}

public String getUserType() {
    return userType;
}

public void setUserType(String userType) {
    this.userType = userType;
}

public void saveAction() {
    logger.info("Saving the object to database...." + userType);
    if (userType.trim().length() < 1) {
        FacesMessage msg = new FacesMessage("Can not save empty user type.");
        FacesContext.getCurrentInstance().addMessage(null, msg);
    }
    UserTypeMasterDAO userTypeMasterDAO = new UserTypeMasterDAO();
    UserTypeMaster userTypeMaster = new UserTypeMaster();
    userTypeMaster.setType(userType);
    int result = userTypeMasterDAO
            .saveOrUpdateUserTypeMaster(userTypeMaster);
    if (result == 1) {
        FacesMessage msg = new FacesMessage("User type saved.");
        FacesContext.getCurrentInstance().addMessage(null, msg);
    } else {
        FacesMessage msg = new FacesMessage("Failed to save user type.");
        FacesContext.getCurrentInstance().addMessage(null, msg);
    }
}
}

UserTypeMasterDAO.java

package com.convoy.gpack.dao;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.convoy.gpack.hibernate.util.HibernateUtil;
import com.convoy.gpack.pack.models.UserTypeMaster;

public class UserTypeMasterDAO implements Serializable {

/**
 * 
 */
private static final long serialVersionUID = -4005898944892506760L;

public UserTypeMaster getUserTypeById(long long1) {

    return null;
}

public List<String> getAllUserTypesInString() {
    try {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction transaction = session.beginTransaction();
        Criteria criteria = session.createCriteria(UserTypeMaster.class);
        @SuppressWarnings("unchecked")
        List<UserTypeMaster> userTypeMasters = criteria.list();
        List<String> userTypeMastersString = new ArrayList<String>();
        for (UserTypeMaster userTypeMaster : userTypeMasters) {
            userTypeMastersString.add(userTypeMaster.getType());
        }
        transaction.commit();
        session.close();
        return userTypeMastersString;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

public List<UserTypeMaster> getAllUserTypes() {
    try {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction transaction = session.beginTransaction();
        Criteria criteria = session.createCriteria(UserTypeMaster.class);
        @SuppressWarnings("unchecked")
        List<UserTypeMaster> userTypeMasters = criteria.list();
        transaction.commit();
        session.close();
        return userTypeMasters;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

public int saveOrUpdateUserTypeMaster(UserTypeMaster userTypeMaster) {
    try {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction transaction = session.beginTransaction();
        userTypeMaster.setType(userTypeMaster.getType().toUpperCase());
        session.saveOrUpdate(userTypeMaster);
        transaction.commit();
        session.close();
        return 1;
    } catch (Exception e) {
        e.printStackTrace();
        return -1;
    }

}

public int deleteUserType(UserTypeMaster userTypeMaster) {
    try {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction transaction = session.beginTransaction();
        session.delete(userTypeMaster);
        transaction.commit();
        session.close();
        return 1;
    } catch (Exception e) {
        e.printStackTrace();
        return -1;
    }
}
}

Thanks in advance ~ :)


Solution

  • Exactly Primefaces follows strict structure and I think it is compulsory to add init() method with @PostConstruct annotation.

    @PostConstruct
    public void init(){
        userTypeMasters = userTypeMasterDAO.getAllUserTypes();
    }
    

    Here is the solution.

    package com.convoy.gpack.managedbean;
    
    import java.io.Serializable;
    import java.util.List;
    
    import javax.annotation.PostConstruct;
    import javax.faces.application.FacesMessage;
    import javax.faces.bean.ManagedBean;
    import javax.faces.bean.SessionScoped;
    import javax.faces.context.FacesContext;
    
    import org.apache.log4j.Logger;
    import org.primefaces.event.RowEditEvent;
    
    import com.convoy.gpack.dao.UserTypeMasterDAO;
    import com.convoy.gpack.pack.models.UserTypeMaster;
    
    @ManagedBean(name = "userTypeTabMenuBean")
    @SessionScoped
    public class UserTypeTabMenuBean implements Serializable {
        private static final long serialVersionUID = 1467465633405172689L;
        private static final Logger logger = Logger
                .getLogger(UserTypeTabMenuBean.class);
        private List<UserTypeMaster> userTypeMasters;
        private UserTypeMasterDAO userTypeMasterDAO = new UserTypeMasterDAO();
        private UserTypeMaster selectedUserType;
        private String userType;
    
    // This is the required method to get the datatable list.
        @PostConstruct
        public void init() {
            userTypeMasters = userTypeMasterDAO.getAllUserTypes();
            logger.info("getUserTypeMasters=" + userTypeMasters);
        }
    
        public List<UserTypeMaster> getUserTypeMasters() {
            return userTypeMasters;
        }
    
        public String deleteAction(UserTypeMaster userTypeMaster) {
            logger.info("Deleting the object with id = " + userTypeMaster.getId());
            UserTypeMasterDAO userTypeMasterDAO = new UserTypeMasterDAO();
            int result = userTypeMasterDAO.deleteUserType(userTypeMaster);
            userTypeMasters.remove(userTypeMaster);
            if (result == 1) {
                FacesMessage msg = new FacesMessage(
                        "User type deleted successfuly.");
                FacesContext.getCurrentInstance().addMessage(null, msg);
            } else {
                FacesMessage msg = new FacesMessage("Failed to delete user types.");
                FacesContext.getCurrentInstance().addMessage(null, msg);
            }
            return null;
        }
    
        public void onRowEdit(RowEditEvent event) {
            UserTypeMaster userTypeMaster = (UserTypeMaster) event.getObject();
            logger.info("UPDATING THE USER TYPE MASTER : "
                    + userTypeMaster.getType());
            UserTypeMasterDAO userTypeMasterDAO = new UserTypeMasterDAO();
            userTypeMasterDAO.saveOrUpdateUserTypeMaster(userTypeMaster);
            logger.info("UPDATING THE USER TYPE MASTER : "
                    + userTypeMaster.getType());
    
            FacesMessage msg = new FacesMessage("Edited type ",
                    ((UserTypeMaster) event.getObject()).getType() + "");
            FacesContext.getCurrentInstance().addMessage(null, msg);
        }
    
        public void onRowCancel(RowEditEvent event) {
            FacesMessage msg = new FacesMessage("Editing Cancelled for ",
                    ((UserTypeMaster) event.getObject()).getType() + "");
            FacesContext.getCurrentInstance().addMessage(null, msg);
        }
    
        public UserTypeMaster getSelectedUserType() {
            return selectedUserType;
        }
    
        public void setSelectedUserType(UserTypeMaster selectedUserType) {
            this.selectedUserType = selectedUserType;
        }
    
        public String getUserType() {
            return userType;
        }
    
        public void setUserType(String userType) {
            this.userType = userType;
        }
    
        public void saveAction() {
            logger.info("Saving the object to database...." + userType);
            if (userType.trim().length() < 1) {
                FacesMessage msg = new FacesMessage("Can not save empty user type.");
                FacesContext.getCurrentInstance().addMessage(null, msg);
            }
            UserTypeMasterDAO userTypeMasterDAO = new UserTypeMasterDAO();
            UserTypeMaster userTypeMaster = new UserTypeMaster();
            userTypeMaster.setType(userType);
            int result = userTypeMasterDAO
                    .saveOrUpdateUserTypeMaster(userTypeMaster);
            if (result == 1) {
                FacesMessage msg = new FacesMessage("User type saved.");
                FacesContext.getCurrentInstance().addMessage(null, msg);
                init();
            } else if (result == -2) {
                FacesMessage msg = new FacesMessage("User type already exist..");
                FacesContext.getCurrentInstance().addMessage(null, msg);
            } else {
                FacesMessage msg = new FacesMessage("Failed to save user type.");
                FacesContext.getCurrentInstance().addMessage(null, msg);
            }
        }
    }
    

    That's it~ :)

    Thanks @Swathi for the hint.