创建复杂OData对象的条目的正确方法是什么

时间:2017-05-25 21:46:57

标签: odata sapui5

我有这个复杂的OData对象:

<?xml version="1.0" encoding="UTF-8"?>
<EntityType Name="ApprovalsRequest">
   <Key>
      <PropertyRef Name="IdUser" />
      <PropertyRef Name="RequestId" />
   </Key>
   <Property Name="ApprovalTime" Type="Edm.DateTime" Nullable="true" />
   <Property Name="IdUser" Type="Edm.Int32" Nullable="false" />
   <Property Name="RequestId" Type="Edm.Int32" Nullable="false" />
   <Property Name="Request" Type="Edm.Int32" Nullable="true" />
   <Property Name="Status" Type="Edm.String" />
   <Property Name="User" Type="Edm.Int32" Nullable="true" />
   <NavigationProperty Name="RequestDetails" Relationship="rva-persistence.ApprovalsRequest_Request_Many_ZeroToOne0" FromRole="ApprovalsRequest" ToRole="Request" />
   <NavigationProperty Name="UserDetails" Relationship="rva-persistence.ApprovalsRequest_User_Many_ZeroToOne0" FromRole="ApprovalsRequest" ToRole="User" />
</EntityType>

我有some problems用于从SAPUI上的Web应用程序创建此对象的enries。最后,我这样做了。

但是,我认为我的方式不是很优雅。

首先,遵循OOP范例,我有以下代码:

//userModel and requestModel representing a JSN object of type
//User and Request respectively

aprovalRequestModel.IdUser = userModel.IdUser;
aprovalRequestModel.RequestId = requestModel.RequestId;
aprovalRequestModel.User = userModel.IdUser;
aprovalRequestModel.Request = requestModel.RequestId;
aprovalRequestModel.RequestDetails = requestModel;
aprovalRequestModel.UserDetails = userModel;
aprovalRequestModel.ApprovalTime = new Date().toLocaleString();

var oDateFormat = sap.ui.core.format.DateFormat.getDateInstance({pattern: "yyyy-MM-ddThh:mm:ss"});
aprovalRequestModel.ApprovalTime = oDateFormat.format(new Date());

aprovalRequestModel.Status = "1";

var saveRequestModel = model.create(
        "ApprovalsRequests", 
        aprovalRequestModel
);

此代码生成了一个请求,其中引用了NavigationProperties的对象已展开但未完成。测试此请求后,我收到“请求格式错误”错误消息。

我调查并发现this question。在这篇文章之后,我编写了以下内容(我创建的JSON对象类似于帖子但不相同):

//userModel and requestModel representing a JSN object of type
//User and Request respectively

aprovalRequestModel.IdUser = userModel.IdUser;
aprovalRequestModel.RequestId = requestModel.RequestId;
aprovalRequestModel.User = userModel.IdUser;
aprovalRequestModel.Request = requestModel.RequestId;
aprovalRequestModel.UserDetails = {
        __deferred: {
            uri: "/Users(" + userModel.IdUser + ")"
        }
};
aprovalRequestModel.RequestDetails = {
        __deferred: {
            uri: "/Requests(" + requestModel.RequestId + ")"
        }
};

var oDateFormat = sap.ui.core.format.DateFormat.getDateInstance({pattern: "yyyy-MM-ddThh:mm:ss"});
aprovalRequestModel.ApprovalTime = oDateFormat.format(new Date());

aprovalRequestModel.Status = "1";

var saveRequestModel = model.create(
        "ApprovalsRequests", 
        aprovalRequestModel
);

此代码确实生成了正确的XML请求(它不等于OData guideliness的缩写方式,但效果较差):

<?xml version="1.0" encoding="UTF-8"?>
<a:entry xmlns:a="http://www.w3.org/2005/Atom" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata">
   <a:author>
      <a:name />
   </a:author>
   <a:content type="application/xml">
      <m:properties>
         <d:IdUser>1</d:IdUser>
         <d:RequestId>1</d:RequestId>
         <d:User>1</d:User>
         <d:Request>1</d:Request>
         <d:ApprovalTime>2017-05-23T10:39:54</d:ApprovalTime>
         <d:Status>1</d:Status>
      </m:properties>
   </a:content>
   <a:link href="/Users(1)" rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/UserDetails" type="application/atom+xml;type=entry">
      <m:inline>
         <a:entry>
            <a:author>
               <a:name />
            </a:author>
            <a:id>/Users(1)</a:id>
            <a:content type="application/xml">
               <m:properties />
            </a:content>
         </a:entry>
      </m:inline>
   </a:link>
   <a:link href="/Requests(1)" rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/RequestDetails" type="application/atom+xml;type=entry">
      <m:inline>
         <a:entry>
            <a:author>
               <a:name />
            </a:author>
            <a:id>/Requests(1)</a:id>
            <a:content type="application/xml">
               <m:properties />
            </a:content>
         </a:entry>
      </m:inline>
   </a:link>
</a:entry>

我想如果存在一种更专注于OOP范式的方式(就像我的第一种方式)?我认为我的第二种方式是一些手册。我不应该构建元数据的属性。我认为这必须自动生成。

我希望你的意见。

0 个答案:

没有答案