[OLINGO-1048] Add exception for EntityListeners
diff --git a/odata2-jpa-processor/jpa-api/src/main/java/org/apache/olingo/odata2/jpa/processor/api/ODataJPAQueryExtensionEntityListener.java b/odata2-jpa-processor/jpa-api/src/main/java/org/apache/olingo/odata2/jpa/processor/api/ODataJPAQueryExtensionEntityListener.java
index 0ad3ac4..e2c09d5 100644
--- a/odata2-jpa-processor/jpa-api/src/main/java/org/apache/olingo/odata2/jpa/processor/api/ODataJPAQueryExtensionEntityListener.java
+++ b/odata2-jpa-processor/jpa-api/src/main/java/org/apache/olingo/odata2/jpa/processor/api/ODataJPAQueryExtensionEntityListener.java
@@ -19,16 +19,19 @@
 package org.apache.olingo.odata2.jpa.processor.api;
 
 import java.util.List;
+import java.util.Locale;
 
 import javax.persistence.EntityManager;
 import javax.persistence.Query;
 
+import org.apache.olingo.odata2.api.exception.ODataApplicationException;
 import org.apache.olingo.odata2.api.uri.info.DeleteUriInfo;
 import org.apache.olingo.odata2.api.uri.info.GetEntityCountUriInfo;
 import org.apache.olingo.odata2.api.uri.info.GetEntitySetCountUriInfo;
 import org.apache.olingo.odata2.api.uri.info.GetEntitySetUriInfo;
 import org.apache.olingo.odata2.api.uri.info.GetEntityUriInfo;
 import org.apache.olingo.odata2.api.uri.info.PutMergePatchUriInfo;
+import org.apache.olingo.odata2.jpa.processor.api.exception.ODataJPARuntimeException;
 
 /**
  * Extend this class to build JPA Query object for a given OData request. The extended class can be registered as JPA
@@ -41,7 +44,7 @@
    * @param em is a reference to {@link javax.persistence.EntityManager}
    * @return an instance of type {@link javax.persistence.Query}
    */
-  public Query getQuery(GetEntitySetUriInfo uriInfo, EntityManager em) {
+  public Query getQuery(GetEntitySetUriInfo uriInfo, EntityManager em) throws ODataJPARuntimeException {
     return null;
   }
 
@@ -52,7 +55,7 @@
    * @param em is a reference to {@link javax.persistence.EntityManager}
    * @return an instance of type {@link javax.persistence.Query}
    */
-  public Query getQuery(GetEntityUriInfo uriInfo, EntityManager em) {
+  public Query getQuery(GetEntityUriInfo uriInfo, EntityManager em) throws ODataJPARuntimeException {
     return null;
   }
 
@@ -63,7 +66,7 @@
    * @param em is a reference to {@link javax.persistence.EntityManager}
    * @return an instance of type {@link javax.persistence.Query}
    */
-  public Query getQuery(GetEntityCountUriInfo uriInfo, EntityManager em) {
+  public Query getQuery(GetEntityCountUriInfo uriInfo, EntityManager em) throws ODataJPARuntimeException {
     return null;
   }
 
@@ -73,7 +76,7 @@
    * @param em is a reference to {@link javax.persistence.EntityManager}
    * @return an instance of type {@link javax.persistence.Query}
    */
-  public Query getQuery(GetEntitySetCountUriInfo uriInfo, EntityManager em) {
+  public Query getQuery(GetEntitySetCountUriInfo uriInfo, EntityManager em) throws ODataJPARuntimeException {
     return null;
   }
 
@@ -84,7 +87,7 @@
    * @param em is a reference to {@link javax.persistence.EntityManager}
    * @return an instance of type {@link javax.persistence.Query}
    */
-  public Query getQuery(PutMergePatchUriInfo uriInfo, EntityManager em) {
+  public Query getQuery(PutMergePatchUriInfo uriInfo, EntityManager em) throws ODataJPARuntimeException {
     return null;
   }
 
@@ -95,7 +98,7 @@
    * @param em is a reference to {@link javax.persistence.EntityManager}
    * @return an instance of type {@link javax.persistence.Query}
    */
-  public Query getQuery(DeleteUriInfo uriInfo, EntityManager em) {
+  public Query getQuery(DeleteUriInfo uriInfo, EntityManager em) throws ODataJPARuntimeException {
     return null;
   }
 
@@ -113,4 +116,8 @@
     return false;
   }
 
+  protected ODataJPARuntimeException createApplicationError(String message, Locale locale) {
+    return ODataJPARuntimeException.throwException(
+        ODataJPARuntimeException.GENERAL, new ODataApplicationException(message, locale));
+  }
 }
diff --git a/odata2-jpa-processor/jpa-api/src/main/java/org/apache/olingo/odata2/jpa/processor/api/ODataJPATombstoneEntityListener.java b/odata2-jpa-processor/jpa-api/src/main/java/org/apache/olingo/odata2/jpa/processor/api/ODataJPATombstoneEntityListener.java
index ff85653..0dfc935 100644
--- a/odata2-jpa-processor/jpa-api/src/main/java/org/apache/olingo/odata2/jpa/processor/api/ODataJPATombstoneEntityListener.java
+++ b/odata2-jpa-processor/jpa-api/src/main/java/org/apache/olingo/odata2/jpa/processor/api/ODataJPATombstoneEntityListener.java
@@ -24,6 +24,7 @@
 import javax.persistence.Query;
 
 import org.apache.olingo.odata2.api.uri.info.GetEntitySetUriInfo;
+import org.apache.olingo.odata2.jpa.processor.api.exception.ODataJPARuntimeException;
 
 /**
  * Extend this class and implement a JPA Entity Listener as specified in JSR 317 Java Persistence 2.0.
@@ -47,7 +48,7 @@
    * @param em is a reference to {@link javax.persistence.EntityManager}
    * @return an instance of type {@link javax.persistence.Query}
    */
-  public abstract Query getQuery(GetEntitySetUriInfo resultsView, EntityManager em);
+  public abstract Query getQuery(GetEntitySetUriInfo resultsView, EntityManager em) throws ODataJPARuntimeException;
 
   /**
    * Implement this method to create a delta token.
diff --git a/odata2-jpa-processor/jpa-ref/src/main/java/org/apache/olingo/odata2/jpa/processor/ref/listeners/CustomerQueryExtension.java b/odata2-jpa-processor/jpa-ref/src/main/java/org/apache/olingo/odata2/jpa/processor/ref/listeners/CustomerQueryExtension.java
index 8fcee7a..ef385fe 100644
--- a/odata2-jpa-processor/jpa-ref/src/main/java/org/apache/olingo/odata2/jpa/processor/ref/listeners/CustomerQueryExtension.java
+++ b/odata2-jpa-processor/jpa-ref/src/main/java/org/apache/olingo/odata2/jpa/processor/ref/listeners/CustomerQueryExtension.java
@@ -18,9 +18,8 @@
  ******************************************************************************/
 package org.apache.olingo.odata2.jpa.processor.ref.listeners;
 
-import javax.persistence.EntityManager;
-import javax.persistence.Query;
-
+import org.apache.olingo.odata2.api.uri.expression.FilterExpression;
+import org.apache.olingo.odata2.api.uri.info.GetEntitySetUriInfo;
 import org.apache.olingo.odata2.api.uri.info.GetEntityUriInfo;
 import org.apache.olingo.odata2.jpa.processor.api.ODataJPAQueryExtensionEntityListener;
 import org.apache.olingo.odata2.jpa.processor.api.exception.ODataJPAModelException;
@@ -29,7 +28,21 @@
 import org.apache.olingo.odata2.jpa.processor.api.jpql.JPQLContextType;
 import org.apache.olingo.odata2.jpa.processor.api.jpql.JPQLStatement;
 
+import javax.persistence.EntityManager;
+import javax.persistence.Query;
+import java.util.Locale;
+
 public class CustomerQueryExtension extends ODataJPAQueryExtensionEntityListener {
+
+  @Override
+  public Query getQuery(GetEntitySetUriInfo uriInfo, EntityManager em) throws ODataJPARuntimeException {
+    FilterExpression filter = uriInfo.getFilter();
+    if(filter != null && filter.getExpressionString().startsWith("name")) {
+      throw createApplicationError("Filter on name not allowed.", Locale.ENGLISH);
+    }
+    return null;
+  }
+
   @Override
   public Query getQuery(GetEntityUriInfo uriInfo, EntityManager em) {
     Query query = null;
@@ -48,7 +61,7 @@
     } catch (ODataJPARuntimeException e) {
       // Log and return null query object;
     }
-    return query;
+    return null;
   }
 
   @Override