30分钟用Restful ABAP Programming模型开发一个支持增删改查的Fiori应用
2016年時,Jerry曾經(jīng)寫過一系列關(guān)于SAP Fiori Smart Template(現(xiàn)在更名為Fiori Elements了)的博客,介紹了所謂的MDD開發(fā)方法論 - Metadata Driven Development,即通過開發(fā)維護(hù)了對應(yīng)annotation(注解)的CDS view,結(jié)合SAP WebIDE,能夠花費(fèi)最少的編程代價,就能夠在短時間內(nèi)獲得一個支持增刪改查的Fiori應(yīng)用。
這個系列的博客集可以在Jerry這篇公眾號文章里獲得:Jerry的通過CDS view + Smart Template 開發(fā)Fiori應(yīng)用的blog合集。
三年的時間過去了,ABAP在不斷向前進(jìn)化,如今我們有了新的編程模型:Restful ABAP Programming模型,簡稱為RAP模型。該模型定義了一套架構(gòu)體系,應(yīng)用開發(fā)人員能夠憑借其來高效地進(jìn)行應(yīng)用的端到端開發(fā),這種應(yīng)用具有與生俱來的Restful特質(zhì),能充分利用HANA平臺的強(qiáng)大計算能力,支持云環(huán)境和Fiori UX。
RAP模型的三大支柱:
- Business Service
- Core Data Service
- Behavior Definition
下面請跟著Jerry一起,通過一個實(shí)際的例子,了解一下這種全新的通過Restful ABAP Programming模型進(jìn)行Fiori應(yīng)用開發(fā)的步驟吧。
Jerry還是沿用傳統(tǒng)ABAP On-Premises編程培訓(xùn)教材里使用過的經(jīng)典的SFLIGHT模型來作為底層數(shù)據(jù)庫存儲。
(1)首先創(chuàng)建一個數(shù)據(jù)庫表ZTRAVEL_JERRY:(如果想復(fù)制這段源代碼,請點(diǎn)擊文末的“閱讀原文”獲得)
@EndUserText.label : 'Database table for travel data XXX' @AbapCatalog.enhancementCategory : #NOT_EXTENSIBLE @AbapCatalog.tableCategory : #TRANSPARENT @AbapCatalog.deliveryClass : #A @AbapCatalog.dataMaintenance : #LIMITED define table ztravel_jerry {key client : abap.clnt not null;key travel_id : /dmo/travel_id not null;agency_id : /dmo/agency_id;customer_id : /dmo/customer_id;begin_date : /dmo/begin_date;end_date : /dmo/end_date;@Semantics.amount.currencyCode : 'ztravel_jerry.currency_code'booking_fee : /dmo/booking_fee;@Semantics.amount.currencyCode : 'ztravel_jerry.currency_code'total_price : /dmo/total_price;currency_code : /dmo/currency_code;description : /dmo/description;created_by : syuname;created_at : timestampl;last_changed_by : syuname;last_changed_at : timestampl;}因為我們在ABAP Development Tools里無法用事務(wù)碼SE16手動往這張表里插入數(shù)據(jù),所以我創(chuàng)建一個ABAP類,用ABAP代碼往這個表里插入三條數(shù)據(jù)。
按F9執(zhí)行這個ABAP類,然后看到三條數(shù)據(jù)成功插入了:
(2) 我們最終的目的是創(chuàng)建一個支持對這張表進(jìn)行增刪改查的Fiori應(yīng)用,而Restful ABAP Programming模型的三大支柱之一為Core Data Service,因此我們首先得有基于數(shù)據(jù)庫表ZTRAVEL_JERRY的CDS view.
所以我首先創(chuàng)建一個CDS view:
@AbapCatalog.sqlViewName: 'ZVI_TRAVEL' @AbapCatalog.compiler.compareFilter: true @AbapCatalog.preserveKey: true @AccessControl.authorizationCheck: #CHECK @EndUserText.label: 'Travel data - XXX' define root view ZI_TRAVEL_JERRYas select from ztravel_jerry as Travel/* Associations */association [0..1] to /DMO/I_Agency as _Agency on $projection.agency_id = _Agency.AgencyIDassociation [0..1] to /DMO/I_Customer as _Customer on $projection.customer_id = _Customer.CustomerIDassociation [0..1] to I_Currency as _Currency on $projection.currency_code = _Currency.Currency{key travel_id,agency_id,customer_id,begin_date,end_date,@Semantics.amount.currencyCode: 'currency_code'booking_fee,@Semantics.amount.currencyCode: 'currency_code'total_price,@Semantics.currencyCode: truecurrency_code,description,/*-- Admin data --*/@Semantics.user.createdBy: truecreated_by,@Semantics.systemDateTime.createdAt: truecreated_at,@Semantics.user.lastChangedBy: truelast_changed_by,@Semantics.systemDateTime.lastChangedAt: truelast_changed_at,/* Public associations */_Agency,_Customer,_Currency }然后創(chuàng)建一個projection view,將該view的字段有選擇性地暴露出來。
@EndUserText.label: 'Travel projection view - Processor' @AccessControl.authorizationCheck: #NOT_REQUIRED@UI: {headerInfo: { typeName: 'Travel', typeNamePlural: 'Travels', title: { type: #STANDARD, value: 'TravelID' } } }@Search.searchable: truedefine root view entity ZC_TRAVEL_JERRY as projection on ZI_TRAVEL_JERRY {@UI.facet: [ { id: 'Travel',purpose: #STANDARD,type: #IDENTIFICATION_REFERENCE,label: 'Travel',position: 10 } ]@UI: {lineItem: [ { position: 10, importance: #HIGH } ],identification: [ { position: 10, label: 'Travel ID [1,...,99999999]' } ] }@Search.defaultSearchElement: truekey travel_id as TravelID,@UI: {lineItem: [ { position: 20, importance: #HIGH } ],identification: [ { position: 20 } ],selectionField: [ { position: 20 } ] }@Consumption.valueHelpDefinition: [{ entity : {name: '/DMO/I_Agency', element: 'AgencyID' } }]@ObjectModel.text.element: ['AgencyName'] ----meaning?@Search.defaultSearchElement: trueagency_id as AgencyID, _Agency.Name as AgencyName,@UI: {lineItem: [ { position: 30, importance: #HIGH } ],identification: [ { position: 30 } ],selectionField: [ { position: 30 } ] }@Consumption.valueHelpDefinition: [{ entity : {name: '/DMO/I_Customer', element: 'CustomerID' } }]@ObjectModel.text.element: ['CustomerName']@Search.defaultSearchElement: truecustomer_id as CustomerID,@UI.hidden: true_Customer.LastName as CustomerName,@UI: {lineItem: [ { position: 40, importance: #MEDIUM } ],identification: [ { position: 40 } ] }begin_date as BeginDate,@UI: {lineItem: [ { position: 41, importance: #MEDIUM } ],identification: [ { position: 41 } ] }end_date as EndDate,@UI: {lineItem: [ { position: 50, importance: #MEDIUM } ],identification: [ { position: 50, label: 'Total Price' } ] }@Semantics.amount.currencyCode: 'CurrencyCode'total_price as TotalPrice,@Consumption.valueHelpDefinition: [{entity: {name: 'I_Currency', element: 'Currency' }}]currency_code as CurrencyCode,@UI.identification: [ { position: 60, label: 'Remarks' } ]description as Description,@UI.hidden: truelast_changed_at as LastChangedAt}大家可以注意到,這個projection view里包含了很多@UI注解,作用和Fiori Elements一樣,作為元數(shù)據(jù),告訴對應(yīng)的渲染框架,運(yùn)行時這些字段應(yīng)該以什么樣的方式渲染在Fiori UI上。
(3) 現(xiàn)在三大支柱之一的Core Data Service已經(jīng)就位了,接下來我們基于前一步得到的projection view創(chuàng)建Business Service. 選中projection view,右鍵選擇New Service Definition:
這個服務(wù)定義的第一條記錄,就是通過ABAP expose關(guān)鍵字把projection view ZC_TRAVEL_JERRY暴露出來,模型名稱為TravelProcessor:
@EndUserText.label: 'Service Defintion for ZC_Travel_JERRY' define service ZUI_C_TRAVEL_JERRY {expose ZC_TRAVEL_JERRY as TravelProcessor;expose /DMO/I_Customer as Passenger;expose /DMO/I_Agency as TravelAgency;expose /DMO/I_Airport as Airport;expose I_Currency as Currency;expose I_Country as Country; }然后基于這個Service Definition創(chuàng)建一個Service Binding,可以簡單把Service Binding理解成Service Definition的一個實(shí)例:
Service Binding創(chuàng)建完畢后,點(diǎn)擊Activate激活:
之前Service Definition里用expose關(guān)鍵字暴露并指定成的模型TravelProcessor此時就可見了,雙擊:
雙擊后會自動打開一個鏈接,一個Fiori應(yīng)用就呈現(xiàn)在我們眼前了。我們沒有進(jìn)行一行的JavaScript web編程,就得到了一個專業(yè)的支持高級搜索的Fiori應(yīng)用,能查看底層數(shù)據(jù)庫表ZTRAVEL_JERRY的內(nèi)容。
(4) 至此我們已經(jīng)了解了Restful ABAP Programming模型的前兩大支柱,還剩下Behavior Definition. 既然RAP的口號是打造具有Restful特性的應(yīng)用,但到目前為止我們還沒有感受到RAP對Restful的支持,這有待Behavior Definition來完成。
選中之前創(chuàng)建的CDS view,創(chuàng)建一個新的Behavior Definition:
實(shí)現(xiàn)類型指定為Managed:
我們可以看到這個Behavior Definition的定義里,又多了一些新的ABAP關(guān)鍵字。這個Behavior Definition負(fù)責(zé)定義底層模型的Transaction Behavior,即代碼第18到20行的create,update,delete.
當(dāng)然增刪改查的功能光定義不行,還得創(chuàng)建其對應(yīng)的實(shí)現(xiàn)。上圖Definition中已經(jīng)指定了實(shí)現(xiàn)這些行為的ABAP類名稱為ZCL_BP_I_TRAVEL_M_JERRY. 為此,右鍵選擇New Behavior Implementation:
創(chuàng)建這個特殊的ABAP實(shí)現(xiàn)類:
這個實(shí)現(xiàn)類里面也不需要開發(fā)人員手動編寫代碼來完成對底層數(shù)據(jù)庫表的增刪改查操作——既然能稱之為一個編程模型,那么這些通用的功能都通過框架類CL_ABAP_BEHAVIOR_HANDLER統(tǒng)一完成了,應(yīng)用開發(fā)人員只需要定義一個對該類的聲明即可。
把這一步創(chuàng)建好的Behavior Definition模型和其實(shí)現(xiàn)全部激活,然后回到我們之前瀏覽器里打開的Fiori應(yīng)用,刷新,會發(fā)現(xiàn)多了Create和Delete兩個按鈕,這意味著該應(yīng)用對創(chuàng)建和刪除的支持也已經(jīng)自動可用了。
同之前的搜索功能一樣,這些功能的自動獲得,都是建立在應(yīng)用開發(fā)人員一行JavaScript代碼都不用編寫的基礎(chǔ)上的,由此大家感受到了Restful ABAP Programming模型的強(qiáng)大威力了嗎?
后續(xù)Jerry會繼續(xù)介紹如何給這個Fiori應(yīng)用底層使用的模型增添Action和Validation功能,敬請期待。
要獲取更多Jerry的原創(chuàng)文章,請關(guān)注公眾號"汪子熙":總結(jié)
以上是生活随笔為你收集整理的30分钟用Restful ABAP Programming模型开发一个支持增删改查的Fiori应用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 特斯拉发布2023年第一季度财报:营收2
- 下一篇: 如何安装最新版本的ABAP Develo