At SAP Inside Track Milan 2012 I presented in a 6 minutes session (unfortuntely my friend, colleague and co-speaker fabio.dimicco2 was unable to present) how you can easily consume NetWeaver Gateway services using SAPUI5 library, in this blog I will resume the concept and the code and share it with the SCN community.
Backend - Gateway service
Gateway service can be easily created on you ABAP Gateway system, but to speed up the presentation I used one of the demo services provided by SAP on ES Workplace (available here) and in particular the classical Flight Sample.
Frontend - SAPUI5
Create a new SAPUI5 project in Eclipse and name it "SITMIL" and check "Create an Initial view", in the next screen name the view "retrieveFlight"
you should have a project structure like below. This looks like a classical MVC structure.
Now we should start coding.
First of all we need to create the container of our application.
Edit the index.html file an paste this code:
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<script src="./resources/sap-ui-core.js" type="text/javascript"
id="sap-ui-bootstrap"
data-sap-ui-libs="sap.ui.ux3, sap.ui.commons, sap.ui.table"
data-sap-ui-theme="sap_goldreflection">
</script>
<script type="text/javascript">
// Create a SAP UI5 shell element
var oShell = new sap.ui.ux3.Shell("flightAppShell", {
appIcon : "http://www.sap.com/global/images/SAPLogo.gif",
appTitle : "Flight manager", });
// Just take the shell and place it in the html area called shellArea
oShell.placeAt("shellArea");
sap.ui.localResources("sitmil");
var view = sap.ui.view({
id : "idFlightService1",
viewName : "sitmil.retrieveFlight",
type : sap.ui.core.mvc.ViewType.JS });
oShell.addContent(view);
</script>
</head>
<body class="sapUiBody" role="application">
<div id="shellArea"></div>
</body>
</html>
Now let's try to explain the code and understand what we are doing.
First step is to import sapui5 libraries used in this demo
<script src="./resources/sap-ui-core.js" type="text/javascript" id="sap-ui-bootstrap"
data-sap-ui-libs="sap.ui.ux3, sap.ui.commons, sap.ui.table"
data-sap-ui-theme="sap_goldreflection">
</script>
Then we initialize the Shell element, that creates a nice look & feel to our page, and we attach it in block element <div id="shellArea">. The shell will be our main container.
// Create a SAP UI5 shell element
var oShell = new sap.ui.ux3.Shell("flightAppShell", {
appIcon : "http://www.sap.com/global/images/SAPLogo.gif",
appTitle : "Flight manager", });
// Just take the shell and place it in the html area called shellArea
oShell.placeAt("shellArea");
As said, the shell will be our main container, what we finally need is to instantiate the main view content and attach it to the shell
sap.ui.localResources("sitmil");
var view = sap.ui.view({
id : "idFlightService1",
viewName : "sitmil.retrieveFlight",
type : sap.ui.core.mvc.ViewType.JS });
oShell.addContent(view);
SAPUI5 plugin automatically creates the main view JS class with two methods
sap.ui.jsview("sitmil.retrieveFlight", {
getControllerName : function() {
return "sitmil.retrieveFlight";
},
createContent : function(oController) {
}
});
what we need it is to implement the createContent method, in this method we will create our user interface.
var layout = new sap.ui.commons.layout.MatrixLayout('layout');
layout.setWidth('80%');
var rPannel = new sap.ui.commons.Panel('rPannel');
var rTitle = new sap.ui.commons.Title('rTitle');
rTitle.setText('All - Flights');
rPannel.setTitle(rTitle);
var oTable = new sap.ui.table.DataTable();
oTable.addColumn(
new sap.ui.table.Column({
label: new sap.ui.commons.Label({text: "AirLine"}),
template: new sap.ui.commons.TextField().bindProperty("value", "AirLineID"),
sortProperty: "AirLineID"
}));
oTable.addColumn(
new sap.ui.table.Column({
label: new sap.ui.commons.Label({text: "Flight Number"}),
template: new sap.ui.commons.TextField().bindProperty("value", "FlightConnectionID"),
sortProperty: "FlightConnectionID"
}));
oTable.addColumn(
new sap.ui.table.Column({
label: new sap.ui.commons.Label({text: "Date"}),
template: new sap.ui.commons.TextField().bindProperty("value", "FlightDate"),
sortProperty: "FlightDate"
}));
oTable.addColumn(
new sap.ui.table.Column({
label: new sap.ui.commons.Label({text: "Airfare"}),
template: new sap.ui.commons.TextField().bindProperty("value", "AirFare"),
sortProperty: "AirFare"
}));
oTable.addColumn(
new sap.ui.table.Column({
label: new sap.ui.commons.Label({text: "Airline Currency"}),
template: new sap.ui.commons.TextField().bindProperty("value", "LocalCurrencyCode"),
sortProperty: "LocalCurrencyCode"
}));
oTable.addColumn(
new sap.ui.table.Column({
label: new sap.ui.commons.Label({text: "Type of the plane"}),
template: new sap.ui.commons.TextField().bindProperty("value", "AirCraftType"),
sortProperty: "AirCraftType"
}));
var oModel = new sap.ui.model.odata.ODataModel(
"http://gw.esworkplace.sap.com/sap/opu/odata/IWBEP/RMTSAMPLEFLIGHT_2",
false,
"GW@ESW",
"ESW4GW");
oTable.setModel(oModel);
oTable.bindRows("FlightCollection");
rPannel.addContent(oTable);
layout.createRow(rPannel);
this.addContent(layout);
}
As did before let's try to understand our code.
First of all we create a matrix layout, a panel ( something like the tray bar in WDA just to make it clear :smile: ) and we associate a panel title, this panel will contain the table list of all the fights exposed by our Gateway service
var layout = new sap.ui.commons.layout.MatrixLayout('layout');
layout.setWidth('80%');
var rPannel = new sap.ui.commons.Panel('rPannel');
var rTitle = new sap.ui.commons.Title('rTitle');
rTitle.setText('All - Flights');
rPannel.setTitle(rTitle);
Next step we configure our table layout, it remembers me the WDINIT method in WDA and the ALV configuration. For each column we need to specify the field name of the collection property (FlightCollection in the gateway service, see metadata of our Gateway service for more details) that we want to bind and display in that column
var oTable = new sap.ui.table.DataTable();
oTable.addColumn(
new sap.ui.table.Column({
label: new sap.ui.commons.Label({text: "AirLine"}),
template: new sap.ui.commons.TextField().bindProperty("value", "AirLineID"),
sortProperty: "AirLineID"
}));
oTable.addColumn(
new sap.ui.table.Column({
label: new sap.ui.commons.Label({text: "Flight Number"}),
template: new sap.ui.commons.TextField().bindProperty("value", "FlightConnectionID"),
sortProperty: "FlightConnectionID"
}));
oTable.addColumn(
new sap.ui.table.Column({
label: new sap.ui.commons.Label({text: "Date"}),
template: new sap.ui.commons.TextField().bindProperty("value", "FlightDate"),
sortProperty: "FlightDate"
}));
oTable.addColumn(
new sap.ui.table.Column({
label: new sap.ui.commons.Label({text: "Airfare"}),
template: new sap.ui.commons.TextField().bindProperty("value", "AirFare"),
sortProperty: "AirFare"
}));
oTable.addColumn(
new sap.ui.table.Column({
label: new sap.ui.commons.Label({text: "Airline Currency"}),
template: new sap.ui.commons.TextField().bindProperty("value", "LocalCurrencyCode"),
sortProperty: "LocalCurrencyCode"
}));
oTable.addColumn(
new sap.ui.table.Column({
label: new sap.ui.commons.Label({text: "Type of the plane"}),
template: new sap.ui.commons.TextField().bindProperty("value", "AirCraftType"),
sortProperty: "AirCraftType"
}));
We need to attach our layout (yes we draw the layout until this point) to our oData service. Here it comes the power of SAPUI5 library, with only 3 lines of code we are able to consume an oData resource and bind a collection to our table; each row of our table will be an entity of the FlighCollection
var oModel = new sap.ui.model.odata.ODataModel(
"http://gw.esworkplace.sap.com/sap/opu/odata/IWBEP/RMTSAMPLEFLIGHT_2",
false,
"GW@ESW",
"ESW4GW");
oTable.setModel(oModel);
oTable.bindRows("FlightCollection");
We are almost done, we only need to attach our table to the panel
rPannel.addContent(oTable);
layout.createRow(rPannel);
this.addContent(layout);
Final step is to export the WAR file into Tomcat webapps directory. You can also deploy it on your preferred HTTP web server, for example I also deployed a live demo on my Apache HTTP server.
Warning: You have to accept Cross Origin Request on you browser, otherwise you will see empty tables :smile:
In part 2 we enhance this demo focusing on navigation property. In the meantime, below the recording of my session at SAP Inside Track Milan 2012.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
37 | |
10 | |
7 | |
5 | |
5 | |
4 | |
4 | |
3 | |
3 | |
3 |