You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@olingo.apache.org by ch...@apache.org on 2014/01/01 15:13:31 UTC
[2/2] git commit: [OLINGO-103] Expose ODataJPAResponseBuilder for
External Usage
[OLINGO-103] Expose ODataJPAResponseBuilder for External Usage
Signed-off-by: Chandan V A <ch...@sap.com>
Project: http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/commit/f8638bb7
Tree: http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/tree/f8638bb7
Diff: http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/diff/f8638bb7
Branch: refs/heads/master
Commit: f8638bb7a910fd44f4bd88d0368df760aa189db5
Parents: 63b621a
Author: Chandan V A <ch...@sap.com>
Authored: Wed Jan 1 19:42:29 2014 +0530
Committer: Chandan V A <ch...@sap.com>
Committed: Wed Jan 1 19:42:29 2014 +0530
----------------------------------------------------------------------
.../jpa/processor/api/ODataJPAProcessor.java | 12 +-
.../processor/api/ODataJPAResponseBuilder.java | 158 +++++
.../api/factory/ODataJPAAccessFactory.java | 12 +
.../core/ODataJPAProcessorDefault.java | 24 +-
.../processor/core/ODataJPAResponseBuilder.java | 629 ------------------
.../core/ODataJPAResponseBuilderDefault.java | 646 +++++++++++++++++++
.../core/factory/ODataJPAFactoryImpl.java | 11 +-
.../core/ODataJPAResponseBuilderTest.java | 40 +-
8 files changed, 866 insertions(+), 666 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/blob/f8638bb7/odata2-jpa-processor/jpa-api/src/main/java/org/apache/olingo/odata2/jpa/processor/api/ODataJPAProcessor.java
----------------------------------------------------------------------
diff --git a/odata2-jpa-processor/jpa-api/src/main/java/org/apache/olingo/odata2/jpa/processor/api/ODataJPAProcessor.java b/odata2-jpa-processor/jpa-api/src/main/java/org/apache/olingo/odata2/jpa/processor/api/ODataJPAProcessor.java
index 4ce0f19..d08eee9 100644
--- a/odata2-jpa-processor/jpa-api/src/main/java/org/apache/olingo/odata2/jpa/processor/api/ODataJPAProcessor.java
+++ b/odata2-jpa-processor/jpa-api/src/main/java/org/apache/olingo/odata2/jpa/processor/api/ODataJPAProcessor.java
@@ -27,8 +27,8 @@ import org.apache.olingo.odata2.jpa.processor.api.factory.ODataJPAFactory;
* Extend this class and implement an OData JPA processor if the default
* behavior of OData JPA Processor library has to be overwritten.
*
- *
- *
+ * The class encapsulates a default processor for handling OData requests and
+ * a default builder to build an OData response.
*
*/
public abstract class ODataJPAProcessor extends ODataSingleProcessor {
@@ -44,6 +44,12 @@ public abstract class ODataJPAProcessor extends ODataSingleProcessor {
*/
protected JPAProcessor jpaProcessor;
+ /**
+ * An instance of {@link org.apache.olingo.odata2.jpa.processor.api.ODataJPAResponseBuilder}. The instance
+ * is created using {@link org.apache.olingo.odata2.jpa.processor.api.factory.ODataJPAAccessFactory}.
+ */
+ protected ODataJPAResponseBuilder responseBuilder;
+
public ODataJPAContext getOdataJPAContext() {
return oDataJPAContext;
}
@@ -64,6 +70,8 @@ public abstract class ODataJPAProcessor extends ODataSingleProcessor {
}
this.oDataJPAContext = oDataJPAContext;
jpaProcessor = ODataJPAFactory.createFactory().getJPAAccessFactory().getJPAProcessor(this.oDataJPAContext);
+ responseBuilder =
+ ODataJPAFactory.createFactory().getODataJPAAccessFactory().getODataJPAResponseBuilder(this.oDataJPAContext);
}
}
http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/blob/f8638bb7/odata2-jpa-processor/jpa-api/src/main/java/org/apache/olingo/odata2/jpa/processor/api/ODataJPAResponseBuilder.java
----------------------------------------------------------------------
diff --git a/odata2-jpa-processor/jpa-api/src/main/java/org/apache/olingo/odata2/jpa/processor/api/ODataJPAResponseBuilder.java b/odata2-jpa-processor/jpa-api/src/main/java/org/apache/olingo/odata2/jpa/processor/api/ODataJPAResponseBuilder.java
new file mode 100644
index 0000000..63d692b
--- /dev/null
+++ b/odata2-jpa-processor/jpa-api/src/main/java/org/apache/olingo/odata2/jpa/processor/api/ODataJPAResponseBuilder.java
@@ -0,0 +1,158 @@
+/*******************************************************************************
+ * 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.olingo.odata2.jpa.processor.api;
+
+import java.util.List;
+
+import org.apache.olingo.odata2.api.exception.ODataNotFoundException;
+import org.apache.olingo.odata2.api.processor.ODataResponse;
+import org.apache.olingo.odata2.api.uri.info.DeleteUriInfo;
+import org.apache.olingo.odata2.api.uri.info.GetEntityLinkUriInfo;
+import org.apache.olingo.odata2.api.uri.info.GetEntitySetLinksUriInfo;
+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.GetFunctionImportUriInfo;
+import org.apache.olingo.odata2.api.uri.info.PostUriInfo;
+import org.apache.olingo.odata2.api.uri.info.PutMergePatchUriInfo;
+import org.apache.olingo.odata2.jpa.processor.api.exception.ODataJPARuntimeException;
+
+/**
+ * The interface provides methods for building an OData response from a JPA Entity.
+ * Implement this interface for building an OData response from a JPA Entity.
+ */
+public interface ODataJPAResponseBuilder {
+
+ /**
+ * The method builds an OData response for an OData Query Request from a queried list of JPA Entities.
+ * @param queryUriInfo is an information about the request URI
+ * @param jpaEntities is an empty or non empty list of queried instances of JPA Entities
+ * @param contentType of the response
+ * @return an instance of type {@link org.apache.olingo.odata2.api.processor.ODataResponse}
+ * @throws ODataJPARuntimeException
+ */
+ public ODataResponse build(final GetEntitySetUriInfo queryUriInfo, final List<Object> jpaEntities,
+ final String contentType) throws ODataJPARuntimeException;
+
+ /**
+ * The method builds an OData response for an OData Read Request from a read JPA Entity
+ * @param readUriInfo is an information about the request URI
+ * @param jpaEntity is a null or non null instances of read JPA Entity
+ * @param contentType of the response
+ * @return an instance of type {@link org.apache.olingo.odata2.api.processor.ODataResponse}
+ * @throws ODataJPARuntimeException
+ * @throws ODataNotFoundException
+ */
+ public ODataResponse build(final GetEntityUriInfo readUriInfo, final Object jpaEntity,
+ final String contentType) throws ODataJPARuntimeException,
+ ODataNotFoundException;
+
+ /**
+ * The method builds an OData response for an OData Create Request from a created List of JPA entities.
+ * @param postUriInfo is an information about the request URI
+ * @param createdObjectList is an empty or non empty list of created instances of JPA Entities
+ * @param contentType of the response
+ * @return an instance of type {@link org.apache.olingo.odata2.api.processor.ODataResponse}
+ * @throws ODataJPARuntimeException
+ * @throws ODataNotFoundException
+ */
+ public ODataResponse build(final PostUriInfo postUriInfo, final List<Object> createdObjectList,
+ final String contentType) throws ODataJPARuntimeException,
+ ODataNotFoundException;
+
+ /**
+ * The method builds an OData response for an OData Update Request from an updated JPA Entity
+ * @param putUriInfo is an information about the request URI
+ * @param updatedObject is an updated instance of JPA Entity
+ * @return an instance of type {@link org.apache.olingo.odata2.api.processor.ODataResponse}
+ * @throws ODataJPARuntimeException
+ * @throws ODataNotFoundException
+ */
+ public ODataResponse build(final PutMergePatchUriInfo putUriInfo, final Object updatedObject)
+ throws ODataJPARuntimeException, ODataNotFoundException;
+
+ /**
+ * The method builds an OData response for an OData Delete Request from a deleted JPA Entity
+ * @param deleteUriInfo is an information about the request URI
+ * @param deletedObject is an null or non null instance of deleted JPA Entity. Null implies Entity not found.
+ * @return an instance of type {@link org.apache.olingo.odata2.api.processor.ODataResponse}
+ * @throws ODataJPARuntimeException
+ * @throws ODataNotFoundException
+ */
+ public ODataResponse build(final DeleteUriInfo deleteUriInfo, final Object deletedObject)
+ throws ODataJPARuntimeException, ODataNotFoundException;
+
+ /**
+ * The method builds an OData response for an OData function Import Request from a registered processor method's
+ * return parameter.
+ * @param functionImportUriInfo is an information about the request URI
+ * @param result is a method's return parameter
+ * @return an instance of type {@link org.apache.olingo.odata2.api.processor.ODataResponse}
+ * @throws ODataJPARuntimeException
+ */
+ public ODataResponse build(final GetFunctionImportUriInfo functionImportUriInfo, final Object result)
+ throws ODataJPARuntimeException;
+
+ /**
+ * The method builds an OData response for an OData function Import Request from a registered processor method's
+ * return parameter. The return parameter is a collection of objects.
+ * @param functionImportUriInfo is an information about the request URI
+ * @param result is a method's return parameter is a collection of objects.
+ * @return an instance of type {@link org.apache.olingo.odata2.api.processor.ODataResponse}
+ * @throws ODataJPARuntimeException
+ */
+ public ODataResponse build(final GetFunctionImportUriInfo functionImportUriInfo, final List<Object> resultList,
+ final String contentType)
+ throws ODataJPARuntimeException, ODataNotFoundException;
+
+ /**
+ * The method builds an OData response for an OData Read Link Request from a read JPA Entity and its related JPA
+ * Entities.
+ * @param readLinkUriInfo is an information about the request URI
+ * @param jpaEntity is a null or non null read JPA Entity and its related JPA Entities.
+ * @param contentType of the response
+ * @return an instance of type {@link org.apache.olingo.odata2.api.processor.ODataResponse}
+ * @throws ODataNotFoundException
+ * @throws ODataJPARuntimeException
+ */
+ public ODataResponse build(final GetEntityLinkUriInfo readLinkUriInfo, final Object jpaEntity,
+ final String contentType) throws ODataNotFoundException,
+ ODataJPARuntimeException;
+
+ /**
+ * The method builds an OData response for an OData Query Link Request from a queried JPA Entity and its related JPA
+ * Entities.
+ * @param queryLinkUriInfo is an information about the request URI
+ * @param jpaEntity is an empty or non empty list of queried JPA Entities
+ * @param contentType of the response
+ * @return an instance of type {@link org.apache.olingo.odata2.api.processor.ODataResponse}
+ * @throws ODataJPARuntimeException
+ */
+ public ODataResponse build(final GetEntitySetLinksUriInfo queryLinkUriInfo, final List<Object> jpaEntity,
+ final String contentType) throws ODataJPARuntimeException;
+
+ /**
+ * The method builds an OData response from a count representing total number of JPA Entities
+ * @param jpaEntityCount is the count value
+ * @return an instance of type {@link org.apache.olingo.odata2.api.processor.ODataResponse}
+ * @throws ODataJPARuntimeException
+ */
+ public ODataResponse build(final long jpaEntityCount)
+ throws ODataJPARuntimeException;
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/blob/f8638bb7/odata2-jpa-processor/jpa-api/src/main/java/org/apache/olingo/odata2/jpa/processor/api/factory/ODataJPAAccessFactory.java
----------------------------------------------------------------------
diff --git a/odata2-jpa-processor/jpa-api/src/main/java/org/apache/olingo/odata2/jpa/processor/api/factory/ODataJPAAccessFactory.java b/odata2-jpa-processor/jpa-api/src/main/java/org/apache/olingo/odata2/jpa/processor/api/factory/ODataJPAAccessFactory.java
index 50f84f6..3d5010d 100644
--- a/odata2-jpa-processor/jpa-api/src/main/java/org/apache/olingo/odata2/jpa/processor/api/factory/ODataJPAAccessFactory.java
+++ b/odata2-jpa-processor/jpa-api/src/main/java/org/apache/olingo/odata2/jpa/processor/api/factory/ODataJPAAccessFactory.java
@@ -23,6 +23,7 @@ import java.util.Locale;
import org.apache.olingo.odata2.api.edm.provider.EdmProvider;
import org.apache.olingo.odata2.api.processor.ODataSingleProcessor;
import org.apache.olingo.odata2.jpa.processor.api.ODataJPAContext;
+import org.apache.olingo.odata2.jpa.processor.api.ODataJPAResponseBuilder;
import org.apache.olingo.odata2.jpa.processor.api.exception.ODataJPAMessageService;
/**
@@ -33,6 +34,7 @@ import org.apache.olingo.odata2.jpa.processor.api.exception.ODataJPAMessageServi
* <li>OData JPA Processor of type {@link org.apache.olingo.odata2.api.processor.ODataSingleProcessor}</li>
* <li>JPA EDM Provider of type {@link org.apache.olingo.odata2.api.edm.provider.EdmProvider}</li>
* <li>OData JPA Context {@link org.apache.olingo.odata2.jpa.processor.api.ODataJPAContext}</li>
+ * <li>OData JPA Response Builder {@link org.apache.olingo.odata2.jpa.processor.api.ODataJPAResponseBuilder}</li>
* </ul>
* </p>
*
@@ -79,4 +81,14 @@ public interface ODataJPAAccessFactory {
* @return an instance of type {@link org.apache.olingo.odata2.jpa.processor.api.exception.ODataJPAMessageService}
*/
public ODataJPAMessageService getODataJPAMessageService(Locale locale);
+
+ /**
+ * @param oDataJPAContext
+ * an instance of type {@link org.apache.olingo.odata2.jpa.processor.api.ODataJPAContext}.
+ * The context should be initialized properly and cannot be null.
+ *
+ * The method creates an instance of OData Response Builder for building OData Responses from a JPA Entity.
+ * @return an instance of type{@link org.apache.olingo.odata2.jpa.processor.api.ODataJPAResponseBuilder}
+ */
+ public ODataJPAResponseBuilder getODataJPAResponseBuilder(ODataJPAContext oDataJPAContext);
}
http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/blob/f8638bb7/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/ODataJPAProcessorDefault.java
----------------------------------------------------------------------
diff --git a/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/ODataJPAProcessorDefault.java b/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/ODataJPAProcessorDefault.java
index 320fce7..96782b8 100644
--- a/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/ODataJPAProcessorDefault.java
+++ b/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/ODataJPAProcessorDefault.java
@@ -50,10 +50,10 @@ public class ODataJPAProcessorDefault extends ODataJPAProcessor {
public ODataResponse readEntitySet(final GetEntitySetUriInfo uriParserResultView, final String contentType)
throws ODataException {
- List<?> jpaEntities = jpaProcessor.process(uriParserResultView);
+ List<Object> jpaEntities = jpaProcessor.process(uriParserResultView);
ODataResponse oDataResponse =
- ODataJPAResponseBuilder.build(jpaEntities, uriParserResultView, contentType, oDataJPAContext);
+ responseBuilder.build(uriParserResultView, jpaEntities, contentType);
return oDataResponse;
}
@@ -65,7 +65,7 @@ public class ODataJPAProcessorDefault extends ODataJPAProcessor {
Object jpaEntity = jpaProcessor.process(uriParserResultView);
ODataResponse oDataResponse =
- ODataJPAResponseBuilder.build(jpaEntity, uriParserResultView, contentType, oDataJPAContext);
+ responseBuilder.build(uriParserResultView, jpaEntity, contentType);
return oDataResponse;
}
@@ -76,7 +76,7 @@ public class ODataJPAProcessorDefault extends ODataJPAProcessor {
long jpaEntityCount = jpaProcessor.process(uriParserResultView);
- ODataResponse oDataResponse = ODataJPAResponseBuilder.build(jpaEntityCount, oDataJPAContext);
+ ODataResponse oDataResponse = responseBuilder.build(jpaEntityCount);
return oDataResponse;
}
@@ -87,7 +87,7 @@ public class ODataJPAProcessorDefault extends ODataJPAProcessor {
long jpaEntityCount = jpaProcessor.process(uriInfo);
- ODataResponse oDataResponse = ODataJPAResponseBuilder.build(jpaEntityCount, oDataJPAContext);
+ ODataResponse oDataResponse = responseBuilder.build(jpaEntityCount);
return oDataResponse;
}
@@ -99,7 +99,7 @@ public class ODataJPAProcessorDefault extends ODataJPAProcessor {
List<Object> createdJpaEntityList = jpaProcessor.process(uriParserResultView, content, requestContentType);
ODataResponse oDataResponse =
- ODataJPAResponseBuilder.build(createdJpaEntityList, uriParserResultView, contentType, oDataJPAContext);
+ responseBuilder.build(uriParserResultView, createdJpaEntityList, contentType);
return oDataResponse;
}
@@ -110,7 +110,7 @@ public class ODataJPAProcessorDefault extends ODataJPAProcessor {
Object jpaEntity = jpaProcessor.process(uriParserResultView, content, requestContentType);
- ODataResponse oDataResponse = ODataJPAResponseBuilder.build(jpaEntity, uriParserResultView);
+ ODataResponse oDataResponse = responseBuilder.build(uriParserResultView, jpaEntity);
return oDataResponse;
}
@@ -121,7 +121,7 @@ public class ODataJPAProcessorDefault extends ODataJPAProcessor {
Object deletedObj = jpaProcessor.process(uriParserResultView, contentType);
- ODataResponse oDataResponse = ODataJPAResponseBuilder.build(deletedObj, uriParserResultView);
+ ODataResponse oDataResponse = responseBuilder.build(uriParserResultView, deletedObj);
return oDataResponse;
}
@@ -132,7 +132,7 @@ public class ODataJPAProcessorDefault extends ODataJPAProcessor {
List<Object> resultEntity = jpaProcessor.process(uriParserResultView);
ODataResponse oDataResponse =
- ODataJPAResponseBuilder.build(resultEntity, uriParserResultView, contentType, oDataJPAContext);
+ responseBuilder.build(uriParserResultView, resultEntity, contentType);
return oDataResponse;
}
@@ -144,7 +144,7 @@ public class ODataJPAProcessorDefault extends ODataJPAProcessor {
List<Object> result = jpaProcessor.process(uriParserResultView);
ODataResponse oDataResponse =
- ODataJPAResponseBuilder.build(result, uriParserResultView, contentType, oDataJPAContext);
+ responseBuilder.build(uriParserResultView, result, contentType);
return oDataResponse;
}
@@ -156,7 +156,7 @@ public class ODataJPAProcessorDefault extends ODataJPAProcessor {
Object jpaEntity = jpaProcessor.process(uriParserResultView);
ODataResponse oDataResponse =
- ODataJPAResponseBuilder.build(jpaEntity, uriParserResultView, contentType, oDataJPAContext);
+ responseBuilder.build(uriParserResultView, jpaEntity, contentType);
return oDataResponse;
}
@@ -168,7 +168,7 @@ public class ODataJPAProcessorDefault extends ODataJPAProcessor {
List<Object> jpaEntity = jpaProcessor.process(uriParserResultView);
ODataResponse oDataResponse =
- ODataJPAResponseBuilder.build(jpaEntity, uriParserResultView, contentType, oDataJPAContext);
+ responseBuilder.build(uriParserResultView, jpaEntity, contentType);
return oDataResponse;
}
http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/blob/f8638bb7/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/ODataJPAResponseBuilder.java
----------------------------------------------------------------------
diff --git a/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/ODataJPAResponseBuilder.java b/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/ODataJPAResponseBuilder.java
deleted file mode 100644
index f63e9d3..0000000
--- a/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/ODataJPAResponseBuilder.java
+++ /dev/null
@@ -1,629 +0,0 @@
-/*******************************************************************************
- * 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.olingo.odata2.jpa.processor.core;
-
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-
-import org.apache.olingo.odata2.api.commons.HttpStatusCodes;
-import org.apache.olingo.odata2.api.commons.InlineCount;
-import org.apache.olingo.odata2.api.edm.EdmEntitySet;
-import org.apache.olingo.odata2.api.edm.EdmEntityType;
-import org.apache.olingo.odata2.api.edm.EdmException;
-import org.apache.olingo.odata2.api.edm.EdmFunctionImport;
-import org.apache.olingo.odata2.api.edm.EdmLiteralKind;
-import org.apache.olingo.odata2.api.edm.EdmMultiplicity;
-import org.apache.olingo.odata2.api.edm.EdmNavigationProperty;
-import org.apache.olingo.odata2.api.edm.EdmProperty;
-import org.apache.olingo.odata2.api.edm.EdmSimpleType;
-import org.apache.olingo.odata2.api.edm.EdmStructuralType;
-import org.apache.olingo.odata2.api.edm.EdmType;
-import org.apache.olingo.odata2.api.edm.EdmTypeKind;
-import org.apache.olingo.odata2.api.ep.EntityProvider;
-import org.apache.olingo.odata2.api.ep.EntityProviderException;
-import org.apache.olingo.odata2.api.ep.EntityProviderWriteProperties;
-import org.apache.olingo.odata2.api.ep.EntityProviderWriteProperties.ODataEntityProviderPropertiesBuilder;
-import org.apache.olingo.odata2.api.exception.ODataException;
-import org.apache.olingo.odata2.api.exception.ODataHttpException;
-import org.apache.olingo.odata2.api.exception.ODataNotFoundException;
-import org.apache.olingo.odata2.api.processor.ODataContext;
-import org.apache.olingo.odata2.api.processor.ODataResponse;
-import org.apache.olingo.odata2.api.uri.ExpandSelectTreeNode;
-import org.apache.olingo.odata2.api.uri.NavigationPropertySegment;
-import org.apache.olingo.odata2.api.uri.SelectItem;
-import org.apache.olingo.odata2.api.uri.UriParser;
-import org.apache.olingo.odata2.api.uri.info.DeleteUriInfo;
-import org.apache.olingo.odata2.api.uri.info.GetEntityLinkUriInfo;
-import org.apache.olingo.odata2.api.uri.info.GetEntitySetLinksUriInfo;
-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.GetFunctionImportUriInfo;
-import org.apache.olingo.odata2.api.uri.info.PostUriInfo;
-import org.apache.olingo.odata2.api.uri.info.PutMergePatchUriInfo;
-import org.apache.olingo.odata2.jpa.processor.api.ODataJPAContext;
-import org.apache.olingo.odata2.jpa.processor.api.exception.ODataJPARuntimeException;
-import org.apache.olingo.odata2.jpa.processor.core.access.data.JPAEntityParser;
-import org.apache.olingo.odata2.jpa.processor.core.access.data.JPAExpandCallBack;
-
-public final class ODataJPAResponseBuilder {
-
- /* Response for Read Entity Set */
- public static <T> ODataResponse build(final List<T> jpaEntities, final GetEntitySetUriInfo resultsView,
- final String contentType, final ODataJPAContext odataJPAContext) throws ODataJPARuntimeException {
-
- EdmEntityType edmEntityType = null;
- ODataResponse odataResponse = null;
- List<ArrayList<NavigationPropertySegment>> expandList = null;
-
- try {
- edmEntityType = resultsView.getTargetEntitySet().getEntityType();
- List<Map<String, Object>> edmEntityList = new ArrayList<Map<String, Object>>();
- Map<String, Object> edmPropertyValueMap = null;
- JPAEntityParser jpaResultParser = new JPAEntityParser();
- final List<SelectItem> selectedItems = resultsView.getSelect();
- if (selectedItems != null && selectedItems.size() > 0) {
- for (Object jpaEntity : jpaEntities) {
- edmPropertyValueMap =
- jpaResultParser.parse2EdmPropertyValueMap(jpaEntity, buildSelectItemList(selectedItems, edmEntityType));
- edmEntityList.add(edmPropertyValueMap);
- }
- } else {
- for (Object jpaEntity : jpaEntities) {
- edmPropertyValueMap = jpaResultParser.parse2EdmPropertyValueMap(jpaEntity, edmEntityType);
- edmEntityList.add(edmPropertyValueMap);
- }
- }
- expandList = resultsView.getExpand();
- if (expandList != null && expandList.size() != 0) {
- int count = 0;
- List<EdmNavigationProperty> edmNavPropertyList = constructListofNavProperty(expandList);
- for (Object jpaEntity : jpaEntities) {
- Map<String, Object> relationShipMap = edmEntityList.get(count);
- HashMap<String, Object> navigationMap =
- jpaResultParser.parse2EdmNavigationValueMap(jpaEntity, edmNavPropertyList);
- relationShipMap.putAll(navigationMap);
- count++;
- }
- }
-
- EntityProviderWriteProperties feedProperties = null;
-
- feedProperties = getEntityProviderProperties(odataJPAContext, resultsView, edmEntityList);
- odataResponse =
- EntityProvider.writeFeed(contentType, resultsView.getTargetEntitySet(), edmEntityList, feedProperties);
- odataResponse = ODataResponse.fromResponse(odataResponse).status(HttpStatusCodes.OK).build();
-
- } catch (EntityProviderException e) {
- throw ODataJPARuntimeException.throwException(ODataJPARuntimeException.GENERAL.addContent(e.getMessage()), e);
- } catch (EdmException e) {
- throw ODataJPARuntimeException.throwException(ODataJPARuntimeException.GENERAL.addContent(e.getMessage()), e);
- }
-
- return odataResponse;
- }
-
- /* Response for Read Entity */
- public static ODataResponse build(final Object jpaEntity, final GetEntityUriInfo resultsView,
- final String contentType, final ODataJPAContext oDataJPAContext) throws ODataJPARuntimeException,
- ODataNotFoundException {
-
- List<ArrayList<NavigationPropertySegment>> expandList = null;
- if (jpaEntity == null) {
- throw new ODataNotFoundException(ODataNotFoundException.ENTITY);
- }
- EdmEntityType edmEntityType = null;
- ODataResponse odataResponse = null;
-
- try {
-
- edmEntityType = resultsView.getTargetEntitySet().getEntityType();
- Map<String, Object> edmPropertyValueMap = null;
-
- JPAEntityParser jpaResultParser = new JPAEntityParser();
- final List<SelectItem> selectedItems = resultsView.getSelect();
- if (selectedItems != null && selectedItems.size() > 0) {
- edmPropertyValueMap =
- jpaResultParser.parse2EdmPropertyValueMap(jpaEntity, buildSelectItemList(selectedItems, resultsView
- .getTargetEntitySet().getEntityType()));
- } else {
- edmPropertyValueMap = jpaResultParser.parse2EdmPropertyValueMap(jpaEntity, edmEntityType);
- }
-
- expandList = resultsView.getExpand();
- if (expandList != null && expandList.size() != 0) {
- HashMap<String, Object> navigationMap =
- jpaResultParser.parse2EdmNavigationValueMap(jpaEntity, constructListofNavProperty(expandList));
- edmPropertyValueMap.putAll(navigationMap);
- }
- EntityProviderWriteProperties feedProperties = null;
- feedProperties = getEntityProviderProperties(oDataJPAContext, resultsView);
- odataResponse =
- EntityProvider.writeEntry(contentType, resultsView.getTargetEntitySet(), edmPropertyValueMap, feedProperties);
-
- odataResponse = ODataResponse.fromResponse(odataResponse).status(HttpStatusCodes.OK).build();
-
- } catch (EntityProviderException e) {
- throw ODataJPARuntimeException.throwException(ODataJPARuntimeException.GENERAL.addContent(e.getMessage()), e);
- } catch (EdmException e) {
- throw ODataJPARuntimeException.throwException(ODataJPARuntimeException.GENERAL.addContent(e.getMessage()), e);
- }
-
- return odataResponse;
- }
-
- /* Response for $count */
- public static ODataResponse build(final long jpaEntityCount, final ODataJPAContext oDataJPAContext)
- throws ODataJPARuntimeException {
-
- ODataResponse odataResponse = null;
- try {
- odataResponse = EntityProvider.writeText(String.valueOf(jpaEntityCount));
- odataResponse = ODataResponse.fromResponse(odataResponse).build();
- } catch (EntityProviderException e) {
- throw ODataJPARuntimeException.throwException(ODataJPARuntimeException.GENERAL.addContent(e.getMessage()), e);
- }
- return odataResponse;
- }
-
- /* Response for Create Entity */
- @SuppressWarnings("unchecked")
- public static ODataResponse build(final List<Object> createdObjectList, final PostUriInfo uriInfo,
- final String contentType, final ODataJPAContext oDataJPAContext) throws ODataJPARuntimeException,
- ODataNotFoundException {
-
- if (createdObjectList == null || createdObjectList.size() == 0 || createdObjectList.get(0) == null) {
- throw new ODataNotFoundException(ODataNotFoundException.ENTITY);
- }
-
- EdmEntityType edmEntityType = null;
- ODataResponse odataResponse = null;
-
- try {
-
- edmEntityType = uriInfo.getTargetEntitySet().getEntityType();
- Map<String, Object> edmPropertyValueMap = null;
-
- JPAEntityParser jpaResultParser = new JPAEntityParser();
- edmPropertyValueMap = jpaResultParser.parse2EdmPropertyValueMap(createdObjectList.get(0), edmEntityType);
-
- List<ArrayList<NavigationPropertySegment>> expandList = null;
- if (createdObjectList.get(1) != null
- && ((Map<EdmNavigationProperty, EdmEntitySet>) createdObjectList.get(1)).size() > 0) {
- expandList = getExpandList((Map<EdmNavigationProperty, EdmEntitySet>) createdObjectList.get(1));
- HashMap<String, Object> navigationMap =
- jpaResultParser.parse2EdmNavigationValueMap(createdObjectList.get(0),
- constructListofNavProperty(expandList));
- edmPropertyValueMap.putAll(navigationMap);
- }
- EntityProviderWriteProperties feedProperties = null;
- try {
- feedProperties = getEntityProviderPropertiesforPost(oDataJPAContext, uriInfo, expandList);
- } catch (ODataException e) {
- throw ODataJPARuntimeException.throwException(ODataJPARuntimeException.INNER_EXCEPTION, e);
- }
-
- odataResponse =
- EntityProvider.writeEntry(contentType, uriInfo.getTargetEntitySet(), edmPropertyValueMap, feedProperties);
-
- odataResponse = ODataResponse.fromResponse(odataResponse).status(HttpStatusCodes.CREATED).build();
-
- } catch (EntityProviderException e) {
- throw ODataJPARuntimeException.throwException(ODataJPARuntimeException.GENERAL.addContent(e.getMessage()), e);
- } catch (EdmException e) {
- throw ODataJPARuntimeException.throwException(ODataJPARuntimeException.GENERAL.addContent(e.getMessage()), e);
- }
-
- return odataResponse;
- }
-
- /* Response for Update Entity */
- public static ODataResponse build(final Object updatedObject, final PutMergePatchUriInfo putUriInfo)
- throws ODataJPARuntimeException, ODataNotFoundException {
- if (updatedObject == null) {
- throw new ODataNotFoundException(ODataNotFoundException.ENTITY);
- }
- return ODataResponse.status(HttpStatusCodes.NO_CONTENT).build();
- }
-
- /* Response for Delete Entity */
- public static ODataResponse build(final Object deletedObject, final DeleteUriInfo deleteUriInfo)
- throws ODataJPARuntimeException, ODataNotFoundException {
-
- if (deletedObject == null) {
- throw new ODataNotFoundException(ODataNotFoundException.ENTITY);
- }
- return ODataResponse.status(HttpStatusCodes.NO_CONTENT).build();
- }
-
- /* Response for Function Import Single Result */
- public static ODataResponse build(final Object result, final GetFunctionImportUriInfo resultsView)
- throws ODataJPARuntimeException {
-
- try {
- final EdmFunctionImport functionImport = resultsView.getFunctionImport();
- final EdmSimpleType type = (EdmSimpleType) functionImport.getReturnType().getType();
-
- if (result != null) {
- ODataResponse response = null;
-
- final String value = type.valueToString(result, EdmLiteralKind.DEFAULT, null);
- response = EntityProvider.writeText(value);
-
- return ODataResponse.fromResponse(response).build();
- } else {
- throw new ODataNotFoundException(ODataHttpException.COMMON);
- }
- } catch (EdmException e) {
- throw ODataJPARuntimeException.throwException(ODataJPARuntimeException.GENERAL.addContent(e.getMessage()), e);
- } catch (EntityProviderException e) {
- throw ODataJPARuntimeException.throwException(ODataJPARuntimeException.GENERAL.addContent(e.getMessage()), e);
- } catch (ODataException e) {
- throw ODataJPARuntimeException.throwException(ODataJPARuntimeException.INNER_EXCEPTION, e);
- }
- }
-
- /* Response for Function Import Multiple Result */
- public static ODataResponse build(final List<Object> resultList, final GetFunctionImportUriInfo resultsView,
- final String contentType, final ODataJPAContext oDataJPAContext) throws ODataJPARuntimeException,
- ODataNotFoundException {
-
- ODataResponse odataResponse = null;
-
- if (resultList != null && !resultList.isEmpty()) {
- JPAEntityParser jpaResultParser = new JPAEntityParser();
- EdmType edmType = null;
- EdmFunctionImport functionImport = null;
- Map<String, Object> edmPropertyValueMap = null;
- List<Map<String, Object>> edmEntityList = null;
- Object result = null;
- try {
- EntityProviderWriteProperties feedProperties = null;
-
- feedProperties =
- EntityProviderWriteProperties.serviceRoot(oDataJPAContext.getODataContext().getPathInfo().getServiceRoot())
- .build();
-
- functionImport = resultsView.getFunctionImport();
- edmType = functionImport.getReturnType().getType();
-
- if (edmType.getKind().equals(EdmTypeKind.ENTITY) || edmType.getKind().equals(EdmTypeKind.COMPLEX)) {
- if (functionImport.getReturnType().getMultiplicity().equals(EdmMultiplicity.MANY)) {
- edmEntityList = new ArrayList<Map<String, Object>>();
- for (Object jpaEntity : resultList) {
- edmPropertyValueMap = jpaResultParser.parse2EdmPropertyValueMap(jpaEntity, (EdmStructuralType) edmType);
- edmEntityList.add(edmPropertyValueMap);
- }
- result = edmEntityList;
- } else {
-
- Object resultObject = resultList.get(0);
- edmPropertyValueMap = jpaResultParser.parse2EdmPropertyValueMap(resultObject, (EdmStructuralType) edmType);
-
- result = edmPropertyValueMap;
- }
-
- } else if (edmType.getKind().equals(EdmTypeKind.SIMPLE)) {
- result = resultList.get(0);
- }
-
- odataResponse =
- EntityProvider.writeFunctionImport(contentType, resultsView.getFunctionImport(), result, feedProperties);
- odataResponse = ODataResponse.fromResponse(odataResponse).status(HttpStatusCodes.OK).build();
-
- } catch (EdmException e) {
- throw ODataJPARuntimeException.throwException(ODataJPARuntimeException.GENERAL.addContent(e.getMessage()), e);
- } catch (EntityProviderException e) {
- throw ODataJPARuntimeException.throwException(ODataJPARuntimeException.GENERAL.addContent(e.getMessage()), e);
- } catch (ODataException e) {
- throw ODataJPARuntimeException.throwException(ODataJPARuntimeException.INNER_EXCEPTION, e);
- }
-
- } else {
- throw new ODataNotFoundException(ODataHttpException.COMMON);
- }
-
- return odataResponse;
- }
-
- /* Response for Read Entity Link */
- public static ODataResponse build(final Object jpaEntity, final GetEntityLinkUriInfo resultsView,
- final String contentType, final ODataJPAContext oDataJPAContext) throws ODataNotFoundException,
- ODataJPARuntimeException {
-
- if (jpaEntity == null) {
- throw new ODataNotFoundException(ODataNotFoundException.ENTITY);
- }
- EdmEntityType edmEntityType = null;
- ODataResponse odataResponse = null;
-
- try {
-
- EdmEntitySet entitySet = resultsView.getTargetEntitySet();
- edmEntityType = entitySet.getEntityType();
- Map<String, Object> edmPropertyValueMap = null;
-
- JPAEntityParser jpaResultParser = new JPAEntityParser();
- edmPropertyValueMap = jpaResultParser.parse2EdmPropertyValueMap(jpaEntity, edmEntityType.getKeyProperties());
-
- EntityProviderWriteProperties entryProperties =
- EntityProviderWriteProperties.serviceRoot(oDataJPAContext.getODataContext().getPathInfo().getServiceRoot())
- .build();
-
- ODataResponse response = EntityProvider.writeLink(contentType, entitySet, edmPropertyValueMap, entryProperties);
-
- odataResponse = ODataResponse.fromResponse(response).build();
-
- } catch (ODataException e) {
- throw ODataJPARuntimeException.throwException(ODataJPARuntimeException.INNER_EXCEPTION, e);
-
- }
-
- return odataResponse;
- }
-
- /* Response for Read Entity Links */
- public static <T> ODataResponse build(final List<T> jpaEntities, final GetEntitySetLinksUriInfo resultsView,
- final String contentType, final ODataJPAContext oDataJPAContext) throws ODataJPARuntimeException {
- EdmEntityType edmEntityType = null;
- ODataResponse odataResponse = null;
-
- try {
-
- EdmEntitySet entitySet = resultsView.getTargetEntitySet();
- edmEntityType = entitySet.getEntityType();
- List<EdmProperty> keyProperties = edmEntityType.getKeyProperties();
-
- List<Map<String, Object>> edmEntityList = new ArrayList<Map<String, Object>>();
- Map<String, Object> edmPropertyValueMap = null;
- JPAEntityParser jpaResultParser = new JPAEntityParser();
-
- for (Object jpaEntity : jpaEntities) {
- edmPropertyValueMap = jpaResultParser.parse2EdmPropertyValueMap(jpaEntity, keyProperties);
- edmEntityList.add(edmPropertyValueMap);
- }
-
- Integer count = null;
- if (resultsView.getInlineCount() != null) {
- if ((resultsView.getSkip() != null || resultsView.getTop() != null)) {
- // when $skip and/or $top is present with $inlinecount
- count = getInlineCountForNonFilterQueryLinks(edmEntityList, resultsView);
- } else {
- // In all other cases
- count = resultsView.getInlineCount() == InlineCount.ALLPAGES ? edmEntityList.size() : null;
- }
- }
-
- ODataContext context = oDataJPAContext.getODataContext();
- EntityProviderWriteProperties entryProperties =
- EntityProviderWriteProperties.serviceRoot(context.getPathInfo().getServiceRoot()).inlineCountType(
- resultsView.getInlineCount()).inlineCount(count).build();
-
- odataResponse = EntityProvider.writeLinks(contentType, entitySet, edmEntityList, entryProperties);
-
- odataResponse = ODataResponse.fromResponse(odataResponse).build();
-
- } catch (ODataException e) {
- throw ODataJPARuntimeException.throwException(ODataJPARuntimeException.GENERAL.addContent(e.getMessage()), e);
- }
-
- return odataResponse;
-
- }
-
- /*
- * This method handles $inlinecount request. It also modifies the list of results in case of
- * $inlinecount and $top/$skip combinations. Specific to LinksUriInfo.
- *
- * @param edmEntityList
- *
- * @param resultsView
- *
- * @return
- */
- private static Integer getInlineCountForNonFilterQueryLinks(final List<Map<String, Object>> edmEntityList,
- final GetEntitySetLinksUriInfo resultsView) {
- // when $skip and/or $top is present with $inlinecount, first get the total count
- Integer count = null;
- if (resultsView.getInlineCount() == InlineCount.ALLPAGES) {
- if (resultsView.getSkip() != null || resultsView.getTop() != null) {
- count = edmEntityList.size();
- // Now update the list
- if (resultsView.getSkip() != null) {
- // Index checks to avoid IndexOutOfBoundsException
- if (resultsView.getSkip() > edmEntityList.size()) {
- edmEntityList.clear();
- return count;
- }
- edmEntityList.subList(0, resultsView.getSkip()).clear();
- }
- if (resultsView.getTop() != null && resultsView.getTop() >= 0 && resultsView.getTop() < edmEntityList.size()) {
- edmEntityList.subList(0, resultsView.getTop());
- }
- }
- }// Inlinecount of None is handled by default - null
- return count;
- }
-
- /*
- * Method to build the entity provider Property.Callbacks for $expand would
- * be registered here
- */
- private static EntityProviderWriteProperties getEntityProviderProperties(final ODataJPAContext odataJPAContext,
- final GetEntitySetUriInfo resultsView, final List<Map<String, Object>> edmEntityList)
- throws ODataJPARuntimeException {
- ODataEntityProviderPropertiesBuilder entityFeedPropertiesBuilder = null;
-
- Integer count = null;
- if (resultsView.getInlineCount() != null) {
- if ((resultsView.getSkip() != null || resultsView.getTop() != null)) {
- // when $skip and/or $top is present with $inlinecount
- count = getInlineCountForNonFilterQueryEntitySet(edmEntityList, resultsView);
- } else {
- // In all other cases
- count = resultsView.getInlineCount() == InlineCount.ALLPAGES ? edmEntityList.size() : null;
- }
- }
-
- try {
- entityFeedPropertiesBuilder =
- EntityProviderWriteProperties.serviceRoot(odataJPAContext.getODataContext().getPathInfo().getServiceRoot());
- entityFeedPropertiesBuilder.inlineCount(count);
- entityFeedPropertiesBuilder.inlineCountType(resultsView.getInlineCount());
- ExpandSelectTreeNode expandSelectTree =
- UriParser.createExpandSelectTree(resultsView.getSelect(), resultsView.getExpand());
- entityFeedPropertiesBuilder.callbacks(JPAExpandCallBack.getCallbacks(odataJPAContext.getODataContext()
- .getPathInfo().getServiceRoot(), expandSelectTree, resultsView.getExpand()));
- entityFeedPropertiesBuilder.expandSelectTree(expandSelectTree);
-
- } catch (ODataException e) {
- throw ODataJPARuntimeException.throwException(ODataJPARuntimeException.INNER_EXCEPTION, e);
- }
-
- return entityFeedPropertiesBuilder.build();
- }
-
- /*
- * This method handles $inlinecount request. It also modifies the list of results in case of
- * $inlinecount and $top/$skip combinations. Specific to Entity Set.
- */
- private static Integer getInlineCountForNonFilterQueryEntitySet(final List<Map<String, Object>> edmEntityList,
- final GetEntitySetUriInfo resultsView) {
- // when $skip and/or $top is present with $inlinecount, first get the total count
- Integer count = null;
- if (resultsView.getInlineCount() == InlineCount.ALLPAGES) {
- if (resultsView.getSkip() != null || resultsView.getTop() != null) {
- count = edmEntityList.size();
- // Now update the list
- if (resultsView.getSkip() != null) {
- // Index checks to avoid IndexOutOfBoundsException
- if (resultsView.getSkip() > edmEntityList.size()) {
- edmEntityList.clear();
- return count;
- }
- edmEntityList.subList(0, resultsView.getSkip()).clear();
- }
- if (resultsView.getTop() != null && resultsView.getTop() >= 0 && resultsView.getTop() < edmEntityList.size()) {
- edmEntityList.retainAll(edmEntityList.subList(0, resultsView.getTop()));
- }
- }
- }// Inlinecount of None is handled by default - null
- return count;
- }
-
- private static EntityProviderWriteProperties getEntityProviderProperties(final ODataJPAContext odataJPAContext,
- final GetEntityUriInfo resultsView) throws ODataJPARuntimeException {
- ODataEntityProviderPropertiesBuilder entityFeedPropertiesBuilder = null;
- ExpandSelectTreeNode expandSelectTree = null;
- try {
- entityFeedPropertiesBuilder =
- EntityProviderWriteProperties.serviceRoot(odataJPAContext.getODataContext().getPathInfo().getServiceRoot());
- expandSelectTree = UriParser.createExpandSelectTree(resultsView.getSelect(), resultsView.getExpand());
- entityFeedPropertiesBuilder.expandSelectTree(expandSelectTree);
- entityFeedPropertiesBuilder.callbacks(JPAExpandCallBack.getCallbacks(odataJPAContext.getODataContext()
- .getPathInfo().getServiceRoot(), expandSelectTree, resultsView.getExpand()));
- } catch (ODataException e) {
- throw ODataJPARuntimeException.throwException(ODataJPARuntimeException.INNER_EXCEPTION, e);
- }
-
- return entityFeedPropertiesBuilder.build();
- }
-
- private static EntityProviderWriteProperties getEntityProviderPropertiesforPost(
- final ODataJPAContext odataJPAContext, final PostUriInfo resultsView,
- final List<ArrayList<NavigationPropertySegment>> expandList) throws ODataJPARuntimeException {
- ODataEntityProviderPropertiesBuilder entityFeedPropertiesBuilder = null;
- ExpandSelectTreeNode expandSelectTree = null;
- try {
- entityFeedPropertiesBuilder =
- EntityProviderWriteProperties.serviceRoot(odataJPAContext.getODataContext().getPathInfo().getServiceRoot());
- expandSelectTree = UriParser.createExpandSelectTree(null, expandList);
- entityFeedPropertiesBuilder.expandSelectTree(expandSelectTree);
- entityFeedPropertiesBuilder.callbacks(JPAExpandCallBack.getCallbacks(odataJPAContext.getODataContext()
- .getPathInfo().getServiceRoot(), expandSelectTree, expandList));
- } catch (ODataException e) {
- throw ODataJPARuntimeException.throwException(ODataJPARuntimeException.INNER_EXCEPTION, e);
- }
-
- return entityFeedPropertiesBuilder.build();
- }
-
- private static List<ArrayList<NavigationPropertySegment>> getExpandList(
- final Map<EdmNavigationProperty, EdmEntitySet> navPropEntitySetMap) {
- List<ArrayList<NavigationPropertySegment>> expandList = new ArrayList<ArrayList<NavigationPropertySegment>>();
- ArrayList<NavigationPropertySegment> navigationPropertySegmentList = new ArrayList<NavigationPropertySegment>();
- for (Map.Entry<EdmNavigationProperty, EdmEntitySet> entry : navPropEntitySetMap.entrySet()) {
- final EdmNavigationProperty edmNavigationProperty = entry.getKey();
- final EdmEntitySet edmEntitySet = entry.getValue();
- NavigationPropertySegment navigationPropertySegment = new NavigationPropertySegment() {
-
- @Override
- public EdmEntitySet getTargetEntitySet() {
- return edmEntitySet;
- }
-
- @Override
- public EdmNavigationProperty getNavigationProperty() {
- return edmNavigationProperty;
- }
- };
- navigationPropertySegmentList.add(navigationPropertySegment);
- }
- expandList.add(navigationPropertySegmentList);
- return expandList;
- }
-
- private static List<EdmProperty> buildSelectItemList(final List<SelectItem> selectItems, final EdmEntityType entity)
- throws ODataJPARuntimeException {
- boolean flag = false;
- List<EdmProperty> selectPropertyList = new ArrayList<EdmProperty>();
- try {
- for (SelectItem selectItem : selectItems) {
- selectPropertyList.add(selectItem.getProperty());
- }
- for (EdmProperty keyProperty : entity.getKeyProperties()) {
- flag = true;
- for (SelectItem selectedItem : selectItems) {
- if (selectedItem.getProperty().equals(keyProperty)) {
- flag = false;
- break;
- }
- }
- if (flag == true) {
- selectPropertyList.add(keyProperty);
- }
- }
-
- } catch (EdmException e) {
- throw ODataJPARuntimeException.throwException(ODataJPARuntimeException.GENERAL.addContent(e.getMessage()), e);
- }
- return selectPropertyList;
- }
-
- private static List<EdmNavigationProperty> constructListofNavProperty(
- final List<ArrayList<NavigationPropertySegment>> expandList) {
- List<EdmNavigationProperty> navigationPropertyList = new ArrayList<EdmNavigationProperty>();
- for (ArrayList<NavigationPropertySegment> navpropSegment : expandList) {
- navigationPropertyList.add(navpropSegment.get(0).getNavigationProperty());
- }
- return navigationPropertyList;
- }
-
-}
http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/blob/f8638bb7/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/ODataJPAResponseBuilderDefault.java
----------------------------------------------------------------------
diff --git a/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/ODataJPAResponseBuilderDefault.java b/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/ODataJPAResponseBuilderDefault.java
new file mode 100644
index 0000000..c11713c
--- /dev/null
+++ b/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/ODataJPAResponseBuilderDefault.java
@@ -0,0 +1,646 @@
+/*******************************************************************************
+ * 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.olingo.odata2.jpa.processor.core;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import org.apache.olingo.odata2.api.commons.HttpStatusCodes;
+import org.apache.olingo.odata2.api.commons.InlineCount;
+import org.apache.olingo.odata2.api.edm.EdmEntitySet;
+import org.apache.olingo.odata2.api.edm.EdmEntityType;
+import org.apache.olingo.odata2.api.edm.EdmException;
+import org.apache.olingo.odata2.api.edm.EdmFunctionImport;
+import org.apache.olingo.odata2.api.edm.EdmLiteralKind;
+import org.apache.olingo.odata2.api.edm.EdmMultiplicity;
+import org.apache.olingo.odata2.api.edm.EdmNavigationProperty;
+import org.apache.olingo.odata2.api.edm.EdmProperty;
+import org.apache.olingo.odata2.api.edm.EdmSimpleType;
+import org.apache.olingo.odata2.api.edm.EdmStructuralType;
+import org.apache.olingo.odata2.api.edm.EdmType;
+import org.apache.olingo.odata2.api.edm.EdmTypeKind;
+import org.apache.olingo.odata2.api.ep.EntityProvider;
+import org.apache.olingo.odata2.api.ep.EntityProviderException;
+import org.apache.olingo.odata2.api.ep.EntityProviderWriteProperties;
+import org.apache.olingo.odata2.api.ep.EntityProviderWriteProperties.ODataEntityProviderPropertiesBuilder;
+import org.apache.olingo.odata2.api.exception.ODataException;
+import org.apache.olingo.odata2.api.exception.ODataHttpException;
+import org.apache.olingo.odata2.api.exception.ODataNotFoundException;
+import org.apache.olingo.odata2.api.processor.ODataContext;
+import org.apache.olingo.odata2.api.processor.ODataResponse;
+import org.apache.olingo.odata2.api.uri.ExpandSelectTreeNode;
+import org.apache.olingo.odata2.api.uri.NavigationPropertySegment;
+import org.apache.olingo.odata2.api.uri.SelectItem;
+import org.apache.olingo.odata2.api.uri.UriParser;
+import org.apache.olingo.odata2.api.uri.info.DeleteUriInfo;
+import org.apache.olingo.odata2.api.uri.info.GetEntityLinkUriInfo;
+import org.apache.olingo.odata2.api.uri.info.GetEntitySetLinksUriInfo;
+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.GetFunctionImportUriInfo;
+import org.apache.olingo.odata2.api.uri.info.PostUriInfo;
+import org.apache.olingo.odata2.api.uri.info.PutMergePatchUriInfo;
+import org.apache.olingo.odata2.jpa.processor.api.ODataJPAContext;
+import org.apache.olingo.odata2.jpa.processor.api.ODataJPAResponseBuilder;
+import org.apache.olingo.odata2.jpa.processor.api.exception.ODataJPARuntimeException;
+import org.apache.olingo.odata2.jpa.processor.core.access.data.JPAEntityParser;
+import org.apache.olingo.odata2.jpa.processor.core.access.data.JPAExpandCallBack;
+
+public final class ODataJPAResponseBuilderDefault implements ODataJPAResponseBuilder {
+
+ private final ODataJPAContext oDataJPAContext;
+
+ public ODataJPAResponseBuilderDefault(final ODataJPAContext context) {
+ oDataJPAContext = context;
+ }
+
+ /* Response for Read Entity Set */
+ @Override
+ public ODataResponse build(final GetEntitySetUriInfo resultsView, final List<Object> jpaEntities,
+ final String contentType) throws ODataJPARuntimeException {
+
+ EdmEntityType edmEntityType = null;
+ ODataResponse odataResponse = null;
+ List<ArrayList<NavigationPropertySegment>> expandList = null;
+
+ try {
+ edmEntityType = resultsView.getTargetEntitySet().getEntityType();
+ List<Map<String, Object>> edmEntityList = new ArrayList<Map<String, Object>>();
+ Map<String, Object> edmPropertyValueMap = null;
+ JPAEntityParser jpaResultParser = new JPAEntityParser();
+ final List<SelectItem> selectedItems = resultsView.getSelect();
+ if (selectedItems != null && selectedItems.size() > 0) {
+ for (Object jpaEntity : jpaEntities) {
+ edmPropertyValueMap =
+ jpaResultParser.parse2EdmPropertyValueMap(jpaEntity, buildSelectItemList(selectedItems, edmEntityType));
+ edmEntityList.add(edmPropertyValueMap);
+ }
+ } else {
+ for (Object jpaEntity : jpaEntities) {
+ edmPropertyValueMap = jpaResultParser.parse2EdmPropertyValueMap(jpaEntity, edmEntityType);
+ edmEntityList.add(edmPropertyValueMap);
+ }
+ }
+ expandList = resultsView.getExpand();
+ if (expandList != null && expandList.size() != 0) {
+ int count = 0;
+ List<EdmNavigationProperty> edmNavPropertyList = constructListofNavProperty(expandList);
+ for (Object jpaEntity : jpaEntities) {
+ Map<String, Object> relationShipMap = edmEntityList.get(count);
+ HashMap<String, Object> navigationMap =
+ jpaResultParser.parse2EdmNavigationValueMap(jpaEntity, edmNavPropertyList);
+ relationShipMap.putAll(navigationMap);
+ count++;
+ }
+ }
+
+ EntityProviderWriteProperties feedProperties = null;
+
+ feedProperties = getEntityProviderProperties(oDataJPAContext, resultsView, edmEntityList);
+ odataResponse =
+ EntityProvider.writeFeed(contentType, resultsView.getTargetEntitySet(), edmEntityList, feedProperties);
+ odataResponse = ODataResponse.fromResponse(odataResponse).status(HttpStatusCodes.OK).build();
+
+ } catch (EntityProviderException e) {
+ throw ODataJPARuntimeException.throwException(ODataJPARuntimeException.GENERAL.addContent(e.getMessage()), e);
+ } catch (EdmException e) {
+ throw ODataJPARuntimeException.throwException(ODataJPARuntimeException.GENERAL.addContent(e.getMessage()), e);
+ }
+
+ return odataResponse;
+ }
+
+ /* Response for Read Entity */
+ @Override
+ public ODataResponse build(final GetEntityUriInfo resultsView, final Object jpaEntity,
+ final String contentType) throws ODataJPARuntimeException,
+ ODataNotFoundException {
+
+ List<ArrayList<NavigationPropertySegment>> expandList = null;
+ if (jpaEntity == null) {
+ throw new ODataNotFoundException(ODataNotFoundException.ENTITY);
+ }
+ EdmEntityType edmEntityType = null;
+ ODataResponse odataResponse = null;
+
+ try {
+
+ edmEntityType = resultsView.getTargetEntitySet().getEntityType();
+ Map<String, Object> edmPropertyValueMap = null;
+
+ JPAEntityParser jpaResultParser = new JPAEntityParser();
+ final List<SelectItem> selectedItems = resultsView.getSelect();
+ if (selectedItems != null && selectedItems.size() > 0) {
+ edmPropertyValueMap =
+ jpaResultParser.parse2EdmPropertyValueMap(jpaEntity, buildSelectItemList(selectedItems, resultsView
+ .getTargetEntitySet().getEntityType()));
+ } else {
+ edmPropertyValueMap = jpaResultParser.parse2EdmPropertyValueMap(jpaEntity, edmEntityType);
+ }
+
+ expandList = resultsView.getExpand();
+ if (expandList != null && expandList.size() != 0) {
+ HashMap<String, Object> navigationMap =
+ jpaResultParser.parse2EdmNavigationValueMap(jpaEntity, constructListofNavProperty(expandList));
+ edmPropertyValueMap.putAll(navigationMap);
+ }
+ EntityProviderWriteProperties feedProperties = null;
+ feedProperties = getEntityProviderProperties(oDataJPAContext, resultsView);
+ odataResponse =
+ EntityProvider.writeEntry(contentType, resultsView.getTargetEntitySet(), edmPropertyValueMap, feedProperties);
+
+ odataResponse = ODataResponse.fromResponse(odataResponse).status(HttpStatusCodes.OK).build();
+
+ } catch (EntityProviderException e) {
+ throw ODataJPARuntimeException.throwException(ODataJPARuntimeException.GENERAL.addContent(e.getMessage()), e);
+ } catch (EdmException e) {
+ throw ODataJPARuntimeException.throwException(ODataJPARuntimeException.GENERAL.addContent(e.getMessage()), e);
+ }
+
+ return odataResponse;
+ }
+
+ /* Response for $count */
+ @Override
+ public ODataResponse build(final long jpaEntityCount)
+ throws ODataJPARuntimeException {
+
+ ODataResponse odataResponse = null;
+ try {
+ odataResponse = EntityProvider.writeText(String.valueOf(jpaEntityCount));
+ odataResponse = ODataResponse.fromResponse(odataResponse).build();
+ } catch (EntityProviderException e) {
+ throw ODataJPARuntimeException.throwException(ODataJPARuntimeException.GENERAL.addContent(e.getMessage()), e);
+ }
+ return odataResponse;
+ }
+
+ /* Response for Create Entity */
+ @SuppressWarnings("unchecked")
+ @Override
+ public ODataResponse build(final PostUriInfo uriInfo, final List<Object> createdObjectList,
+ final String contentType) throws ODataJPARuntimeException,
+ ODataNotFoundException {
+
+ if (createdObjectList == null || createdObjectList.size() == 0 || createdObjectList.get(0) == null) {
+ throw new ODataNotFoundException(ODataNotFoundException.ENTITY);
+ }
+
+ EdmEntityType edmEntityType = null;
+ ODataResponse odataResponse = null;
+
+ try {
+
+ edmEntityType = uriInfo.getTargetEntitySet().getEntityType();
+ Map<String, Object> edmPropertyValueMap = null;
+
+ JPAEntityParser jpaResultParser = new JPAEntityParser();
+ edmPropertyValueMap = jpaResultParser.parse2EdmPropertyValueMap(createdObjectList.get(0), edmEntityType);
+
+ List<ArrayList<NavigationPropertySegment>> expandList = null;
+ if (createdObjectList.get(1) != null
+ && ((Map<EdmNavigationProperty, EdmEntitySet>) createdObjectList.get(1)).size() > 0) {
+ expandList = getExpandList((Map<EdmNavigationProperty, EdmEntitySet>) createdObjectList.get(1));
+ HashMap<String, Object> navigationMap =
+ jpaResultParser.parse2EdmNavigationValueMap(createdObjectList.get(0),
+ constructListofNavProperty(expandList));
+ edmPropertyValueMap.putAll(navigationMap);
+ }
+ EntityProviderWriteProperties feedProperties = null;
+ try {
+ feedProperties = getEntityProviderPropertiesforPost(oDataJPAContext, uriInfo, expandList);
+ } catch (ODataException e) {
+ throw ODataJPARuntimeException.throwException(ODataJPARuntimeException.INNER_EXCEPTION, e);
+ }
+
+ odataResponse =
+ EntityProvider.writeEntry(contentType, uriInfo.getTargetEntitySet(), edmPropertyValueMap, feedProperties);
+
+ odataResponse = ODataResponse.fromResponse(odataResponse).status(HttpStatusCodes.CREATED).build();
+
+ } catch (EntityProviderException e) {
+ throw ODataJPARuntimeException.throwException(ODataJPARuntimeException.GENERAL.addContent(e.getMessage()), e);
+ } catch (EdmException e) {
+ throw ODataJPARuntimeException.throwException(ODataJPARuntimeException.GENERAL.addContent(e.getMessage()), e);
+ }
+
+ return odataResponse;
+ }
+
+ /* Response for Update Entity */
+ @Override
+ public ODataResponse build(final PutMergePatchUriInfo putUriInfo, final Object updatedObject)
+ throws ODataJPARuntimeException, ODataNotFoundException {
+ if (updatedObject == null) {
+ throw new ODataNotFoundException(ODataNotFoundException.ENTITY);
+ }
+ return ODataResponse.status(HttpStatusCodes.NO_CONTENT).build();
+ }
+
+ /* Response for Delete Entity */
+ @Override
+ public ODataResponse build(final DeleteUriInfo deleteUriInfo, final Object deletedObject)
+ throws ODataJPARuntimeException, ODataNotFoundException {
+
+ if (deletedObject == null) {
+ throw new ODataNotFoundException(ODataNotFoundException.ENTITY);
+ }
+ return ODataResponse.status(HttpStatusCodes.NO_CONTENT).build();
+ }
+
+ /* Response for Function Import Single Result */
+ @Override
+ public ODataResponse build(final GetFunctionImportUriInfo resultsView, final Object result)
+ throws ODataJPARuntimeException {
+
+ try {
+ final EdmFunctionImport functionImport = resultsView.getFunctionImport();
+ final EdmSimpleType type = (EdmSimpleType) functionImport.getReturnType().getType();
+
+ if (result != null) {
+ ODataResponse response = null;
+
+ final String value = type.valueToString(result, EdmLiteralKind.DEFAULT, null);
+ response = EntityProvider.writeText(value);
+
+ return ODataResponse.fromResponse(response).build();
+ } else {
+ throw new ODataNotFoundException(ODataHttpException.COMMON);
+ }
+ } catch (EdmException e) {
+ throw ODataJPARuntimeException.throwException(ODataJPARuntimeException.GENERAL.addContent(e.getMessage()), e);
+ } catch (EntityProviderException e) {
+ throw ODataJPARuntimeException.throwException(ODataJPARuntimeException.GENERAL.addContent(e.getMessage()), e);
+ } catch (ODataException e) {
+ throw ODataJPARuntimeException.throwException(ODataJPARuntimeException.INNER_EXCEPTION, e);
+ }
+ }
+
+ /* Response for Function Import Multiple Result */
+ @Override
+ public ODataResponse build(final GetFunctionImportUriInfo resultsView, final List<Object> resultList,
+ final String contentType) throws ODataJPARuntimeException,
+ ODataNotFoundException {
+
+ ODataResponse odataResponse = null;
+
+ if (resultList != null && !resultList.isEmpty()) {
+ JPAEntityParser jpaResultParser = new JPAEntityParser();
+ EdmType edmType = null;
+ EdmFunctionImport functionImport = null;
+ Map<String, Object> edmPropertyValueMap = null;
+ List<Map<String, Object>> edmEntityList = null;
+ Object result = null;
+ try {
+ EntityProviderWriteProperties feedProperties = null;
+
+ feedProperties =
+ EntityProviderWriteProperties.serviceRoot(oDataJPAContext.getODataContext().getPathInfo().getServiceRoot())
+ .build();
+
+ functionImport = resultsView.getFunctionImport();
+ edmType = functionImport.getReturnType().getType();
+
+ if (edmType.getKind().equals(EdmTypeKind.ENTITY) || edmType.getKind().equals(EdmTypeKind.COMPLEX)) {
+ if (functionImport.getReturnType().getMultiplicity().equals(EdmMultiplicity.MANY)) {
+ edmEntityList = new ArrayList<Map<String, Object>>();
+ for (Object jpaEntity : resultList) {
+ edmPropertyValueMap = jpaResultParser.parse2EdmPropertyValueMap(jpaEntity, (EdmStructuralType) edmType);
+ edmEntityList.add(edmPropertyValueMap);
+ }
+ result = edmEntityList;
+ } else {
+
+ Object resultObject = resultList.get(0);
+ edmPropertyValueMap = jpaResultParser.parse2EdmPropertyValueMap(resultObject, (EdmStructuralType) edmType);
+
+ result = edmPropertyValueMap;
+ }
+
+ } else if (edmType.getKind().equals(EdmTypeKind.SIMPLE)) {
+ result = resultList.get(0);
+ }
+
+ odataResponse =
+ EntityProvider.writeFunctionImport(contentType, resultsView.getFunctionImport(), result, feedProperties);
+ odataResponse = ODataResponse.fromResponse(odataResponse).status(HttpStatusCodes.OK).build();
+
+ } catch (EdmException e) {
+ throw ODataJPARuntimeException.throwException(ODataJPARuntimeException.GENERAL.addContent(e.getMessage()), e);
+ } catch (EntityProviderException e) {
+ throw ODataJPARuntimeException.throwException(ODataJPARuntimeException.GENERAL.addContent(e.getMessage()), e);
+ } catch (ODataException e) {
+ throw ODataJPARuntimeException.throwException(ODataJPARuntimeException.INNER_EXCEPTION, e);
+ }
+
+ } else {
+ throw new ODataNotFoundException(ODataHttpException.COMMON);
+ }
+
+ return odataResponse;
+ }
+
+ /* Response for Read Entity Link */
+ @Override
+ public ODataResponse build(final GetEntityLinkUriInfo resultsView, final Object jpaEntity,
+ final String contentType) throws ODataNotFoundException,
+ ODataJPARuntimeException {
+
+ if (jpaEntity == null) {
+ throw new ODataNotFoundException(ODataNotFoundException.ENTITY);
+ }
+ EdmEntityType edmEntityType = null;
+ ODataResponse odataResponse = null;
+
+ try {
+
+ EdmEntitySet entitySet = resultsView.getTargetEntitySet();
+ edmEntityType = entitySet.getEntityType();
+ Map<String, Object> edmPropertyValueMap = null;
+
+ JPAEntityParser jpaResultParser = new JPAEntityParser();
+ edmPropertyValueMap = jpaResultParser.parse2EdmPropertyValueMap(jpaEntity, edmEntityType.getKeyProperties());
+
+ EntityProviderWriteProperties entryProperties =
+ EntityProviderWriteProperties.serviceRoot(oDataJPAContext.getODataContext().getPathInfo().getServiceRoot())
+ .build();
+
+ ODataResponse response = EntityProvider.writeLink(contentType, entitySet, edmPropertyValueMap, entryProperties);
+
+ odataResponse = ODataResponse.fromResponse(response).build();
+
+ } catch (ODataException e) {
+ throw ODataJPARuntimeException.throwException(ODataJPARuntimeException.INNER_EXCEPTION, e);
+
+ }
+
+ return odataResponse;
+ }
+
+ /* Response for Read Entity Links */
+ @Override
+ public ODataResponse build(final GetEntitySetLinksUriInfo resultsView, final List<Object> jpaEntities,
+ final String contentType) throws ODataJPARuntimeException {
+ EdmEntityType edmEntityType = null;
+ ODataResponse odataResponse = null;
+
+ try {
+
+ EdmEntitySet entitySet = resultsView.getTargetEntitySet();
+ edmEntityType = entitySet.getEntityType();
+ List<EdmProperty> keyProperties = edmEntityType.getKeyProperties();
+
+ List<Map<String, Object>> edmEntityList = new ArrayList<Map<String, Object>>();
+ Map<String, Object> edmPropertyValueMap = null;
+ JPAEntityParser jpaResultParser = new JPAEntityParser();
+
+ for (Object jpaEntity : jpaEntities) {
+ edmPropertyValueMap = jpaResultParser.parse2EdmPropertyValueMap(jpaEntity, keyProperties);
+ edmEntityList.add(edmPropertyValueMap);
+ }
+
+ Integer count = null;
+ if (resultsView.getInlineCount() != null) {
+ if ((resultsView.getSkip() != null || resultsView.getTop() != null)) {
+ // when $skip and/or $top is present with $inlinecount
+ count = getInlineCountForNonFilterQueryLinks(edmEntityList, resultsView);
+ } else {
+ // In all other cases
+ count = resultsView.getInlineCount() == InlineCount.ALLPAGES ? edmEntityList.size() : null;
+ }
+ }
+
+ ODataContext context = oDataJPAContext.getODataContext();
+ EntityProviderWriteProperties entryProperties =
+ EntityProviderWriteProperties.serviceRoot(context.getPathInfo().getServiceRoot()).inlineCountType(
+ resultsView.getInlineCount()).inlineCount(count).build();
+
+ odataResponse = EntityProvider.writeLinks(contentType, entitySet, edmEntityList, entryProperties);
+
+ odataResponse = ODataResponse.fromResponse(odataResponse).build();
+
+ } catch (ODataException e) {
+ throw ODataJPARuntimeException.throwException(ODataJPARuntimeException.GENERAL.addContent(e.getMessage()), e);
+ }
+
+ return odataResponse;
+
+ }
+
+ /*
+ * This method handles $inlinecount request. It also modifies the list of results in case of
+ * $inlinecount and $top/$skip combinations. Specific to LinksUriInfo.
+ *
+ * @param edmEntityList
+ *
+ * @param resultsView
+ *
+ * @return
+ */
+ private static Integer getInlineCountForNonFilterQueryLinks(final List<Map<String, Object>> edmEntityList,
+ final GetEntitySetLinksUriInfo resultsView) {
+ // when $skip and/or $top is present with $inlinecount, first get the total count
+ Integer count = null;
+ if (resultsView.getInlineCount() == InlineCount.ALLPAGES) {
+ if (resultsView.getSkip() != null || resultsView.getTop() != null) {
+ count = edmEntityList.size();
+ // Now update the list
+ if (resultsView.getSkip() != null) {
+ // Index checks to avoid IndexOutOfBoundsException
+ if (resultsView.getSkip() > edmEntityList.size()) {
+ edmEntityList.clear();
+ return count;
+ }
+ edmEntityList.subList(0, resultsView.getSkip()).clear();
+ }
+ if (resultsView.getTop() != null && resultsView.getTop() >= 0 && resultsView.getTop() < edmEntityList.size()) {
+ edmEntityList.subList(0, resultsView.getTop());
+ }
+ }
+ }// Inlinecount of None is handled by default - null
+ return count;
+ }
+
+ /*
+ * Method to build the entity provider Property.Callbacks for $expand would
+ * be registered here
+ */
+ private static EntityProviderWriteProperties getEntityProviderProperties(final ODataJPAContext odataJPAContext,
+ final GetEntitySetUriInfo resultsView, final List<Map<String, Object>> edmEntityList)
+ throws ODataJPARuntimeException {
+ ODataEntityProviderPropertiesBuilder entityFeedPropertiesBuilder = null;
+
+ Integer count = null;
+ if (resultsView.getInlineCount() != null) {
+ if ((resultsView.getSkip() != null || resultsView.getTop() != null)) {
+ // when $skip and/or $top is present with $inlinecount
+ count = getInlineCountForNonFilterQueryEntitySet(edmEntityList, resultsView);
+ } else {
+ // In all other cases
+ count = resultsView.getInlineCount() == InlineCount.ALLPAGES ? edmEntityList.size() : null;
+ }
+ }
+
+ try {
+ entityFeedPropertiesBuilder =
+ EntityProviderWriteProperties.serviceRoot(odataJPAContext.getODataContext().getPathInfo().getServiceRoot());
+ entityFeedPropertiesBuilder.inlineCount(count);
+ entityFeedPropertiesBuilder.inlineCountType(resultsView.getInlineCount());
+ ExpandSelectTreeNode expandSelectTree =
+ UriParser.createExpandSelectTree(resultsView.getSelect(), resultsView.getExpand());
+ entityFeedPropertiesBuilder.callbacks(JPAExpandCallBack.getCallbacks(odataJPAContext.getODataContext()
+ .getPathInfo().getServiceRoot(), expandSelectTree, resultsView.getExpand()));
+ entityFeedPropertiesBuilder.expandSelectTree(expandSelectTree);
+
+ } catch (ODataException e) {
+ throw ODataJPARuntimeException.throwException(ODataJPARuntimeException.INNER_EXCEPTION, e);
+ }
+
+ return entityFeedPropertiesBuilder.build();
+ }
+
+ /*
+ * This method handles $inlinecount request. It also modifies the list of results in case of
+ * $inlinecount and $top/$skip combinations. Specific to Entity Set.
+ */
+ private static Integer getInlineCountForNonFilterQueryEntitySet(final List<Map<String, Object>> edmEntityList,
+ final GetEntitySetUriInfo resultsView) {
+ // when $skip and/or $top is present with $inlinecount, first get the total count
+ Integer count = null;
+ if (resultsView.getInlineCount() == InlineCount.ALLPAGES) {
+ if (resultsView.getSkip() != null || resultsView.getTop() != null) {
+ count = edmEntityList.size();
+ // Now update the list
+ if (resultsView.getSkip() != null) {
+ // Index checks to avoid IndexOutOfBoundsException
+ if (resultsView.getSkip() > edmEntityList.size()) {
+ edmEntityList.clear();
+ return count;
+ }
+ edmEntityList.subList(0, resultsView.getSkip()).clear();
+ }
+ if (resultsView.getTop() != null && resultsView.getTop() >= 0 && resultsView.getTop() < edmEntityList.size()) {
+ edmEntityList.retainAll(edmEntityList.subList(0, resultsView.getTop()));
+ }
+ }
+ }// Inlinecount of None is handled by default - null
+ return count;
+ }
+
+ private static EntityProviderWriteProperties getEntityProviderProperties(final ODataJPAContext odataJPAContext,
+ final GetEntityUriInfo resultsView) throws ODataJPARuntimeException {
+ ODataEntityProviderPropertiesBuilder entityFeedPropertiesBuilder = null;
+ ExpandSelectTreeNode expandSelectTree = null;
+ try {
+ entityFeedPropertiesBuilder =
+ EntityProviderWriteProperties.serviceRoot(odataJPAContext.getODataContext().getPathInfo().getServiceRoot());
+ expandSelectTree = UriParser.createExpandSelectTree(resultsView.getSelect(), resultsView.getExpand());
+ entityFeedPropertiesBuilder.expandSelectTree(expandSelectTree);
+ entityFeedPropertiesBuilder.callbacks(JPAExpandCallBack.getCallbacks(odataJPAContext.getODataContext()
+ .getPathInfo().getServiceRoot(), expandSelectTree, resultsView.getExpand()));
+ } catch (ODataException e) {
+ throw ODataJPARuntimeException.throwException(ODataJPARuntimeException.INNER_EXCEPTION, e);
+ }
+
+ return entityFeedPropertiesBuilder.build();
+ }
+
+ private static EntityProviderWriteProperties getEntityProviderPropertiesforPost(
+ final ODataJPAContext odataJPAContext, final PostUriInfo resultsView,
+ final List<ArrayList<NavigationPropertySegment>> expandList) throws ODataJPARuntimeException {
+ ODataEntityProviderPropertiesBuilder entityFeedPropertiesBuilder = null;
+ ExpandSelectTreeNode expandSelectTree = null;
+ try {
+ entityFeedPropertiesBuilder =
+ EntityProviderWriteProperties.serviceRoot(odataJPAContext.getODataContext().getPathInfo().getServiceRoot());
+ expandSelectTree = UriParser.createExpandSelectTree(null, expandList);
+ entityFeedPropertiesBuilder.expandSelectTree(expandSelectTree);
+ entityFeedPropertiesBuilder.callbacks(JPAExpandCallBack.getCallbacks(odataJPAContext.getODataContext()
+ .getPathInfo().getServiceRoot(), expandSelectTree, expandList));
+ } catch (ODataException e) {
+ throw ODataJPARuntimeException.throwException(ODataJPARuntimeException.INNER_EXCEPTION, e);
+ }
+
+ return entityFeedPropertiesBuilder.build();
+ }
+
+ private static List<ArrayList<NavigationPropertySegment>> getExpandList(
+ final Map<EdmNavigationProperty, EdmEntitySet> navPropEntitySetMap) {
+ List<ArrayList<NavigationPropertySegment>> expandList = new ArrayList<ArrayList<NavigationPropertySegment>>();
+ ArrayList<NavigationPropertySegment> navigationPropertySegmentList = new ArrayList<NavigationPropertySegment>();
+ for (Map.Entry<EdmNavigationProperty, EdmEntitySet> entry : navPropEntitySetMap.entrySet()) {
+ final EdmNavigationProperty edmNavigationProperty = entry.getKey();
+ final EdmEntitySet edmEntitySet = entry.getValue();
+ NavigationPropertySegment navigationPropertySegment = new NavigationPropertySegment() {
+
+ @Override
+ public EdmEntitySet getTargetEntitySet() {
+ return edmEntitySet;
+ }
+
+ @Override
+ public EdmNavigationProperty getNavigationProperty() {
+ return edmNavigationProperty;
+ }
+ };
+ navigationPropertySegmentList.add(navigationPropertySegment);
+ }
+ expandList.add(navigationPropertySegmentList);
+ return expandList;
+ }
+
+ private static List<EdmProperty> buildSelectItemList(final List<SelectItem> selectItems, final EdmEntityType entity)
+ throws ODataJPARuntimeException {
+ boolean flag = false;
+ List<EdmProperty> selectPropertyList = new ArrayList<EdmProperty>();
+ try {
+ for (SelectItem selectItem : selectItems) {
+ selectPropertyList.add(selectItem.getProperty());
+ }
+ for (EdmProperty keyProperty : entity.getKeyProperties()) {
+ flag = true;
+ for (SelectItem selectedItem : selectItems) {
+ if (selectedItem.getProperty().equals(keyProperty)) {
+ flag = false;
+ break;
+ }
+ }
+ if (flag == true) {
+ selectPropertyList.add(keyProperty);
+ }
+ }
+
+ } catch (EdmException e) {
+ throw ODataJPARuntimeException.throwException(ODataJPARuntimeException.GENERAL.addContent(e.getMessage()), e);
+ }
+ return selectPropertyList;
+ }
+
+ private static List<EdmNavigationProperty> constructListofNavProperty(
+ final List<ArrayList<NavigationPropertySegment>> expandList) {
+ List<EdmNavigationProperty> navigationPropertyList = new ArrayList<EdmNavigationProperty>();
+ for (ArrayList<NavigationPropertySegment> navpropSegment : expandList) {
+ navigationPropertyList.add(navpropSegment.get(0).getNavigationProperty());
+ }
+ return navigationPropertyList;
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/blob/f8638bb7/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/factory/ODataJPAFactoryImpl.java
----------------------------------------------------------------------
diff --git a/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/factory/ODataJPAFactoryImpl.java b/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/factory/ODataJPAFactoryImpl.java
index d27713f..789afde 100644
--- a/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/factory/ODataJPAFactoryImpl.java
+++ b/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/factory/ODataJPAFactoryImpl.java
@@ -23,21 +23,23 @@ import java.util.Locale;
import org.apache.olingo.odata2.api.edm.provider.EdmProvider;
import org.apache.olingo.odata2.api.processor.ODataSingleProcessor;
import org.apache.olingo.odata2.jpa.processor.api.ODataJPAContext;
+import org.apache.olingo.odata2.jpa.processor.api.ODataJPAResponseBuilder;
import org.apache.olingo.odata2.jpa.processor.api.access.JPAEdmMappingModelAccess;
-import org.apache.olingo.odata2.jpa.processor.api.access.JPAProcessor;
import org.apache.olingo.odata2.jpa.processor.api.access.JPAMethodContext.JPAMethodContextBuilder;
+import org.apache.olingo.odata2.jpa.processor.api.access.JPAProcessor;
import org.apache.olingo.odata2.jpa.processor.api.exception.ODataJPAMessageService;
import org.apache.olingo.odata2.jpa.processor.api.factory.JPAAccessFactory;
import org.apache.olingo.odata2.jpa.processor.api.factory.JPQLBuilderFactory;
import org.apache.olingo.odata2.jpa.processor.api.factory.ODataJPAAccessFactory;
import org.apache.olingo.odata2.jpa.processor.api.factory.ODataJPAFactory;
+import org.apache.olingo.odata2.jpa.processor.api.jpql.JPQLContext.JPQLContextBuilder;
import org.apache.olingo.odata2.jpa.processor.api.jpql.JPQLContextType;
import org.apache.olingo.odata2.jpa.processor.api.jpql.JPQLContextView;
-import org.apache.olingo.odata2.jpa.processor.api.jpql.JPQLContext.JPQLContextBuilder;
import org.apache.olingo.odata2.jpa.processor.api.jpql.JPQLStatement.JPQLStatementBuilder;
import org.apache.olingo.odata2.jpa.processor.api.model.JPAEdmModelView;
import org.apache.olingo.odata2.jpa.processor.core.ODataJPAContextImpl;
import org.apache.olingo.odata2.jpa.processor.core.ODataJPAProcessorDefault;
+import org.apache.olingo.odata2.jpa.processor.core.ODataJPAResponseBuilderDefault;
import org.apache.olingo.odata2.jpa.processor.core.access.data.JPAFunctionContext;
import org.apache.olingo.odata2.jpa.processor.core.access.data.JPAProcessorImpl;
import org.apache.olingo.odata2.jpa.processor.core.access.model.JPAEdmMappingModelService;
@@ -195,6 +197,11 @@ public class ODataJPAFactoryImpl extends ODataJPAFactory {
return ODataJPAMessageServiceDefault.getInstance(locale);
}
+ @Override
+ public ODataJPAResponseBuilder getODataJPAResponseBuilder(final ODataJPAContext oDataJPAContext) {
+ return new ODataJPAResponseBuilderDefault(oDataJPAContext);
+ }
+
}
private static class JPAAccessFactoryImpl implements JPAAccessFactory {