Rowkey is null when celledit with LazyDataModel - jsf

I am using PrimeFaces 6.1. I have implemented LazyDataModel and I am able to load the rows, owever, when I update a cell, the method onCellEdit is called but event.rowKey is always null.
<p:dataTable emptyMessage="Nessun record trovato."
style="margin-top: 50px" id="cars" var="produit"
value="#{gecomparBean.lazyModel}"
editable="true" editMode="cell" widgetVar="carsTable"
filteredValue="#{gecomparBean.filteredCars}"
rows="10"
lazy="true"
scrollRows="10"
scrollable="true" liveScroll="true" scrollHeight="250"
resizableColumns="true" resizeMode="expand"
draggableColumns="true"
rowStyleClass="#{produit.id == gecomparBean.selectedObject.id ? 'selectedRow' : null}"
>
<p:ajax event="colReorder"
listener="#{gecomparBean.onColumnReorder}" />
<p:ajax event="cellEdit" listener="#{gecomparBean.onCellEdit}"
oncomplete="updateTable()" update=":form:remote" />
Managed Bean: [Session scoped]
wrapper.setSession(activeSession.getId());
lazyModel = new LazyProductDataModel(wrapper);
LazyProductDataModel
public class LazyProductDataModel extends LazyDataModel<ProductSessionDTO> implements Serializable {
private List<ProductSessionDTO> products;
private ProductSearchWrapper wrapper;
public LazyProductDataModel(ProductSearchWrapper wrapp) throws RestRequestException {
super();
wrapper = wrapp;
}
#Override
public ProductSessionDTO getRowData(String rowKey) {
for (ProductSessionDTO car : getProducts()) {
if (car.getId() == (Long.parseLong(rowKey))) {
return car;
}
}
return null;
}
#Override
public Object getRowKey(ProductSessionDTO car) {
return car.getId();
}
#Override
public List<ProductSessionDTO> load(int first, int pageSize, String sortField, SortOrder sortOrder, Map<String, Object> filters) {
List<ProductSessionDTO> data = new ArrayList<ProductSessionDTO>();
try {
System.out.println("First " + first + " Max " + pageSize);
wrapper.setFirst(first);
wrapper.setPageSize(pageSize);
products=ServiceUtils.getInstance().getProducts(wrapper);
if (getProducts() != null) {
Comparator<ProductSessionDTO> lengthComparator = new Comparator<ProductSessionDTO>() {
#Override
public int compare(ProductSessionDTO o1, ProductSessionDTO o2) {
return Long.compare(o1.getId(), o2.getId());
}
};
Collections.sort(products, lengthComparator);
}
} catch (RestRequestException ex) {
Logger.getLogger(LazyProductDataModel.class.getName()).log(Level.SEVERE, null, ex);
}
data = products;
//rowCount
int dataSize = data.size();
this.setRowCount(1000);
//paginate
if (dataSize > pageSize) {
try {
return data.subList(first, first + pageSize);
} catch (IndexOutOfBoundsException e) {
return data.subList(first, first + (dataSize % pageSize));
}
} else {
return data;
}
}
}
Note: I found a post which mentioned that we need to remove the rowkey in order for the getRowKey() to get called automatically - so I have removed that attribute.

Related

Display list of images using p:graphicImage in p:dataGrid after uploading image through p:fileUpload

i am uploading multiple images through p:fileUpload and showing uploaded images in datagrid but after upload the blank panel is created inside datagrid
<p:panelGrid columns="1" layout="grid" style="width: 1000px">
<p:outputLabel value="Upload Images"></p:outputLabel>
<p:fileUpload mode="advanced" multiple="false"
allowTypes="/(\.|\/)(gif|jpe?g|png)$/" dragDropSupport="true"
update="messages,updimgsfields,carouselcomp" sizeLimit="100000"
fileUploadListener="#{drawingattachmnt.handleFileUpload}"></p:fileUpload>
<!-- <p:graphicImage id="gimPhoto" value="#{fileuploadSection.image}" /> -->
</p:panelGrid>
<p:fieldset id="updimgsfields" legend="Uploaded Images">
<p:dataGrid id="updimgsdatagrid" var="upldimg"
value="#{drawingattachmnt.uploadedimages}" columns="4">
<p:panel id="drawingattachmntpnl" header="#{upldimg.displayId}"
style="text-align:center">
<h:panelGrid columns="1" cellpadding="5"
style="width: 100%; height: 200px;">
<p:graphicImage value="#{upldimg.imgcontent}" cache="false"
stream="true">
<f:param id="imgId" name="photo_id" value="#{upldimg.id}" />
</p:graphicImage>
</h:panelGrid>
</p:panel>
<p:draggable for="drawingattachmntpnl" revert="true"
handle=".ui-panel-titlebar" stack=".ui-panel" />
</p:dataGrid>
</p:fieldset>
// file upload function inside bean
public void handleFileUpload(final FileUploadEvent event) throws IOException {
if (event.getFile().getContents() != null) {
final ByteArrayOutputStream byteArrOutputStream = new ByteArrayOutputStream();
BufferedImage uploadedImage = null;
byte[] imageBytes = null;
try {
final String imageType = event.getFile().getContentType() != null
|| event.getFile().getContentType().split("/") != null ? event.getFile().getContentType()
.split("/")[1] : "jpeg";
uploadedImage = ImageIO.read(event.getFile().getInputstream());
ImageIO.write(uploadedImage, imageType, byteArrOutputStream);
imageBytes = byteArrOutputStream.toByteArray();
updimg.setImgcontent(new DefaultStreamedContent(new ByteArrayInputStream(imageBytes), imageType));
updimg.setId(UUID.randomUUID().toString().substring(0, 8));
updimg.setDisplayId("FIG: ");
uploadedimages.add(updimg);
} catch (final IOException io) {
} finally {
try {
byteArrOutputStream.close();
// imageInputStream.close();
} catch (final IOException e1) {
e1.printStackTrace();
}
uploadedImage.flush();
}
final FacesContext context = FacesContext.getCurrentInstance();
context.addMessage(null, new FacesMessage("Successful", "File uploaded Successfully "));
}
// List POJO
import org.primefaces.model.StreamedContent;
public class UploadImage {
public String displayId;
public String id;
public StreamedContent imgcontent;
public UploadImage() {
}
public UploadImage(final String id, final String displayId, final StreamedContent imgcontent) {
this.id = id;
this.displayId = displayId;
this.imgcontent = imgcontent;
}
#Override
public boolean equals(final Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final UploadImage other = (UploadImage) obj;
if (this.id == null ? other.id != null : !this.id.equals(other.id)) {
return false;
}
return true;
}
public String getDisplayId() {
return displayId;
}
public String getId() {
return id;
}
public StreamedContent getImgcontent() {
return imgcontent;
}
#Override
public int hashCode() {
int hash = 7;
hash = 59 * hash + (this.id != null ? this.id.hashCode() : 0);
return hash;
}
public void setDisplayId(final String displayId) {
this.displayId = displayId;
}
public void setId(final String id) {
this.id = id;
}
public void setImgcontent(final StreamedContent imgcontent) {
this.imgcontent = imgcontent;
}
}
I need to show images in datagrid dynamically, but i am getting blank image panel inside datagrid. what should be done ?

Columns (table body) not visible in datatable when using lazydatamodel

Using LazyDataModel does not display columns in the datatable.
I have implemented custom LazyDataModel and I have implemented load method init. However, when I pass the datamodel as a value to the datatable, the columns are not rendered/displayed. But when I pass it as a list, the columns are rendered/displayed.
My LazyDataModel class
public class SearchPmrLazyDataModel extends LazyBaseDataModel<SearchDTO> {
public SearchPmrLazyDataModel() {
}
public void setData() {
if (searchCriteria == null) {
searchCriteria = new PmrSearchCriteriaDTO();
}
setWrappedData(pmrService.searchPmr(searchCriteria, teamId));
}
#Override
public List<PmrSearchResultViewDTO> load(int first, int pageSize, String sortField, SortOrder sortOrder, Map<String, Object> filters) {
setData();
searchCriteria.setPageRequest(determinePage(first, pageSize));
List<PmrSearchResultViewDTO> data = this.cachedData;
// rowCount
int dataSize = data.size();
this.setRowCount(dataSize);
// paginate
if (dataSize > pageSize) {
try {
return data.subList(first, first + pageSize);
} catch (IndexOutOfBoundsException e) {
return data.subList(first, first + (dataSize % pageSize));
}
} else {
return data;
}
}
#Override
public void setRowIndex(int rowIndex) {
if (rowIndex == -1 || getPageSize() == 0) {
super.setRowIndex(-1);
}
else
super.setRowIndex(rowIndex % getPageSize());
}
#Override
public Object getRowKey(PmrSearchResultViewDTO obj) {
return obj.getId();
}
}
My bean
public LazyDataModel<SearchDTO> getCorresLazyDataModel(String selectedTabName, String selectedStateName, String selectedStateName2) {
//returns corresponding lazy data model according to a business logic
return getLazyDataModel(selectedTabName, selectedStateName, selectedStateName2);
}
My XHTML page
<p:dataTable
value="#{formBean.getCorresLazyDataModel(pmrStatusTab, pmrState, pmrState2)}"
var="pmr" id="pmrStatusResultTable#{pmrStatusTab}#{pmrState}"
widgetVar="pmrStatusResultTable#{pmrStatusTab}#{pmrState}"
style="overflow: auto; width: auto! important; table-width:fixed"
rows="10" rowKey="#{pmr.id}" rowStyleClass="odd, even"
reflow="true" scrollWidth="#{formBean.scrollWidth}"
scrollable="true" scrollRows="10" paginator="true" lazy="true"
selectionMode="single" selection="#{formBean.selectedPmr}">
<p:ajax event="rowSelect" process="#this"
listener="#{formBean.onRowSelect}" />
<p:columns value="#{formBean.columns}" var="column"
columnIndexVar="colIndex" sortBy="#{pmr[column.property]}"
sortable="true">
<f:facet name="header">
<h:outputText value="#{column.header}" />
</f:facet>
<h:outputText value="#{pmr[column.property]}" />
</p:columns>
</p:dataTable>
The columns are not rendered/displayed only if I pass datamodel directly. If I pass the value as list, they are displayed, is there anything which I am missing?

Primefaces Datatable converter with filtering

I have the datatable and selectonemenu inside it.
I want to show images in select one menu items.So a have Rating class and converter ratingConverter for Rating class.
I have the next problem here: filtering by this column works just once! When I try to change item, method load(..) from LazyComicsDataModel calls. But after
filtering one time nothing worked! Method load not calls anymore. There are't any exceptions in server.log. When I don't use converter and class Rating (use just String class) it works right any number of times.
I would like to get any ideas why it might be happened.
Code:
Part of xhtml page:
<p:ajax
event="rowSelect"
update=":form:comicsDetail"
oncomplete="PF('comicsDialog').show()"/>
<p:column width="20%"
headerText="#{comicsCatalogueView.columnComicsRating}"
sortBy="#{comics.rating}"
filterBy="#{comics.rating}"
filterMatchMode="exact" >
<f:facet name="filter" >
<p:selectOneMenu
value="#{comicsCatalogueView.rating}"
var="r"
onchange="PF('comicsTable').filter()"
converter="ratingConverter"
>
<f:selectItem
value="#{null}"
itemValue="#{null}" />
<f:selectItems
value="#{comicsFinderManagedBean.ratings}"
var="rating"
itemLabel="#{rating.value} - #{rating.value+1}"
itemValue="#{rating}"
/>
<p:column>
<p:graphicImage value="#{r.image}"/>
</p:column>
</p:selectOneMenu>
</f:facet>
<h:panelGroup>
<pf:rating disabled="true"
totalStars="5"
step="0.1"
value="#{comics.rating}"
>
</pf:rating>
</h:panelGroup>
</p:column>
Rating class:
public class Rating {
public Rating(Integer value, String image) {
this.value = value;
this.image = image;
}
private Integer value;
private String image;
public Integer getValue() {
return value;
}
public void setValue(Integer value) {
this.value = value;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
#Override
public int hashCode() {
int hash = 5;
hash = 71 * hash + (this.value != null ? this.value.hashCode() : 0);
hash = 71 * hash + (this.image != null ? this.image.hashCode() : 0);
return hash;
}
#Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Rating other = (Rating) obj;
if (this.value != other.value && (this.value == null || !this.value.equals(other.value))) {
return false;
}
if ((this.image == null) ? (other.image != null) : !this.image.equals(other.image)) {
return false;
}
return true;
}
#Override
public String toString() {
return "Rating{" + "value=" + value + ", image=" + image + '}';
}
Rating converter:
#FacesConverter("ratingConverter")
public class RatingConverter implements Converter {
#Override
public Object getAsObject(FacesContext fc, UIComponent uic, String value) {
if(value != null && value.trim().length() > 0) {
ComicsFinderManagedBean ratingFinderManagedBean = (ComicsFinderManagedBean)
fc.getExternalContext().getApplicationMap().get("comicsFinderManagedBean");
return ratingFinderManagedBean.getRating(Integer.parseInt(value));
}
else {
return null;
}
}
#Override
public String getAsString(FacesContext fc, UIComponent uic, Object object) {
if(object != null) {
return String.valueOf(((Rating) object).getValue());
}
else {
return null;
}
}
}

Failed to parse the expression [#{privilegeManagedBean.deleteAction(p)}]

I want to delete selected row from data table when click on GraphicImage under CommandLink.
but it is don't work for me.
it gives error :-
/privilegepage.xhtml #66,21 action="#{privilegeManagedBean.deleteAction(p)}" Failed to parse the expression [#{privilegeManagedBean.deleteAction(p)}]
Bean:-Privilege
public class Privilege {
private int id;
private String privilege;
public Privilege() {
}
public Privilege(int id, String privilege) {
this.id = id;
this.privilege = privilege;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getPrivilege() {
return privilege;
}
public void setPrivilege(String privilege) {
this.privilege = privilege;
}
}
bean:- PrivilegeDao.java
public int deletePrivilege(int id) {
PreparedStatement preparedStatement = null;
String sqlprivilege;
Connection dbConnection = null;
int pinsert = 0;
try {
sqlprivilege = "delete privilege from privilege where id=?";
dbConnection = ConnectionDao.getDBConnection();
preparedStatement = dbConnection.prepareStatement(sqlprivilege);
preparedStatement.setInt(2, id);
if(preparedStatement.executeUpdate()==1)
pinsert=1;
else
pinsert=0;
System.out.println("privilege is delete :- ");
} catch (SQLException e) {
System.out.println(e.getMessage());
} finally {
if (preparedStatement != null) {
try {
preparedStatement.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (dbConnection != null) {
try {
dbConnection.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return pinsert;
}
bean :-PrivilegeManagedBean
#ManagedBean(name = "privilegeManagedBean", eager = true)
#SessionScoped
/* #ManagedProperty(value="#param.id") */
public class PrivilegeManagedBean {
private int id;
private String privilege;
private PrivilegeDao pdao;
#SuppressWarnings("unused")
private List<Privilege> privilegeData;
private static int srno;
private int selectedRowIndex = -1;
public PrivilegeManagedBean() {
privilegeData = new ArrayList<Privilege>();
pdao = new PrivilegeDao();
srno = 0;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getPrivilege() {
return privilege;
}
public void setPrivilege(String privilege) {
this.privilege = privilege;
}
public void setPrivilegeData(List<Privilege> privilegeData) {
this.privilegeData = privilegeData;
}
public List<Privilege> getPrivilegeData() {
return this.privilegeData = pdao.getUserList();
}
public int getSelectedRowIndex() {
return selectedRowIndex;
}
public void setSelectedRowIndex(int selectedRowIndex) {
this.selectedRowIndex = selectedRowIndex;
}
public void addDataTableRow() {
pdao.addRow(this.id, this.privilege);
}
private static ArrayList<Privilege> privilegeList = new ArrayList<Privilege>();
public ArrayList<Privilege> getPrivilegeList() {
return privilegeList;
}
public void setPrivilegeList(ArrayList<Privilege> privilege) {
privilegeList = (ArrayList<Privilege>) pdao.getUserList();
}
public int addAction() {
Privilege privilegeitem = new Privilege(this.id, this.privilege);
privilegeList.add(privilegeitem);
return pdao.addPrivilege(this.privilege);
}
public int deleteAction() {
Privilege privilegeitem = new Privilege(this.id, this.privilege);
privilegeList.remove(privilegeitem);
System.out.println("delete Action...");
return pdao.deletePrivilege(this.id);
}
public int onEdit(RowEditEvent event) {
FacesMessage msg = new FacesMessage("Privilege Edited",
((Privilege) event.getObject()).getPrivilege());
FacesContext.getCurrentInstance().addMessage(null, msg);
int pid = pdao.getPrivilegeId(this.privilege);
System.out.println("Privilege Name For Id :- " + this.privilege);
System.out.println("Privilege Id :- " + pid);
return pdao.updatePrivilege(pid, this.privilege);
}
public void onCancel(RowEditEvent event) {
FacesMessage msg = new FacesMessage("Privilege Cancelled");
FacesContext.getCurrentInstance().addMessage(null, msg);
privilegeList.remove((Privilege) event.getObject());
}
public String deletePrivilege(Privilege privilege) {
privilegeList.remove(privilege);
return null;
}
public int getSrno() {
return ++srno;
}
}
Privilege.xhtml
<p:growl id="messages" showDetail="true" />
<p:dataTable value="#{privilegeDao.userList}" var="p"
id="datatbldispprivilege" style="width:500px" editable="true" lazy="true">
<f:facet name="header">
Privilege List
</f:facet>
<p:ajax event="rowEdit" listener="#{privilegeManagedBean.onEdit}"
update=":form1:messages" />
<p:ajax event="rowEditCancel"
listener="#{privilegeManagedBean.onCancel}" update=":form1:messages" />
<p:column headerText="Privileges Name">
<p:cellEditor>
<f:facet name="output">
<p:outputLabel value="#{p.privilege}" name="privilegeoutputname" />
</f:facet>
<f:facet name="input">
<p:inputText value="#{privilegeManagedBean.privilege}"
name="privilegeinputname" />
</f:facet>
</p:cellEditor>
</p:column>
<p:column headerText="Options" style="width:100px">
<p:rowEditor>
</p:rowEditor>
</p:column>
<p:column headerText="Delete" style="width:100px">
<p:commandLink action="#{privilegeManagedBean.deleteAction(p)}"
update="#form">
<p:graphicImage value="/images/deleteicon.png" library="images"
onclick="if (!confirm('Are you sure you want to delete the current record?')) return false"
width="20px" height="20px" />
<f:param name="pname" value="#{p.name}" />
</p:commandLink>
</p:column>
</p:dataTable>
Your PrivilegeManagedBean#deleteAction don't accepts any arguments, but your JSF code passes the current iteration of the data table value to the method. So either don't pass anything to the method:
<p:commandLink action="#{privilegeManagedBean.deleteAction()}" update="#form">
or change the method signature of PrivilegeManagedBean#deleteAction.
you're trying to call the wrong method. action should be like this:
<p:commandLink action="#{privilegeManagedBean.deletePrivilege(p)}" update="#form">

Primefaces PickList target is empty on ajax transfer listener

I have the following p:pickList:
<p:pickList id="activityUser" value="#{activityEntryController.activityUserDualListModel}" var="model" itemLabel="#{model.fullName}"
itemValue="#{model}" converter="activityUserListConverter" styleClass="activityUser" required="true" showSourceFilter="true"
showTargetFilter="true" filterMatchMode="contains">
<f:facet name="sourceCaption">Available</f:facet>
<f:facet name="targetCaption">Selected</f:facet>
<p:column style="width: 400px">
<h:outputText value="#{model.fullName}" />
</p:column>
<p:ajax event="transfer" listener="#{activityEntryController.sortUsersOnTransfer}" update="activityUser" />
</p:pickList>
The value of this p:pickList is a class which extends the DualListModel<T>:
public class ActivityUserDualListModel extends DualListModel<ActivityUserDTO> {
private static final long serialVersionUID = -5369543532197736480L;
public ActivityUserDualListModel() {
super();
initModel();
}
public ActivityUserDualListModel(List<ActivityUserDTO> source, List<ActivityUserDTO> target) {
super(source, target);
initModel();
}
private void initModel() {
IServiceProvider serviceProvider = (IServiceProvider) ContextLoader.getCurrentWebApplicationContext().getBean(Beans.SERVICE_PROVIDER.getName());
IUserService userService = serviceProvider.getUserService();
List<ActivityUserDTO> activityUserDTOs = new ArrayList<ActivityUserDTO>();
for(User user : userService.getAllUser()) {
if(user.isActive()) {
activityUserDTOs.add(constructActivityUserDTO(user));
}
}
Collections.sort(activityUserDTOs, ComparatorUtils.getActivityUserFullNameComparator());
setSource(activityUserDTOs);
setTarget(new ArrayList<ActivityUserDTO>());
}
private ActivityUserDTO constructActivityUserDTO(User user) {
ActivityUserDTO activityUserDTO = new ActivityUserDTO();
activityUserDTO.setId(user.getId());
activityUserDTO.setUsername(user.getUsername());
activityUserDTO.setFullName(user.getFullName());
return activityUserDTO;
}
}
I am instantiation the ActivityUserDualListModel from the PostConstruct method of the ActivityEntryController managed bean.
The listener of the p:ajax which fires at the time of event transfer is:
public void sortUsersOnTransfer(TransferEvent event) {
List<ActivityUserDTO> source = activityUserDualListModel.getSource();
List<ActivityUserDTO> target = activityUserDualListModel.getTarget();
System.out.println("=============================================");
for(ActivityUserDTO dto : source) {
System.out.println(dto.getFullName());
}
System.out.println("=============================================");
System.out.println("=============================================");
for(ActivityUserDTO dto : target) {
System.out.println(dto.getFullName());
}
System.out.println("=============================================");
if(source != null) {
Collections.sort(source, ComparatorUtils.getActivityUserFullNameComparator());
}
if(target != null) {
Collections.sort(target, ComparatorUtils.getActivityUserFullNameComparator());
}
}
The strange thing is the target list in the above method is empty. I am unable to find what I am missing. Any pointer would be very helpful to me.
Edit:
The following class is the converter:
#FacesConverter("activityUserListConverter")
public class ActivityUserListConverter implements Converter {
#Override
public Object getAsObject(FacesContext context, UIComponent component, String submittedValue) {
if (!(component instanceof PickList)) {
return null;
}
if (StringUtils.isBlank(submittedValue)) {
return null;
}
#SuppressWarnings("unchecked")
DualListModel<ActivityUserDTO> pickListModel = (DualListModel<ActivityUserDTO>) ((PickList) component).getValue();
ActivityUserDTO ret = null;
for (ActivityUserDTO activityUserDTO : pickListModel.getSource()) {
if (activityUserDTO.getId().equals(Long.parseLong(submittedValue))) {
ret = activityUserDTO;
break;
}
}
if (ret == null) {
for (ActivityUserDTO activityUserDTO : pickListModel.getTarget()) {
if (activityUserDTO.getId().equals(Long.parseLong(submittedValue))) {
ret = activityUserDTO;
break;
}
}
}
return ret;
}
#Override
public String getAsString(FacesContext context, UIComponent component, Object value) {
if (value == null || value.equals("")) {
return "";
}
if (value instanceof ActivityUserDTO) {
return String.valueOf(((ActivityUserDTO) value).getId());
}
return "";
}
}
Also I have the following p:commandButton for submitting the form. Now the actionListener is not firing, but it fires if I remove the p:pickList form the xhtml.

Resources