This is an automated email from the ASF dual-hosted git repository.

tandraschko pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/deltaspike.git


The following commit(s) were added to refs/heads/master by this push:
     new 8d0d3a21f removed reflection hacks
8d0d3a21f is described below

commit 8d0d3a21f5ab77e3d48a2d8497d888b6849c3921
Author: Thomas Andraschko <[email protected]>
AuthorDate: Thu Mar 30 15:05:23 2023 +0200

    removed reflection hacks
---
 .../data/impl/graph/EntityGraphHelper.java         | 117 +---
 .../impl/handler/CdiQueryInvocationContext.java    | 758 ++++++++++-----------
 2 files changed, 395 insertions(+), 480 deletions(-)

diff --git 
a/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/graph/EntityGraphHelper.java
 
b/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/graph/EntityGraphHelper.java
index 9881b2dbd..a9d8b9530 100644
--- 
a/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/graph/EntityGraphHelper.java
+++ 
b/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/graph/EntityGraphHelper.java
@@ -18,8 +18,6 @@
  */
 package org.apache.deltaspike.data.impl.graph;
 
-import java.lang.reflect.InvocationTargetException;
-import java.lang.reflect.Method;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collections;
@@ -27,95 +25,20 @@ import java.util.List;
 
 import jakarta.persistence.EntityManager;
 
-import org.apache.deltaspike.core.util.ClassUtils;
-import org.apache.deltaspike.core.util.ExceptionUtils;
 import org.apache.deltaspike.data.api.EntityGraph;
 
 /**
- * Helper for invoking methods related to entity graphs by reflection.
- * <p>
- * Reflection is required to stay compatible with JPA 2.0.
+ * Helper for entity graphs.
  */
 public final class EntityGraphHelper
 {
-
-    private static final Class<?> ENTITY_GRAPH_CLASS;
-    private static final Class<?> SUBGRAPH_CLASS;
-    private static final Method EG_ADD_ATTRIBUTE_NODES;
-    private static final Method EG_ADD_SUBGRAPH;
-    private static final Method SUBGRAPH_ADD_ATTRIBUTE_NODES;
-    private static final Method SUBGRAPH_ADD_SUBGRAPH;
-    private static final Method EM_GET_ENTITY_GRAPH;
-    private static final Method EM_CREATE_ENTITY_GRAPH;
-
-    static
-    {
-        ENTITY_GRAPH_CLASS = 
ClassUtils.tryToLoadClassForName("jakarta.persistence.EntityGraph");
-        SUBGRAPH_CLASS = 
ClassUtils.tryToLoadClassForName("jakarta.persistence.Subgraph");
-        if (ENTITY_GRAPH_CLASS == null)
-        {
-            EG_ADD_ATTRIBUTE_NODES = null;
-            EG_ADD_SUBGRAPH = null;
-            SUBGRAPH_ADD_ATTRIBUTE_NODES = null;
-            SUBGRAPH_ADD_SUBGRAPH = null;
-            EM_GET_ENTITY_GRAPH = null;
-            EM_CREATE_ENTITY_GRAPH = null;
-        }
-        else
-        {
-            try
-            {
-                EG_ADD_ATTRIBUTE_NODES = 
ENTITY_GRAPH_CLASS.getMethod("addAttributeNodes",
-                    String[].class);
-                EG_ADD_SUBGRAPH = ENTITY_GRAPH_CLASS.getMethod("addSubgraph", 
String.class);
-                SUBGRAPH_ADD_ATTRIBUTE_NODES = 
SUBGRAPH_CLASS.getMethod("addAttributeNodes",
-                    String[].class);
-                SUBGRAPH_ADD_SUBGRAPH = 
SUBGRAPH_CLASS.getMethod("addSubgraph", String.class);
-                EM_GET_ENTITY_GRAPH = 
EntityManager.class.getMethod("getEntityGraph", String.class);
-                EM_CREATE_ENTITY_GRAPH = 
EntityManager.class.getMethod("createEntityGraph",
-                    Class.class);
-            }
-            catch (NoSuchMethodException e)
-            {
-                throw ExceptionUtils.throwAsRuntimeException(e);
-            }
-        }
-    }
-
     private EntityGraphHelper()
     {
-        // hidden constructor
-    }
-
-    public static boolean isAvailable()
-    {
-        return ENTITY_GRAPH_CLASS != null;
-    }
 
-    /*
-     * TODO Check if this can be replaced by 
org.apache.deltaspike.core.util.ReflectionUtils.invokeMethod.
-     * This does not work at the moment due to an issue with exception 
handling in that method
-     * which needs further analysis. 
-     */
-    private static Object uncheckedInvoke(Method method, Object target, 
Object... args)
-    {
-        try
-        {
-            return method.invoke(target, args);
-        }
-        catch (IllegalAccessException e)
-        {
-            throw ExceptionUtils.throwAsRuntimeException(e);
-        }
-        catch (InvocationTargetException e)
-        {
-            throw ExceptionUtils.throwAsRuntimeException(e.getCause());
-        }
     }
-
+    
     public static Object getEntityGraph(EntityManager em, Class<?> 
entityClass, EntityGraph entityGraphAnn)
     {
-        ensureAvailable();
         String graphName = entityGraphAnn.value();
         if (graphName.isEmpty())
         {
@@ -123,48 +46,40 @@ public final class EntityGraphHelper
         }
         else
         {
-            return uncheckedInvoke(EM_GET_ENTITY_GRAPH, em, graphName);
+            return em.getEntityGraph(graphName);
         }
     }
 
     private static Object createEntityGraph(EntityManager em, Class<?> 
entityClass)
     {
-        return uncheckedInvoke(EM_CREATE_ENTITY_GRAPH, em, entityClass);
-    }
-
-    private static void ensureAvailable()
-    {
-        if (!isAvailable())
-        {
-            throw new EntityGraphException("Class java.persistence.EntityGraph 
is not available. "
-                + "Does your PersistenceProvider support JPA 2.1?");
-        }
+        return em.createEntityGraph(entityClass);
     }
 
     private static Object addSubgraph(Object graph, String attributeName)
     {
-        if (ENTITY_GRAPH_CLASS.isInstance(graph))
+        if (graph instanceof jakarta.persistence.EntityGraph)
         {
-            return uncheckedInvoke(EG_ADD_SUBGRAPH, graph, attributeName);
+            return ((jakarta.persistence.EntityGraph) 
graph).addSubgraph(attributeName);
         }
-        else if (SUBGRAPH_CLASS.isInstance(graph))
+        else if (graph instanceof jakarta.persistence.Subgraph)
         {
-            return uncheckedInvoke(SUBGRAPH_ADD_SUBGRAPH, graph, 
attributeName);
+            return ((jakarta.persistence.Subgraph) 
graph).addSubgraph(attributeName);
         }
+
         return null;
     }
 
     private static void addAttributeNodes(Object graph, String attributeName)
     {
-        if (ENTITY_GRAPH_CLASS.isInstance(graph))
+        if (graph instanceof jakarta.persistence.EntityGraph)
         {
-            uncheckedInvoke(EG_ADD_ATTRIBUTE_NODES, graph,
-                new Object[] { new String[] { attributeName } });
+            ((jakarta.persistence.EntityGraph) graph).addAttributeNodes(
+                    new String[] { attributeName });
         }
-        else if (SUBGRAPH_CLASS.isInstance(graph))
+        else if (graph instanceof jakarta.persistence.Subgraph)
         {
-            uncheckedInvoke(SUBGRAPH_ADD_ATTRIBUTE_NODES, graph,
-                new Object[] { new String[] { attributeName } });
+            ((jakarta.persistence.Subgraph) graph).addAttributeNodes(
+                    new String[] { attributeName });
         }
     }
 
@@ -172,7 +87,7 @@ public final class EntityGraphHelper
         String[] attributePaths)
     {
         Object graph = createEntityGraph(em, entityClass);
-        List<String> paths = new 
ArrayList<String>(Arrays.asList(attributePaths));
+        List<String> paths = new ArrayList<>(Arrays.asList(attributePaths));
 
         // handle longest paths first
         Collections.sort(paths);
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 e44a3d98f..96571eb93 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
@@ -1,379 +1,379 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you 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.deltaspike.data.impl.handler;
-
-import java.io.Serializable;
-import java.lang.reflect.Method;
-import java.util.LinkedList;
-import java.util.List;
-
-import jakarta.persistence.EntityManager;
-import jakarta.persistence.LockModeType;
-import jakarta.persistence.Query;
-import jakarta.persistence.QueryHint;
-import org.apache.deltaspike.core.api.provider.BeanProvider;
-import org.apache.deltaspike.core.api.provider.DependentProvider;
-
-import org.apache.deltaspike.data.api.EntityGraph;
-import org.apache.deltaspike.data.api.mapping.QueryInOutMapper;
-import org.apache.deltaspike.data.impl.graph.EntityGraphHelper;
-import org.apache.deltaspike.data.impl.meta.EntityMetadata;
-import org.apache.deltaspike.data.impl.meta.RepositoryMetadata;
-import org.apache.deltaspike.data.impl.meta.RepositoryMethodMetadata;
-import org.apache.deltaspike.data.impl.param.Parameters;
-import org.apache.deltaspike.data.impl.property.Property;
-import org.apache.deltaspike.data.impl.util.EntityUtils;
-import org.apache.deltaspike.data.impl.util.bean.DependentProviderDestroyable;
-import org.apache.deltaspike.data.impl.util.bean.Destroyable;
-import org.apache.deltaspike.data.spi.QueryInvocationContext;
-
-public class CdiQueryInvocationContext implements QueryInvocationContext
-{
-
-    private final EntityManager entityManager;
-    private final Parameters params;
-    private final Object proxy;
-    private final Method method;
-    private final Object[] args;
-    
-    private final RepositoryMetadata repositoryMetadata;
-    private final RepositoryMethodMetadata repositoryMethodMetadata;
-    
-    private final List<QueryStringPostProcessor> queryPostProcessors;
-    private final List<JpaQueryPostProcessor> jpaPostProcessors;
-    private final List<Destroyable> cleanup;
-
-    private String queryString;
-
-    public CdiQueryInvocationContext(Object proxy, Method method, Object[] 
args,
-            RepositoryMetadata repositoryMetadata,
-            RepositoryMethodMetadata repositoryMethodMetadata, EntityManager 
entityManager)
-    {
-        this.proxy = proxy;
-        this.method = method;
-        this.args = args == null ? new Object[]{} : args;
-        this.repositoryMetadata = repositoryMetadata;
-        this.repositoryMethodMetadata = repositoryMethodMetadata;
-        this.entityManager = entityManager;
-        
-        this.params = Parameters.create(method, this.args, 
repositoryMethodMetadata);
-        this.queryPostProcessors = new LinkedList<QueryStringPostProcessor>();
-        this.jpaPostProcessors = new LinkedList<JpaQueryPostProcessor>();
-        this.cleanup = new LinkedList<Destroyable>();
-    }
-
-    public void init()
-    {
-        if (hasQueryInOutMapper())
-        {
-            QueryInOutMapper<?> mapper = getQueryInOutMapper();
-            params.applyMapper(mapper);
-            for (int i = 0; i < args.length; i++)
-            {
-                if (mapper.mapsParameter(args[i]))
-                {
-                    args[i] = mapper.mapParameter(args[i]);
-                }
-            }
-        }
-    }
-
-    @Override
-    public EntityManager getEntityManager()
-    {
-        return entityManager;
-    }
-
-    @Override
-    public boolean isNew(Object entity)
-    {
-        try
-        {
-            Property<Serializable> versionProperty = 
repositoryMetadata.getEntityMetadata().getVersionProperty();
-            if (versionProperty != null)
-            {
-                return versionProperty.getValue(entity) == null;
-            }
-
-            Property<Serializable> primaryKeyProperty = 
repositoryMetadata.getEntityMetadata().getPrimaryKeyProperty();
-            if (EntityUtils.primaryKeyValue(entity, primaryKeyProperty) == 
null)
-            {
-                return true;
-            }
-
-            if (!entityManager.contains(entity) && countCheck(entity, 
primaryKeyProperty))
-            {
-                return true;
-            }
-
-            return false;
-        }
-        catch (IllegalArgumentException e)
-        {
-            // Not an entity
-            return false;
-        }
-    }
-
-    @Override
-    public Class<?> getEntityClass()
-    {
-        return repositoryMetadata.getEntityMetadata().getEntityClass();
-    }
-
-    @Override
-    public Class<?> getRepositoryClass()
-    {
-        return repositoryMetadata.getRepositoryClass();
-    }
-
-    public Object proceed() throws Exception
-    {
-        return method.invoke(proxy, args);
-    }
-
-    @Override
-    public Method getMethod()
-    {
-        return method;
-    }
-
-    public Query applyRestrictions(Query query)
-    {
-        Parameters params = getParams();
-        Method method = getMethod();
-        
-        if (params.hasSizeRestriction())
-        {
-            query.setMaxResults(params.getSizeRestriciton());
-        }
-        
-        if (params.hasFirstResult())
-        {
-            query.setFirstResult(params.getFirstResult());
-        }
-        
-        LockModeType lockMode = extractLockMode();
-        if (lockMode != null)
-        {
-            query.setLockMode(lockMode);
-        }
-        
-        QueryHint[] hints = extractQueryHints();
-        if (hints != null)
-        {
-            for (QueryHint hint : hints)
-            {
-                query.setHint(hint.name(), hint.value());
-            }
-        }
-
-        applyEntityGraph(query, method);
-        query = applyJpaQueryPostProcessors(query);
-        return query;
-    }
-
-    public Object[] getMethodParameters()
-    {
-        return args;
-    }
-
-    public void addQueryStringPostProcessor(QueryStringPostProcessor 
postProcessor)
-    {
-        queryPostProcessors.add(postProcessor);
-    }
-
-    public void addJpaQueryPostProcessor(JpaQueryPostProcessor postProcessor)
-    {
-        jpaPostProcessors.add(postProcessor);
-    }
-
-    public void removeJpaQueryPostProcessor(JpaQueryPostProcessor 
postProcessor)
-    {
-        jpaPostProcessors.remove(postProcessor);
-    }
-
-    public boolean hasQueryStringPostProcessors()
-    {
-        return !queryPostProcessors.isEmpty();
-    }
-
-    public String applyQueryStringPostProcessors(String queryString)
-    {
-        String result = queryString;
-        for (QueryStringPostProcessor processor : queryPostProcessors)
-        {
-            result = processor.postProcess(result);
-        }
-        return result;
-    }
-
-    public Query applyJpaQueryPostProcessors(Query query)
-    {
-        Query result = query;
-        for (JpaQueryPostProcessor processor : jpaPostProcessors)
-        {
-            result = processor.postProcess(this, result);
-        }
-        return result;
-    }
-
-    public void addDestroyable(Destroyable destroyable)
-    {
-        cleanup.add(destroyable);
-    }
-
-    public void cleanup()
-    {
-        for (Destroyable destroy : cleanup)
-        {
-            destroy.destroy();
-        }
-        cleanup.clear();
-    }
-
-    public Object executeQuery(Query jpaQuery)
-    {
-        return 
repositoryMethodMetadata.getQueryProcessor().executeQuery(jpaQuery, this);
-    }
-
-    public Parameters getParams()
-    {
-        return params;
-    }
-
-    public String getQueryString()
-    {
-        return queryString;
-    }
-
-    public void setQueryString(String queryString)
-    {
-        this.queryString = queryString;
-    }
-
-    public List<QueryStringPostProcessor> getQueryStringPostProcessors()
-    {
-        return queryPostProcessors;
-    }
-
-    public boolean hasQueryInOutMapper()
-    {
-        return repositoryMethodMetadata.getQueryInOutMapperClass() != null;
-    }
-
-    public QueryInOutMapper<?> getQueryInOutMapper()
-    {
-        if (repositoryMethodMetadata.getQueryInOutMapperClass() == null)
-        {
-            return null;
-        }
-
-        QueryInOutMapper<?> result = null;
-        if (repositoryMethodMetadata.isQueryInOutMapperIsNormalScope())
-        {
-            result = 
BeanProvider.getContextualReference(repositoryMethodMetadata.getQueryInOutMapperClass());
-        }
-        else
-        {
-            DependentProvider<? extends QueryInOutMapper<?>> mappedProvider =
-                    
BeanProvider.getDependent(repositoryMethodMetadata.getQueryInOutMapperClass());
-            
-            result = mappedProvider.get();
-            
-            this.addDestroyable(new 
DependentProviderDestroyable(mappedProvider));
-        }
-        
-        return result;
-    }
-
-    public Object getProxy()
-    {
-        return proxy;
-    }
-
-    private LockModeType extractLockMode()
-    {
-        org.apache.deltaspike.data.api.Query query = 
getRepositoryMethodMetadata().getQuery();
-        if (query != null && query.lock() != LockModeType.NONE)
-        {
-            return query.lock();
-        }
-
-        return null;
-    }
-
-    private QueryHint[] extractQueryHints()
-    {
-        org.apache.deltaspike.data.api.Query query = 
getRepositoryMethodMetadata().getQuery();        
-        if (query != null && query.hints().length > 0)
-        {
-            return query.hints();
-        }
-
-        return null;
-    }
-
-    private void applyEntityGraph(Query query, Method method)
-    {
-        EntityGraph entityGraphAnn = method.getAnnotation(EntityGraph.class);
-        if (entityGraphAnn == null)
-        {
-            return;
-        }
-        
-        Object graph = EntityGraphHelper.getEntityGraph(getEntityManager(),
-                repositoryMetadata.getEntityMetadata().getEntityClass(),
-                entityGraphAnn);
-        query.setHint(entityGraphAnn.type().getHintName(), graph);
-    }
-
-    private boolean countCheck(Object entity, Property<Serializable> 
primaryKeyProperty)
-    {
-        StringBuilder jpql = new StringBuilder("SELECT COUNT(e) FROM " + 
getEntityClass()
-                .getSimpleName() + " e ");
-        jpql.append("WHERE e.");
-        jpql.append(primaryKeyProperty.getName());
-        jpql.append(" = :id");
-
-        final Query query = entityManager.createQuery(jpql.toString());
-        query.setParameter("id", EntityUtils.primaryKeyValue(entity, 
primaryKeyProperty));
-        final Long result = (Long) query.getSingleResult();
-        if (Long.valueOf(0).equals(result))
-        {
-            return true;
-        }
-        return false;
-    }
-
-    public RepositoryMetadata getRepositoryMetadata()
-    {
-        return repositoryMetadata;
-    }
-
-    public EntityMetadata getEntityMetadata()
-    {
-        return repositoryMetadata.getEntityMetadata();
-    }
-    
-    public RepositoryMethodMetadata getRepositoryMethodMetadata()
-    {
-        return repositoryMethodMetadata;
-    } 
-}
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.deltaspike.data.impl.handler;
+
+import java.io.Serializable;
+import java.lang.reflect.Method;
+import java.util.LinkedList;
+import java.util.List;
+
+import jakarta.persistence.EntityManager;
+import jakarta.persistence.LockModeType;
+import jakarta.persistence.Query;
+import jakarta.persistence.QueryHint;
+import org.apache.deltaspike.core.api.provider.BeanProvider;
+import org.apache.deltaspike.core.api.provider.DependentProvider;
+
+import org.apache.deltaspike.data.api.EntityGraph;
+import org.apache.deltaspike.data.api.mapping.QueryInOutMapper;
+import org.apache.deltaspike.data.impl.graph.EntityGraphHelper;
+import org.apache.deltaspike.data.impl.meta.EntityMetadata;
+import org.apache.deltaspike.data.impl.meta.RepositoryMetadata;
+import org.apache.deltaspike.data.impl.meta.RepositoryMethodMetadata;
+import org.apache.deltaspike.data.impl.param.Parameters;
+import org.apache.deltaspike.data.impl.property.Property;
+import org.apache.deltaspike.data.impl.util.EntityUtils;
+import org.apache.deltaspike.data.impl.util.bean.DependentProviderDestroyable;
+import org.apache.deltaspike.data.impl.util.bean.Destroyable;
+import org.apache.deltaspike.data.spi.QueryInvocationContext;
+
+public class CdiQueryInvocationContext implements QueryInvocationContext
+{
+
+    private final EntityManager entityManager;
+    private final Parameters params;
+    private final Object proxy;
+    private final Method method;
+    private final Object[] args;
+    
+    private final RepositoryMetadata repositoryMetadata;
+    private final RepositoryMethodMetadata repositoryMethodMetadata;
+    
+    private final List<QueryStringPostProcessor> queryPostProcessors;
+    private final List<JpaQueryPostProcessor> jpaPostProcessors;
+    private final List<Destroyable> cleanup;
+
+    private String queryString;
+
+    public CdiQueryInvocationContext(Object proxy, Method method, Object[] 
args,
+            RepositoryMetadata repositoryMetadata,
+            RepositoryMethodMetadata repositoryMethodMetadata, EntityManager 
entityManager)
+    {
+        this.proxy = proxy;
+        this.method = method;
+        this.args = args == null ? new Object[]{} : args;
+        this.repositoryMetadata = repositoryMetadata;
+        this.repositoryMethodMetadata = repositoryMethodMetadata;
+        this.entityManager = entityManager;
+        
+        this.params = Parameters.create(method, this.args, 
repositoryMethodMetadata);
+        this.queryPostProcessors = new LinkedList<QueryStringPostProcessor>();
+        this.jpaPostProcessors = new LinkedList<JpaQueryPostProcessor>();
+        this.cleanup = new LinkedList<Destroyable>();
+    }
+
+    public void init()
+    {
+        if (hasQueryInOutMapper())
+        {
+            QueryInOutMapper<?> mapper = getQueryInOutMapper();
+            params.applyMapper(mapper);
+            for (int i = 0; i < args.length; i++)
+            {
+                if (mapper.mapsParameter(args[i]))
+                {
+                    args[i] = mapper.mapParameter(args[i]);
+                }
+            }
+        }
+    }
+
+    @Override
+    public EntityManager getEntityManager()
+    {
+        return entityManager;
+    }
+
+    @Override
+    public boolean isNew(Object entity)
+    {
+        try
+        {
+            Property<Serializable> versionProperty = 
repositoryMetadata.getEntityMetadata().getVersionProperty();
+            if (versionProperty != null)
+            {
+                return versionProperty.getValue(entity) == null;
+            }
+
+            Property<Serializable> primaryKeyProperty = 
repositoryMetadata.getEntityMetadata().getPrimaryKeyProperty();
+            if (EntityUtils.primaryKeyValue(entity, primaryKeyProperty) == 
null)
+            {
+                return true;
+            }
+
+            if (!entityManager.contains(entity) && countCheck(entity, 
primaryKeyProperty))
+            {
+                return true;
+            }
+
+            return false;
+        }
+        catch (IllegalArgumentException e)
+        {
+            // Not an entity
+            return false;
+        }
+    }
+
+    @Override
+    public Class<?> getEntityClass()
+    {
+        return repositoryMetadata.getEntityMetadata().getEntityClass();
+    }
+
+    @Override
+    public Class<?> getRepositoryClass()
+    {
+        return repositoryMetadata.getRepositoryClass();
+    }
+
+    public Object proceed() throws Exception
+    {
+        return method.invoke(proxy, args);
+    }
+
+    @Override
+    public Method getMethod()
+    {
+        return method;
+    }
+
+    public Query applyRestrictions(Query query)
+    {
+        Parameters params = getParams();
+        Method method = getMethod();
+        
+        if (params.hasSizeRestriction())
+        {
+            query.setMaxResults(params.getSizeRestriciton());
+        }
+        
+        if (params.hasFirstResult())
+        {
+            query.setFirstResult(params.getFirstResult());
+        }
+        
+        LockModeType lockMode = extractLockMode();
+        if (lockMode != null)
+        {
+            query.setLockMode(lockMode);
+        }
+        
+        QueryHint[] hints = extractQueryHints();
+        if (hints != null)
+        {
+            for (QueryHint hint : hints)
+            {
+                query.setHint(hint.name(), hint.value());
+            }
+        }
+
+        applyEntityGraph(query, method);
+        query = applyJpaQueryPostProcessors(query);
+        return query;
+    }
+
+    public Object[] getMethodParameters()
+    {
+        return args;
+    }
+
+    public void addQueryStringPostProcessor(QueryStringPostProcessor 
postProcessor)
+    {
+        queryPostProcessors.add(postProcessor);
+    }
+
+    public void addJpaQueryPostProcessor(JpaQueryPostProcessor postProcessor)
+    {
+        jpaPostProcessors.add(postProcessor);
+    }
+
+    public void removeJpaQueryPostProcessor(JpaQueryPostProcessor 
postProcessor)
+    {
+        jpaPostProcessors.remove(postProcessor);
+    }
+
+    public boolean hasQueryStringPostProcessors()
+    {
+        return !queryPostProcessors.isEmpty();
+    }
+
+    public String applyQueryStringPostProcessors(String queryString)
+    {
+        String result = queryString;
+        for (QueryStringPostProcessor processor : queryPostProcessors)
+        {
+            result = processor.postProcess(result);
+        }
+        return result;
+    }
+
+    public Query applyJpaQueryPostProcessors(Query query)
+    {
+        Query result = query;
+        for (JpaQueryPostProcessor processor : jpaPostProcessors)
+        {
+            result = processor.postProcess(this, result);
+        }
+        return result;
+    }
+
+    public void addDestroyable(Destroyable destroyable)
+    {
+        cleanup.add(destroyable);
+    }
+
+    public void cleanup()
+    {
+        for (Destroyable destroy : cleanup)
+        {
+            destroy.destroy();
+        }
+        cleanup.clear();
+    }
+
+    public Object executeQuery(Query jpaQuery)
+    {
+        return 
repositoryMethodMetadata.getQueryProcessor().executeQuery(jpaQuery, this);
+    }
+
+    public Parameters getParams()
+    {
+        return params;
+    }
+
+    public String getQueryString()
+    {
+        return queryString;
+    }
+
+    public void setQueryString(String queryString)
+    {
+        this.queryString = queryString;
+    }
+
+    public List<QueryStringPostProcessor> getQueryStringPostProcessors()
+    {
+        return queryPostProcessors;
+    }
+
+    public boolean hasQueryInOutMapper()
+    {
+        return repositoryMethodMetadata.getQueryInOutMapperClass() != null;
+    }
+
+    public QueryInOutMapper<?> getQueryInOutMapper()
+    {
+        if (repositoryMethodMetadata.getQueryInOutMapperClass() == null)
+        {
+            return null;
+        }
+
+        QueryInOutMapper<?> result = null;
+        if (repositoryMethodMetadata.isQueryInOutMapperIsNormalScope())
+        {
+            result = 
BeanProvider.getContextualReference(repositoryMethodMetadata.getQueryInOutMapperClass());
+        }
+        else
+        {
+            DependentProvider<? extends QueryInOutMapper<?>> mappedProvider =
+                    
BeanProvider.getDependent(repositoryMethodMetadata.getQueryInOutMapperClass());
+            
+            result = mappedProvider.get();
+            
+            this.addDestroyable(new 
DependentProviderDestroyable(mappedProvider));
+        }
+        
+        return result;
+    }
+
+    public Object getProxy()
+    {
+        return proxy;
+    }
+
+    private LockModeType extractLockMode()
+    {
+        org.apache.deltaspike.data.api.Query query = 
getRepositoryMethodMetadata().getQuery();
+        if (query != null && query.lock() != LockModeType.NONE)
+        {
+            return query.lock();
+        }
+
+        return null;
+    }
+
+    private QueryHint[] extractQueryHints()
+    {
+        org.apache.deltaspike.data.api.Query query = 
getRepositoryMethodMetadata().getQuery();        
+        if (query != null && query.hints().length > 0)
+        {
+            return query.hints();
+        }
+
+        return null;
+    }
+
+    private void applyEntityGraph(Query query, Method method)
+    {
+        EntityGraph entityGraphAnn = method.getAnnotation(EntityGraph.class);
+        if (entityGraphAnn == null)
+        {
+            return;
+        }
+        
+        Object graph = EntityGraphHelper.getEntityGraph(getEntityManager(),
+                repositoryMetadata.getEntityMetadata().getEntityClass(),
+                entityGraphAnn);
+        query.setHint(entityGraphAnn.type().getHintName(), graph);
+    }
+
+    private boolean countCheck(Object entity, Property<Serializable> 
primaryKeyProperty)
+    {
+        StringBuilder jpql = new StringBuilder("SELECT COUNT(e) FROM " + 
getEntityClass()
+                .getSimpleName() + " e ");
+        jpql.append("WHERE e.");
+        jpql.append(primaryKeyProperty.getName());
+        jpql.append(" = :id");
+
+        final Query query = entityManager.createQuery(jpql.toString());
+        query.setParameter("id", EntityUtils.primaryKeyValue(entity, 
primaryKeyProperty));
+        final Long result = (Long) query.getSingleResult();
+        if (Long.valueOf(0).equals(result))
+        {
+            return true;
+        }
+        return false;
+    }
+
+    public RepositoryMetadata getRepositoryMetadata()
+    {
+        return repositoryMetadata;
+    }
+
+    public EntityMetadata getEntityMetadata()
+    {
+        return repositoryMetadata.getEntityMetadata();
+    }
+    
+    public RepositoryMethodMetadata getRepositoryMethodMetadata()
+    {
+        return repositoryMethodMetadata;
+    } 
+}

Reply via email to