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 2015/11/17 17:53:08 UTC
[3/5] olingo-odata4 git commit: [OLINGO-713] Batch and Deep Insert
Tutorial
http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/a16c9d9c/samples/tutorials/p11_batch/src/main/java/myservice/mynamespace/service/DemoBatchProcessor.java
----------------------------------------------------------------------
diff --git a/samples/tutorials/p11_batch/src/main/java/myservice/mynamespace/service/DemoBatchProcessor.java b/samples/tutorials/p11_batch/src/main/java/myservice/mynamespace/service/DemoBatchProcessor.java
new file mode 100644
index 0000000..49da784
--- /dev/null
+++ b/samples/tutorials/p11_batch/src/main/java/myservice/mynamespace/service/DemoBatchProcessor.java
@@ -0,0 +1,209 @@
+/*
+ * 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 myservice.mynamespace.service;
+
+import java.io.InputStream;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.UUID;
+
+import org.apache.olingo.commons.api.format.ContentType;
+import org.apache.olingo.commons.api.http.HttpHeader;
+import org.apache.olingo.commons.api.http.HttpStatusCode;
+import org.apache.olingo.server.api.OData;
+import org.apache.olingo.server.api.ODataApplicationException;
+import org.apache.olingo.server.api.ODataLibraryException;
+import org.apache.olingo.server.api.ODataRequest;
+import org.apache.olingo.server.api.ODataResponse;
+import org.apache.olingo.server.api.ServiceMetadata;
+import org.apache.olingo.server.api.batch.BatchFacade;
+import org.apache.olingo.server.api.deserializer.batch.BatchOptions;
+import org.apache.olingo.server.api.deserializer.batch.BatchRequestPart;
+import org.apache.olingo.server.api.deserializer.batch.ODataResponsePart;
+import org.apache.olingo.server.api.processor.BatchProcessor;
+
+import myservice.mynamespace.data.Storage;
+
+/**
+ * --abc123
+Content-Type: application/http
+Content-Transfer-Encoding: binary
+
+GET Products HTTP/1.1
+Content-Type: application/json
+
+--abc123
+Content-Type: multipart/mixed;boundary=changeset_abc
+
+--changeset_abc
+Content-Type: application/http
+Content-Transfer-Encoding:binary
+Content-Id: abc1234
+
+POST Products HTTP/1.1
+Content-Type: application/json
+
+{"Name": "Test", "Description": "This is a test product"}
+
+--changeset_abc
+Content-Type: application/http
+Content-Transfer-Encoding:binary
+Content-ID: 2
+
+PATCH $abc1234 HTTP/1.1
+Content-Type: application/json
+Accept: application/json
+
+{"Name": "Bäääääm"}
+
+--changeset_abc--
+
+--abc123
+Content-Type: application/http
+Content-Transfer-Encoding: binary
+
+GET Products HTTP/1.1
+Content-Type: application/json
+
+--abc123--
+
+ *
+ */
+
+public class DemoBatchProcessor implements BatchProcessor {
+
+ private OData odata;
+ private Storage storage;
+
+ public DemoBatchProcessor(final Storage storage) {
+ this.storage = storage;
+ }
+
+ @Override
+ public void init(final OData odata, final ServiceMetadata serviceMetadata) {
+ this.odata = odata;
+ }
+
+ @Override
+ public void processBatch(final BatchFacade facade, final ODataRequest request, final ODataResponse response)
+ throws ODataApplicationException, ODataLibraryException {
+
+ // 1. Extract the boundary
+ final String boundary = facade.extractBoundaryFromContentType(request.getHeader(HttpHeader.CONTENT_TYPE));
+
+ // 2. Prepare the batch options
+ final BatchOptions options = BatchOptions.with().rawBaseUri(request.getRawBaseUri())
+ .rawServiceResolutionUri(request.getRawServiceResolutionUri())
+ .build();
+
+ // 3. Deserialize the batch request
+ final List<BatchRequestPart> requestParts = odata.createFixedFormatDeserializer()
+ .parseBatchRequest(request.getBody(), boundary, options);
+
+ // 4. Execute the batch request parts
+ final List<ODataResponsePart> responseParts = new ArrayList<ODataResponsePart>();
+ for (final BatchRequestPart part : requestParts) {
+ responseParts.add(facade.handleBatchRequest(part));
+ }
+
+ // 5. Serialize the response content
+ final InputStream responseContent = odata.createFixedFormatSerializer().batchResponse(responseParts, boundary);
+
+ // 6. Create a new boundary for the response
+ final String responseBoundary = "batch_" + UUID.randomUUID().toString();
+
+ // 7. Setup response
+ response.setHeader(HttpHeader.CONTENT_TYPE, ContentType.MULTIPART_MIXED + ";boundary=" + responseBoundary);
+ response.setContent(responseContent);
+ response.setStatusCode(HttpStatusCode.ACCEPTED.getStatusCode());
+ }
+
+
+ @Override
+ public ODataResponsePart processChangeSet(final BatchFacade facade, final List<ODataRequest> requests)
+ throws ODataApplicationException, ODataLibraryException {
+ /*
+ * OData Version 4.0 Part 1: Protocol Plus Errata 02
+ * 11.7.4 Responding to a Batch Request
+ *
+ * All operations in a change set represent a single change unit so a service MUST successfully process and
+ * apply all the requests in the change set or else apply none of them. It is up to the service implementation
+ * to define rollback semantics to undo any requests within a change set that may have been applied before
+ * another request in that same change set failed and thereby apply this all-or-nothing requirement.
+ * The service MAY execute the requests within a change set in any order and MAY return the responses to the
+ * individual requests in any order. The service MUST include the Content-ID header in each response with the
+ * same value that the client specified in the corresponding request, so clients can correlate requests
+ * and responses.
+ *
+ * To keep things simple, we dispatch the requests within the change set to the other processor interfaces.
+ */
+ final List<ODataResponse> responses = new ArrayList<ODataResponse>();
+
+ try {
+ storage.beginTransaction();
+
+ for(final ODataRequest request : requests) {
+ // Actual request dispatching to the other processor interfaces.
+ final ODataResponse response = facade.handleODataRequest(request);
+
+ // Determine if an error occurred while executing the request.
+ // Exceptions thrown by the processors get caught and result in a proper OData response.
+ final int statusCode = response.getStatusCode();
+ if(statusCode < 400) {
+ // The request has been executed successfully. Return the response as a part of the change set
+ responses.add(response);
+ } else {
+ // Something went wrong. Undo all previous requests in this Change Set
+ storage.rollbackTranscation();
+
+ /*
+ * In addition the response must be provided as follows:
+ *
+ * OData Version 4.0 Part 1: Protocol Plus Errata 02
+ * 11.7.4 Responding to a Batch Request
+ *
+ * When a request within a change set fails, the change set response is not represented using
+ * the multipart/mixed media type. Instead, a single response, using the application/http media type
+ * and a Content-Transfer-Encoding header with a value of binary, is returned that applies to all requests
+ * in the change set and MUST be formatted according to the Error Handling defined
+ * for the particular response format.
+ *
+ * This can be simply done by passing the response of the failed ODataRequest to a new instance of
+ * ODataResponsePart and setting the second parameter "isChangeSet" to false.
+ */
+ return new ODataResponsePart(response, false);
+ }
+ }
+
+ // Everything went well, so commit the changes.
+ storage.commitTransaction();
+ return new ODataResponsePart(responses, true);
+
+ } catch(ODataApplicationException e) {
+ // See below
+ storage.rollbackTranscation();
+ throw e;
+ } catch(ODataLibraryException e) {
+ // The request is malformed or the processor implementation is not correct.
+ // Throwing an exception will stop the whole batch request not only the change set!
+ storage.rollbackTranscation();
+ throw e;
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/a16c9d9c/samples/tutorials/p11_batch/src/main/java/myservice/mynamespace/service/DemoEdmProvider.java
----------------------------------------------------------------------
diff --git a/samples/tutorials/p11_batch/src/main/java/myservice/mynamespace/service/DemoEdmProvider.java b/samples/tutorials/p11_batch/src/main/java/myservice/mynamespace/service/DemoEdmProvider.java
new file mode 100644
index 0000000..b19d7fd
--- /dev/null
+++ b/samples/tutorials/p11_batch/src/main/java/myservice/mynamespace/service/DemoEdmProvider.java
@@ -0,0 +1,156 @@
+/*
+ * 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 myservice.mynamespace.service;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+
+import org.apache.olingo.commons.api.edm.EdmPrimitiveTypeKind;
+import org.apache.olingo.commons.api.edm.FullQualifiedName;
+import org.apache.olingo.commons.api.edm.provider.CsdlAbstractEdmProvider;
+import org.apache.olingo.commons.api.edm.provider.CsdlEntityContainer;
+import org.apache.olingo.commons.api.edm.provider.CsdlEntityContainerInfo;
+import org.apache.olingo.commons.api.edm.provider.CsdlEntitySet;
+import org.apache.olingo.commons.api.edm.provider.CsdlEntityType;
+import org.apache.olingo.commons.api.edm.provider.CsdlProperty;
+import org.apache.olingo.commons.api.edm.provider.CsdlPropertyRef;
+import org.apache.olingo.commons.api.edm.provider.CsdlSchema;
+
+/*
+ * this class is supposed to declare the metadata of the OData service
+ * it is invoked by the Olingo framework e.g. when the metadata document of the service is invoked
+ * e.g. http://localhost:8080/ExampleService1/ExampleService1.svc/$metadata
+ */
+public class DemoEdmProvider extends CsdlAbstractEdmProvider {
+
+ // Service Namespace
+ public static final String NAMESPACE = "OData.Demo";
+
+ // EDM Container
+ public static final String CONTAINER_NAME = "Container";
+ public static final FullQualifiedName CONTAINER = new FullQualifiedName(NAMESPACE, CONTAINER_NAME);
+
+ // Entity Types Names
+ public static final String ET_PRODUCT_NAME = "Product";
+ public static final FullQualifiedName ET_PRODUCT_FQN = new FullQualifiedName(NAMESPACE, ET_PRODUCT_NAME);
+
+ // Entity Set Names
+ public static final String ES_PRODUCTS_NAME = "Products";
+
+
+ @Override
+ public List<CsdlSchema> getSchemas() {
+
+ // create Schema
+ CsdlSchema schema = new CsdlSchema();
+ schema.setNamespace(NAMESPACE);
+
+ // add EntityTypes
+ List<CsdlEntityType> entityTypes = new ArrayList<CsdlEntityType>();
+ entityTypes.add(getEntityType(ET_PRODUCT_FQN));
+ schema.setEntityTypes(entityTypes);
+
+ // add EntityContainer
+ schema.setEntityContainer(getEntityContainer());
+
+ // finally
+ List<CsdlSchema> schemas = new ArrayList<CsdlSchema>();
+ schemas.add(schema);
+
+ return schemas;
+ }
+
+
+ @Override
+ public CsdlEntityType getEntityType(FullQualifiedName entityTypeName) {
+ // this method is called for one of the EntityTypes that are configured in the Schema
+ if(entityTypeName.equals(ET_PRODUCT_FQN)){
+
+ //create EntityType properties
+ CsdlProperty id = new CsdlProperty().setName("ID")
+ .setType(EdmPrimitiveTypeKind.Int32.getFullQualifiedName());
+ CsdlProperty name = new CsdlProperty().setName("Name")
+ .setType(EdmPrimitiveTypeKind.String.getFullQualifiedName());
+ CsdlProperty description = new CsdlProperty().setName("Description")
+ .setType(EdmPrimitiveTypeKind.String.getFullQualifiedName());
+
+ // create CsdlPropertyRef for Key element
+ CsdlPropertyRef propertyRef = new CsdlPropertyRef();
+ propertyRef.setName("ID");
+
+ // configure EntityType
+ CsdlEntityType entityType = new CsdlEntityType();
+ entityType.setName(ET_PRODUCT_NAME);
+ entityType.setProperties(Arrays.asList(id, name, description));
+ entityType.setKey(Collections.singletonList(propertyRef));
+
+ return entityType;
+ }
+
+ return null;
+
+ }
+
+ @Override
+ public CsdlEntitySet getEntitySet(FullQualifiedName entityContainer, String entitySetName) {
+
+ if(entityContainer.equals(CONTAINER)){
+ if(entitySetName.equals(ES_PRODUCTS_NAME)){
+ CsdlEntitySet entitySet = new CsdlEntitySet();
+ entitySet.setName(ES_PRODUCTS_NAME);
+ entitySet.setType(ET_PRODUCT_FQN);
+
+ return entitySet;
+ }
+ }
+
+ return null;
+
+ }
+
+
+ public CsdlEntityContainer getEntityContainer() {
+ // create EntitySets
+ List<CsdlEntitySet> entitySets = new ArrayList<CsdlEntitySet>();
+ entitySets.add(getEntitySet(CONTAINER, ES_PRODUCTS_NAME));
+
+ // create EntityContainer
+ CsdlEntityContainer entityContainer = new CsdlEntityContainer();
+ entityContainer.setName(CONTAINER_NAME);
+ entityContainer.setEntitySets(entitySets);
+
+ return entityContainer;
+
+ }
+
+ @Override
+ public CsdlEntityContainerInfo getEntityContainerInfo(FullQualifiedName entityContainerName) {
+
+ // This method is invoked when displaying the service document at
+ // e.g. http://localhost:8080/DemoService/DemoService.svc
+ if (entityContainerName == null || entityContainerName.equals(CONTAINER)) {
+ CsdlEntityContainerInfo entityContainerInfo = new CsdlEntityContainerInfo();
+ entityContainerInfo.setContainerName(CONTAINER);
+ return entityContainerInfo;
+ }
+ return null;
+ }
+}
http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/a16c9d9c/samples/tutorials/p11_batch/src/main/java/myservice/mynamespace/service/DemoEntityCollectionProcessor.java
----------------------------------------------------------------------
diff --git a/samples/tutorials/p11_batch/src/main/java/myservice/mynamespace/service/DemoEntityCollectionProcessor.java b/samples/tutorials/p11_batch/src/main/java/myservice/mynamespace/service/DemoEntityCollectionProcessor.java
new file mode 100644
index 0000000..6eb4e21
--- /dev/null
+++ b/samples/tutorials/p11_batch/src/main/java/myservice/mynamespace/service/DemoEntityCollectionProcessor.java
@@ -0,0 +1,93 @@
+/*
+ * 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 myservice.mynamespace.service;
+
+import java.util.List;
+
+import myservice.mynamespace.data.Storage;
+
+import org.apache.olingo.commons.api.data.ContextURL;
+import org.apache.olingo.commons.api.data.EntityCollection;
+import org.apache.olingo.commons.api.edm.EdmEntitySet;
+import org.apache.olingo.commons.api.edm.EdmEntityType;
+import org.apache.olingo.commons.api.format.ContentType;
+import org.apache.olingo.commons.api.http.HttpHeader;
+import org.apache.olingo.commons.api.http.HttpStatusCode;
+import org.apache.olingo.server.api.OData;
+import org.apache.olingo.server.api.ODataApplicationException;
+import org.apache.olingo.server.api.ODataRequest;
+import org.apache.olingo.server.api.ODataResponse;
+import org.apache.olingo.server.api.ServiceMetadata;
+import org.apache.olingo.server.api.processor.EntityCollectionProcessor;
+import org.apache.olingo.server.api.serializer.EntityCollectionSerializerOptions;
+import org.apache.olingo.server.api.serializer.ODataSerializer;
+import org.apache.olingo.server.api.serializer.SerializerException;
+import org.apache.olingo.server.api.serializer.SerializerResult;
+import org.apache.olingo.server.api.uri.UriInfo;
+import org.apache.olingo.server.api.uri.UriResource;
+import org.apache.olingo.server.api.uri.UriResourceEntitySet;
+
+public class DemoEntityCollectionProcessor implements EntityCollectionProcessor {
+
+ private OData odata;
+ private Storage storage;
+ private ServiceMetadata serviceMetadata;
+
+
+ public DemoEntityCollectionProcessor(Storage storage) {
+ this.storage = storage;
+ }
+
+
+ public void init(OData odata, ServiceMetadata serviceMetadata) {
+ this.odata = odata;
+ this.serviceMetadata = serviceMetadata;
+ }
+
+ public void readEntityCollection(ODataRequest request, ODataResponse response, UriInfo uriInfo,
+ ContentType responseFormat) throws ODataApplicationException, SerializerException {
+
+ // 1st retrieve the requested EntitySet from the uriInfo (representation of the parsed URI)
+ List<UriResource> resourcePaths = uriInfo.getUriResourceParts();
+ // in our example, the first segment is the EntitySet
+ UriResourceEntitySet uriResourceEntitySet = (UriResourceEntitySet) resourcePaths.get(0);
+ EdmEntitySet edmEntitySet = uriResourceEntitySet.getEntitySet();
+
+ // 2nd: fetch the data from backend for this requested EntitySetName
+ // it has to be delivered as EntitySet object
+ EntityCollection entitySet = storage.readEntitySetData(edmEntitySet);
+
+ // 3rd: create a serializer based on the requested format (json)
+ ODataSerializer serializer = odata.createSerializer(responseFormat);
+
+ // and serialize the content: transform from the EntitySet object to InputStream
+ EdmEntityType edmEntityType = edmEntitySet.getEntityType();
+ ContextURL contextUrl = ContextURL.with().entitySet(edmEntitySet).build();
+
+ final String id = request.getRawBaseUri() + "/" + edmEntitySet.getName();
+ EntityCollectionSerializerOptions opts =
+ EntityCollectionSerializerOptions.with().id(id).contextURL(contextUrl).build();
+ SerializerResult serializedContent = serializer.entityCollection(serviceMetadata, edmEntityType, entitySet, opts);
+
+ // Finally: configure the response object: set the body, headers and status code
+ response.setContent(serializedContent.getContent());
+ response.setStatusCode(HttpStatusCode.OK.getStatusCode());
+ response.setHeader(HttpHeader.CONTENT_TYPE, responseFormat.toContentTypeString());
+ }
+}
http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/a16c9d9c/samples/tutorials/p11_batch/src/main/java/myservice/mynamespace/service/DemoEntityProcessor.java
----------------------------------------------------------------------
diff --git a/samples/tutorials/p11_batch/src/main/java/myservice/mynamespace/service/DemoEntityProcessor.java b/samples/tutorials/p11_batch/src/main/java/myservice/mynamespace/service/DemoEntityProcessor.java
new file mode 100644
index 0000000..4624ee6
--- /dev/null
+++ b/samples/tutorials/p11_batch/src/main/java/myservice/mynamespace/service/DemoEntityProcessor.java
@@ -0,0 +1,189 @@
+/*
+ * 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 myservice.mynamespace.service;
+
+import java.io.InputStream;
+import java.util.List;
+
+import myservice.mynamespace.data.Storage;
+import myservice.mynamespace.util.Util;
+
+import org.apache.olingo.commons.api.data.ContextURL;
+import org.apache.olingo.commons.api.data.Entity;
+import org.apache.olingo.commons.api.edm.EdmEntitySet;
+import org.apache.olingo.commons.api.edm.EdmEntityType;
+import org.apache.olingo.commons.api.format.ContentType;
+import org.apache.olingo.commons.api.http.HttpHeader;
+import org.apache.olingo.commons.api.http.HttpMethod;
+import org.apache.olingo.commons.api.http.HttpStatusCode;
+import org.apache.olingo.server.api.OData;
+import org.apache.olingo.server.api.ODataApplicationException;
+import org.apache.olingo.server.api.ODataRequest;
+import org.apache.olingo.server.api.ODataResponse;
+import org.apache.olingo.server.api.ServiceMetadata;
+import org.apache.olingo.server.api.deserializer.DeserializerException;
+import org.apache.olingo.server.api.deserializer.DeserializerResult;
+import org.apache.olingo.server.api.deserializer.ODataDeserializer;
+import org.apache.olingo.server.api.processor.EntityProcessor;
+import org.apache.olingo.server.api.serializer.EntitySerializerOptions;
+import org.apache.olingo.server.api.serializer.ODataSerializer;
+import org.apache.olingo.server.api.serializer.SerializerException;
+import org.apache.olingo.server.api.serializer.SerializerResult;
+import org.apache.olingo.server.api.uri.UriInfo;
+import org.apache.olingo.server.api.uri.UriParameter;
+import org.apache.olingo.server.api.uri.UriResource;
+import org.apache.olingo.server.api.uri.UriResourceEntitySet;
+
+public class DemoEntityProcessor implements EntityProcessor {
+
+ private OData odata;
+ private Storage storage;
+ private ServiceMetadata serviceMetadata;
+
+ public DemoEntityProcessor(Storage storage) {
+ this.storage = storage;
+ }
+
+ public void init(OData odata, ServiceMetadata serviceMetadata) {
+ this.odata = odata;
+ this.serviceMetadata = serviceMetadata;
+ }
+
+
+ public void readEntity(ODataRequest request, ODataResponse response, UriInfo uriInfo, ContentType responseFormat)
+ throws ODataApplicationException, SerializerException {
+
+ // 1. retrieve the Entity Type
+ List<UriResource> resourcePaths = uriInfo.getUriResourceParts();
+ // Note: only in our example we can assume that the first segment is the EntitySet
+ UriResourceEntitySet uriResourceEntitySet = (UriResourceEntitySet) resourcePaths.get(0);
+ EdmEntitySet edmEntitySet = uriResourceEntitySet.getEntitySet();
+
+ // 2. retrieve the data from backend
+ List<UriParameter> keyPredicates = uriResourceEntitySet.getKeyPredicates();
+ Entity entity = storage.readEntityData(edmEntitySet, keyPredicates);
+
+ // 3. serialize
+ EdmEntityType entityType = edmEntitySet.getEntityType();
+
+ ContextURL contextUrl = ContextURL.with().entitySet(edmEntitySet).suffix(ContextURL.Suffix.ENTITY).build();
+ // expand and select currently not supported
+ EntitySerializerOptions options = EntitySerializerOptions.with().contextURL(contextUrl).build();
+
+ ODataSerializer serializer = this.odata.createSerializer(responseFormat);
+ SerializerResult result = serializer.entity(serviceMetadata, entityType, entity, options);
+
+ //4. configure the response object
+ response.setContent(result.getContent());
+ response.setStatusCode(HttpStatusCode.OK.getStatusCode());
+ response.setHeader(HttpHeader.CONTENT_TYPE, responseFormat.toContentTypeString());
+ }
+
+ /*
+ * Example request:
+ *
+ * POST URL: http://localhost:8080/DemoService/DemoService.svc/Products
+ * Header: Content-Type: application/json; odata.metadata=minimal
+ * Request body:
+ {
+ "ID":3,
+ "Name":"Ergo Screen",
+ "Description":"17 Optimum Resolution 1024 x 768 @ 85Hz, resolution 1280 x 960"
+ }
+ * */
+ public void createEntity(ODataRequest request, ODataResponse response, UriInfo uriInfo,
+ ContentType requestFormat, ContentType responseFormat)
+ throws ODataApplicationException, DeserializerException, SerializerException {
+
+ // 1. Retrieve the entity type from the URI
+ EdmEntitySet edmEntitySet = Util.getEdmEntitySet(uriInfo);
+ EdmEntityType edmEntityType = edmEntitySet.getEntityType();
+
+ // 2. create the data in backend
+ // 2.1. retrieve the payload from the POST request for the entity to create and deserialize it
+ InputStream requestInputStream = request.getBody();
+ ODataDeserializer deserializer = this.odata.createDeserializer(requestFormat);
+ DeserializerResult result = deserializer.entity(requestInputStream, edmEntityType);
+ Entity requestEntity = result.getEntity();
+ // 2.2 do the creation in backend, which returns the newly created entity
+ Entity createdEntity = storage.createEntityData(edmEntitySet, requestEntity);
+
+ // 3. serialize the response (we have to return the created entity)
+ ContextURL contextUrl = ContextURL.with().entitySet(edmEntitySet).build();
+ EntitySerializerOptions options = EntitySerializerOptions.with().contextURL(contextUrl).build(); // expand and select currently not supported
+
+ ODataSerializer serializer = this.odata.createSerializer(responseFormat);
+ SerializerResult serializedResponse = serializer.entity(serviceMetadata, edmEntityType, createdEntity, options);
+
+ //4. configure the response object
+ final String location = request.getRawBaseUri() + '/'
+ + odata.createUriHelper().buildCanonicalURL(edmEntitySet, createdEntity);
+
+ response.setHeader(HttpHeader.LOCATION, location);
+ response.setContent(serializedResponse.getContent());
+ response.setStatusCode(HttpStatusCode.CREATED.getStatusCode());
+ response.setHeader(HttpHeader.CONTENT_TYPE, responseFormat.toContentTypeString());
+ }
+
+
+ public void updateEntity(ODataRequest request, ODataResponse response, UriInfo uriInfo,
+ ContentType requestFormat, ContentType responseFormat)
+ throws ODataApplicationException, DeserializerException, SerializerException {
+
+ // 1. Retrieve the entity set which belongs to the requested entity
+ List<UriResource> resourcePaths = uriInfo.getUriResourceParts();
+ // Note: only in our example we can assume that the first segment is the EntitySet
+ UriResourceEntitySet uriResourceEntitySet = (UriResourceEntitySet) resourcePaths.get(0);
+ EdmEntitySet edmEntitySet = uriResourceEntitySet.getEntitySet();
+ EdmEntityType edmEntityType = edmEntitySet.getEntityType();
+
+ // 2. update the data in backend
+ // 2.1. retrieve the payload from the PUT request for the entity to be updated
+ InputStream requestInputStream = request.getBody();
+ ODataDeserializer deserializer = this.odata.createDeserializer(requestFormat);
+ DeserializerResult result = deserializer.entity(requestInputStream, edmEntityType);
+ Entity requestEntity = result.getEntity();
+ // 2.2 do the modification in backend
+ List<UriParameter> keyPredicates = uriResourceEntitySet.getKeyPredicates();
+ // Note that this updateEntity()-method is invoked for both PUT or PATCH operations
+ HttpMethod httpMethod = request.getMethod();
+ storage.updateEntityData(edmEntitySet, keyPredicates, requestEntity, httpMethod);
+
+ //3. configure the response object
+ response.setStatusCode(HttpStatusCode.NO_CONTENT.getStatusCode());
+ }
+
+
+ public void deleteEntity(ODataRequest request, ODataResponse response, UriInfo uriInfo)
+ throws ODataApplicationException {
+
+ // 1. Retrieve the entity set which belongs to the requested entity
+ List<UriResource> resourcePaths = uriInfo.getUriResourceParts();
+ // Note: only in our example we can assume that the first segment is the EntitySet
+ UriResourceEntitySet uriResourceEntitySet = (UriResourceEntitySet) resourcePaths.get(0);
+ EdmEntitySet edmEntitySet = uriResourceEntitySet.getEntitySet();
+
+ // 2. delete the data in backend
+ List<UriParameter> keyPredicates = uriResourceEntitySet.getKeyPredicates();
+ storage.deleteEntityData(edmEntitySet, keyPredicates);
+
+ //3. configure the response object
+ response.setStatusCode(HttpStatusCode.NO_CONTENT.getStatusCode());
+ }
+}
http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/a16c9d9c/samples/tutorials/p11_batch/src/main/java/myservice/mynamespace/service/DemoPrimitiveProcessor.java
----------------------------------------------------------------------
diff --git a/samples/tutorials/p11_batch/src/main/java/myservice/mynamespace/service/DemoPrimitiveProcessor.java b/samples/tutorials/p11_batch/src/main/java/myservice/mynamespace/service/DemoPrimitiveProcessor.java
new file mode 100644
index 0000000..8766b8d
--- /dev/null
+++ b/samples/tutorials/p11_batch/src/main/java/myservice/mynamespace/service/DemoPrimitiveProcessor.java
@@ -0,0 +1,146 @@
+/*
+ * 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 myservice.mynamespace.service;
+
+import java.util.List;
+import java.util.Locale;
+
+import myservice.mynamespace.data.Storage;
+
+import org.apache.olingo.commons.api.data.ContextURL;
+import org.apache.olingo.commons.api.data.Entity;
+import org.apache.olingo.commons.api.data.Property;
+import org.apache.olingo.commons.api.edm.EdmEntitySet;
+import org.apache.olingo.commons.api.edm.EdmPrimitiveType;
+import org.apache.olingo.commons.api.edm.EdmProperty;
+import org.apache.olingo.commons.api.format.ContentType;
+import org.apache.olingo.commons.api.http.HttpHeader;
+import org.apache.olingo.commons.api.http.HttpStatusCode;
+import org.apache.olingo.server.api.OData;
+import org.apache.olingo.server.api.ODataApplicationException;
+import org.apache.olingo.server.api.ODataRequest;
+import org.apache.olingo.server.api.ODataResponse;
+import org.apache.olingo.server.api.ServiceMetadata;
+import org.apache.olingo.server.api.deserializer.DeserializerException;
+import org.apache.olingo.server.api.processor.PrimitiveProcessor;
+import org.apache.olingo.server.api.serializer.ODataSerializer;
+import org.apache.olingo.server.api.serializer.PrimitiveSerializerOptions;
+import org.apache.olingo.server.api.serializer.SerializerException;
+import org.apache.olingo.server.api.serializer.SerializerResult;
+import org.apache.olingo.server.api.uri.UriInfo;
+import org.apache.olingo.server.api.uri.UriParameter;
+import org.apache.olingo.server.api.uri.UriResource;
+import org.apache.olingo.server.api.uri.UriResourceEntitySet;
+import org.apache.olingo.server.api.uri.UriResourceProperty;
+
+public class DemoPrimitiveProcessor implements PrimitiveProcessor {
+
+ private OData odata;
+ private Storage storage;
+ private ServiceMetadata serviceMetadata;
+
+ public DemoPrimitiveProcessor(Storage storage) {
+ this.storage = storage;
+ }
+
+ public void init(OData odata, ServiceMetadata serviceMetadata) {
+ this.odata = odata;
+ this.serviceMetadata = serviceMetadata;
+ }
+
+ /*
+ * In our example, the URL would be: http://localhost:8080/DemoService/DemoService.svc/Products(1)/Name
+ * and the response:
+ * {
+ * @odata.context: "$metadata#Products/Name",
+ * value: "Notebook Basic 15"
+ * }
+ * */
+ public void readPrimitive(ODataRequest request, ODataResponse response,
+ UriInfo uriInfo, ContentType responseFormat)
+ throws ODataApplicationException, SerializerException {
+
+ // 1. Retrieve info from URI
+ // 1.1. retrieve the info about the requested entity set
+ List<UriResource> resourceParts = uriInfo.getUriResourceParts();
+ // Note: only in our example we can rely that the first segment is the EntitySet
+ UriResourceEntitySet uriEntityset = (UriResourceEntitySet) resourceParts.get(0);
+ EdmEntitySet edmEntitySet = uriEntityset.getEntitySet();
+ // the key for the entity
+ List<UriParameter> keyPredicates = uriEntityset.getKeyPredicates();
+
+ // 1.2. retrieve the requested (Edm) property
+ UriResourceProperty uriProperty = (UriResourceProperty)resourceParts.get(resourceParts.size() -1); // the last segment is the Property
+ EdmProperty edmProperty = uriProperty.getProperty();
+ String edmPropertyName = edmProperty.getName();
+ // in our example, we know we have only primitive types in our model
+ EdmPrimitiveType edmPropertyType = (EdmPrimitiveType) edmProperty.getType();
+
+
+ // 2. retrieve data from backend
+ // 2.1. retrieve the entity data, for which the property has to be read
+ Entity entity = storage.readEntityData(edmEntitySet, keyPredicates);
+ if (entity == null) { // Bad request
+ throw new ODataApplicationException("Entity not found",
+ HttpStatusCode.NOT_FOUND.getStatusCode(), Locale.ENGLISH);
+ }
+
+ // 2.2. retrieve the property data from the entity
+ Property property = entity.getProperty(edmPropertyName);
+ if (property == null) {
+ throw new ODataApplicationException("Property not found",
+ HttpStatusCode.NOT_FOUND.getStatusCode(), Locale.ENGLISH);
+ }
+
+ // 3. serialize
+ Object value = property.getValue();
+ if (value != null) {
+ // 3.1. configure the serializer
+ ODataSerializer serializer = odata.createSerializer(responseFormat);
+
+ ContextURL contextUrl = ContextURL.with().entitySet(edmEntitySet).navOrPropertyPath(edmPropertyName).build();
+ PrimitiveSerializerOptions options = PrimitiveSerializerOptions.with().contextURL(contextUrl).build();
+ // 3.2. serialize
+ SerializerResult result = serializer.primitive(serviceMetadata, edmPropertyType, property, options);
+
+ //4. configure the response object
+ response.setContent(result.getContent());
+ response.setStatusCode(HttpStatusCode.OK.getStatusCode());
+ response.setHeader(HttpHeader.CONTENT_TYPE, responseFormat.toContentTypeString());
+ }else{
+ // in case there's no value for the property, we can skip the serialization
+ response.setStatusCode(HttpStatusCode.NO_CONTENT.getStatusCode());
+ }
+ }
+
+ /*
+ * These processor methods are not handled in this tutorial
+ *
+ * */
+ public void updatePrimitive(ODataRequest request, ODataResponse response, UriInfo uriInfo,
+ ContentType requestFormat, ContentType responseFormat)
+ throws ODataApplicationException, DeserializerException, SerializerException {
+ throw new ODataApplicationException("Not supported.", HttpStatusCode.NOT_IMPLEMENTED.getStatusCode(), Locale.ROOT);
+ }
+
+ public void deletePrimitive(ODataRequest request, ODataResponse response, UriInfo uriInfo)
+ throws ODataApplicationException {
+ throw new ODataApplicationException("Not supported.", HttpStatusCode.NOT_IMPLEMENTED.getStatusCode(), Locale.ROOT);
+ }
+}
http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/a16c9d9c/samples/tutorials/p11_batch/src/main/java/myservice/mynamespace/util/Util.java
----------------------------------------------------------------------
diff --git a/samples/tutorials/p11_batch/src/main/java/myservice/mynamespace/util/Util.java b/samples/tutorials/p11_batch/src/main/java/myservice/mynamespace/util/Util.java
new file mode 100644
index 0000000..035cc7b
--- /dev/null
+++ b/samples/tutorials/p11_batch/src/main/java/myservice/mynamespace/util/Util.java
@@ -0,0 +1,121 @@
+/*
+ * 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 myservice.mynamespace.util;
+
+import java.util.List;
+import java.util.Locale;
+
+import org.apache.olingo.commons.api.data.Entity;
+import org.apache.olingo.commons.api.data.EntityCollection;
+import org.apache.olingo.commons.api.edm.EdmEntitySet;
+import org.apache.olingo.commons.api.edm.EdmEntityType;
+import org.apache.olingo.commons.api.edm.EdmPrimitiveType;
+import org.apache.olingo.commons.api.edm.EdmPrimitiveTypeException;
+import org.apache.olingo.commons.api.edm.EdmProperty;
+import org.apache.olingo.commons.api.edm.EdmType;
+import org.apache.olingo.commons.api.http.HttpStatusCode;
+import org.apache.olingo.server.api.ODataApplicationException;
+import org.apache.olingo.server.api.uri.UriInfoResource;
+import org.apache.olingo.server.api.uri.UriParameter;
+import org.apache.olingo.server.api.uri.UriResource;
+import org.apache.olingo.server.api.uri.UriResourceEntitySet;
+
+public class Util {
+
+ public static EdmEntitySet getEdmEntitySet(UriInfoResource uriInfo) throws ODataApplicationException {
+
+ List<UriResource> resourcePaths = uriInfo.getUriResourceParts();
+ // To get the entity set we have to interpret all URI segments
+ if (!(resourcePaths.get(0) instanceof UriResourceEntitySet)) {
+ // Here we should interpret the whole URI but in this example we do not support navigation so we throw an
+ // exception
+ throw new ODataApplicationException("Invalid resource type for first segment.", HttpStatusCode.NOT_IMPLEMENTED
+ .getStatusCode(), Locale.ENGLISH);
+ }
+
+ UriResourceEntitySet uriResource = (UriResourceEntitySet) resourcePaths.get(0);
+
+ return uriResource.getEntitySet();
+ }
+
+ public static Entity findEntity(EdmEntityType edmEntityType, EntityCollection entitySet,
+ List<UriParameter> keyParams) throws ODataApplicationException {
+
+ List<Entity> entityList = entitySet.getEntities();
+
+ // loop over all entities in order to find that one that matches all keys in request
+ // e.g. contacts(ContactID=1, CompanyID=1)
+ for (Entity entity: entityList) {
+ boolean foundEntity = entityMatchesAllKeys(edmEntityType, entity, keyParams);
+ if (foundEntity) {
+ return entity;
+ }
+ }
+
+ return null;
+ }
+
+ public static boolean entityMatchesAllKeys(EdmEntityType edmEntityType, Entity entity, List<UriParameter> keyParams)
+ throws ODataApplicationException {
+
+ // loop over all keys
+ for (final UriParameter key : keyParams) {
+ // key
+ String keyName = key.getName();
+ String keyText = key.getText();
+
+ // Edm: we need this info for the comparison below
+ EdmProperty edmKeyProperty = (EdmProperty) edmEntityType.getProperty(keyName);
+ Boolean isNullable = edmKeyProperty.isNullable();
+ Integer maxLength = edmKeyProperty.getMaxLength();
+ Integer precision = edmKeyProperty.getPrecision();
+ Boolean isUnicode = edmKeyProperty.isUnicode();
+ Integer scale = edmKeyProperty.getScale();
+ // get the EdmType in order to compare
+ EdmType edmType = edmKeyProperty.getType();
+ EdmPrimitiveType edmPrimitiveType = (EdmPrimitiveType) edmType;
+
+ // Runtime data: the value of the current entity
+ // don't need to check for null, this is done in olingo library
+ Object valueObject = entity.getProperty(keyName).getValue();
+
+ // now need to compare the valueObject with the keyText String
+ // this is done using the type.valueToString //
+ String valueAsString = null;
+ try {
+ valueAsString = edmPrimitiveType.valueToString(valueObject, isNullable, maxLength, precision, scale, isUnicode);
+ } catch (EdmPrimitiveTypeException e) {
+ throw new ODataApplicationException("Failed to retrieve String value", HttpStatusCode.INTERNAL_SERVER_ERROR
+ .getStatusCode(), Locale.ENGLISH, e);
+ }
+
+ if (valueAsString == null) {
+ return false;
+ }
+
+ boolean matches = valueAsString.equals(keyText);
+ if (!matches) {
+ // if any of the key properties is not found in the entity, we don't need to search further
+ return false;
+ }
+ }
+
+ return true;
+ }
+}
http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/a16c9d9c/samples/tutorials/p11_batch/src/main/java/myservice/mynamespace/web/DemoServlet.java
----------------------------------------------------------------------
diff --git a/samples/tutorials/p11_batch/src/main/java/myservice/mynamespace/web/DemoServlet.java b/samples/tutorials/p11_batch/src/main/java/myservice/mynamespace/web/DemoServlet.java
new file mode 100644
index 0000000..85939c7
--- /dev/null
+++ b/samples/tutorials/p11_batch/src/main/java/myservice/mynamespace/web/DemoServlet.java
@@ -0,0 +1,75 @@
+/*
+ * 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 myservice.mynamespace.web;
+
+import java.io.IOException;
+import java.lang.Override;import java.lang.RuntimeException;import java.util.ArrayList;
+
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import javax.servlet.http.HttpSession;
+
+import myservice.mynamespace.data.Storage;
+import myservice.mynamespace.service.DemoBatchProcessor;
+import myservice.mynamespace.service.DemoEdmProvider;
+import myservice.mynamespace.service.DemoEntityCollectionProcessor;
+import myservice.mynamespace.service.DemoEntityProcessor;
+import myservice.mynamespace.service.DemoPrimitiveProcessor;
+
+import org.apache.olingo.server.api.OData;
+import org.apache.olingo.server.api.ODataHttpHandler;
+import org.apache.olingo.server.api.ServiceMetadata;
+import org.apache.olingo.server.api.edmx.EdmxReference;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class DemoServlet extends HttpServlet {
+
+ private static final long serialVersionUID = 1L;
+ private static final Logger LOG = LoggerFactory.getLogger(DemoServlet.class);
+
+ @Override
+ protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
+ try {
+ HttpSession session = req.getSession(true);
+ Storage storage = (Storage) session.getAttribute(Storage.class.getName());
+ if (storage == null) {
+ storage = new Storage();
+ session.setAttribute(Storage.class.getName(), storage);
+ }
+
+ // create odata handler and configure it with EdmProvider and Processor
+ OData odata = OData.newInstance();
+ ServiceMetadata edm = odata.createServiceMetadata(new DemoEdmProvider(), new ArrayList<EdmxReference>());
+ ODataHttpHandler handler = odata.createHandler(edm);
+ handler.register(new DemoEntityCollectionProcessor(storage));
+ handler.register(new DemoEntityProcessor(storage));
+ handler.register(new DemoPrimitiveProcessor(storage));
+ handler.register(new DemoBatchProcessor(storage));
+
+ // let the handler do the work
+ handler.process(req, resp);
+ } catch (RuntimeException e) {
+ LOG.error("Server Error occurred in ExampleServlet", e);
+ throw new ServletException(e);
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/a16c9d9c/samples/tutorials/p11_batch/src/main/webapp/WEB-INF/web.xml
----------------------------------------------------------------------
diff --git a/samples/tutorials/p11_batch/src/main/webapp/WEB-INF/web.xml b/samples/tutorials/p11_batch/src/main/webapp/WEB-INF/web.xml
new file mode 100644
index 0000000..21de52a
--- /dev/null
+++ b/samples/tutorials/p11_batch/src/main/webapp/WEB-INF/web.xml
@@ -0,0 +1,40 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ 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.
+-->
+<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
+ xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
+ id="WebApp_ID" version="2.5">
+
+ <!-- Register the HttpServlet implementation -->
+ <servlet>
+ <servlet-name>DemoServlet</servlet-name>
+ <servlet-class>myservice.mynamespace.web.DemoServlet</servlet-class>
+ <load-on-startup>1</load-on-startup>
+ </servlet>
+
+ <!--
+ Our OData service can be invoked at
+ http://localhost:8080/DemoService/DemoService.svc
+ -->
+ <servlet-mapping>
+ <servlet-name>DemoServlet</servlet-name>
+ <url-pattern>/DemoService.svc/*</url-pattern>
+ </servlet-mapping>
+</web-app>
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/a16c9d9c/samples/tutorials/p11_batch/src/main/webapp/index.jsp
----------------------------------------------------------------------
diff --git a/samples/tutorials/p11_batch/src/main/webapp/index.jsp b/samples/tutorials/p11_batch/src/main/webapp/index.jsp
new file mode 100644
index 0000000..7ffb4ba
--- /dev/null
+++ b/samples/tutorials/p11_batch/src/main/webapp/index.jsp
@@ -0,0 +1,26 @@
+<!--
+
+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.
+
+-->
+<html>
+<body>
+<h2>Hello World!</h2>
+<a href="DemoService.svc/">OData Olingo V4 Demo Service</a>
+</body>
+</html>
http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/a16c9d9c/samples/tutorials/p12_deep_insert/pom.xml
----------------------------------------------------------------------
diff --git a/samples/tutorials/p12_deep_insert/pom.xml b/samples/tutorials/p12_deep_insert/pom.xml
new file mode 100644
index 0000000..4560876
--- /dev/null
+++ b/samples/tutorials/p12_deep_insert/pom.xml
@@ -0,0 +1,85 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+
+ 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.
+
+-->
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+ <groupId>my.group.id</groupId>
+ <artifactId>DemoService-DeepInsert</artifactId>
+ <packaging>war</packaging>
+ <version>4.0.0</version>
+
+ <name>${project.artifactId}-Webapp</name>
+
+ <build>
+ <finalName>DemoService</finalName>
+ </build>
+
+ <properties>
+ <javax.version>2.5</javax.version>
+ <odata.version>4.1.0-SNAPSHOT</odata.version>
+ <slf4j.version>1.7.7</slf4j.version>
+ </properties>
+
+ <dependencies>
+ <dependency>
+ <groupId>javax.servlet</groupId>
+ <artifactId>servlet-api</artifactId>
+ <version>${javax.version}</version>
+ <scope>provided</scope>
+ </dependency>
+
+ <dependency>
+ <groupId>org.apache.olingo</groupId>
+ <artifactId>odata-server-api</artifactId>
+ <version>${odata.version}</version>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.olingo</groupId>
+ <artifactId>odata-server-core</artifactId>
+ <version>${odata.version}</version>
+ </dependency>
+
+ <dependency>
+ <groupId>org.apache.olingo</groupId>
+ <artifactId>odata-commons-api</artifactId>
+ <version>${odata.version}</version>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.olingo</groupId>
+ <artifactId>odata-commons-core</artifactId>
+ <version>${odata.version}</version>
+ </dependency>
+
+ <dependency>
+ <groupId>org.slf4j</groupId>
+ <artifactId>slf4j-simple</artifactId>
+ <version>${slf4j.version}</version>
+ <scope>runtime</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.slf4j</groupId>
+ <artifactId>slf4j-api</artifactId>
+ <version>1.7.11</version>
+ <scope>compile</scope>
+ </dependency>
+ </dependencies>
+</project>
http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/a16c9d9c/samples/tutorials/p12_deep_insert/src/main/java/myservice/mynamespace/data/Storage.java
----------------------------------------------------------------------
diff --git a/samples/tutorials/p12_deep_insert/src/main/java/myservice/mynamespace/data/Storage.java b/samples/tutorials/p12_deep_insert/src/main/java/myservice/mynamespace/data/Storage.java
new file mode 100644
index 0000000..f9f249c
--- /dev/null
+++ b/samples/tutorials/p12_deep_insert/src/main/java/myservice/mynamespace/data/Storage.java
@@ -0,0 +1,534 @@
+/*
+ * 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 myservice.mynamespace.data;
+
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Locale;
+
+import org.apache.olingo.commons.api.Constants;
+import org.apache.olingo.commons.api.data.Entity;
+import org.apache.olingo.commons.api.data.EntityCollection;
+import org.apache.olingo.commons.api.data.Link;
+import org.apache.olingo.commons.api.data.Property;
+import org.apache.olingo.commons.api.data.ValueType;
+import org.apache.olingo.commons.api.edm.Edm;
+import org.apache.olingo.commons.api.edm.EdmEntitySet;
+import org.apache.olingo.commons.api.edm.EdmEntityType;
+import org.apache.olingo.commons.api.edm.EdmKeyPropertyRef;
+import org.apache.olingo.commons.api.edm.EdmNavigationProperty;
+import org.apache.olingo.commons.api.ex.ODataRuntimeException;
+import org.apache.olingo.commons.api.http.HttpMethod;
+import org.apache.olingo.commons.api.http.HttpStatusCode;
+import org.apache.olingo.server.api.OData;
+import org.apache.olingo.server.api.ODataApplicationException;
+import org.apache.olingo.server.api.deserializer.DeserializerException;
+import org.apache.olingo.server.api.uri.UriParameter;
+import org.apache.olingo.server.api.uri.UriResourceEntitySet;
+import org.apache.olingo.server.api.uri.UriResourceNavigation;
+
+import myservice.mynamespace.service.DemoEdmProvider;
+import myservice.mynamespace.util.Util;
+
+public class Storage {
+
+ private OData odata;
+ private Edm edm;
+
+ final private TransactionalEntityManager manager;
+
+ public Storage(final OData odata, final Edm edm) {
+
+ this.odata = odata;
+ this.edm = edm;
+ this.manager = new TransactionalEntityManager(edm);
+
+ initProductSampleData();
+ initCategorySampleData();
+ linkProductsAndCategories();
+ }
+
+ /* PUBLIC FACADE */
+
+ public void beginTransaction() throws ODataApplicationException {
+ manager.beginTransaction();
+ }
+
+ public void rollbackTransaction() throws ODataApplicationException {
+ manager.rollbackTransaction();
+ }
+
+ public void commitTransaction() throws ODataApplicationException {
+ manager.commitTransaction();
+ }
+
+ public EntityCollection readEntitySetData(EdmEntitySet edmEntitySet) throws ODataApplicationException {
+
+ if (edmEntitySet.getName().equals(DemoEdmProvider.ES_PRODUCTS_NAME)) {
+ return getEntityCollection(manager.getEntityCollection(DemoEdmProvider.ES_PRODUCTS_NAME));
+ } else if(edmEntitySet.getName().equals(DemoEdmProvider.ES_CATEGORIES_NAME)) {
+ return getEntityCollection(manager.getEntityCollection(DemoEdmProvider.ES_CATEGORIES_NAME));
+ }
+
+ return null;
+ }
+
+ public Entity readEntityData(EdmEntitySet edmEntitySet, List<UriParameter> keyParams)
+ throws ODataApplicationException {
+
+ EdmEntityType edmEntityType = edmEntitySet.getEntityType();
+
+ if (edmEntitySet.getName().equals(DemoEdmProvider.ES_PRODUCTS_NAME)) {
+ return getEntity(edmEntityType, keyParams, manager.getEntityCollection(DemoEdmProvider.ES_PRODUCTS_NAME));
+ } else if(edmEntitySet.getName().equals(DemoEdmProvider.ES_CATEGORIES_NAME)) {
+ return getEntity(edmEntityType, keyParams, manager.getEntityCollection(DemoEdmProvider.ES_CATEGORIES_NAME));
+ }
+
+ return null;
+ }
+
+ public Entity createEntityData(EdmEntitySet edmEntitySet, Entity entityToCreate, String rawServiceUri)
+ throws ODataApplicationException {
+
+ EdmEntityType edmEntityType = edmEntitySet.getEntityType();
+
+ if (edmEntitySet.getName().equals(DemoEdmProvider.ES_PRODUCTS_NAME)) {
+ return createEntity(edmEntitySet, edmEntityType, entityToCreate,
+ manager.getEntityCollection(DemoEdmProvider.ES_PRODUCTS_NAME), rawServiceUri);
+ } else if(edmEntitySet.getName().equals(DemoEdmProvider.ES_CATEGORIES_NAME)) {
+ return createEntity(edmEntitySet, edmEntityType, entityToCreate,
+ manager.getEntityCollection(DemoEdmProvider.ES_CATEGORIES_NAME), rawServiceUri);
+ }
+
+ return null;
+ }
+
+ /**
+ * This method is invoked for PATCH or PUT requests
+ * */
+ public void updateEntityData(EdmEntitySet edmEntitySet, List<UriParameter> keyParams, Entity updateEntity,
+ HttpMethod httpMethod) throws ODataApplicationException {
+
+ EdmEntityType edmEntityType = edmEntitySet.getEntityType();
+
+ if (edmEntitySet.getName().equals(DemoEdmProvider.ES_PRODUCTS_NAME)) {
+ updateEntity(edmEntityType, keyParams, updateEntity, httpMethod,
+ manager.getEntityCollection(DemoEdmProvider.ES_PRODUCTS_NAME));
+ } else if(edmEntitySet.getName().equals(DemoEdmProvider.ES_CATEGORIES_NAME)) {
+ updateEntity(edmEntityType, keyParams, updateEntity, httpMethod,
+ manager.getEntityCollection(DemoEdmProvider.ES_CATEGORIES_NAME));
+ }
+ }
+
+ public void deleteEntityData(EdmEntitySet edmEntitySet, List<UriParameter> keyParams)
+ throws ODataApplicationException {
+
+ EdmEntityType edmEntityType = edmEntitySet.getEntityType();
+
+ if (edmEntitySet.getName().equals(DemoEdmProvider.ES_PRODUCTS_NAME)) {
+ deleteEntity(edmEntityType, keyParams, manager.getEntityCollection(DemoEdmProvider.ES_PRODUCTS_NAME));
+ } else if(edmEntitySet.getName().equals(DemoEdmProvider.ES_CATEGORIES_NAME)) {
+ deleteEntity(edmEntityType, keyParams, manager.getEntityCollection(DemoEdmProvider.ES_CATEGORIES_NAME));
+ }
+ }
+
+ // Navigation
+ public Entity getRelatedEntity(Entity entity, UriResourceNavigation navigationResource)
+ throws ODataApplicationException {
+
+ final EdmNavigationProperty edmNavigationProperty = navigationResource.getProperty();
+
+ if(edmNavigationProperty.isCollection()) {
+ return Util.findEntity(edmNavigationProperty.getType(), getRelatedEntityCollection(entity, navigationResource),
+ navigationResource.getKeyPredicates());
+ } else {
+ final Link link = entity.getNavigationLink(edmNavigationProperty.getName());
+ return link == null ? null : link.getInlineEntity();
+ }
+ }
+
+ public EntityCollection getRelatedEntityCollection(Entity entity, UriResourceNavigation navigationResource) {
+ final Link link = entity.getNavigationLink(navigationResource.getProperty().getName());
+ return link == null ? new EntityCollection() : link.getInlineEntitySet();
+ }
+
+ /* INTERNAL */
+
+ private EntityCollection getEntityCollection(final List<Entity> entityList) {
+
+ EntityCollection retEntitySet = new EntityCollection();
+ retEntitySet.getEntities().addAll(entityList);
+
+ return retEntitySet;
+ }
+
+ private Entity getEntity(EdmEntityType edmEntityType, List<UriParameter> keyParams, List<Entity> entityList)
+ throws ODataApplicationException {
+
+ // the list of entities at runtime
+ EntityCollection entitySet = getEntityCollection(entityList);
+
+ /* generic approach to find the requested entity */
+ Entity requestedEntity = Util.findEntity(edmEntityType, entitySet, keyParams);
+
+ if (requestedEntity == null) {
+ // this variable is null if our data doesn't contain an entity for the requested key
+ // Throw suitable exception
+ throw new ODataApplicationException("Entity for requested key doesn't exist",
+ HttpStatusCode.NOT_FOUND.getStatusCode(), Locale.ENGLISH);
+ }
+
+ return requestedEntity;
+ }
+
+ private Entity createEntity(EdmEntitySet edmEntitySet, EdmEntityType edmEntityType, Entity entity,
+ List<Entity> entityList, final String rawServiceUri) throws ODataApplicationException {
+
+ // 1.) Create the entity
+ final Entity newEntity = new Entity();
+ newEntity.setType(entity.getType());
+
+ // Create the new key of the entity
+ int newId = 1;
+ while (entityIdExists(newId, entityList)) {
+ newId++;
+ }
+
+ // Add all provided properties
+ newEntity.getProperties().addAll(entity.getProperties());
+
+ // Add the key property
+ newEntity.getProperties().add(new Property(null, "ID", ValueType.PRIMITIVE, newId));
+ newEntity.setId(createId(newEntity, "ID"));
+
+ // 2.1.) Apply binding links
+ for(final Link link : entity.getNavigationBindings()) {
+ final EdmNavigationProperty edmNavigationProperty = edmEntityType.getNavigationProperty(link.getTitle());
+ final EdmEntitySet targetEntitySet = (EdmEntitySet) edmEntitySet.getRelatedBindingTarget(link.getTitle());
+
+ if(edmNavigationProperty.isCollection() && link.getBindingLinks() != null) {
+ for(final String bindingLink : link.getBindingLinks()) {
+ final Entity relatedEntity = readEntityByBindingLink(bindingLink, targetEntitySet, rawServiceUri);
+ createLink(edmNavigationProperty, newEntity, relatedEntity);
+ }
+ } else if(!edmNavigationProperty.isCollection() && link.getBindingLink() != null) {
+ final Entity relatedEntity = readEntityByBindingLink(link.getBindingLink(), targetEntitySet, rawServiceUri);
+ createLink(edmNavigationProperty, newEntity, relatedEntity);
+ }
+ }
+
+ // 2.2.) Create nested entities
+ for(final Link link : entity.getNavigationLinks()) {
+ final EdmNavigationProperty edmNavigationProperty = edmEntityType.getNavigationProperty(link.getTitle());
+ final EdmEntitySet targetEntitySet = (EdmEntitySet) edmEntitySet.getRelatedBindingTarget(link.getTitle());
+
+ if(edmNavigationProperty.isCollection() && link.getInlineEntitySet() != null) {
+ for(final Entity nestedEntity : link.getInlineEntitySet().getEntities()) {
+ final Entity newNestedEntity = createEntityData(targetEntitySet, nestedEntity, rawServiceUri);
+ createLink(edmNavigationProperty, newEntity, newNestedEntity);
+ }
+ } else if(!edmNavigationProperty.isCollection() && link.getInlineEntity() != null){
+ final Entity newNestedEntity = createEntityData(targetEntitySet, link.getInlineEntity(), rawServiceUri);
+ createLink(edmNavigationProperty, newEntity, newNestedEntity);
+ }
+ }
+
+ entityList.add(newEntity);
+
+ return newEntity;
+ }
+
+ private Entity readEntityByBindingLink(final String entityId, final EdmEntitySet edmEntitySet,
+ final String rawServiceUri) throws ODataApplicationException {
+
+ UriResourceEntitySet entitySetResource = null;
+ try {
+ entitySetResource = odata.createUriHelper().parseEntityId(edm, entityId, rawServiceUri);
+
+ if(!entitySetResource.getEntitySet().getName().equals(edmEntitySet.getName())) {
+ throw new ODataApplicationException("Execpted an entity-id for entity set " + edmEntitySet.getName()
+ + " but found id for entity set " + entitySetResource.getEntitySet().getName(),
+ HttpStatusCode.BAD_REQUEST.getStatusCode(), Locale.ENGLISH);
+ }
+ } catch (DeserializerException e) {
+ throw new ODataApplicationException(entityId + " is not a valid entity-Id",
+ HttpStatusCode.BAD_REQUEST.getStatusCode(), Locale.ENGLISH);
+ }
+
+ return readEntityData(entitySetResource.getEntitySet(), entitySetResource.getKeyPredicates());
+ }
+
+ private boolean entityIdExists(int id, List<Entity> entityList) {
+
+ for (Entity entity : entityList) {
+ Integer existingID = (Integer) entity.getProperty("ID").getValue();
+ if (existingID.intValue() == id) {
+ return true;
+ }
+ }
+
+ return false;
+ }
+
+ private void updateEntity(EdmEntityType edmEntityType, List<UriParameter> keyParams, Entity updateEntity,
+ HttpMethod httpMethod, List<Entity> entityList) throws ODataApplicationException {
+
+ Entity entity = getEntity(edmEntityType, keyParams, entityList);
+ if (entity == null) {
+ throw new ODataApplicationException("Entity not found", HttpStatusCode.NOT_FOUND.getStatusCode(), Locale.ENGLISH);
+ }
+
+ // loop over all properties and replace the values with the values of the given payload
+ // Note: ignoring ComplexType, as we don't have it in our odata model
+ List<Property> existingProperties = entity.getProperties();
+ for (Property existingProp : existingProperties) {
+ String propName = existingProp.getName();
+
+ // ignore the key properties, they aren't updateable
+ if (isKey(edmEntityType, propName)) {
+ continue;
+ }
+
+ Property updateProperty = updateEntity.getProperty(propName);
+ // the request payload might not consider ALL properties, so it can be null
+ if (updateProperty == null) {
+ // if a property has NOT been added to the request payload
+ // depending on the HttpMethod, our behavior is different
+ if (httpMethod.equals(HttpMethod.PATCH)) {
+ // as of the OData spec, in case of PATCH, the existing property is not touched
+ continue; // do nothing
+ } else if (httpMethod.equals(HttpMethod.PUT)) {
+ // as of the OData spec, in case of PUT, the existing property is set to null (or to default value)
+ existingProp.setValue(existingProp.getValueType(), null);
+ continue;
+ }
+ }
+
+ // change the value of the properties
+ existingProp.setValue(existingProp.getValueType(), updateProperty.getValue());
+ }
+ }
+
+ private void deleteEntity(EdmEntityType edmEntityType, List<UriParameter> keyParams, List<Entity> entityList)
+ throws ODataApplicationException {
+
+ Entity entity = getEntity(edmEntityType, keyParams, entityList);
+ if (entity == null) {
+ throw new ODataApplicationException("Entity not found", HttpStatusCode.NOT_FOUND.getStatusCode(), Locale.ENGLISH);
+ }
+
+ entityList.remove(entity);
+ }
+
+ /* HELPER */
+
+ private boolean isKey(EdmEntityType edmEntityType, String propertyName) {
+
+ List<EdmKeyPropertyRef> keyPropertyRefs = edmEntityType.getKeyPropertyRefs();
+ for (EdmKeyPropertyRef propRef : keyPropertyRefs) {
+ String keyPropertyName = propRef.getName();
+ if (keyPropertyName.equals(propertyName)) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ private void initProductSampleData() {
+
+ final List<Entity> productList = manager.getEntityCollection(DemoEdmProvider.ES_PRODUCTS_NAME);
+ Entity entity = new Entity();
+
+ entity.addProperty(new Property(null, "ID", ValueType.PRIMITIVE, 0));
+ entity.addProperty(new Property(null, "Name", ValueType.PRIMITIVE, "Notebook Basic 15"));
+ entity.addProperty(new Property(null, "Description", ValueType.PRIMITIVE,
+ "Notebook Basic, 1.7GHz - 15 XGA - 1024MB DDR2 SDRAM - 40GB"));
+ entity.setType(DemoEdmProvider.ET_PRODUCT_FQN.getFullQualifiedNameAsString());
+ entity.setId(createId(entity, "ID"));
+ productList.add(entity);
+
+ entity = new Entity();
+ entity.addProperty(new Property(null, "ID", ValueType.PRIMITIVE, 1));
+ entity.addProperty(new Property(null, "Name", ValueType.PRIMITIVE, "Notebook Professional 17"));
+ entity.addProperty(new Property(null, "Description", ValueType.PRIMITIVE,
+ "Notebook Professional, 2.8GHz - 15 XGA - 8GB DDR3 RAM - 500GB"));
+ entity.setType(DemoEdmProvider.ET_PRODUCT_FQN.getFullQualifiedNameAsString());
+ entity.setId(createId(entity, "ID"));
+ productList.add(entity);
+
+ entity = new Entity();
+ entity.addProperty(new Property(null, "ID", ValueType.PRIMITIVE, 2));
+ entity.addProperty(new Property(null, "Name", ValueType.PRIMITIVE, "1UMTS PDA"));
+ entity.addProperty(new Property(null, "Description", ValueType.PRIMITIVE,
+ "Ultrafast 3G UMTS/HSDPA Pocket PC, supports GSM network"));
+ entity.setType(DemoEdmProvider.ET_PRODUCT_FQN.getFullQualifiedNameAsString());
+ entity.setId(createId(entity, "ID"));
+ productList.add(entity);
+
+ entity = new Entity();
+ entity.addProperty(new Property(null, "ID", ValueType.PRIMITIVE, 3));
+ entity.addProperty(new Property(null, "Name", ValueType.PRIMITIVE, "Comfort Easy"));
+ entity.addProperty(new Property(null, "Description", ValueType.PRIMITIVE,
+ "32 GB Digital Assitant with high-resolution color screen"));
+ entity.setType(DemoEdmProvider.ET_PRODUCT_FQN.getFullQualifiedNameAsString());
+ entity.setId(createId(entity, "ID"));
+ productList.add(entity);
+
+ entity = new Entity();
+ entity.addProperty(new Property(null, "ID", ValueType.PRIMITIVE, 4));
+ entity.addProperty(new Property(null, "Name", ValueType.PRIMITIVE, "Ergo Screen"));
+ entity.addProperty(new Property(null, "Description", ValueType.PRIMITIVE,
+ "19 Optimum Resolution 1024 x 768 @ 85Hz, resolution 1280 x 960"));
+ entity.setType(DemoEdmProvider.ET_PRODUCT_FQN.getFullQualifiedNameAsString());
+ entity.setId(createId(entity, "ID"));
+ productList.add(entity);
+
+ entity = new Entity();
+ entity.addProperty(new Property(null, "ID", ValueType.PRIMITIVE, 5));
+ entity.addProperty(new Property(null, "Name", ValueType.PRIMITIVE, "Flat Basic"));
+ entity.addProperty(new Property(null, "Description", ValueType.PRIMITIVE,
+ "Optimum Hi-Resolution max. 1600 x 1200 @ 85Hz, Dot Pitch: 0.24mm"));
+ entity.setType(DemoEdmProvider.ET_PRODUCT_FQN.getFullQualifiedNameAsString());
+ entity.setId(createId(entity, "ID"));
+ productList.add(entity);
+ }
+
+ private void initCategorySampleData() {
+ final List<Entity> categoryList = manager.getEntityCollection(DemoEdmProvider.ES_CATEGORIES_NAME);
+
+ Entity entity = new Entity();
+
+ entity.addProperty(new Property(null, "ID", ValueType.PRIMITIVE, 0));
+ entity.addProperty(new Property(null, "Name", ValueType.PRIMITIVE, "Notebooks"));
+ entity.setType(DemoEdmProvider.ET_CATEGORY_FQN.getFullQualifiedNameAsString());
+ entity.setId(createId(entity, "ID"));
+ categoryList.add(entity);
+
+ entity = new Entity();
+ entity.addProperty(new Property(null, "ID", ValueType.PRIMITIVE, 1));
+ entity.addProperty(new Property(null, "Name", ValueType.PRIMITIVE, "Organizers"));
+ entity.setType(DemoEdmProvider.ET_CATEGORY_FQN.getFullQualifiedNameAsString());
+ entity.setId(createId(entity, "ID"));
+ categoryList.add(entity);
+
+ entity = new Entity();
+ entity.addProperty(new Property(null, "ID", ValueType.PRIMITIVE, 2));
+ entity.addProperty(new Property(null, "Name", ValueType.PRIMITIVE, "Monitors"));
+ entity.setType(DemoEdmProvider.ET_CATEGORY_FQN.getFullQualifiedNameAsString());
+ entity.setId(createId(entity, "ID"));
+ categoryList.add(entity);
+ }
+
+ private void linkProductsAndCategories() {
+ final List<Entity> productList = manager.getEntityCollection(DemoEdmProvider.ES_PRODUCTS_NAME);
+ final List<Entity> categoryList = manager.getEntityCollection(DemoEdmProvider.ES_CATEGORIES_NAME);
+
+ setLink(productList.get(0), "Category", categoryList.get(0));
+ setLink(productList.get(1), "Category", categoryList.get(0));
+ setLink(productList.get(2), "Category", categoryList.get(1));
+ setLink(productList.get(3), "Category", categoryList.get(1));
+ setLink(productList.get(4), "Category", categoryList.get(2));
+ setLink(productList.get(5), "Category", categoryList.get(2));
+
+ setLinks(categoryList.get(0), "Products", productList.subList(0, 2).toArray(new Entity[0]));
+ setLinks(categoryList.get(1), "Products", productList.subList(2, 4).toArray(new Entity[0]));
+ setLinks(categoryList.get(2), "Products", productList.subList(4, 6).toArray(new Entity[0]));
+ }
+
+ private URI createId(Entity entity, String idPropertyName) {
+ return createId(entity, idPropertyName, null);
+ }
+
+ private URI createId(Entity entity, String idPropertyName, String navigationName) {
+ try {
+ StringBuilder sb = new StringBuilder(getEntitySetName(entity)).append("(");
+ final Property property = entity.getProperty(idPropertyName);
+ sb.append(property.asPrimitive()).append(")");
+ if(navigationName != null) {
+ sb.append("/").append(navigationName);
+ }
+ return new URI(sb.toString());
+ } catch (URISyntaxException e) {
+ throw new ODataRuntimeException("Unable to create (Atom) id for entity: " + entity, e);
+ }
+ }
+
+ private String getEntitySetName(Entity entity) {
+ if(DemoEdmProvider.ET_CATEGORY_FQN.getFullQualifiedNameAsString().equals(entity.getType())) {
+ return DemoEdmProvider.ES_CATEGORIES_NAME;
+ } else if(DemoEdmProvider.ET_PRODUCT_FQN.getFullQualifiedNameAsString().equals(entity.getType())) {
+ return DemoEdmProvider.ES_PRODUCTS_NAME;
+ }
+ return entity.getType();
+ }
+
+ private void createLink(final EdmNavigationProperty navigationProperty, final Entity srcEntity,
+ final Entity destEntity) {
+ setLink(navigationProperty, srcEntity, destEntity);
+
+ final EdmNavigationProperty partnerNavigationProperty = navigationProperty.getPartner();
+ if (partnerNavigationProperty != null) {
+ setLink(partnerNavigationProperty, destEntity, srcEntity);
+ }
+ }
+
+ private void setLink(final EdmNavigationProperty navigationProperty, final Entity srcEntity,
+ final Entity targetEntity) {
+ if (navigationProperty.isCollection()) {
+ setLinks(srcEntity, navigationProperty.getName(), targetEntity);
+ } else {
+ setLink(srcEntity, navigationProperty.getName(), targetEntity);
+ }
+ }
+
+ private void setLink(final Entity entity, final String navigationPropertyName, final Entity target) {
+ Link link = entity.getNavigationLink(navigationPropertyName);
+ if (link == null) {
+ link = new Link();
+ link.setRel(Constants.NS_NAVIGATION_LINK_REL + navigationPropertyName);
+ link.setType(Constants.ENTITY_NAVIGATION_LINK_TYPE);
+ link.setTitle(navigationPropertyName);
+ link.setHref(target.getId().toASCIIString());
+
+ entity.getNavigationLinks().add(link);
+ }
+ link.setInlineEntity(target);
+ }
+
+ private void setLinks(final Entity entity, final String navigationPropertyName, final Entity... targets) {
+ Link link = entity.getNavigationLink(navigationPropertyName);
+ if (link == null) {
+ link = new Link();
+ link.setRel(Constants.NS_NAVIGATION_LINK_REL + navigationPropertyName);
+ link.setType(Constants.ENTITY_SET_NAVIGATION_LINK_TYPE);
+ link.setTitle(navigationPropertyName);
+ link.setHref(entity.getId().toASCIIString() + "/" + navigationPropertyName);
+
+ EntityCollection target = new EntityCollection();
+ target.getEntities().addAll(Arrays.asList(targets));
+ link.setInlineEntitySet(target);
+
+ entity.getNavigationLinks().add(link);
+ } else {
+ link.getInlineEntitySet().getEntities().addAll(Arrays.asList(targets));
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/a16c9d9c/samples/tutorials/p12_deep_insert/src/main/java/myservice/mynamespace/data/TransactionalEntityManager.java
----------------------------------------------------------------------
diff --git a/samples/tutorials/p12_deep_insert/src/main/java/myservice/mynamespace/data/TransactionalEntityManager.java b/samples/tutorials/p12_deep_insert/src/main/java/myservice/mynamespace/data/TransactionalEntityManager.java
new file mode 100644
index 0000000..e4b28cb
--- /dev/null
+++ b/samples/tutorials/p12_deep_insert/src/main/java/myservice/mynamespace/data/TransactionalEntityManager.java
@@ -0,0 +1,192 @@
+/*
+ * 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 myservice.mynamespace.data;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.IdentityHashMap;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+
+import org.apache.olingo.commons.api.data.Entity;
+import org.apache.olingo.commons.api.data.EntityCollection;
+import org.apache.olingo.commons.api.data.Link;
+import org.apache.olingo.commons.api.edm.Edm;
+import org.apache.olingo.commons.api.edm.EdmEntitySet;
+import org.apache.olingo.commons.api.http.HttpStatusCode;
+import org.apache.olingo.server.api.ODataApplicationException;
+
+public class TransactionalEntityManager {
+
+ private Map<String, List<Entity>> entities = new HashMap<String, List<Entity>>();
+ private Map<String, List<Entity>> backupEntities = new HashMap<String, List<Entity>>();
+ private Map<String, IdentityHashMap<Entity, Entity>> copyMap = new HashMap<String, IdentityHashMap<Entity, Entity>>();
+ private boolean isInTransaction = false;
+ private Edm edm;
+
+ public TransactionalEntityManager(final Edm edm) {
+ this.edm = edm;
+ }
+
+ public List<Entity> getEntityCollection(final String entitySetName) {
+ if(!entities.containsKey(entitySetName)) {
+ entities.put(entitySetName, new ArrayList<Entity>());
+ }
+
+ return entities.get(entitySetName);
+ }
+
+ public void beginTransaction() throws ODataApplicationException {
+ if(!isInTransaction) {
+ isInTransaction = true;
+ copyCurrentState();
+ } else {
+ throw new ODataApplicationException("Transaction already in progress",
+ HttpStatusCode.INTERNAL_SERVER_ERROR.getStatusCode(), Locale.ENGLISH);
+ }
+ }
+
+ public void rollbackTransaction() throws ODataApplicationException {
+ if(isInTransaction) {
+ entities = backupEntities;
+ backupEntities = new HashMap<String, List<Entity>>();
+ isInTransaction = false;
+ } else {
+ throw new ODataApplicationException("No transaction in progress",
+ HttpStatusCode.INTERNAL_SERVER_ERROR.getStatusCode(), Locale.ENGLISH);
+ }
+ }
+
+ public void commitTransaction() throws ODataApplicationException {
+ if(isInTransaction) {
+ backupEntities.clear();
+ isInTransaction = false;
+ } else {
+ throw new ODataApplicationException("No transaction in progress",
+ HttpStatusCode.INTERNAL_SERVER_ERROR.getStatusCode(), Locale.ENGLISH);
+ }
+ }
+
+ private void copyCurrentState() {
+ copyMap.clear();
+ backupEntities.clear();
+
+ for(final String entitySetName : entities.keySet()) {
+ final List<Entity> entityList = entities.get(entitySetName);
+ backupEntities.put(entitySetName, new ArrayList<Entity>());
+ final List<Entity> backupEntityList = backupEntities.get(entitySetName);
+
+ for(final Entity entity : entityList) {
+ final EdmEntitySet entitySet = edm.getEntityContainer().getEntitySet(entitySetName);
+ backupEntityList.add(copyEntityRecursively(entitySet, entity));
+ }
+ }
+ }
+
+ private Entity copyEntityRecursively(final EdmEntitySet edmEntitySet, final Entity entity) {
+ // Check if entity is already copied
+ if(containsEntityInCopyMap(edmEntitySet.getName(), entity)) {
+ return getEntityFromCopyMap(edmEntitySet.getName(), entity);
+ } else {
+ final Entity newEntity = copyEntity(entity);
+ addEntityToCopyMap(edmEntitySet.getName(), entity, newEntity);
+
+ // Create nested entities recursively
+ for(final Link link : entity.getNavigationLinks()) {
+ newEntity.getNavigationLinks().add(copyLink(edmEntitySet, link));
+ }
+
+ return newEntity;
+ }
+ }
+
+ private Link copyLink(final EdmEntitySet edmEntitySet, final Link link) {
+ final Link newLink = new Link();
+ newLink.setBindingLink(link.getBindingLink());
+ newLink.setBindingLinks(new ArrayList<String>(link.getBindingLinks()));
+ newLink.setHref(link.getHref());
+ newLink.setMediaETag(link.getMediaETag());
+ newLink.setRel(link.getRel());
+ newLink.setTitle(link.getTitle());
+ newLink.setType(link.getType());
+
+ // Single navigation link
+ if(link.getInlineEntity() != null) {
+ final EdmEntitySet linkedEdmEntitySet = (EdmEntitySet) edmEntitySet.getRelatedBindingTarget(link.getTitle());
+ newLink.setInlineEntity(copyEntityRecursively(linkedEdmEntitySet, link.getInlineEntity()));
+ }
+
+ // Collection navigation link
+ if(link.getInlineEntitySet() != null) {
+ final EdmEntitySet linkedEdmEntitySet = (EdmEntitySet) edmEntitySet.getRelatedBindingTarget(link.getTitle());
+ final EntityCollection inlineEntitySet = link.getInlineEntitySet();
+ final EntityCollection newInlineEntitySet = new EntityCollection();
+ newInlineEntitySet.setBaseURI(inlineEntitySet.getBaseURI());
+ newInlineEntitySet.setCount(inlineEntitySet.getCount());
+ newInlineEntitySet.setDeltaLink(inlineEntitySet.getDeltaLink());
+ newInlineEntitySet.setId(inlineEntitySet.getId());
+ newInlineEntitySet.setNext(inlineEntitySet.getNext());
+
+ for(final Entity inlineEntity : inlineEntitySet.getEntities()) {
+ newInlineEntitySet.getEntities().add(copyEntityRecursively(linkedEdmEntitySet, inlineEntity));
+ }
+
+ newLink.setInlineEntitySet(newInlineEntitySet);
+ }
+
+ return newLink;
+ }
+
+ private Entity copyEntity(final Entity entity) {
+ final Entity newEntity = new Entity();
+ newEntity.setBaseURI(entity.getBaseURI());
+ newEntity.setEditLink(entity.getEditLink());
+ newEntity.setETag(entity.getETag());
+ newEntity.setId(entity.getId());
+ newEntity.setMediaContentSource(entity.getMediaContentSource());
+ newEntity.setMediaContentType(entity.getMediaContentType());
+ newEntity.setSelfLink(entity.getSelfLink());
+ newEntity.setMediaETag(entity.getMediaETag());
+ newEntity.setType(entity.getType());
+ newEntity.getProperties().addAll(entity.getProperties());
+
+ return newEntity;
+ }
+
+ private void addEntityToCopyMap(final String entitySetName, final Entity srcEntity, final Entity destEntity) {
+ if(!copyMap.containsKey(entitySetName)) {
+ copyMap.put(entitySetName, new IdentityHashMap<Entity, Entity>());
+ }
+
+ copyMap.get(entitySetName).put(srcEntity, destEntity);
+ }
+
+ private boolean containsEntityInCopyMap(final String entitySetName, final Entity srcEntity) {
+ return getEntityFromCopyMap(entitySetName, srcEntity) != null;
+ }
+
+ private Entity getEntityFromCopyMap(final String entitySetName, final Entity srcEntity) {
+ if(!copyMap.containsKey(entitySetName)) {
+ return null;
+ }
+
+ return copyMap.get(entitySetName).get(srcEntity);
+ }
+}