Git Product home page Git Product logo

cloud-cap-samples-java's Introduction

Welcome to CAP Samples for Java

Welcome to the bookshop-java project. It demonstrates how to build business applications using the CAP Java SDK providing a book shop web application as an example. The application in this project enables browsing books, managing books, and managing orders.

Application Overview in Fiori Launchpad

Outline

Overview

This sample application shows how to conveniently create business applications based on CDS domain models, persisting data with SQLite, or SAP HANA, and exposing an OData V4 frontend with an SAP Fiori frontend on top.

This sample uses Spring Boot as an application framework. Although a CAP Java application isn’t required to build on Spring Boot, it’s the first choice of framework, as it’s seamlessly integrated.

The domain models are defined using CDS entity definitions.

By default, an in-memory or optionally a file-based SQLite database is used for data persistency. Once productively deployed to SAP Cloud Platform, SAP HANA can be used.

Services are defined using CDS Service Models. The OData V4 Protocol Adapter translates the CDS service models into corresponding OData schemas and maps the incoming OData requests to the corresponding CDS services.

Although CAP provides generic event handlers to serve most CRUD requests out-of-the-box, it’s possible to add business logic through Custom Event Handlers.

A Fiori UI is added using predefined SAP Fiori elements templates. Fiori annotations add information to the service definitions, on how to render the data.

Demonstrated Features

Framework and Infrastructure-related Features:

Domain Model related Features:

Service Model related Features:

User Interface related Features:

Getting Started

The following sections describe how to set up, build, and run the project.

Prerequisites

Make sure you have set up a development environment (that means, you’ve installed the CDS Compiler, Java, and Apache Maven) as described here.

Clone Build & Run

  1. Clone the project:
  git clone https://github.com/SAP-samples/cloud-cap-samples-java.git
  1. Build and run the application:
  mvn spring-boot:run

Using Eclipse

Optionally, use the following steps to import the project to Eclipse:

  1. Import the project using File > Import > Existing Maven Projects.

    Now, you should see the projects bookshop and bookshop-parent in the project/package explorer view.

  2. In Project Explorer, change the property "Package Presentation" from "Flat" to "Hierarchical" for better understanding.

Building and Running

  1. To compile the project, right-click the file pom.xml in the bookshop-parent project root folder and select Run as > Maven build.

    In the following dialog, enter the string clean install into the field labeled with "Goals" and click "Run".

    Note: This step also compiles the CDS artifacts, thus repeat this once you made changes to the CDS model. This step also generates source files, therefore refresh the "bookshop" project in your IDE.

  2. To run the application, right-click the bookshop project root in the Package Explorer and select Run as > Spring Boot App (make sure you have Spring Tools 4 installed).

    This step creates a default Run Configuration named Bookshop - Application and starts the application afterwards. To go on with the next step, stop the application again.

  3. Then, set the default working directory by editing your Run Configuration via Run > Run Configurations > Bookshop - Application. On the tab Arguments change the default Working Directory to:

    ${workspace_loc:bookshop-parent}

    Afterwards, click Run. This step starts the applications main method located in src/main/java/my/bookshop/Application.java.

  4. Use the following links in the browser to check if everything works fine:

    http://localhost:8080/: This should show the automatically generated index page of served paths. http://localhost:8080/fiori.html: This is the actual bookshop application UI

    You'll start with an empty stock of books as this procedure starts the bookshop application with an empty in-memory sqlite database.

    Two mock users are defined for local development:

    • User: user, password: user to browse books
    • User: admin, password: admin to manage books and orders

Database Setup and Spring Profiles

The application comes with three predefined profiles: default, sqlite, and cloud (see src/main/resources/application.yaml).

  • The default profile specifies to use an in-memory SQLite database. The in-memory database is set up automatically during startup of the application. However, example data from CSV files aren’t yet automatically imported, therefore some content needs to be created via OData V4 API requests.

  • The sqlite profile specifies to use a persistent SQLite database from root directory of the project. This database needs to be created first, to ensure it’s initialized with the correct schema and with the CSV-based example data. To initialize the database, simply run cds deploy from the project's root directory. Repeat this step, once you make changes to the CDS model.

  • When deploying the application to the CloudFoundry, the CF Java Buildpack automatically configures the cloud Spring profile. This profile doesn’t specify any datasource location. In that case CAP Java can automatically detect HANA service bindings available in the environment.

Using a File-Based SQLite Database

To switch from the default in-memory SQLite database to a file-based SQLite database in this sample application perform the following steps:

  1. Deploy the example data stored in .csv files in the folder db/data to a file-based SQLite database by executing the command-line utility

    cds deploy

    from your project root folder.

  2. Edit your Run Configuration via Run > Run Configurations... and select enter the Profile sqlite on tab Spring and click Run.

Get Support

Check out the CAP documentation here. In case you find a bug or need support, please open an issue in here.

License

Copyright (c) 2020 SAP SE or an SAP affiliate company. All rights reserved. This file is licensed under the Apache Software License, version 2.0 except as noted otherwise in the LICENSE file.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.