ok, I forgot a small fix which I did today:
@SuppressWarnings(value={"unchecked"})
private Object getObjectFromCache() {
if (_cacheWindow.size() == 0)
return null;
if(getRowIndex() - _cacheWindowPos >=
_cacheWindow.size())
refillCache();
return
_cacheWindow.get(Math.min(_cacheWindow.size()-1,
Math.max(0,
getRowIndex() - _cacheWindowPos)));
}
replace the same method below with the fixed one
here
Werner Punz wrote:
Joao Bortoletto wrote:
Hi friends,
Following your suggestions I built my own
DataModel...
(Werner, you warned me about "a" or "b"
ways...
Option "b" really seemed usefull, but I chose "a"
because I have a rope rounding my neck... :-D)
Ok... now I have my own model, but it is
static
yet. So... How can I access other data from
controller
or view from it?
Let me explain better... I'm working
with a managed bean
and its gives data
to a x:dataTable through a "getList" method.
These
data may depends on criteria inside a request,
for
example... how can I achieve this task?
Is there another way to accomplish it?
Yes, you dont have to serve an entire list... you
can implement your own
Datamodel and
have the datamodel itself serving it...
Here is a rather huge example from my code which
has not really publish
quality yet:
note as you can see the code is rather overly
complicated the reason for
this is sort of the prefetching of single pages,
with a pageview
controller I probably could have skipped half the
lines of code, hence,
I warned against approach a of prefetching:
Explanation follows below the code:
public class HibernateDataModel extends DataModel
implements ICacheObject {
// define a cachingWindow to keep the number
of
// connections/accessno at a sane level
// from time to time an isolation level error
// can occur since we seal the objects off
// and then close the connection
// so no real data integrity can be performed
that way
// but it should be good enough for the
average webapp
int _cacheWindowSize = 30;
// array list due to the fact that this one
has the fastes
// absolute access which is necessary for
paging
List _cacheWindow = new
ArrayList(_cacheWindowSize);
// helper idcache for faster access
Map _idCache = new
TreeMap();
// the cache window Position relative to the 0
absolute
int _cacheWindowPos = 0;
boolean _cacheInvalidated =
true;
// the standard variables which are defined by
the system
int _rowCount = -1;
int _rowIndex = -1;
// the associated query accessor
IDataAccessor _queryDelegate = null;
public void resetModel() {
_rowCount = -1;
_rowIndex = -1;
invalidateCache();
refillCache();
}
/**
*
*/
public HibernateDataModel(IDataAccessor
queryDelegate) {
super();
setQueryDelegate(queryDelegate);
// refillCache();
}
public HibernateDataModel() {
super();
}
/*
* (non-Javadoc)
*
* @see
javax.faces.model.DataModel#getRowCount() Return the
number
of rows
* of data objects represented by this
DataModel. If the number
of rows
* is unknown, or no wrappedData is
available, return -1.
*/
public int getRowCount() {
if (_rowCount == 0) {
return -1;
}
return _rowCount;
}
/**
* setter accessed by the query object
*
* @param rowCount
*/
public void setRowCount(int rowCount) {
this._rowCount = rowCount;
// if(_rowIndex >= _rowCount-1)
// _rowIndex = -1;
}
// returns true if the dataset is within
boolean isCached() {
if (_cacheWindow == null ||
_cacheWindow.size() == 0)
return false;
else
return ((_cacheWindowPos <=
getRowIndex()) && (getRowIndex()
< (_cacheWindowPos + _cacheWindow.size())));
}
/**
* refills the cache window upon the given row
index this one in the
long
* term will possible be called from outside
also
*/
@SuppressWarnings(value={"unchecked"})
public void refillCache() {
// calculate a decent new cache window
pos, if possible with the
current
// row index
// in the middle
_cacheWindowPos = Math.max(0,
getRowIndex() - (_cacheWindowSize
/ 2));
// refill the cache with the query
if (_cacheWindowSize > 0) {
_cacheWindow =
_queryDelegate.queryDatasets(_cacheWindowPos,
_cacheWindowSize);
_idCache = new TreeMap();
for (Object elem : _cacheWindow) {
_idCache.put(((BaseDatabaseObject)
elem).getId(), elem);
}
setRowCount(_queryDelegate.getSize()
);