deltaspike-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tandrasc...@apache.org
Subject deltaspike git commit: DELTASPIKE-1268 QueryProcessorFactory should be a bean
Date Wed, 07 Jun 2017 14:55:00 GMT
Repository: deltaspike
Updated Branches:
  refs/heads/master b48416ae6 -> d9b1acb2b


DELTASPIKE-1268 QueryProcessorFactory should be a bean

Project: http://git-wip-us.apache.org/repos/asf/deltaspike/repo
Commit: http://git-wip-us.apache.org/repos/asf/deltaspike/commit/d9b1acb2
Tree: http://git-wip-us.apache.org/repos/asf/deltaspike/tree/d9b1acb2
Diff: http://git-wip-us.apache.org/repos/asf/deltaspike/diff/d9b1acb2

Branch: refs/heads/master
Commit: d9b1acb2b54b87d5ba66bba4305d2723318d869a
Parents: b48416a
Author: Thomas Andraschko <tandraschko@apache.org>
Authored: Wed Jun 7 16:54:52 2017 +0200
Committer: Thomas Andraschko <tandraschko@apache.org>
Committed: Wed Jun 7 16:54:52 2017 +0200

----------------------------------------------------------------------
 .../data/impl/builder/DelegateQueryBuilder.java |  4 +-
 .../builder/result/QueryProcessorFactory.java   | 67 ++++++--------------
 .../impl/handler/CdiQueryInvocationContext.java |  2 +-
 .../impl/meta/RepositoryMethodMetadata.java     | 32 +++++-----
 .../RepositoryMethodMetadataInitializer.java    | 20 +++---
 5 files changed, 49 insertions(+), 76 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/deltaspike/blob/d9b1acb2/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/builder/DelegateQueryBuilder.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/builder/DelegateQueryBuilder.java
b/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/builder/DelegateQueryBuilder.java
index b86252c..05d0e57 100644
--- a/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/builder/DelegateQueryBuilder.java
+++ b/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/builder/DelegateQueryBuilder.java
@@ -60,11 +60,11 @@ public class DelegateQueryBuilder extends QueryBuilder
             if (delegate != null)
             {
                 Object result = invoke(delegate, context);
-                if (result instanceof Collection && context.getRepositoryMethodMetadata().isStreamAsReturnType())
+                if (result instanceof Collection && context.getRepositoryMethodMetadata().isReturnsStream())
                 {
                     return StreamUtil.wrap(result);
                 }
-                else if (context.getRepositoryMethodMetadata().isOptionalAsReturnType())
+                else if (context.getRepositoryMethodMetadata().isReturnsOptional())
                 {
                     return OptionalUtil.wrap(result);
                 }

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/d9b1acb2/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/builder/result/QueryProcessorFactory.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/builder/result/QueryProcessorFactory.java
b/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/builder/result/QueryProcessorFactory.java
index 8812ae1..4139cae 100644
--- a/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/builder/result/QueryProcessorFactory.java
+++ b/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/builder/result/QueryProcessorFactory.java
@@ -18,8 +18,8 @@
  */
 package org.apache.deltaspike.data.impl.builder.result;
 
-import java.lang.reflect.Method;
 import java.util.List;
+import javax.enterprise.context.ApplicationScoped;
 
 import javax.persistence.NoResultException;
 import javax.persistence.Query;
@@ -30,73 +30,44 @@ import org.apache.deltaspike.data.api.Modifying;
 import org.apache.deltaspike.data.api.QueryResult;
 import org.apache.deltaspike.data.api.SingleResultType;
 import org.apache.deltaspike.data.impl.handler.CdiQueryInvocationContext;
-import org.apache.deltaspike.data.impl.meta.RepositoryMethodPrefix;
+import org.apache.deltaspike.data.impl.meta.RepositoryMethodMetadata;
 
-public final class QueryProcessorFactory
+@ApplicationScoped
+public class QueryProcessorFactory
 {
-
-    private final Method method;
-    private final RepositoryMethodPrefix methodPrefix;
-
-    private QueryProcessorFactory(Method method)
-    {
-        this.method = method;
-        this.methodPrefix = new RepositoryMethodPrefix("", method.getName());
-    }
-
-    private QueryProcessorFactory(Method method, RepositoryMethodPrefix methodPrefix)
+    public QueryProcessor build(RepositoryMethodMetadata methodMetadata)
     {
-        this.method = method;
-        this.methodPrefix = methodPrefix;
-    }
-
-    public static QueryProcessorFactory newInstance(Method method)
-    {
-        return new QueryProcessorFactory(method);
-    }
-
-    public static QueryProcessorFactory newInstance(Method method, RepositoryMethodPrefix
methodPrefix)
-    {
-        return new QueryProcessorFactory(method, methodPrefix);
-    }
-
-    public QueryProcessor build()
-    {
-        if (returns(QueryResult.class))
+        if (returns(methodMetadata, QueryResult.class))
         {
             return new NoOpQueryProcessor();
         }
-        if (returns(List.class))
+        if (returns(methodMetadata, List.class))
         {
             return new ListQueryProcessor();
         }
-        if (streams())
+        if (methodMetadata.isReturnsStream())
         {
             return new StreamQueryProcessor();
         }
-        if (isModifying())
+        if (isModifying(methodMetadata))
         {
-            return new ExecuteUpdateQueryProcessor(returns(Void.TYPE));
+            return new ExecuteUpdateQueryProcessor(returns(methodMetadata, Void.TYPE));
         }
         return new SingleResultQueryProcessor();
     }
 
-    private boolean isModifying()
-    {
-        boolean matchesType = Void.TYPE.equals(method.getReturnType()) ||
-                int.class.equals(method.getReturnType()) ||
-                Integer.class.equals(method.getReturnType());
-        return (method.isAnnotationPresent(Modifying.class) && matchesType) || methodPrefix.isDelete();
-    }
-
-    private boolean returns(Class<?> clazz)
+    private boolean isModifying(RepositoryMethodMetadata methodMetadata)
     {
-        return method.getReturnType().isAssignableFrom(clazz);
+        boolean matchesType = Void.TYPE.equals(methodMetadata.getMethod().getReturnType())
||
+                int.class.equals(methodMetadata.getMethod().getReturnType()) ||
+                Integer.class.equals(methodMetadata.getMethod().getReturnType());
+        return (methodMetadata.getMethod().isAnnotationPresent(Modifying.class) &&
matchesType)
+                || methodMetadata.getMethodPrefix().isDelete();
     }
 
-    private boolean streams()
+    private boolean returns(RepositoryMethodMetadata methodMetadata, Class<?> clazz)
     {
-        return StreamUtil.isStreamReturned(method);
+        return methodMetadata.getMethod().getReturnType().isAssignableFrom(clazz);
     }
 
     private static final class ListQueryProcessor implements QueryProcessor
@@ -152,7 +123,7 @@ public final class QueryProcessorFactory
                     result = !queryResult.isEmpty() ? queryResult.get(0) : null;
             }
             
-            if (context.getRepositoryMethodMetadata().isOptionalAsReturnType())
+            if (context.getRepositoryMethodMetadata().isReturnsOptional())
             {
                 return OptionalUtil.wrap(result);
             }

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/d9b1acb2/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/handler/CdiQueryInvocationContext.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/handler/CdiQueryInvocationContext.java
b/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/handler/CdiQueryInvocationContext.java
index 16d9aee..5586daf 100644
--- a/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/handler/CdiQueryInvocationContext.java
+++ b/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/handler/CdiQueryInvocationContext.java
@@ -304,7 +304,7 @@ public class CdiQueryInvocationContext implements QueryInvocationContext
                 ? repositoryMethodMetadata.getQuery().singleResult()
                 : repositoryMethodMetadata.getMethodPrefix().getSingleResultStyle();
         
-        if (repositoryMethodMetadata.isOptionalAsReturnType() && baseSingleResultType
== SingleResultType.JPA)
+        if (repositoryMethodMetadata.isReturnsOptional() && baseSingleResultType
== SingleResultType.JPA)
         {
             return SingleResultType.OPTIONAL;
         }

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/d9b1acb2/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/meta/RepositoryMethodMetadata.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/meta/RepositoryMethodMetadata.java
b/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/meta/RepositoryMethodMetadata.java
index c57d834..3c4e6af 100644
--- a/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/meta/RepositoryMethodMetadata.java
+++ b/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/meta/RepositoryMethodMetadata.java
@@ -40,8 +40,8 @@ public class RepositoryMethodMetadata
     private Class<? extends QueryInOutMapper<?>> queryInOutMapperClass;
     private boolean queryInOutMapperIsNormalScope;
     
-    private boolean optionalAsReturnType;
-    private boolean streamAsReturnType;
+    private boolean returnsOptional;
+    private boolean returnsStream;
 
     public RepositoryMethodMetadata()
     {
@@ -123,16 +123,6 @@ public class RepositoryMethodMetadata
         this.queryInOutMapperIsNormalScope = queryInOutMapperIsNormalScope;
     }
 
-    public boolean isOptionalAsReturnType()
-    {
-        return optionalAsReturnType;
-    }
-
-    public void setOptionalAsReturnType(boolean optionalAsReturnType)
-    {
-        this.optionalAsReturnType = optionalAsReturnType;
-    }
-
     public Query getQuery()
     {
         return query;
@@ -153,13 +143,23 @@ public class RepositoryMethodMetadata
         this.modifying = modifying;
     } 
 
-    public boolean isStreamAsReturnType()
+    public boolean isReturnsOptional()
+    {
+        return returnsOptional;
+    }
+
+    public void setReturnsOptional(boolean returnsOptional)
+    {
+        this.returnsOptional = returnsOptional;
+    }
+
+    public boolean isReturnsStream()
     {
-        return streamAsReturnType;
+        return returnsStream;
     }
 
-    public void setStreamAsReturnType(boolean streamAsReturnType)
+    public void setReturnsStream(boolean returnsStream)
     {
-        this.streamAsReturnType = streamAsReturnType;
+        this.returnsStream = returnsStream;
     }
 }

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/d9b1acb2/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/meta/RepositoryMethodMetadataInitializer.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/meta/RepositoryMethodMetadataInitializer.java
b/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/meta/RepositoryMethodMetadataInitializer.java
index f0519d2..e21fcdc 100644
--- a/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/meta/RepositoryMethodMetadataInitializer.java
+++ b/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/meta/RepositoryMethodMetadataInitializer.java
@@ -28,6 +28,7 @@ import javax.enterprise.context.ApplicationScoped;
 
 import javax.enterprise.inject.spi.Bean;
 import javax.enterprise.inject.spi.BeanManager;
+import javax.inject.Inject;
 
 import org.apache.deltaspike.core.util.OptionalUtil;
 import org.apache.deltaspike.core.util.StreamUtil;
@@ -43,34 +44,35 @@ import org.apache.deltaspike.data.impl.builder.result.QueryProcessorFactory;
 @ApplicationScoped
 public class RepositoryMethodMetadataInitializer
 {
+    @Inject
+    private QueryProcessorFactory queryProcessorFactory;
+    
     public RepositoryMethodMetadata init(RepositoryMetadata repositoryMetadata, Method method,
BeanManager beanManager)
     {
         RepositoryMethodMetadata repositoryMethodMetadata = new RepositoryMethodMetadata();
         
         repositoryMethodMetadata.setMethod(method);
 
+        repositoryMethodMetadata.setReturnsOptional(
+                OptionalUtil.isOptionalReturned(method));
+        repositoryMethodMetadata.setReturnsStream(
+                StreamUtil.isStreamReturned(method));
+        
         repositoryMethodMetadata.setQuery(method.isAnnotationPresent(Query.class)
                 ? method.getAnnotation(Query.class) : null);
         repositoryMethodMetadata.setModifying(method.isAnnotationPresent(Modifying.class)
                 ? method.getAnnotation(Modifying.class) : null);
-        
-        
+
         repositoryMethodMetadata.setMethodPrefix(new RepositoryMethodPrefix(
                     repositoryMetadata.getRepositoryClass().getAnnotation(Repository.class).methodPrefix(),
                     method.getName()));
         repositoryMethodMetadata.setMethodType(
                 extractMethodType(repositoryMetadata, repositoryMethodMetadata));
         
-        repositoryMethodMetadata.setQueryProcessor(
-                QueryProcessorFactory.newInstance(method, repositoryMethodMetadata.getMethodPrefix()).build());
+        repositoryMethodMetadata.setQueryProcessor(queryProcessorFactory.build(repositoryMethodMetadata));
         
         repositoryMethodMetadata.setQueryInOutMapperClass(
                 extractMapper(method, repositoryMetadata));
-        
-        repositoryMethodMetadata.setOptionalAsReturnType(
-                OptionalUtil.isOptionalReturned(method));
-        repositoryMethodMetadata.setStreamAsReturnType(
-                StreamUtil.isStreamReturned(method));
 
         initQueryRoot(repositoryMetadata, repositoryMethodMetadata);
         initQueryInOutMapperIsNormalScope(repositoryMetadata, repositoryMethodMetadata, beanManager);


Mime
View raw message