Hi Joao....

I refactored and cleaned up the datamodel code
yesterday night in a late night session,
you can find something working, you can build upon in sourceforge
also combined with an example in the webapps dir. The code is under the apache2 license so feel free to use and alter it as you like.
There are probably one or two bugs along the way, but it should do its job.

The trick is with this datamodel, that you basically can hook pretty much ever orm mapper into it and it keeps the session time to a minimum.
All you have to implement is a basic query object which serves
the number of total datasets and the dataset pages.
The objects have to implement some kind of surrogate pattern, that means they have to be identifiable by a single id which should work in most cases. Datamodel save state does not work yet, I simply have not implemented it yet propery.



Here is the link to the code:
http://cvs.sourceforge.net/viewcvs.py/jsf-comp/componentsandbox/src/java/net/sf/myfacessandbox/backend/

or directly via anon cvs from cvs.sourceforge.net/jsf-comp

http://cvs.sourceforge.net/viewcvs.py/jsf-comp/componentsandbox/WebRoot/ here is the relevant webroot dir for the example the example is the orm datamodelTest.jsp file
here is the relevant faces-config file:
http://cvs.sourceforge.net/viewcvs.py/jsf-comp/componentsandbox/WebRoot/WEB-INF/components.xml?view=markup

with the relevant part being
<managed-bean>
                <managed-bean-name>ormDatabase</managed-bean-name>
                <managed-bean-class>
                        net.sf.myfacessandbox.backend.database.tests.ORMTable
                </managed-bean-class>
                <managed-bean-scope>application</managed-bean-scope>
        </managed-bean>

for a simple simulated data source which serves the data (you have to replace that one with a real database, but once you see the code you get the clue anyway)

and for the backend bean:
<managed-bean>
<description>backend bean for the generic orm data model demo form</description>
  <managed-bean-name>ormdemoBackendBean</managed-bean-name>

<managed-bean-class>net.sf.myfacessandbox.backend.database.tests.DemoBackendBean</managed-bean-class>
  <managed-bean-scope>request</managed-bean-scope>
 </managed-bean>
 <render-kit>

under
http://cvs.sourceforge.net/viewcvs.py/jsf-comp/componentsandbox/WebRoot/WEB-INF/faces-config.xml?rev=1.15&view=markup

Sorry for shoving in here so much data, but I think once you figure it out how to use it it will help you a lot... what I posted here is just the relevant data for the demo to be able to connect things together, all you basically need is the datamodel, and an implemementation of the query delegate, which you have to do yourself, that is it basically.
The rest of the show is done by the model itself.





Joao Bortoletto wrote:
Hello Werner,

   Thanks for your response... Your code was very
useful to me!
   I have a thousand of doubts about other jsf engines
yet...
   I will try to carry on studying examples and
reading messages from this mailing list...
Thanks again, Joao Bortoletto
--- Werner Punz <[EMAIL PROTECTED]> wrote:


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()

);


setRowIndex(Math.min(Math.max(_queryDelegate.getSize()-1,

0), getRowIndex()));
NavigationBean navBean2 =

(NavigationBean)
JSFUtil.getManagedBean(Constants.MBEAN_NAVBEAN);

=== message truncated ===



                
____________________________________________________ Yahoo! Sports Rekindle the Rivalries. Sign up for Fantasy Football http://football.fantasysports.yahoo.com


Reply via email to