001    package org.tynamo;
002    
003    import org.apache.tapestry5.grid.GridDataSource;
004    import org.apache.tapestry5.grid.SortConstraint;
005    import org.tynamo.services.PersistenceService;
006    
007    import java.util.List;
008    
009    /**
010     * A simple implementation of {@link org.apache.tapestry5.grid.GridDataSource} based on a Tynamo PersistenceService and a known
011     * entity class.  This implementation does support multiple {@link org.apache.tapestry5.grid.SortConstraint sort
012     * constraints}; however it assumes a direct mapping from sort constraint property to Hibernate property.
013     * <p/>
014     * This class is <em>not</em> thread-safe; it maintains internal state.
015     * <p/>
016     * Typically, an instance of this object is created fresh as needed (that is, it is not stored between requests).
017     */
018    public class TynamoGridDataSource implements GridDataSource
019    {
020    
021            private final PersistenceService persistenceService;
022    
023            private final Class entityType;
024    
025            private int startIndex;
026    
027            private List preparedResults;
028    
029            public TynamoGridDataSource(PersistenceService persistenceService, Class entityType)
030            {
031                    this.persistenceService = persistenceService;
032                    this.entityType = entityType;
033            }
034    
035            /**
036             * Returns the total number of rows for the configured entity type.
037             */
038            public int getAvailableRows()
039            {
040                    return persistenceService.count(entityType);
041            }
042    
043            /**
044             * Prepares the results, performing a query (applying the sort results, and the provided start and end index). The
045             * results can later be obtained from {@link #getRowValue(int)} }.
046             *
047             * @param startIndex      index, from zero, of the first item to be retrieved
048             * @param endIndex        index, from zero, of the last item to be retrieved
049             * @param sortConstraints zero or more constraints used to set the order of the returned values
050             */
051            public void prepare(int startIndex, int endIndex, List<SortConstraint> sortConstraints)
052            {
053                    this.startIndex = startIndex;
054                    preparedResults = persistenceService.getInstances(entityType, startIndex, (endIndex - startIndex) + 1);
055            }
056    
057            /**
058             * Returns a row value at the given index (which must be within the range defined by the call to {@link
059             * #prepare(int, int, java.util.List)} ).
060             *
061             * @param index of object
062             * @return object at that index
063             */
064            public Object getRowValue(int index)
065            {
066                    return preparedResults.get(index - startIndex);
067            }
068    
069            /**
070             * Returns the entity type, as provided via the constructor.
071             */
072            public Class getRowType()
073            {
074                    return entityType;
075            }
076    }