Skip to Content
Event Information

Annotated links: Episode 18 of Hands-on SAP dev with qmacro

This is a searchable description of the content of a live stream recording, specifically “Episode 18 – A first look at CAP with Java” in the “Hands-on SAP dev with qmacro” series. There are links directly to specific highlights in the video recording. For links to annotations of other episodes, please see the “Catch the replays” section of the series blog post.

This episode, titled “A first look at CAP with Java”, was streamed live on Fri 12 Apr 2019 and is approximately one hour in length. The stream recording is available on YouTube.

Below is a brief synopsis, and links to specific highlights – use these links to jump directly to particular places of interest in the recording, based on ‘hh:mm:ss’ style timestamps.

Brief synopsis

After DJ Adams has been working with the Cloud Application Programming Model in a local development environment so far, we are now looking at the development possibilities in SAP Cloud Platform. Where we used to work with SQLite, SAP HANA is now used as a database. Where node.js used to be the programming language for Business Logic, we take a look at Java. And the IDE used in this episode is the SAP Cloud Platform WebIDE and not Visual Studio code. Keep in mind, both approaches (VS Code vs. WebIDE, SQLite vs. SAP HANA) can be legitimate and complementary, not substituting.

00:00:40: DJ Adams introduces me and my today’s goal to show you CAP with Java in the SAP Cloud Platform WebIDE.

00:02:45: Slightly offtopic, but how do you pronounce the “/etc/” directory in a Unix context? Following his twitter poll: Most folks (61%) think it should be “eee tee cee” instead of “et cetera” or “et cee”.

00:03:10: The occassional midweek livestream will now be more fixed and is now fortnightly every Wednesday. Take a look at the exact streaming times at the schedule. Weekday and times will not change, but it’s not anymore occassional.

00:04:50: We introduced a new feedback form to improve the whole livestream flow and to get your ideas and critic. Please spent a few seconds after each episode to enter some of your thoughts and feelings, which are helpful for us in any case.

00:05:30: How do you dynamically add values to a selectbox in Google Forms? If you interested in how Google Apps Script works, let us know. The scripts takes some content of a Google Spreadsheet and manipulates the form to have a more convenient and faster way of selectable episode names, in this case.

00:07:30: I’m in Timisoara, Romania, one day before SAP InsideTrack Timisoara where I will speak about Serverless topics. Telling viewers a little bit about my background: I started as a Java developer, switched over to SAP topics like SAP Business Warehouse and ABAP. Since the beginning of 2018 I’m focusing on SAP Cloud Platform topics.

00:09:45: What have we done so far, what’s coming today? The general concept of CAP is already familiar to the majority of our livestream viewers, but we will dive into some Java specifics on the basis of our already existing datamodel and CAP knowledge we have. So it’s mostly just a new perspective on concepts we have already learned.

00:12:00: Before starting to develop any new artefacts we take a minute to summarize a few upsides of SAP Cloud Platform WebIDE. It’s available without any additional installation on your own machine and available on the free SAP Cloud Platform trial. So, get started in a few seconds without spending too much time on installation activities. And on top, it offers so many great features (sample applications, templates, etc.) and integrations to other SAP Products (e.g. SAP API Business Hub).

00:13:30: SAP Cloud Platform WebIDE also has the option to organize your various projects in different workspaces, as it’s already known from other IDEs like Eclipse. If you want to know how to customize your SAP Cloud Platform WebIDE, take a look at the tutorial.

00:14:35: We set our Cloud Foundry space in the workspace settings in order to have a defined deployment target for all the artefacts we create from now on.

00:16:30: Creating a new project from template called “SAP Cloud Platform Business Application” which is more or less the equivalent as “cds init” on your local machine, including your database and service module.

00:19:00: Exploring which files initially are created by the previous wizzard walk-through – some files which are new compared to the node.js approach, most of the files should already look fairly familiar to you. Additionally, exploring where the databases differences are as we are not anymore using SQLite but SAP HANA as our database.

00:23:15: Some tips and tricks on tiny features in SAP Cloud Platform WebIDE and how to make your development more convenient, like “Link Workspace to Editor” which causes an emphasis on the file in your project which is opened in your editor. We also switch off that every single file save triggers a “CDS Build” in our workspace. We also have a look at how the Code editor can be customized.

00:25:10: We prepare our database layer and start where have almost finished on our local setup with the data model. Uploading CSV files containing some sample data to the right folder structure (project/db/src/) and creating the data-model.cds, with 3 entities: Books, Authors, Orders. Additionally, for our HANA deployment we uploaded a “DATA.hdbtabledata” file to map the right columns from source files to the according target columns in the HANA tables.

namespace my.bookshop;
using { Country, managed } from '@sap/cds/common';

entity Books {
  key ID : Integer;
  title  : localized String;
  author : Association to Authors;
  stock  : Integer;
}

entity Authors {
  key ID : Integer;
  name   : String;
  books  : Association to many Books on books.author = $self;
}

entity Orders : managed {
  key ID  : UUID;
  book    : Association to Books;
  country : Country;
  amount  : Integer;
}

00:27:10: Discussions about LSP Language Server Protocol usage in SAP Cloud Platform WebIDE and in VS Code and test, if everything is working fine so far in our environment.

00:30:30: Building our db module and generating some SAP HANA artefacts.

00:32:30: Going through the generated SAP HANA files (.hdbcds) files for each of our entities and services as well as comparing the generation results with the compilation result with the locally installed CDS commandline tool.

00:38:00: As there are always questions and discussions in every episode, there is a Slack Channel for all kind of discussions and conversations around this livetream series. Get your invitation by simply entering your mail address on this page.

00:39:00: Deploying SAP HANA artefacts via right click on the db module and click on “Build”.

00:40:15: Creation of our service definition “cat-service.cds” corresponding to our already created data model definition:

using my.bookshop from '../db/data-model';

service CatalogService {
  entity Books @readonly as projection on bookshop.Books;
  entity Authors @readonly as projection on bookshop.Authors;
  entity Orders @insertonly as projection on bookshop.Orders;
}

00:41:00: We want to check if the db deployment was successfully executed. Thus, we enable our SAP Cloud Platform WebIDE Extension called “SAP HANA Database Explorer” to get some insights. And, since there are a bunch of HDI Container visible to us, we have to establish to the one we recently deployed. (HDI Container name can be found in the deployment logs of SAP Cloud Platform WebIDE).

00:44:00: Run our service definition as a Java application (as we have defined it in the beginning on the template creation wizzard) without any additional business logici implementation in Cloud Foundry – so it’s only the default service implementation of CAP, since we haven’t created any line of Java Code.

00:46:10: Accessing our recently deployed Java application and navigating through the offered OData Services, if everything is working as expected.

00:48:30: Creating the first Java class as in a Entity Operation Hooks Class in order to add some specific custom logic to the default OData operations. We don’t have to create the whole skeleton of the class on our own, there’s a feature in SAP Cloud Platform WebIDE:

00:54:30: Filling the automatically created Java class with some random code to be able to check if our Hook implementation got effective. There’s no need of a specific Class Name or Method name – the crucial aspect is the annotation of the method. We also have a look at how to organize the Java imports and problems view in SAP Cloud Platform WebIDE.

package my.bookshop;

import com.sap.cloud.sdk.service.prov.api.ExtensionHelper;
import com.sap.cloud.sdk.service.prov.api.annotations.BeforeRead;
import com.sap.cloud.sdk.service.prov.api.exits.BeforeReadResponse;
import com.sap.cloud.sdk.service.prov.api.request.ReadRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class OrderHooksHandler {

    private static final Logger LOG = LoggerFactory.getLogger(OrderHooksHandler.class.getName());

    @BeforeRead(entity = "Orders", serviceName = "CatalogService")
    public BeforeReadResponse thisIsLive(ReadRequest req, ExtensionHelper h) {
        LOG.error("is everybody still alive?!");
        return BeforeReadResponse.setSuccess().response();
    }

}

 

00:57:50: Restart the application. The Java Method implementation of our BeforeReadHook is fine and the according log output is done as expected. Our first custom service implementation is working!

Be the first to leave a comment
You must be Logged on to comment or reply to a post.