Olingo v2 - 如何编写带有导航实体的实体

Olingo v2 - How to write an entity with navigation entities

我使用 Apache Olingo v2。 SAP 是服务器,我负责客户端。 我尝试编写一个带有导航实体的实体。我解释我想说的话。 有一个名为 HUIDENT 的实体集,该集包含一个名为 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>

HUCONTENT 是

<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>

我想发送带有一些 HUCONTENT 的 HUIDENT,但我不知道如何执行此操作。 我想要这样的提要

<?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>

使用 Olingo 我有一些东西

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);

而且我不知道在哪里可以设置 HUCONTENT 实体。

好的,所以我找到了解决方案。 对我来说,这个原则没有经过深思熟虑。 我解释一下。 首先,向您的实体添加一个导航 属性 您需要向 EntityProviderWriteProperties 添加一个 Map。

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

那么,你需要给展开的树

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

最后,要填充您的导航属性,您需要一个回调

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

回调需要实现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;
    }

}

我了解树和回调的使用,但不了解额外的 link... 我阅读了 Olingo libraires 的代码,图书馆需要一棵树、一个额外的链接和一个回调。

我希望这个回答对某人有用