Monday, 9 September 2013

LazyLoading + filtering error on a jsf/primefaces webapp

LazyLoading + filtering error on a jsf/primefaces webapp

Hei there, I have been working these past weeks with JSF/Primefaces/Mybatis.
My current assignment is to lazy load (at least 20.000 records from an
oracle db) and filter/sort the data...
I've looked around at different tutorials/examples from others but
couldn't really find something to suit me.
Can someone please help me? or give me some hints?
I'm getting an erorr like:
java.util.ConcurrentModificationException
java.util.ArrayList$Itr.checkForComodification(Unknown Source)
java.util.ArrayList$Itr.next(Unknown Source)
Here are some relevant files that I work with:
RCLazy.xhtml page where I display the dataTable:
<p:dataTable var="r"
value="#{rcController.lazyModel}"
rows="10"
paginator="true"
paginatorTemplate="
{CurrentPageReport} {FirstPageLink} {PreviousPageLink}
{PageLinks} {NextPageLink} {LastPageLink}
{RowsPerPageDropdown}"
rowsPerPageTemplate="5,10,15,50,100,500,1000"
editable="true"
lazy="true"
filteredValue="#{rcController.filteredrc}"
>
<p:column filterBy="#{r.nr_curent}"
filterMatchMode="contains" sortBy="#{r.nr_curent}"
headerText="NR_CURENT">
<f:facet name="output">
<h:outputText value="#{r.nr_curent}" />
</f:facet>
</p:column>
RaspunsComenziController.java
@ManagedBean(name="rcController")
@SessionScoped
public class RaspunsComenziController {
private Raspuns_Comenzi selectedRCS;
private LazyDataModel<Raspuns_Comenzi> lazyModel;
private List<Raspuns_Comenzi> filteredrc ;
//Constructor
public RaspunsComenziController() {
}
@PostConstruct
public void init() {
lazyModel = new LazyRaspunsComenziDataModel();
}
//GETTERS + SETTERS
My implementation of The LazyDataModel
LazyRaspunsComenziDataModel.java
public class LazyRaspunsComenziDataModel extends
LazyDataModel<Raspuns_Comenzi> implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1894424302830341097L;
// Data Source for binding data to the DataTable
private List<Raspuns_Comenzi> datasource;
// Selected Page size in the DataTable
private int pageSize;
// Current row index number
private int rowIndex;
// Total row number
private int rowCount;
// Data Access Service
private Raspuns_ComenziService rcServ = new Raspuns_ComenziService();
public LazyRaspunsComenziDataModel() {
}
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public List<Raspuns_Comenzi> load(int first, int pageSize, String
sortField, SortOrder sortOrder, Map<String,String> filters) {
HashMap<String, Object> hMap = new HashMap<String, Object>();
hMap.put("offset", first);
hMap.put("limit", first+pageSize);
datasource = rcServ.getWithLimit(hMap);
//(User.ALL, first, first + pageSize);
for (Raspuns_Comenzi rc : datasource) {
boolean match = true;
for (Iterator<String> it = filters.keySet().iterator(); it
.hasNext();) {
try {
String filterProperty = it.next();
String filterValue = filters.get(filterProperty);
String fieldValue = String.valueOf(rc.getClass()
.getField(filterProperty).get(rc));
if (filterValue == null
|| fieldValue.startsWith(filterValue)) {
match = true;
} else {
match = false;
break;
}
} catch (Exception e) {
match = false;
}
}
if (match) {
datasource.add(rc);
}
}
// if sort field is not null then we sort the field according to
sortfield and sortOrder parameter
if(sortField != null) {
Collections.sort(datasource, new LazySorter(sortField,
sortOrder));
}
setRowCount(rcServ.getRowCount());
return datasource;
}
/**
* Checks if the row is available
* @return boolean
*/
@Override
public boolean isRowAvailable() {
if(datasource == null)
return false;
int index = rowIndex % pageSize ;
return index >= 0 && index < datasource.size();
}
/**
* Gets the user object's primary key
* @param user
* @return Object
*/
@Override
public Object getRowKey(Raspuns_Comenzi rc) {
return rc.getNr_curent();
}
/**
* Returns the user object at the specified position in datasource.
* @return
*/
@Override
public Raspuns_Comenzi getRowData() {
if(datasource == null)
return null;
int index = rowIndex % pageSize;
if(index > datasource.size()){
return null;
}
return datasource.get(index);
}
/**
* Returns the user object that has the row key.
* @param rowKey
* @return
*/
@Override
public Raspuns_Comenzi getRowData(String rowKey) {
if(datasource == null)
return null;
for(Raspuns_Comenzi rc : datasource) {
if(rc.getNr_curent() == Integer.parseInt(rowKey))
return rc;
}
return null;
}
//===== Getters and Setters of LazyRaspsunsComenziDataModel fields
LazySorter.java
/**
* Generic sorting utility class
* @param <T>
*/
public class LazySorter<T> implements Comparator<T> {
private String sortField;
private SortOrder sortOrder;
/**
* initializing sorting field and sorting order
* @param sortField
* @param sortOrder
*/
public LazySorter(String sortField, SortOrder sortOrder) {
this.sortField = sortField;
this.sortOrder = sortOrder;
}
/**
* Comparing object1 and object2 with reflection
* @param object1
* @param object2
* @return
*/
@Override
public int compare(T object1, T object2) {
try {
Field field1 =
object1.getClass().getDeclaredField(this.sortField);
Field field2 =
object2.getClass().getDeclaredField(this.sortField);
field1.setAccessible(true);
field2.setAccessible(true);
Object value1 = field1.get(object1);
Object value2 = field2.get(object2);
@SuppressWarnings({ "rawtypes", "unchecked" })
int value = ((Comparable)value1).compareTo(value2);
return SortOrder.ASCENDING.equals(sortOrder) ? value : -1 *
value;
}
catch(Exception e) {
throw new RuntimeException();
}
}
}
My Model class
Raspuns_Comenzi.java
public class Raspuns_Comenzi {
private int nr_curent;
private String numar;
private Date data;
private String observatii;
private String rowg_orig;
private String rowguid;
private String error_text;
private String status;
private Date insDate;
private Date updDate;
private String insUser;
private String updUser;
// getters + setters
And these 2 methods are from my RaspunsComenziService.java
public int getRowCount() {
SqlSession sqlSession = MyBatisUtil.getSqlSessionFactory()
.openSession();
RaspunsComenziMapper rcMapper =
sqlSession.getMapper(RaspunsComenziMapper.class);
return rcMapper.getRowCount();
}
//GET WITH LIMIT FOR LAZY LOADING
public List<Raspuns_Comenzi> getWithLimit(HashMap<String, Object> hMap) {
SqlSession sqlSession = MyBatisUtil.getSqlSessionFactory()
.openSession();
try {
RaspunsComenziMapper rcMapper =
sqlSession.getMapper(RaspunsComenziMapper.class);
return rcMapper.getWithLimit(hMap);
} finally {
sqlSession.close();
}
}

No comments:

Post a Comment