Author: rmannibucau
Date: Mon Aug  1 14:11:00 2011
New Revision: 1152786

URL: http://svn.apache.org/viewvc?rev=1152786&view=rev
Log:
managing multiple criterias in QueryProxy (@repository)

Modified:
    
openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/util/QueryProxy.java

Modified: 
openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/util/QueryProxy.java
URL: 
http://svn.apache.org/viewvc/openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/util/QueryProxy.java?rev=1152786&r1=1152785&r2=1152786&view=diff
==============================================================================
--- 
openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/util/QueryProxy.java
 (original)
+++ 
openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/util/QueryProxy.java
 Mon Aug  1 14:11:00 2011
@@ -4,7 +4,12 @@ import org.apache.commons.lang.StringUti
 
 import javax.persistence.EntityManager;
 import javax.persistence.Query;
-import javax.persistence.criteria.*;
+import javax.persistence.criteria.CriteriaBuilder;
+import javax.persistence.criteria.CriteriaQuery;
+import javax.persistence.criteria.Expression;
+import javax.persistence.criteria.Path;
+import javax.persistence.criteria.Predicate;
+import javax.persistence.criteria.Root;
 import javax.persistence.metamodel.EntityType;
 import javax.persistence.metamodel.SingularAttribute;
 import java.lang.reflect.InvocationHandler;
@@ -12,6 +17,7 @@ import java.lang.reflect.Method;
 import java.lang.reflect.ParameterizedType;
 import java.lang.reflect.Type;
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.Collection;
 import java.util.List;
 import java.util.Map;
@@ -21,8 +27,14 @@ import java.util.concurrent.ConcurrentHa
  * @author rmannibucau
  */
 public class QueryProxy implements InvocationHandler {
+    private static final Logger LOGGER = 
Logger.getInstance(LogCategory.OPENEJB, QueryProxy.class);
+
+    // keywords
     public static final String FIND_PREFIX = "find";
+    public static final String BY = "By";
+    public static final String AND = "And";
 
+    // cache for current instance
     private final Map<String, Class<?>> RETURN_TYPES = new 
ConcurrentHashMap<String, Class<?>>();
     private final Map<String, List<String>> CONDITIONS = new 
ConcurrentHashMap<String, List<String>>();
 
@@ -33,6 +45,10 @@ public class QueryProxy implements Invoc
     }
 
     public Object invoke(Object proxy, Method method, Object[] args) throws 
Throwable {
+        if (method.getDeclaringClass().equals(Object.class)) {
+            return method.invoke(this, args);
+        }
+
         if (!method.getName().startsWith(FIND_PREFIX)) {
             throw new IllegalArgumentException("finder should start with 
find");
         }
@@ -71,17 +87,33 @@ public class QueryProxy implements Invoc
         query = query.select(from);
 
         int i = 0;
+        Predicate where = null;
         for (String condition : conditions) {
             SingularAttribute<? super T, ?> attribute = 
et.getSingularAttribute(condition);
             Path<?> path = from.get(attribute);
             Class<?> javaType = attribute.getType().getJavaType();
+
+            Predicate currentClause;
             if (javaType.equals(String.class)) {
-                query = query.where(cb.like((Expression<String>) path, 
(String) args[i++]));
+                currentClause = cb.like((Expression<String>) path, (String) 
args[i++]);
             } else if (Number.class.isAssignableFrom(javaType) || 
javaType.isPrimitive()) {
-                query = query.where(cb.equal(path, args[i++]));
+                currentClause = cb.equal(path, args[i++]);
+            } else {
+                LOGGER.warning("field " + condition + " not found, ignoring");
+                continue;
+            }
+
+            if (where == null) {
+                where = currentClause;
+            } else {
+                where = cb.and(where, currentClause);
             }
         }
 
+        if (where != null) {
+            query = query.where(where);
+        }
+
         return entityManager.createQuery(query);
     }
 
@@ -91,19 +123,22 @@ public class QueryProxy implements Invoc
             parsed = CONDITIONS.get(methodName);
         } else {
             parsed = new ArrayList<String>();
+
             String toParse = methodName.substring(FIND_PREFIX.length());
-            // TODO: parsing
-            if (toParse.startsWith("By")) {
-                toParse = StringUtils.uncapitalize(toParse.substring(2));
-                parsed.add(toParse);
+            if (toParse.startsWith(BY)) {
+                toParse = toParse.substring(2);
+                String[] columns = toParse.split(AND);
+                for (String column: columns) {
+                    parsed.add(StringUtils.uncapitalize(column));
+                }
             }
+
             CONDITIONS.put(methodName, parsed);
         }
         return parsed;
     }
 
-    @Override
-    public String toString() {
+    @Override public String toString() {
         return "OpenEJB :: QueryProxy";
     }
 }


Reply via email to