You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@olingo.apache.org by Hector Navarro <he...@mes-view.com> on 2016/07/20 08:48:17 UTC

How can I create a contained entity in Olingo?

According to OData documentation, how can I create a contained entity 
using Apache Olingo?

In order to create an entity, I follow next steps:

1. Get EdmEntitySet and EdmEntityType from UriInfo:

         EdmEntitySet edmEntitySet = Util.getEdmEntitySet(uriInfo);
         EdmEntityType edmEntityType = edmEntitySet.getEntityType();

2. 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();

         EntityData entityData = new EntityData(requestEntity);

3. Store data on backend
4. Serialize response and return it.

  I think that I should get referenced EntitySet on the first step, but I 
don't know how to do it because last UriResource is an instance of 
UriResourceNavigationPropertyImpl and it hasn't a getEntitySet method. 
This is my current Util.getEdmEntitySet:

     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)) {
             throw new ODataApplicationException("Invalid resource type 
for first segment.",
                     HttpStatusCode.NOT_IMPLEMENTED.getStatusCode(), 
Locale.ENGLISH);
         }

         UriResourceEntitySet uriResource = (UriResourceEntitySet) 
resourcePaths.get(0);

         return uriResource.getEntitySet();
     }

How can I get EntitySet from NavigationProperty?

Thanks in advance.

Héctor Navarro
Ingeniero de software
hector.navarro@mes-view.com


NOTA LEGAL:Este mensaje y los documentos que en su caso lleve anexos, 
pueden contener información confidencial dirigida exclusivamente a su 
destinatario. Si ha recibido este mensaje por error, le notificamos que 
cualquier uso, difusión copia están prohibidos legalmente y rogamos que 
nos lo comunique por la misma vía y proceda a destruirlo inmediatamente.

Re: How can I create a contained entity in Olingo?

Posted by "Bolz, Michael" <mi...@sap.com>.
Hi Hector,

You can get the type of the navigation via the “EdmNavigationProperty” (see below).

> UriResourceNavigation uriResourceNavigation = (UriResourceNavigation) navSegment;
> EdmNavigationProperty edmNavigationProperty = uriResourceNavigation.getProperty();
> EdmEntityType responseEdmEntityType = edmNavigationProperty.getType();

Before you have to do something similar to this code snippet in your “Util” method:

> } else if (segmentCount == 2) { // navigation
>   UriResource navSegment = resourceParts.get(1); // in our example we don't support more complex URIs
>   if (navSegment instanceof UriResourceNavigation) {
> /* more code …. */

Above code snippets are copied from the navigation tutorial [1].

Best Regards, Michael

[1]: https://github.com/apache/olingo-odata4/blob/master/samples/tutorials/p4_navigation/src/main/java/myservice/mynamespace/service/DemoEntityProcessor.java#L106 <https://github.com/apache/olingo-odata4/blob/master/samples/tutorials/p4_navigation/src/main/java/myservice/mynamespace/service/DemoEntityProcessor.java#L106>

> On 20 Jul 2016, at 10:48, Hector Navarro <he...@mes-view.com> wrote:
> 
> According to OData documentation <http://www.odata.org/getting-started/advanced-tutorial/#createContain>, how can I create a contained entity using Apache Olingo?
>  
> In order to create an entity, I follow next steps:
>  
> 1. Get EdmEntitySet and EdmEntityType from UriInfo:
>  
>         EdmEntitySet edmEntitySet = Util.getEdmEntitySet(uriInfo);
>         EdmEntityType edmEntityType = edmEntitySet.getEntityType();
>  
> 2. 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();
>  
>         EntityData entityData = new EntityData(requestEntity);
>  
> 3. Store data on backend
> 4. Serialize response and return it. 
>  
>  I think that I should get referenced EntitySet on the first step, but I don't know how to do it because last UriResource is an instance of UriResourceNavigationPropertyImpl and it hasn't a getEntitySet method. This is my current Util.getEdmEntitySet:
>  
>     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)) {
>             throw new ODataApplicationException("Invalid resource type for first segment.",
>                     HttpStatusCode.NOT_IMPLEMENTED.getStatusCode(), Locale.ENGLISH);
>         }
>  
>         UriResourceEntitySet uriResource = (UriResourceEntitySet) resourcePaths.get(0);
>  
>         return uriResource.getEntitySet();
>     }
>  
> How can I get EntitySet from NavigationProperty?
>  
> Thanks in advance.
>  
> Héctor Navarro
> Ingeniero de software
> hector.navarro@mes-view.com <x-...@mes-view.com>
>  
> <logo-web.bmp> <http://mes-view.com/>
>  
> NOTA LEGAL:Este mensaje y los documentos que en su caso lleve anexos, pueden contener información confidencial dirigida exclusivamente a su destinatario. Si ha recibido este mensaje por error, le notificamos que cualquier uso, difusión copia están prohibidos legalmente y rogamos que nos lo comunique por la misma vía y proceda a destruirlo inmediatamente.