Author: hlship
Date: Mon Jun 23 14:16:18 2008
New Revision: 670757
URL: http://svn.apache.org/viewvc?rev=670757&view=rev
Log:
TAPESTRY-2475: Add an implementation of GridDataSource that can be used to
optimize Hibernate queries
Added:
tapestry/tapestry5/trunk/tapestry-hibernate/src/main/java/org/apache/tapestry5/hibernate/HibernateGridDataSource.java
tapestry/tapestry5/trunk/tapestry-hibernate/src/test/java/org/example/app0/pages/GridDemo.java
tapestry/tapestry5/trunk/tapestry-hibernate/src/test/webapp/GridDemo.tml
Modified:
tapestry/tapestry5/trunk/tapestry-hibernate/src/test/java/org/apache/tapestry5/hibernate/integration/TapestryHibernateIntegrationTests.java
tapestry/tapestry5/trunk/tapestry-hibernate/src/test/java/org/example/app0/pages/CachedForm.java
tapestry/tapestry5/trunk/tapestry-hibernate/src/test/java/org/example/app0/services/UserDAO.java
tapestry/tapestry5/trunk/tapestry-hibernate/src/test/java/org/example/app0/services/UserDAOImpl.java
tapestry/tapestry5/trunk/tapestry-hibernate/src/test/webapp/CachedForm.tml
tapestry/tapestry5/trunk/tapestry-hibernate/src/test/webapp/Start.tml
Added:
tapestry/tapestry5/trunk/tapestry-hibernate/src/main/java/org/apache/tapestry5/hibernate/HibernateGridDataSource.java
URL:
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-hibernate/src/main/java/org/apache/tapestry5/hibernate/HibernateGridDataSource.java?rev=670757&view=auto
==============================================================================
---
tapestry/tapestry5/trunk/tapestry-hibernate/src/main/java/org/apache/tapestry5/hibernate/HibernateGridDataSource.java
(added)
+++
tapestry/tapestry5/trunk/tapestry-hibernate/src/main/java/org/apache/tapestry5/hibernate/HibernateGridDataSource.java
Mon Jun 23 14:16:18 2008
@@ -0,0 +1,131 @@
+// Copyright 2008 The Apache Software Foundation
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package org.apache.tapestry5.hibernate;
+
+import org.apache.tapestry5.grid.GridDataSource;
+import org.apache.tapestry5.grid.SortConstraint;
+import org.apache.tapestry5.ioc.internal.util.Defense;
+import org.hibernate.Criteria;
+import org.hibernate.Session;
+import org.hibernate.criterion.Order;
+import org.hibernate.criterion.Projections;
+
+import java.util.List;
+
+/**
+ * A simple implementation of [EMAIL PROTECTED]
org.apache.tapestry5.grid.GridDataSource} based on a Hibernate Session and a
known
+ * entity class. This implementation does support multiple [EMAIL PROTECTED]
org.apache.tapestry5.grid.SortConstraint sort
+ * constraints}; however it assumes a direct mapping from sort constraint
property to Hibernate property.
+ * <p/>
+ * This class is <em>not</em> thread-safe; it maintains internal state.
+ * <p/>
+ * Typically, an instance of this object is created fresh as needed (that is,
it is not stored between requests).
+ */
+public class HibernateGridDataSource implements GridDataSource
+{
+ private final Session session;
+
+ private final Class entityType;
+
+ private int startIndex;
+
+ private List preparedResults;
+
+ public HibernateGridDataSource(Session session, Class entityType)
+ {
+ Defense.notNull(session, "session");
+ Defense.notNull(entityType, "entityType");
+
+ this.session = session;
+ this.entityType = entityType;
+ }
+
+ /**
+ * Returns the total number of rows for the configured entity type.
+ */
+ public int getAvailableRows()
+ {
+ Criteria criteria =
session.createCriteria(entityType).setProjection(Projections.rowCount());
+
+ Integer result = (Integer) criteria.uniqueResult();
+
+ return result;
+ }
+
+ /**
+ * Prepares the results, performing a query (applying the sort results,
and the provided start and end index). The
+ * results can later be obtained from [EMAIL PROTECTED] #getRowValue(int)}
}.
+ *
+ * @param startIndex index, from zero, of the first item to be
retrieved
+ * @param endIndex index, from zero, of the last item to be
retrieved
+ * @param sortConstraints zero or more constraints used to set the order
of the returned values
+ */
+ public void prepare(int startIndex, int endIndex, List<SortConstraint>
sortConstraints)
+ {
+ Defense.notNull(sortConstraints, "sortConstraints");
+
+ // We just assume that the property names in the SortContraint match
the Hibernate
+ // properties.
+
+ Criteria crit =
session.createCriteria(entityType).setFirstResult(startIndex).setFetchSize(
+ endIndex - startIndex + 1);
+
+ for (SortConstraint constraint : sortConstraints)
+ {
+
+ String propertyName =
constraint.getPropertyModel().getPropertyName();
+
+ switch (constraint.getColumnSort())
+ {
+
+ case ASCENDING:
+
+ crit.addOrder(Order.asc(propertyName));
+ break;
+
+ case DESCENDING:
+ crit.addOrder(Order.desc(propertyName));
+ break;
+
+ default:
+ }
+ }
+
+
+ this.startIndex = startIndex;
+
+ preparedResults = crit.list();
+ }
+
+ /**
+ * Returns a row value at the given index (which must be within the range
defined by the call to [EMAIL PROTECTED]
+ * #prepare(int, int, java.util.List)} ).
+ *
+ * @param index of object
+ * @return object at that index
+ */
+ public Object getRowValue(int index)
+ {
+ return preparedResults.get(index - startIndex);
+ }
+
+ /**
+ * Returns the entity type, as provided via the constructor.
+ */
+ public Class getRowType()
+ {
+ return entityType;
+ }
+}
Modified:
tapestry/tapestry5/trunk/tapestry-hibernate/src/test/java/org/apache/tapestry5/hibernate/integration/TapestryHibernateIntegrationTests.java
URL:
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-hibernate/src/test/java/org/apache/tapestry5/hibernate/integration/TapestryHibernateIntegrationTests.java?rev=670757&r1=670756&r2=670757&view=diff
==============================================================================
---
tapestry/tapestry5/trunk/tapestry-hibernate/src/test/java/org/apache/tapestry5/hibernate/integration/TapestryHibernateIntegrationTests.java
(original)
+++
tapestry/tapestry5/trunk/tapestry-hibernate/src/test/java/org/apache/tapestry5/hibernate/integration/TapestryHibernateIntegrationTests.java
Mon Jun 23 14:16:18 2008
@@ -72,7 +72,7 @@
*/
public void using_cached_with_form()
{
- open("/cachedform");
+ start("Cached Form", "setup");
assertTextSeries("name_%d", 0);
type("name", "name1");
@@ -86,9 +86,7 @@
public void commit_after_on_component_methods()
{
- open("/");
-
- clickAndWait("link=CommitAfter Demo");
+ start("CommitAfter Demo");
assertText("name", "Diane");
@@ -107,4 +105,18 @@
}
+ public void grid()
+ {
+ start("Grid Demo", "setup");
+
+ clickAndWait("link=First Name");
+
+ assertText("//[EMAIL PROTECTED]'firstName t-sort-column-ascending']",
"Joe_1");
+
+ clickAndWait("link=First Name");
+
+ assertText("//[EMAIL PROTECTED]'firstName t-sort-column-descending']",
"Joe_9");
+ }
+
+
}
Modified:
tapestry/tapestry5/trunk/tapestry-hibernate/src/test/java/org/example/app0/pages/CachedForm.java
URL:
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-hibernate/src/test/java/org/example/app0/pages/CachedForm.java?rev=670757&r1=670756&r2=670757&view=diff
==============================================================================
---
tapestry/tapestry5/trunk/tapestry-hibernate/src/test/java/org/example/app0/pages/CachedForm.java
(original)
+++
tapestry/tapestry5/trunk/tapestry-hibernate/src/test/java/org/example/app0/pages/CachedForm.java
Mon Jun 23 14:16:18 2008
@@ -16,9 +16,10 @@
import org.apache.tapestry5.annotations.Cached;
import org.apache.tapestry5.annotations.Property;
-import org.apache.tapestry5.hibernate.HibernateSessionManager;
+import org.apache.tapestry5.hibernate.annotations.CommitAfter;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.example.app0.entities.User;
+import org.example.app0.services.UserDAO;
import org.hibernate.Session;
import java.util.List;
@@ -39,16 +40,15 @@
private Session session;
@Inject
- private HibernateSessionManager manager;
+ private UserDAO userDAO;
+ @CommitAfter
void onSuccess()
{
User user = new User();
user.setFirstName(name);
session.save(user);
-
- manager.commit();
}
@SuppressWarnings("unchecked")
@@ -58,5 +58,10 @@
return session.createQuery("from User").list();
}
+ void onActionFromSetup()
+ {
+ userDAO.deleteAll();
+ }
+
}
Added:
tapestry/tapestry5/trunk/tapestry-hibernate/src/test/java/org/example/app0/pages/GridDemo.java
URL:
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-hibernate/src/test/java/org/example/app0/pages/GridDemo.java?rev=670757&view=auto
==============================================================================
---
tapestry/tapestry5/trunk/tapestry-hibernate/src/test/java/org/example/app0/pages/GridDemo.java
(added)
+++
tapestry/tapestry5/trunk/tapestry-hibernate/src/test/java/org/example/app0/pages/GridDemo.java
Mon Jun 23 14:16:18 2008
@@ -0,0 +1,58 @@
+// Copyright 2008 The Apache Software Foundation
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package org.example.app0.pages;
+
+import org.apache.tapestry5.grid.GridDataSource;
+import org.apache.tapestry5.hibernate.HibernateGridDataSource;
+import org.apache.tapestry5.hibernate.annotations.CommitAfter;
+import org.apache.tapestry5.ioc.annotations.Inject;
+import org.example.app0.entities.User;
+import org.example.app0.services.UserDAO;
+import org.hibernate.Session;
+
+public class GridDemo
+{
+ @Inject
+ private Session session;
+
+ @Inject
+ private UserDAO userDAO;
+
+ public GridDataSource getSource()
+ {
+ return new HibernateGridDataSource(session, User.class);
+ }
+
+ @CommitAfter
+ void onActionFromSetup()
+ {
+ userDAO.deleteAll();
+
+ for (int i = 1; i <= 20; i++)
+ {
+ User user = new User();
+
+ String suffix = String.valueOf(i);
+
+ user.setFirstName("Joe_" + suffix);
+ user.setLastName("User");
+ user.setEncodedPassword("####");
+ user.setEmail("joe" + suffix + "@null.com");
+
+ session.persist(user);
+ }
+
+ }
+}
Modified:
tapestry/tapestry5/trunk/tapestry-hibernate/src/test/java/org/example/app0/services/UserDAO.java
URL:
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-hibernate/src/test/java/org/example/app0/services/UserDAO.java?rev=670757&r1=670756&r2=670757&view=diff
==============================================================================
---
tapestry/tapestry5/trunk/tapestry-hibernate/src/test/java/org/example/app0/services/UserDAO.java
(original)
+++
tapestry/tapestry5/trunk/tapestry-hibernate/src/test/java/org/example/app0/services/UserDAO.java
Mon Jun 23 14:16:18 2008
@@ -28,4 +28,7 @@
@CommitAfter
void delete(User... users);
+
+ @CommitAfter
+ void deleteAll();
}
Modified:
tapestry/tapestry5/trunk/tapestry-hibernate/src/test/java/org/example/app0/services/UserDAOImpl.java
URL:
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-hibernate/src/test/java/org/example/app0/services/UserDAOImpl.java?rev=670757&r1=670756&r2=670757&view=diff
==============================================================================
---
tapestry/tapestry5/trunk/tapestry-hibernate/src/test/java/org/example/app0/services/UserDAOImpl.java
(original)
+++
tapestry/tapestry5/trunk/tapestry-hibernate/src/test/java/org/example/app0/services/UserDAOImpl.java
Mon Jun 23 14:16:18 2008
@@ -43,4 +43,12 @@
{
for (User user : users) session.delete(user);
}
+
+ public void deleteAll()
+ {
+ for (User u : findAll())
+ {
+ session.delete(u);
+ }
+ }
}
Modified:
tapestry/tapestry5/trunk/tapestry-hibernate/src/test/webapp/CachedForm.tml
URL:
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-hibernate/src/test/webapp/CachedForm.tml?rev=670757&r1=670756&r2=670757&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-hibernate/src/test/webapp/CachedForm.tml
(original)
+++ tapestry/tapestry5/trunk/tapestry-hibernate/src/test/webapp/CachedForm.tml
Mon Jun 23 14:16:18 2008
@@ -1,21 +1,26 @@
<html xmlns:t="http://tapestry.apache.org/schema/tapestry_5_0_0.xsd">
-<body>
- <h1>@Cached with a form</h1>
+ <body>
+ <h1>@Cached with a form</h1>
- <p> Entered data: </p>
+ <p>Entered data:</p>
- <ul>
- <t:loop index="index" source="users" value="user">
- <li id="name_${index}">${user.firstName}</li>
- </t:loop>
- </ul>
+ <ul>
+ <t:loop index="index" source="users" value="user">
+ <li id="name_${index}">${user.firstName}</li>
+ </t:loop>
+ </ul>
- <hr/>
+ <hr/>
- <t:form>
- <t:label for="name" />: <t:textfield t:id="name" t:validate="required"/>
- <input type="submit" />
- </t:form>
+ <t:form>
+ <t:label for="name"/>:
+ <t:textfield t:id="name" t:validate="required"/>
+ <input type="submit"/>
+ </t:form>
-</body>
+ <p>
+ <t:actionlink t:id="setup">setup</t:actionlink>
+ </p>
+
+ </body>
</html>
Added: tapestry/tapestry5/trunk/tapestry-hibernate/src/test/webapp/GridDemo.tml
URL:
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-hibernate/src/test/webapp/GridDemo.tml?rev=670757&view=auto
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-hibernate/src/test/webapp/GridDemo.tml
(added)
+++ tapestry/tapestry5/trunk/tapestry-hibernate/src/test/webapp/GridDemo.tml
Mon Jun 23 14:16:18 2008
@@ -0,0 +1,17 @@
+<html xmlns:t="http://tapestry.apache.org/schema/tapestry_5_0_0.xsd">
+ <head>
+ <title>Grid Data Source Demo</title>
+ </head>
+ <body>
+ <h2>Demo of the Hibernate GridDataSource
+ </h2>
+
+ <t:grid source="source"/>
+
+ <p>
+ <t:actionlink t:id="setup">setup</t:actionlink>
+ the data
+ </p>
+
+ </body>
+</html>
Modified: tapestry/tapestry5/trunk/tapestry-hibernate/src/test/webapp/Start.tml
URL:
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-hibernate/src/test/webapp/Start.tml?rev=670757&r1=670756&r2=670757&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-hibernate/src/test/webapp/Start.tml
(original)
+++ tapestry/tapestry5/trunk/tapestry-hibernate/src/test/webapp/Start.tml Mon
Jun 23 14:16:18 2008
@@ -9,6 +9,12 @@
<li>
<t:actionlink t:id="commitAfter">CommitAfter
Demo</t:actionlink>
</li>
+ <li>
+ <t:pagelink page="GridDemo">Grid Demo</t:pagelink>
+ </li>
+ <li>
+ <t:pagelink page="CachedForm">Cached Form</t:pagelink>
+ </li>
</ul>
</body>
</html>