Combining Netweaver Cloud and SAP HANA – 2
In previous blog I simply explained how to get data from HANA Developer Edition. In this blog I will extend it.
I assume that you created a new Dynamic Web Project for Netweaver Cloud. Now create a new Servlet for project.
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String q = request.getParameter("q");
try{
Context ctx = new InitialContext();
HttpDestination destination = (HttpDestination) ctx.lookup("java:comp/env/awshana");
HttpClient client = destination.createHttpClient();
HttpGet get = new HttpGet("/A20configJ_denemeCollection?q=" + q + "&%24select=MATNR%2CMAKTX&$top=100");
HttpResponse resp = client.execute(get);
HttpEntity entity = resp.getEntity();
String respToString = EntityUtils.toString(entity);
response.setContentType("application/json");
response.getWriter().print(respToString);
}catch (DestinationException e) {
response.getWriter().print(e.getLocalizedMessage());
throw new RuntimeException(e);
}catch (NamingException e) {
response.getWriter().print(e.getLocalizedMessage());
}
}
We will query our view, so getting query parameter from query string is important.
String q = request.getParameter("q");
Concatenating query to our request.
HttpGet get = new HttpGet("/A20configJ_denemeCollection?q=" + q + "&%24select=MATNR%2CMAKTX&$top=100");
We get data from HANA as JSON. So we have to set content type to application/json
response.setContentType("application/json");
Ok, we are done with our servlet. Now create a new html file for project.
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title>HANA Example</title>
<script id="sap-ui-bootstrap"
type="text/javascript"
src="https://sapui5.netweaver.ondemand.com/resources/sap-ui-core.js"
data-sap-ui-theme="sap_goldreflection"
data-sap-ui-libs="sap.ui.commons, sap.ui.table"
data-sap-ui-modules="sap.ui.core.plugin.DeclarativeSupport"></script>
<script>
// Created a new table for data
var oTable = new sap.ui.table.Table({
title: "HANA Example",
visibleRowCount:100,
firstVisibleRow: 3,
selectionMode: sap.ui.table.SelectionMode.Single
});
//Defined the columns and the control templates to be used
var oColumn = new sap.ui.table.Column({
label: new sap.ui.commons.Label({text: "Material Number"}),
template: new sap.ui.commons.TextView().bindProperty("text", "MATNR"),
width: "300px"
});
oTable.addColumn(oColumn);
oTable.addColumn(new sap.ui.table.Column({
label: new sap.ui.commons.Label({text: "Material Description"}),
template: new sap.ui.commons.TextField().bindProperty("value", "MAKTX"),
width: "500px"
}));
//Create a model and bind the table rows to this model
var oModel = new sap.ui.model.json.JSONModel("urlofservlet?q=");
oTable.setModel(oModel);
oTable.bindRows("/d/results");
//Bring the table onto the UI
oTable.placeAt("uiArea");
</script>
</head>
<body class="sapUiBody">
<script>
function loadData(){
// implment js for getting search data. we get data and bind it table again
var stxtSearch = sap.ui.getCore().byId("txtSearch").getValue();
var oModel = new sap.ui.model.json.JSONModel("urlofourservlet?q=" + stxtSearch);
oTable.setModel(oModel);
oTable.bindRows("/d/results");
}
</script>
<div data-sap-ui-type="sap.ui.commons.layout.MatrixLayout" layout-fixed="false" style="margin: 20px; width: 1%;">
<div data-sap-ui-type="sap.ui.commons.layout.MatrixLayoutRow">
<div data-sap-ui-type="sap.ui.commons.layout.MatrixLayoutCell">
<span data-sap-ui-type="sap.ui.commons.Label" text="Text:"></span>
</div>
<div data-sap-ui-type="sap.ui.commons.layout.MatrixLayoutCell">
<span data-sap-ui-type="sap.ui.commons.TextField" id="txtSearch" width="400px"></span>
</div>
<div data-sap-ui-type="sap.ui.commons.layout.MatrixLayoutCell">
<span data-sap-ui-type="sap.ui.commons.Button" text="Load Data..." press="loadData"></span>
</div>
</div>
</div>
<div id="uiArea"></div>
</body>
</html>
And here it is:
This example can be used for getting data from HANA server. In Netweaver Cloud (Beta system) you can not get your data from HANA because port restriction.
Excellent blog Erhan! Please keep them coming 😉
Greetings,
Blag.
Seconded.
Andy Silvey
http://wiki.sdn.sap.com/wiki/pages/viewpage.action?pageId=309461179
Hi Erhan,
I really like your approach of jamming away with SAP Technologies and especially the fact that you are investigating on how-to bridge the AWS based HANA Developer systems and NW Cloud. You sure are pioneering on this front!
If I could express a personal wish it would be to have a bit more prosa in your blogs that explain your reasoning of what you're doing and why people should care. Take the readers on a journey by telling the whole story (your motivation) and providing a more detailed step-by-step tutorial on how-to achieve what you've done so that others can benefit from your expertise more easily!
The code snippets are great and should help people who are familiar with the related technologies, yet for newcomers the entry barrier to making it work for them may still be a bit high. Ultimately, if you could share your code some place (e.g. Github) it would be marvelous 😉
Thanks again for your contribution, I hope you continue to share your experiences and I'm looking forward to your next posts!
Cheers,
Matthias
Thanks Matthias,
I assume some points are known by people. I dont write about creating dynamic web project for example. I think people know that who has interest in NWCloud. But you are right about step-by-step tutorial and motivation. I will be more illustrative about that.
I have Github account. I will make some projects open there 😉
Thanks again!
Excellent post.
Exactly what i was looking for, thanks!