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

标签 java odata client-side olingo

我使用 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 添加映射。

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

}

我了解树和回调的使用,但不了解附加链接... 我阅读了 Olingo libraires 的代码,该库需要一棵树、一个附加链接和一个回调。

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

关于java - Olingo v2 - 如何编写带有导航实体的实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38876239/

相关文章:

Javascript 最佳实践 - 使用服务器端值

javascript - 客户端 JS session 库

java - Spring 集成 MessageDeliveryException

java - 覆盖枚举方法时出现奇怪的行为。 getClass().getInterfaces() 是否正常工作?

odata - 如何根据 OData 中子项的某些属性对对象进行排序?

java - 使用 OKHttp 访问 Graph Microsoft 上的用户个人资料返回特殊字符

java - 在类路径中找不到 jaxws21 前端

java - 如何通知复合 Material 的 child 收到/失去焦点?

asp.net-web-api - 如何返回与在 OData 中运行查询的对象不同的对象?

javascript - 如何在 JavaScript 中导入/包含源文件?