Editable Data table in UserWizard with dynamic value selection query - jsf

initiate.xhtml
I am using the primefaces wizard to show the sequential flow for an application.In one of the tab namely member details, I have an editable data table .For the Name column,I want the user to select the name from the available list of users having Name,designation,division on clicking a button
I am using another datatable in the dialog box on clicking on a button.
But how can i update the selected value in the original data table after clicking on add button.
<h:body>
<h:form id="compositionmaster">
<p:growl id="growl" sticky="true" showDetail="true"/>
<p:wizard widgetVar="wiz" flowListener="#{userWizard.onFlowProcess}" step="address" >
<p:tab id="address" title="Member Details">
<p:panel header="Member Selection" >
<h:messages errorClass="error"/>
<p:dataTable id="outputtable" var="membertable" value="#{tableBean.compositionRoles}" editable="true" editMode="cell" >
<p:column headerText="Role" >
<h:outputText value="#{membertable.role}" />
</p:column>
<p:column headerText="Type">
<p:cellEditor>
<f:facet name="input">
<p:selectOneMenu value="#{userWizard.user.type}">
<f:selectItem itemLabel="--Select--" itemValue="0" />
<f:selectItem itemLabel="Internal" itemValue="1" />
<f:selectItem itemLabel="External" itemValue="2" />
</p:selectOneMenu>
</f:facet>
<f:facet name="output">
<h:outputText value="#{userWizard.user.type}" />
</f:facet>
</p:cellEditor>
</p:column>
<p:column id="namecol" headerText="Name">
<p:commandButton id="basic" value="..." onclick="dlg.show();" type="button" />
<h:outputText value="#{userBean.selectedUser.name}" />
</p:column>
<p:column id="divcol" headerText="Section/Division">
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{userBean.selectedUser.division}" />
</f:facet>
<f:facet name="input">
<h:inputText value="#{userBean.selectedUser.division}" />
</f:facet>
</p:cellEditor>
</p:column>
</p:dataTable>
<p:dialog id="dialog" header="UserList" widgetVar="dlg" >
<h:form id="userform">
<p:dataTable var="user" value="#{userBean.users}" selection="#{userBean.selectedUser}" rowKey="#{user.name}" >
<p:column selectionMode="single"/>
<p:column headerText="Name">
<h:outputText value="#{user.name}" />
</p:column>
<p:column headerText="Division">
<h:outputText value="#{user.division}" />
</p:column>
<f:facet name="footer">
<p:commandButton update=":compositionmaster:namecol" ajax="true" onclick="dlg.hide()" value="Add" id="addmember"/>
</f:facet>
</p:dataTable>
</h:form>
</p:dialog>
UserWizard.java
package committee;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.event.ActionEvent;
import javax.faces.context.FacesContext;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import org.primefaces.event.FlowEvent;
import java.io.Serializable;
import javax.faces.bean.ViewScoped;
import office.UserBean;
#ManagedBean
#ViewScoped
public class UserWizard implements Serializable{
private User user = new User();
private Committee comm =new Committee();
private boolean skip;
private UserBean userbean;
private static final Logger logger = Logger.getLogger(UserWizard.class.getName());
public UserBean getUserbean() {
return userbean;
}
public void setUserbean(UserBean userbean) {
this.userbean = userbean;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public Committee getComm() {
return comm;
}
public void setComm(Committee comm) {
this.comm = comm;
}
public void save(ActionEvent actionEvent) {
//Persist user
FacesMessage msg = new FacesMessage("Successful", "Welcome :" + user.getName());
FacesContext.getCurrentInstance().addMessage(null, msg);
}
public boolean isSkip() {
return skip;
}
public void setSkip(boolean skip) {
this.skip = skip;
}
public String onFlowProcess(FlowEvent event) {
System.out.println(event.getOldStep());
System.out.println(event.getNewStep());
System.out.println(skip);
logger.log(Level.INFO, "Current wizard step:{0}", event.getOldStep());
logger.log(Level.INFO, "Next step:{0}", event.getNewStep());
return event.getNewStep();
}
}
UserBean.java
package office;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import java.io.Serializable;
import committee.User;
import committee.UserDataModel;
import java.util.List;
import java.util.ArrayList;
#ManagedBean
#ViewScoped
public class UserBean implements Serializable{
private List<User> users;
public UserBean() {
users = new ArrayList<User>();
users.add(new User("Kanika","Development"));
users.add(new User("Shreya","Development"));
users.add(new User("Tushti","Development"));
users.add(new User("abc","Marketing"));
}
public List<User> getUsers() {
return users;
}
public void setUsers(List<User> users) {
this.users = users;
}
private User selectedUser;
private UserDataModel userDataModel;
public UserDataModel getUserDataModel() {
return userDataModel;
}
public void setUserDataModel(UserDataModel userDataModel) {
this.userDataModel = userDataModel;
}
public User getSelectedUser() {
return selectedUser;
}
public void setSelectedUser(User selectedUser) {
this.selectedUser = selectedUser;
}
}

Related

p:commandButton in the second datatable doesn't work

I have two datatables in the same page. The first one is loaded when the page loads. The second one is loaded when a row is clicked. I have a command button in each row. In the first datatable works fine. But, in the second one, the parameter value is null. It only works if I use immediate attribute equals true. Does someone know why?
My page:
<h:form id="form">
<p:dataTable id="dtOwner" selectionMode="single" var="owner" value="#{ownerBean.retornaOwners}"
selection="#{ownerBean.ownerSelected}" rowKey="#{owner.id_owner}">
<p:ajax update="form:pgCars" process="#form" event="rowSelect" listener="#{ownerBean.onRowSelect}" />
<p:column headerText="Actions">
<p:commandButton value="Exclude" actionListener="#{ownerBean.excludeOwner(owner)}"></p:commandButton>
</p:column>
<p:column headerText="Name">
<h:outputText value="#{owner.name}"></h:outputText>
</p:column>
<p:column headerText="Surname">
<h:outputText value="#{owner.surname}"></h:outputText>
</p:column>
</p:dataTable>
<p:panelGrid id="pgCars">
<p:dataTable id="dtCars" var="car" value="#{ownerBean.ownerSelected.listCars}"
selection="#{ownerBean.ownerSelected}" rowKey="#{owner.id_owner}">
<p:column headerText="Actions" >
<p:commandButton value="Exclude" process="#all" update="form" actionListener="#{ownerBean.excludeCar(car)}"></p:commandButton>
</p:column>
<p:column headerText="id_Car" >
<h:outputText value="#{car.id_car}"></h:outputText>
</p:column>
<p:column headerText="id_owner" >
<h:outputText value="#{car.id_owner}"></h:outputText>
</p:column>
<p:column headerText="tipo_carro" >
<h:outputText value="#{car.tipo_carro}"></h:outputText>
</p:column>
<p:column headerText="modelo_carro" >
<h:outputText value="#{car.modelo_carro}"></h:outputText>
</p:column>
</p:dataTable>
</p:panelGrid>
</h:form>
My bean:
package com.tutorial;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import org.primefaces.event.SelectEvent;
#ManagedBean(name="ownerBean")
#SessionScoped
public class OwnerBean {
#ManagedProperty(value = "#{ownerDAO}")
private OwnerDAO ownerDAO;
public void setOwnerDAO(OwnerDAO ownerDAO) {
this.ownerDAO = ownerDAO;
}
private Owner ownerSelected;
public void excludeOwner(Owner owner) {
System.out.println("Excluding " + owner.getName());
}
public void excludeCar(Car car) {
System.out.println("Excluding car " + car.getModelo_carro());
}
public void onRowSelect(SelectEvent event) {
FacesMessage msg = new FacesMessage("Owner Selected", Integer.toString(
( (Owner) event.getObject()).getId_owner()));
FacesContext.getCurrentInstance().addMessage(null, msg);
ownerSelected = (Owner) event.getObject();
ownerSelected.setListCars(new ArrayList<Car>());
ownerSelected.setListCars(ownerDAO.getCars(ownerSelected));
}
public List<Owner> getRetornaOwners() {
List<Owner> list = new ArrayList<>();
try {
list = ownerDAO.getOwners();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}
public Owner getOwnerSelected() {
return ownerSelected;
}
public void setOwnerSelected(Owner ownerSelected) {
this.ownerSelected = ownerSelected;
}
}
The excludeOwner(Owner owner) function works fine, but the excludeCars(Car car) doesn't. I always get null in car value.
Thanks
I use process="#this" in the commandButton and works. Also, I remove update attribute from commandButton and process attribute from ajax. Thanks a lot
Ajax:
<p:ajax update="form:dtCars" event="rowSelect" listener="#{ownerBean.onRowSelect}" />
Command button:
<p:commandButton value="Exclude" process="#this" actionListener="#{ownerBean.excludeCar(car)}"></p:commandButton>

Primefaces openDialog(dlg.xhtml) reopens with the same data, shouldn't the viewscope beans get destroyed every time I close the dialog?

I'm trying to use a Primefaces dialog to insert an entity object in a database, it works the first time I use the dialog, the keeps the same bean inside the dialog when I try to use it the second time, here is my code:
this is in the file index.xhtml I have a table called patients and this is the footer:
<f:facet name="footer">
<p:outputPanel>
<p:growl id="growl" showDetail="true" />
<p:commandButton value="Add Patient" icon="ui-icon-extlink"
actionListener="#{patientsViewBean.addNewPatientDialog}">
<p:ajax event="dialogReturn"
listener="#{patientsViewBean.onDialogReturn}" update="patients" />
</p:commandButton>
</p:outputPanel>
</f:facet>
the dialog page is addNewDialog.xhtml it contains:
<!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://xmlns.jcp.org/jsf/facelets"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:p="http://primefaces.org/ui">
<h:head>
<title>Add New Patient</title>
<style type="text/css">
.ui-widget {
font-size: 80%;
}
</style>
</h:head>
<h:body>
<p:panel style="margin: 2px;">
<h:form id="form" >
<p:growl id="msgs" showDetail="true" />
<p:panel title="Patient Personal Information" header="Patient Personal Information">
<p:panelGrid style="margin: 2px 2px 2px 2px" columns="2"
columnClasses="label, value">
<p:outputLabel value="First Name: *"></p:outputLabel>
<p:inputText value="#{newPatientView.patient.firstName}"
required="true" label="First Name"></p:inputText>
<p:outputLabel value="Father Name: *"></p:outputLabel>
<p:inputText value="#{newPatientView.patient.fatherName}"
required="true" label="Father Name"></p:inputText>
<p:outputLabel value="Family Name: *"></p:outputLabel>
<p:inputText value="#{newPatientView.patient.familyName}"
required="true" label="Family Name"></p:inputText>
<p:outputLabel value="Mother Name: *"></p:outputLabel>
<p:inputText value="#{newPatientView.patient.motherName}"
required="true" label="Mother Name"></p:inputText>
<p:outputLabel value="Birth Date"></p:outputLabel>
<p:calendar id="bdate" value="#{newPatientView.patient.birthDate}"
locale="ar" navigator="true" yearRange="c-120:c" effect="fade"
mask="true" pattern="yyyy-MM-dd" required="true" />
<p:outputLabel value="Birth Place: "></p:outputLabel>
<p:inputText value="#{newPatientView.patient.birthPlace}"
required="false" label="Birth Place"></p:inputText>
</p:panelGrid>
</p:panel>
<p:panel title="Contact Information" header="Contact Information">
<p:remoteCommand name="updateButtonsStatus"
process="#this contactInfoTable:buttons"
update="contactInfoTable:buttons"></p:remoteCommand>
<p:dataTable id="contactInfoTable" var="cntctInfo"
selectionMode="single" rowKey="#{cntctInfo}"
selection="#{newPatientView.cntctInfo}"
value="#{newPatientView.patient.contactInfoList}" editable="true"
widgetVar="cellCntctInfo">
<p:ajax event="rowEditInit"
listener="#{newPatientView.onRowEditInit}"
oncomplete="updateButtonsStatus()" />
<p:ajax event="rowEdit" immediate="true"
listener="#{newPatientView.onRowEdit}" update=":form:msgs"
oncomplete="updateButtonsStatus()" />
<p:ajax event="rowEditCancel"
listener="#{newPatientView.onRowCancel}"
oncomplete="updateButtonsStatus()" />
<p:column headerText="Email">
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{cntctInfo.email}" />
</f:facet>
<f:facet name="input">
<p:inputText id="emailInput" value="#{cntctInfo.email}"
style="width:95%" />
</f:facet>
</p:cellEditor>
</p:column>
<p:column headerText="Phone">
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{cntctInfo.phone}" />
</f:facet>
<f:facet name="input">
<p:inputText id="phoneInput" value="#{cntctInfo.phone}"
style="width:95%" />
</f:facet>
</p:cellEditor>
</p:column>
<p:column headerText="Type">
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{cntctInfo.type}" />
</f:facet>
<f:facet name="input">
<p:selectOneMenu id="typeInput" value="#{cntctInfo.type}"
style="width:100%; height: 95%">
<f:selectItem itemLabel="Main" itemValue="Main"></f:selectItem>
<f:selectItem itemLabel="Home" itemValue="Home"></f:selectItem>
<f:selectItem itemLabel="Work" itemValue="Work"></f:selectItem>
<f:selectItem itemLabel="Perm" itemValue="Perm"></f:selectItem>
<f:selectItem itemLabel="Curr" itemValue="Curr"></f:selectItem>
</p:selectOneMenu>
</f:facet>
</p:cellEditor>
</p:column>
<p:column headerText="Edit" style="width:32px">
<p:rowEditor />
</p:column>
<f:facet name="footer">
<p:outputPanel id="buttons" autoUpdate="true">
<p:commandButton value="Add New" process="#this contactInfoTable"
actionListener="#{newPatientView.onAddNewContactInfo}"
update="contactInfoTable :form:msgs"
disabled="#{newPatientView.addContactInfoDisabled}" />
<p:commandButton value="Delete Seleced"
process="#this contactInfoTable"
actionListener="#{newPatientView.onDeleteSelectedContactInfo}"
update="contactInfoTable :form:msgs"
disabled="#{newPatientView.deleteSelectedContactInfoDisabled}" />
</p:outputPanel>
</f:facet>
</p:dataTable>
</p:panel>
<p:panel>
<p:commandButton value="Save" actionListener="#{newPatientView.onDialogApprove}"></p:commandButton>
<p:commandButton value="Discard" actionListener="#{newPatientView.onDialogCancel}"></p:commandButton>
</p:panel>
</h:form>
</p:panel>
</h:body>
</html>
I have two baking beans newPatientView with this code:
package me.bttb.crs.beans.patient;
import java.util.ArrayList;
import java.util.List;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.view.ViewScoped;
import javax.inject.Inject;
import javax.inject.Named;
import org.primefaces.context.RequestContext;
import org.primefaces.event.RowEditEvent;
import me.bttb.crs.model.ContactInfo;
import me.bttb.crs.model.Ptnt;
#Named
#ManagedBean
#ViewScoped
public class NewPatientView {
#Inject
private PatientDAO patientDAO;
private Ptnt patient;
private List<ContactInfo> cntctInfoList;
private ContactInfo cntctInfo;
private boolean addContactInfoDisabled = false;
private boolean deleteSelectedContactInfoDisabled = true;
boolean confirm = false;
public NewPatientView() {
init();
}
public void init() {
this.setPatient(new Ptnt());
this.setCntctInfoList(new ArrayList<>());
this.getPatient().setContactInfoList(this.getCntctInfoList());
}
/////////////// EVENTS//////////////////
public void onAddNewContactInfo(ActionEvent event) {
if (patient.getContactInfoList() == null)
patient.setContactInfoList(new ArrayList<>());
boolean hasNewRow = patient.getContactInfoList().stream()
.filter(ci -> ci.getEmail() == null && ci.getPhone() == null).count() >= 1;
if (hasNewRow) {
FacesMessage msg = new FacesMessage("Can't add new row", "There is an empty row already!!");
FacesContext.getCurrentInstance().addMessage(null, msg);
} else {
ContactInfo cntctInfo = new ContactInfo();
cntctInfo.setType("Main");
cntctInfo.setPrsn(patient);
patient.getContactInfoList().add(cntctInfo);
}
}
public void onDeleteSelectedContactInfo(ActionEvent event) {
if (cntctInfo != null) {
cntctInfoList.remove(cntctInfo);
cntctInfo = null;
}
this.setDeleteSelectedContactInfoDisabled(true);
this.setAddContactInfoDisabled(false);
}
public void onRowEdit(RowEditEvent event) {
this.setDeleteSelectedContactInfoDisabled(true);
this.setAddContactInfoDisabled(false);
ContactInfo editedContactInfo = (ContactInfo) event.getObject();
boolean moreThanOne = patient.getContactInfoList().stream().filter(ci -> ci.equals(editedContactInfo))
.count() > 1;
if (moreThanOne) {
patient.getContactInfoList().remove(editedContactInfo);
FacesMessage msg = new FacesMessage("Row Removed", "The contact information provided is redundent!!");
FacesContext.getCurrentInstance().addMessage(null, msg);
}
}
public void onRowEditInit(RowEditEvent event) {
if (event.getObject() instanceof ContactInfo) {
this.setCntctInfo((ContactInfo) event.getObject());
this.setDeleteSelectedContactInfoDisabled(false);
}
this.setAddContactInfoDisabled(true);
}
public void onRowCancel(RowEditEvent event) {
this.setDeleteSelectedContactInfoDisabled(true);
this.setAddContactInfoDisabled(false);
}
public void onDialogCancel() {
RequestContext.getCurrentInstance().closeDialog(null);
}
public void onDialogApprove() {
RequestContext.getCurrentInstance().closeDialog(patient);
}
public PatientDAO getPatientDAO() {
return patientDAO;
}
public void setPatientDAO(PatientDAO patientDAO) {
this.patientDAO = patientDAO;
}
public Ptnt getPatient() {
return patient;
}
public void setPatient(Ptnt patient) {
this.patient = patient;
}
public ContactInfo getCntctInfo() {
return cntctInfo;
}
public void setCntctInfo(ContactInfo cntctInfo) {
this.cntctInfo = cntctInfo;
}
public boolean isAddContactInfoDisabled() {
return addContactInfoDisabled;
}
public void setAddContactInfoDisabled(boolean addContactInfoDisabled) {
this.addContactInfoDisabled = addContactInfoDisabled;
}
public boolean isDeleteSelectedContactInfoDisabled() {
return deleteSelectedContactInfoDisabled;
}
public void setDeleteSelectedContactInfoDisabled(boolean deleteSelectedContactInfoDisabled) {
this.deleteSelectedContactInfoDisabled = deleteSelectedContactInfoDisabled;
}
public List<ContactInfo> getCntctInfoList() {
return cntctInfoList;
}
public void setCntctInfoList(List<ContactInfo> cntctInfoList) {
this.cntctInfoList = cntctInfoList;
}
}
and here is how I call the dialog in patienViewBean:
package me.bttb.crs.beans.patient;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;
import javax.faces.view.ViewScoped;
import javax.inject.Named;
import org.primefaces.context.RequestContext;
import org.primefaces.event.SelectEvent;
import org.springframework.beans.factory.annotation.Autowired;
import me.bttb.crs.model.Ptnt;
#Named
#ManagedBean
#ViewScoped
public class PatientsViewBean {
#Autowired
private PatientDAO patientDAO;
private List<Ptnt> patients;
private List<Ptnt> filterdPatients;
public PatientsViewBean() {
}
public long getPatientsCount() {
return patientDAO.getPatientsCount();
}
public List<Ptnt> refreshAllPatients() {
this.setPatients(patientDAO.getPatients());
return this.getPatients();
}
public void addNewPatientDialog() {
Map<String, Object> options = new HashMap<String, Object>();
options.put("resizable", false);
options.put("draggable", false);
options.put("modal", true);
//options.put("contentHeight", "95%");
//options.put("height", 400);
//options.put("width", 700);
options.put("closable", false);
RequestContext.getCurrentInstance().openDialog("newPatientDialog", options, null);
}
public void onDialogReturn(SelectEvent event) {
if (event.getObject() != null && event.getObject() instanceof Ptnt) {
patientDAO.addpatient((Ptnt) event.getObject());
refreshAllPatients();
FacesMessage msg = new FacesMessage("Patient Added", "Pateint information added in the database.");
FacesContext.getCurrentInstance().addMessage(null, msg);
} else {
FacesMessage msg = new FacesMessage("operation canceld", "Nothing is saved to the database.");
FacesContext.getCurrentInstance().addMessage(null, msg);
}
}
////////////////////////////////////
public PatientDAO getPatientDAO() {
return patientDAO;
}
public void setPatientDAO(PatientDAO patientDAO) {
this.patientDAO = patientDAO;
}
public List<Ptnt> getPatients() {
return patients;
}
public void setPatients(List<Ptnt> patients) {
this.patients = patients;
}
public List<Ptnt> getFilterdPatients() {
return filterdPatients;
}
public void setFilterdPatients(List<Ptnt> filterdPatients) {
this.filterdPatients = filterdPatients;
}
}
what am I doing wrong, shouldn't the newPatientView get recreated every time the dialog shows up??
please explain the life cycle of the view here.

PrimeFaces - Unable to delete rows in datatable

I am trying to do a basic add and delete db operation using primeface datatable. Add is working fine but cannot get the delete working. When I click on the delete button the delete function is not called.
<h:body>
<h:form>
<p:panel id="toppanel" closable="true" toggleable="true" >
<f:facet name="Store">
<p:outputLabel value="Header" />
</f:facet>
<f:facet name="#Copyright" >
<p:outputLabel value="footer" />
</f:facet>
<p align="left" >
<p:inputText value="#{salesPerson.item.itemId}" id="itemid" >
<f:passThroughAttribute name="placeholder" value="ID"/>
</p:inputText>
<p:inputText value="#{salesPerson.item.itemTitle}" >
<f:passThroughAttribute name="placeholder" value="TITLE"/>
</p:inputText>
<p:inputText value="#{salesPerson.item.itemDesc}" >
<f:passThroughAttribute name="placeholder" value="DESCRIPTION"/>
</p:inputText>
<p:inputText value="#{salesPerson.item.price}" >
<f:passThroughAttribute name="placeholder" value="PRICE"/>
</p:inputText>
</p>
<p align="right" >
<p:commandButton process="#form" id="Create" value="Create" update="out" actionListener="#{salesPerson.addItem()}" />
</p>
<p:dataTable id="out" var="item" value="#{salesPerson.items}" rowKey="#{item.itemId}" selection="#{salesPerson.selectedItems}" >
<p:column selectionMode="multiple" width="1%"/>
<p:column>
<f:facet name="header">
<h:outputText value="ID" />
</f:facet >
<h:outputText value="#{item.itemId}" />
</p:column>
<p:column>
<f:facet name="header">
<h:outputText value="TITLE" />
</f:facet >
<h:outputText value="#{item.itemTitle}" />
</p:column>
<p:column>
<f:facet name="header">
<h:outputText value="DESCRIPTION" />
</f:facet >
<h:outputText value="#{item.itemDesc}" />
</p:column>
<p:column>
<f:facet name="header">
<h:outputText value="PRICE" />
</f:facet >
<h:outputText value="#{item.price}" />
</p:column>
</p:dataTable>
<p align="right" >
<p:commandButton process="#form" id="delete" value="Delete" update="out" actionListener="#{salesPerson.deleteItem(items)}" />
</p>
</p:panel>
</h:form>
</h:body>
</html>
SalesPerson Bean
This is the controller backing bean
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.parthi.controller;
import javax.inject.Inject;
import com.parthi.model.Item;
import com.parthi.backing.ShoppingBean;
import com.parthi.service.StoreKeeper;
import java.io.Serializable;
import java.util.LinkedList;
import java.util.List;
import javax.ejb.EJB;
import javax.enterprise.context.RequestScoped;
import javax.enterprise.context.SessionScoped;
import javax.inject.Named;
#RequestScoped
#Named
public class SalesPerson implements Serializable{
#Inject
private Item item;
private List<Item> items = new LinkedList<>();
private List<Item> selectedItems = new LinkedList<>();
#EJB
private StoreKeeper storeKeeper;
public Item getItem() {
return item;
}
public void setItem(Item item) {
this.item = item;
}
public List<Item> getSelectedItems() {
System.out.println("Number of items getselected" +selectedItems.size());
return selectedItems;
}
public void setSelectedItems(List<Item> selectedItems) {
System.out.println("Number of items setselected" +selectedItems.size());
this.selectedItems = selectedItems;
}
public void addItem(){
storeKeeper.addItem(item);
// System.out.println("Printing add item");
}
public List<Item> getAllItems() {
// System.out.println("Getting all SalesPerson-items");
items = storeKeeper.getAllItems();
return items;
}
public void deleteItem(List<Item> items){
System.out.println("Reached SalesPerson.deleteItem");
int i =0;
while(i <= items.size()){
storeKeeper.deleteItem(items.get(i));
i++;
System.out.println("deleting..." + items.get(i));
}
}
public List<Item> getItems() {
getAllItems();
return items;
}
public void setItems(List<Item> items) {
this.items = items;
}
}
Service EJB
Service is a stateless session bean which perform CRUD operations
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.parthi.service;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import com.parthi.model.Customer;
import com.parthi.model.Item;
#Stateless
public class StoreKeeper {
#PersistenceContext
private EntityManager em;
public List<Item> getAllItems(){
System.out.println("Getting all StoreKeeper-item");
return em.createQuery("FROM Item p", Item.class).getResultList();
}
public void addItem(Item item){
System.out.println("Printing all StoreKeeper add item");
em.persist(item);
}
public void deleteItem(Item item){
em.merge(item);
em.remove(item);
}
}

Selecting from p:dataTable list and show details to another p:dataTable list

I have a table showing list from a bean. When I click one of the rows, I want to view details from another bean list what would I write to value to second detail datatable list ?
Let say I have a bean of list students datatable containing name, surname and numbers, when I click a row, on the second datatable there is a bean list of student's address, city and country
Now I can System.out.print the adress detail of student when I click to row in student table but I can't show it on datatable
I'm asking how I can take the values to a datatable, what will be the value in datatable?
Thanks for your help
<?xml version="1.0" encoding="UTF-8"?>
<!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: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>
<f:view>
<h:form id="form">
<p:dataTable id="users" var="user" value="#{userOS.osList}"
paginator="true" rows="10" rowKey="#{user.kisiid}"
selection="#{userOS.selectedOS}" selectionMode="single">
<f:facet name="header">
Kullanıcı detaylarını görmek için view butonuna tıklayınız
</f:facet>
<p:ajax event="rowSelect" listener="#{userOS.onRowSelect}" update=":form:display"
oncomplete="userDialog" />
<p:column headerText="Student No" sortBy="ogrencino"
filterBy="ogrencino" id="ogrencino">
<h:outputText value="#{user.ogrencino}" />
<f:param name="kid" value="#{userOS.osList.rowIndex}" />
</p:column>
<p:column headerText="Name" sortBy="ad" filterBy="ad" id="ad">
<h:outputText value="#{user.ad}" />
</p:column>
<p:column headerText="Surname" sortBy="soyad" filterBy="soyad"
id="soyad">
<h:outputText value="#{user.soyad}" />
</p:column>
<p:column headerText="Faculty" sortBy="altbirim.ad"
filterBy="altbirim.ad" id="altbirim">
<h:outputText value="#{user.altbirim.ad}" />
</p:column>
<p:column headerText="Department" sortBy="bolum.ad"
filterBy="bolum.ad" id="bolum">
<h:outputText value="#{user.bolum.ad}" />
</p:column>
<p:column headerText="Status" sortBy="ogrencidurum.ad"
filterBy="ogrencidurum.ad" id="ogrencidurum">
<h:outputText value="#{user.ogrencidurum.ad}" />
</p:column>
<f:facet name="footer">
</f:facet>
</p:dataTable>
<p:panel id="dialog" header="User Detail" widgetVar="userDialog">
<h:panelGrid id="panelgrid" columns="2" cellpadding="4">
<p:dataTable id="display" var="adres" value="#{userOS.adresList}">
<p:column headerText="Adres Tipi">
<h:outputText value="#{adres.AddressType}" />
</p:column>
<p:column headerText="Adres">
<h:outputText value="#{adres.Address}" />
</p:column>
<p:column headerText="İl">
<h:outputText value="#{adres.City}" />
</p:column>
<p:column headerText="Ülke">
<h:outputText value="#{adres.Country}" />
</p:column>
</p:dataTable>
</h:panelGrid>
</p:panel>
</h:form>
</f:view>
</h:body>
</html>
And KisiInfoProcess.java code :
package com.revir.process;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
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.hibernate.Session;
import org.hibernate.Transaction;
import org.primefaces.event.SelectEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.revir.managed.bean.AddressBean;
import com.revir.managed.bean.OgrenimSureciBean;
import com.revir.domain.Adres;
import com.revir.domain.AdresDAO;
import com.revir.domain.Kisi;
import com.revir.domain.KisiDAO;
import com.revir.domain.Kisiadresi;
import com.revir.domain.Ogrenimsureci;
import com.revir.domain.OgrenimsureciDAO;
import com.revir.domain.Ulke;
import com.revir.process.KisiInfoProcess;
#ManagedBean(name = "userOS")
#SessionScoped
public class KisiInfoProcess implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private static final Logger log = LoggerFactory
.getLogger(KisiInfoProcess.class);
private List<OgrenimSureciBean> osList;
private List<AddressBean> adresList;
private List<AddressBean> adresListesi;
public List<AddressBean> getAdresListesi() {
return adresListesi;
}
public void setAdresListesi(List<AddressBean> adresListesi) {
this.adresListesi = adresListesi;
}
private OgrenimSureciBean selectedOS;
private AddressBean selectedAdres;
public OgrenimSureciBean getSelectedOS() {
return selectedOS;
}
public void setSelectedOS(OgrenimSureciBean selectedOS) {
this.selectedOS = selectedOS;
}
public AddressBean getSelectedAdres() {
return selectedAdres;
}
public void setSelectedAdres(AddressBean selectedAdres) {
this.selectedAdres = selectedAdres;
}
public List<OgrenimSureciBean> getOsList() {
OgrenimsureciDAO ogrenimsureciDAO = new OgrenimsureciDAO();
List<OgrenimSureciBean> osList = new ArrayList<OgrenimSureciBean>();
for (Iterator i = ogrenimsureciDAO.findByMezunOgrenciler((short) 8)
.iterator(); i.hasNext();) {
Ogrenimsureci og = (Ogrenimsureci) i.next();
OgrenimSureciBean osBean = new OgrenimSureciBean();
osBean.setBolum(og.getBolum());
osBean.setAd(og.getKisiByKisiid().getAd());
osBean.setSoyad(og.getKisiByKisiid().getSoyad());
osBean.setAltbirim(og.getAltbirim());
osBean.setOgrencino(og.getOgrencino());
osBean.setKisiid(og.getKisiByKisiid().getKisiid());
osBean.setOgrencidurum(og.getOgrencidurum());
osList.add(osBean);
System.out.println("osBean : " + osBean.toString());
}
return osList;
}
public void setOsList(List<OgrenimSureciBean> osList) {
this.osList = osList;
}
public void onRowSelect(SelectEvent event) {
System.out.println("On Row Select Metodu çalıştı");
try {
getAdresList();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public List<AddressBean> getAdresList() throws Exception {
if (getSelectedOS() != null) {
log.debug("PersonalInfoProcess - getAddressInfo - Start");
List<AddressBean> adresList = new ArrayList<AddressBean>();
KisiDAO kisiDAO = new KisiDAO();
AdresDAO adresDAO = new AdresDAO();
Long kisiid = getSelectedOS().getKisiid();
System.out.println("kisiid :" + kisiid);
Kisi kisi = kisiDAO.findById(kisiid);
for (Iterator i = kisi.getKisiadresis().iterator(); i.hasNext();) {
Kisiadresi kisiAdresi = (Kisiadresi) i.next();
System.out.println("i :" + i);
Adres tmpAdres = adresDAO.findById(kisiAdresi.getId()
.getAdresid());
if (tmpAdres != null) {
AddressBean address = new AddressBean(kisiid);
if (tmpAdres.getAdresturu() == null) {
address.setAddressType(null);
} else {
address.setAddressType(tmpAdres.getAdresturu().getAd());
System.out.println("Adres Türü:" +tmpAdres.getAdresturu().getAd());
}
address.setAddress(tmpAdres.getAdres());
System.out.println("Şehir:" +tmpAdres.getAdres());
if (tmpAdres.getIl() == null) {
address.setCity(null);
} else {
address.setCity(tmpAdres.getIl().getAd());
System.out.println("Şehir:" +tmpAdres.getIl().getAd());
}
if (tmpAdres.getUlke() == null) {
address.setCountry(null);
} else {
address.setCountry(tmpAdres.getUlke().getAd());
System.out.println("Ülke:" +tmpAdres.getUlke().getAd());
}
adresList.add(address);
System.out.println("adres" + address);
System.out.println("adreslist" + adresList);
}
log.debug("PersonalInfoProcess - getAddressInfo - End / Returning");
}
}
return adresList;
}
public void setAdresList(List<AddressBean> adresList) {
this.adresList = adresList;
}
}

rich dataTable update issue

I have a problem with rich dataTable
actually I populate that dataTable from a database and a made a column editable by putting an inplaceInput in it the problem is when i submit to the backing bean The values doesn't change the backing bean is populated with old values that initially populated the dataTable :
here is my JSF Code of the table:
<center>
<fieldset><rich:dataTable id="tablefiles"
value="#{fileBean.files}" var="file" rows="8"
iterationStatusVar="it">
<rich:column id="Child_Code">
<f:facet name="header">
<h:outputText value="N°tiers filiale" />
</f:facet>
<h:outputText value="#{file.child_Code}" />
</rich:column>
<rich:column id="Mother_Code">
<f:facet name="header">
<h:outputText value="Nom du groupe" />
</f:facet>
<!-- <h:inputText value="#{file.mother_Code}" />-->
<rich:inplaceInput defaultLabel="Click here to edit"
showControls="false" controlsHorizontalPosition="left"
controlsVerticalPosition="bottom" id="inplaceInput"
value="#{file.mother_Code}"
>
</rich:inplaceInput>
</rich:column>
<rich:column>
<f:facet name="header" roles="#riskuser">
<h:outputText value="Détail" />
</f:facet>
<h:commandLink action="#{fileBean.accesDetail}">
<h:graphicImage value="/images/edit.png" alt="edit"
style="border:0;" title="Détail " />
<f:setPropertyActionListener target="#{fileBean.selectedFile}"
value="#{file}" />
</h:commandLink>
</rich:column>
<f:facet name="footer">
<rich:dataScroller page="#{fileBean.page}" />
</f:facet>
</rich:dataTable> <br />
<h:panelGrid columns="3" id="buttonRow">
<a4j:commandButton value="Save" render="tablefiles" execute="#form"
action="#{fileBean.update}">
</a4j:commandButton>
</h:panelGrid></fieldset>
</center>`<center>
<fieldset><rich:dataTable id="tablefiles"
value="#{fileBean.files}" var="file" rows="8"
iterationStatusVar="it">
<rich:column id="Child_Code">
<f:facet name="header">
<h:outputText value="N°tiers filiale" />
</f:facet>
<h:outputText value="#{file.child_Code}" />
</rich:column>
<rich:column id="Mother_Code">
<f:facet name="header">
<h:outputText value="Nom du groupe" />
</f:facet>
<!-- <h:inputText value="#{file.mother_Code}" />-->
<rich:inplaceInput defaultLabel="Click here to edit"
showControls="false" controlsHorizontalPosition="left"
controlsVerticalPosition="bottom" id="inplaceInput"
value="#{file.mother_Code}"
>
</rich:inplaceInput>
</rich:column>
<rich:column>
<f:facet name="header" roles="#riskuser">
<h:outputText value="Détail" />
</f:facet>
<h:commandLink action="#{fileBean.accesDetail}">
<h:graphicImage value="/images/edit.png" alt="edit"
style="border:0;" title="Détail " />
<f:setPropertyActionListener target="#{fileBean.selectedFile}"
value="#{file}" />
</h:commandLink>
</rich:column>
<f:facet name="footer">
<rich:dataScroller page="#{fileBean.page}" />
</f:facet>
</rich:dataTable> <br />
<h:panelGrid columns="3" id="buttonRow">
<a4j:commandButton value="Save" render="tablefiles" execute="#form"
action="#{fileBean.update}">
</a4j:commandButton>
</h:panelGrid></fieldset>
</center>
Here is My bean code :
package ui.bean;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import business.ISRGGestionGarantieCasa;
import common.pojo.SRGarantieCasa;
#ManagedBean
#SessionScoped
public class GarantiCasaBean implements Serializable {
private static final long serialVersionUID = -1509108399715814312L;
private ISRGGestionGarantieCasa fileService;
private int idMsg;
private int page = 1;
private String id_r;
private List<SRGarantieCasa> files = new ArrayList<SRGarantieCasa>();
private SRGarantieCasa selectedFile, file;
private List<String> suggestions = null;
public List<String> getSuggestions() {
return suggestions;
}
public void setSuggestions(List<String> suggestions) {
this.suggestions = suggestions;
}
public SRGarantieCasa getSelectedFile() {
return selectedFile;
}
public void setSelectedFile(SRGarantieCasa selectedfile) {
this.selectedFile = selectedfile;
}
public ISRGGestionGarantieCasa getFileService() {
return fileService;
}
public void setFileService(ISRGGestionGarantieCasa fileService) {
this.fileService = fileService;
}
public int getIdMsg() {
return idMsg;
}
public void setIdMsg(int idMsg) {
this.idMsg = idMsg;
}
public int getPage() {
return page;
}
public void setPage(int page) {
this.page = page;
}
public String getId_r() {
return id_r;
}
public void setId_r(String id_r) {
this.id_r = id_r;
}
public List<SRGarantieCasa> getFiles() {
return files;
}
public void setFiles(List<SRGarantieCasa> files) {
this.files = files;
}
public String viderChamps() {
// vider les champ
id_r = "";
return null;
}
public String search() {
if (id_r == "") {
files = fileService.getListeFiles();
} else {
files.clear();
files = fileService.getFile(id_r);
}
return null;
}
public String accesDetail() {
return "GCUpdate?faces-redirect=true";
}
public String saveFile() {
fileService.updateFile(selectedFile);
return "GarantieCasa?faces-redirect=true";
}
public String ajoutFile() {
fileService.addfile(file);
return "GarantieCasa?faces-redirect=true";
}
public String addFile() {
return "GarantieCasaAdd?faces-redirect=true";
}
public SRGarantieCasa getFile() {
return file;
}
public void setFile(SRGarantieCasa file) {
this.file = file;
}
public GarantiCasaBean() {
/*
* suggestions = new ArrayList<String>(); suggestions.add("Banana");
* suggestions.add("Cranberry"); suggestions.add("Blueberry");
* suggestions.add("Orange"); suggestions.add("Apple");
* suggestions.add("Strawberry");
*/
}
public void update()
{
for (SRGarantieCasa selectedFile : fileService.getListeFiles()){
System.out.println("GarantieCasaBean : Mother Code :"+selectedFile.getMother_Code());
fileService.updateFile(selectedFile);
}
//return "GarantieCasa?faces-redirect=true";
}
}
and here is the spring conf :
<!-- Service UI -->
<bean id="fileBean" class="ui.bean.GarantiCasaBean" scope="request">
<property name="fileService" ref="SO_GestionFile" />
<property name="file" ref="fileCGC" />
</bean>
<bean id="fileCGC" class="common.pojo.SRGarantieCasa" scope="session" />
I found the same issue in that topic :
Richfaces editable dataTable not setting updated values in Bean
but I the #this , #All doesn't work for me :( please Help me

Resources