click-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sa...@apache.org
Subject svn commit: r755555 - /incubator/click/trunk/click/examples/src/org/apache/click/examples/service/CustomerService.java
Date Wed, 18 Mar 2009 11:23:18 GMT
Author: sabob
Date: Wed Mar 18 11:23:18 2009
New Revision: 755555

URL: http://svn.apache.org/viewvc?rev=755555&view=rev
Log:
add customer count service

Modified:
    incubator/click/trunk/click/examples/src/org/apache/click/examples/service/CustomerService.java

Modified: incubator/click/trunk/click/examples/src/org/apache/click/examples/service/CustomerService.java
URL: http://svn.apache.org/viewvc/incubator/click/trunk/click/examples/src/org/apache/click/examples/service/CustomerService.java?rev=755555&r1=755554&r2=755555&view=diff
==============================================================================
--- incubator/click/trunk/click/examples/src/org/apache/click/examples/service/CustomerService.java
(original)
+++ incubator/click/trunk/click/examples/src/org/apache/click/examples/service/CustomerService.java
Wed Mar 18 11:23:18 2009
@@ -22,12 +22,19 @@
 import java.util.Date;
 import java.util.List;
 
+import java.util.Map;
+import org.apache.cayenne.CayenneRuntimeException;
 import org.apache.click.examples.domain.Customer;
 import org.apache.click.extras.cayenne.CayenneTemplate;
 
 import org.apache.commons.lang.StringUtils;
 import org.apache.cayenne.exp.Expression;
 import org.apache.cayenne.exp.ExpressionFactory;
+import org.apache.cayenne.map.DbEntity;
+import org.apache.cayenne.map.EntityResolver;
+import org.apache.cayenne.query.IndirectQuery;
+import org.apache.cayenne.query.Query;
+import org.apache.cayenne.query.SQLTemplate;
 import org.apache.cayenne.query.SelectQuery;
 import org.apache.commons.lang.math.NumberUtils;
 
@@ -46,6 +53,14 @@
         return performQuery(query);
     }
 
+    public int getNumberOfCustomers() {
+        CountQuery query = new CountQuery(Customer.class);
+        List result = performQuery(query);
+        Map row = (Map) result.get(0);
+        Integer count = (Integer) row.get("C");
+        return count.intValue();
+    }
+
     public List getCustomersSortedBy(String property, boolean ascending) {
         SelectQuery query = new SelectQuery(Customer.class);
         if (property != null) {
@@ -194,4 +209,31 @@
         return categories;
     }
 
+    /**
+     * A custom Cayenne query which performs a count(*) query on the database.
+     */
+    class CountQuery extends IndirectQuery {
+
+        protected Class objectClass;
+
+        public CountQuery(Class objectClass) {
+            this.objectClass = objectClass;
+        }
+
+        protected Query createReplacementQuery(EntityResolver resolver) {
+            DbEntity entity = resolver.lookupDbEntity(objectClass);
+
+            if (entity == null) {
+                throw new CayenneRuntimeException(
+                    "No entity is mapped for java class: "
+                    + objectClass.getName());
+            }
+
+            String sql = "SELECT #result('count(*)' 'int' 'C') FROM "
+                + entity.getName();
+            SQLTemplate replacement = new SQLTemplate(entity, sql);
+            replacement.setFetchingDataRows(true);
+            return replacement;
+        }
+    }
 }



Mime
View raw message