001    /*
002     * Databinder: a simple bridge from Wicket to Hibernate
003     * Copyright (C) 2006  Nathan Hamblen nathan@technically.us
004    
005     * This library is free software; you can redistribute it and/or
006     * modify it under the terms of the GNU Lesser General Public
007     * License as published by the Free Software Foundation; either
008     * version 2.1 of the License, or (at your option) any later version.
009     * 
010     * This library is distributed in the hope that it will be useful,
011     * but WITHOUT ANY WARRANTY; without even the implied warranty of
012     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013     * Lesser General Public License for more details.
014     * 
015     * You should have received a copy of the GNU Lesser General Public
016     * License along with this library; if not, write to the Free Software
017     * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
018     */
019    package net.databinder.models.cay;
020    
021    import java.util.Iterator;
022    import java.util.List;
023    
024    import net.databinder.cay.Databinder;
025    import net.databinder.models.PropertyDataProvider;
026    
027    import org.apache.cayenne.DataObject;
028    import org.apache.cayenne.query.SelectQuery;
029    import org.apache.wicket.model.IModel;
030    
031    /**
032     * IDataProvider implementation for Cayenne. Note that because Cayenne pagination
033     * is abstracted through a java.util.List, this provider has no performance advantage 
034     * over a PageableListView with a page size set in the SelectQuery.
035     * @author Nathan Hamblen
036     */
037    public class DataProvider extends PropertyDataProvider {
038            
039            private SelectQuery query;
040    
041            public DataProvider(SelectQuery query) {
042                    this.query = query;
043            }
044            
045            public List getList() {
046                    return Databinder.getContext().performQuery(query);
047            }
048            
049            public Iterator iterator(int first, int count) {
050                    query.setPageSize(count);
051                    return getList().subList(first, first + count).iterator();
052            }
053            
054            public int size() {
055                    return getList().size();
056            }
057            
058            @Override
059            protected IModel dataModel(Object object) {
060                    return new DataObjectModel((DataObject)object);
061            }
062    
063    }