Huret Romain
Huret Romain

Reputation: 41

Olingo v2 - How to write an entity with navigation entities

I use Apache Olingo v2. SAP is the server and I'm in charge of the client-side. I try to write an entity with navigation entities. I explain what I try to say. There is an entity set named HUIDENT, this set contains a navigation property named ToHuContent.

<EntityType Name="HUIDENT" sap:content-version="1">
  <Key>
    <PropertyRef Name="Lgnum"/>
    <PropertyRef Name="Huident"/>
  </Key>
  <Property Name="Lgnum" Type="Edm.String" Nullable="false" MaxLength="4" sap:label="Numéro magasin" sap:creatable="false" sap:updatable="false" sap:sortable="false" sap:filterable="false"/>
  <Property Name="Huident" Type="Edm.String" Nullable="false" MaxLength="20" sap:label="Unité manutent." sap:creatable="false" sap:updatable="false" sap:sortable="false" sap:filterable="false"/>
  <NavigationProperty Name="ToHuContent" Relationship="SERVICE_HU_CONTENT_SRV.Assoc_Hu_Content" FromRole="FromRole_Assoc_Hu_Content" ToRole="ToRole_Assoc_Hu_Content"/>
</EntityType>

And a HUCONTENT is

<EntityType Name="HUCONTENT" sap:content-version="1">
  <Key>
    <PropertyRef Name="Lgnum"/>
    <PropertyRef Name="Huident"/>
  </Key>
  <Property Name="Lgnum" Type="Edm.String" Nullable="false" MaxLength="4" sap:label="Numéro magasin" sap:creatable="false" sap:updatable="false" sap:sortable="false" sap:filterable="false"/>
  <Property Name="Huident" Type="Edm.String" Nullable="false" MaxLength="20" sap:label="Unité manutent." sap:creatable="false" sap:updatable="false" sap:sortable="false" sap:filterable="false"/>
  <Property Name="Matnr" Type="Edm.String" Nullable="false" MaxLength="40" sap:label="Produit" sap:creatable="false" sap:updatable="false" sap:sortable="false" sap:filterable="false"/>
  <Property Name="Ean11" Type="Edm.String" Nullable="false" MaxLength="18" sap:label="Code EAN/UPC" sap:creatable="false" sap:updatable="false" sap:sortable="false" sap:filterable="false"/>
  <Property Name="Batchid" Type="Edm.Binary" Nullable="false" sap:label="Lot" sap:creatable="false" sap:updatable="false" sap:sortable="false" sap:filterable="false"/>
  <Property Name="Quan" Type="Edm.Decimal" Nullable="false" Precision="31" Scale="14" sap:unit="Unit" sap:label="Quantité" sap:creatable="false" sap:updatable="false" sap:sortable="false" sap:filterable="false"/>
  <Property Name="Unit" Type="Edm.String" Nullable="false" MaxLength="3" sap:label="Unité" sap:creatable="false" sap:updatable="false" sap:sortable="false" sap:filterable="false" sap:semantics="unit-of-measure"/>
  <Property Name="Maktx" Type="Edm.String" Nullable="false" MaxLength="40" sap:creatable="false" sap:updatable="false" sap:sortable="false" sap:filterable="false"/>
  <Property Name="Brand" Type="Edm.String" Nullable="false" MaxLength="4" sap:label="Brand" sap:creatable="false" sap:updatable="false" sap:sortable="false" sap:filterable="false"/>
  <NavigationProperty Name="ToHuIdent" Relationship="SERVICE_HU_CONTENT_SRV.Assoc_Hu_Content" FromRole="ToRole_Assoc_Hu_Content" ToRole="FromRole_Assoc_Hu_Content"/>
</EntityType>

I would like to send an HUIDENT with some HUCONTENT but I don't how to do this. I would like a feed like this

<?xml version="1.0" encoding="utf-8"?>
  <entry xml:base="secretUrl" xmlns="http://www.w3.org/2005/Atom" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices">
   <id>secretUrl/HUIDENTSet(Lgnum='IN01',Huident='155000000000001757')</id>
   <title type="text">HUIDENTSet(Lgnum='IN01',Huident='155000000000001757')</title>
   <updated>2016-05-26T22:48:07Z</updated>
   <category term="SERVICE.HUIDENT" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme"/>
   <link href="HUIDENTSet(Lgnum='IN01',Huident='155000000000001757')" rel="self" title="HUIDENT"/>
   <link href="HUIDENTSet(Lgnum='IN01',Huident='155000000000001757')/ToHuContent" rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/ToHuContent" type="application/atom+xml;type=feed" title="ToHuContent">
    <m:inline>
 <feed xml:base="secretUrl/SERVICE/">
 <entry>
  <content type="application/xml">
    <m:properties>
     <d:Lgnum>IN01</d:Lgnum>
     <d:Huident>155000000000001757</d:Huident>
     <d:Epc>12345</d:Epc>
     <d:Matnr>1543473</d:Matnr>
     <d:Ean>122333</d:Ean>
     <d:Expiration_date>2016-05-27T00:00:00</d:Expiration_date>
     <d:CRreatedAt>2016-05-26T22:48:00</d:CRreatedAt>
    </m:properties>
   </content>
  </entry>
  <entry>
   <content type="application/xml">
    <m:properties>
     <d:Lgnum>IN01</d:Lgnum>
     <d:Huident>155000000000001757</d:Huident>
     <d:Epc>12345</d:Epc>
     <d:RfidStatus/>
     <d:Matnr>1543473</d:Matnr>
     <d:Expiration_date>2016-05-27T00:00:00</d:Expiration_date>
     <d:CRreatedAt>2016-05-26T22:48:00</d:CRreatedAt>
    </m:properties>
   </content>
  </entry>
 </feed>
 </m:inline>
 </link>
 <content type="application/xml">
   <m:properties>
     <d:Lgnum>IN01</d:Lgnum>
     <d:Huident>155000000000001757</d:Huident>
   </m:properties>
 </content>
 </entry>

Using Olingo I've something as

final EntityProviderWriteProperties properties = EntityProviderWriteProperties
                    .serviceRoot(new URI(this.serviceRoot + "/"))
                    .build();

final ODataResponse response = EntityProvider.writeEntry("application/xml", entitySet, new ImmutableMap.Builder<String, Object>()
               .put("Lgnum", "IN01")
               .put("Huident", "1")
               .build(), properties);

And I don't know where I can set the HUCONTENT entities.

Upvotes: 2

Views: 1518

Answers (1)

Huret Romain
Huret Romain

Reputation: 41

Ok, so I find a solution. For me the principle is not well thought out. I explain it. First, add a navigation property to your entity you need to add an Map to the EntityProviderWriteProperties.

final Map<String, Map<String, Object>> toHuContent = new ImmutableMap.Builder<String, Map<String, Object>>()
                    .put(<YOUR_NAVIGATION_PROPERTY>, new ImmutableMap.Builder<String, Object>().build())
                .build();

final EntityProviderWriteProperties properties = EntityProviderWriteProperties
                    .serviceRoot(new URI(this.serviceRoot + "/"))
                    .additionalLinks(toHuContent)
                    .build();

Then, you need to give the expanded tree

final ExpandSelectTreeNode node = ExpandSelectTreeNode.entitySet(entitySet).expandedLinks(Lists.newArrayList(<YOUR_NAVIGATION_PROPERTY>).build();
final EntityProviderWriteProperties properties = EntityProviderWriteProperties
                    .serviceRoot(new URI(this.serviceRoot + "/"))
                    .additionalLinks(toHuContent)
                    .expandSelectTree(node)
                    .build();

Finally, to populate your navigation properties you need a callback

final EntityProviderWriteProperties properties = EntityProviderWriteProperties
                    .serviceRoot(new URI(this.serviceRoot + "/"))
                    .additionalLinks(toHuContent)
                    .expandSelectTree(node)
                    .callbacks(new ImmutableMap.Builder<String, ODataCallback>().put(<YOUR_NAVIGATION_PROPERTY>, new PopulateInlineEntry()).build())
                    .build();

And the callback need to implements OnWriteFeedContent

@Log4j
public class PopulateInlineEntry implements OnWriteFeedContent {

    @Override
    public WriteFeedCallbackResult retrieveFeedResult(final WriteFeedCallbackContext writeFeedCallbackContext) throws ODataApplicationException {
        final EntityProviderWriteProperties currentWriteProperties = writeFeedCallbackContext.getCurrentWriteProperties();

        final WriteFeedCallbackResult result = new WriteFeedCallbackResult();
        result.setInlineProperties(currentWriteProperties);
        result.setFeedData(Lists.<Map<String, Object>>newArrayList(
                new ImmutableMap.Builder<String, Object>()
                        .put("Lgnum", "IN01")
                        .put("Huident", "1")
                        .put("Epc", "1")

                        .build(),
                new ImmutableMap.Builder<String, Object>()
                        .put("Lgnum", "IN01")
                        .put("Huident", "1")
                        .put("Epc", "2")

                        .build()
        ));

        return result;
    }

}

I understand the use of the tree and the callback but not the additional link... I read the code of Olingo libraires and the librairy need a tree, an additionalLinks and a callback.

I hope this answer will useful for someone

Upvotes: 2

Related Questions