Library Service Guide

What you'll build

You will build a service managing books, authors and subscriptions.

What you’ll need

Development requirements

A Java™ Development Kit (JDK) installed. We recommend AdoptOpenJDK version 11 or 8.

Apache Maven version 3.1 or later installed.

Optional

An Integrated Developer Environment (IDE)

Popular choices include IntelliJ IDEA, Spring Tools, Visual Studio Code, or Eclipse, and many more.

Testing and running requirements

A FlexiCore based server running locally or on an accessible server.

One-click, no prerequisites installation is available for Linux (AMD64, ARM64) and Windows (AMD64) here 

A Docker image with fully installed FlexiCore and prerequisites is available here

Step 1: People Management

Library Management project is dependent on People Management project , please complete the people management guide

Step 2: Library Model

Step 2.a: Design Basic Model

managing entities for this phase:

  1. Book
  2. Author
  3. Subscription – this will model the fact that a person lend a book from our library.

Create a new Maven Project with the following structure:

Library Model Project Structure

update your pom.xml from here

Step 2.b: Create Book Entity

Open up the project in your IDE and create the Book.java file in the src/main/java/com/flexicore/example/library/model folder. Now change the contents of the file by adding the extra method and annotations shown in the code below. You can copy and paste the code or just type it.

  1. Our Book object inherits from Baseclass since we want to be able to govern permissions for it

Step 2.c: Create Author Entity

Open up the project in your IDE and create the Author.java file in the src/main/java/com/flexicore/example/library/model folder. Now change the contents of the file by adding the extra method and annotations shown in the code below. You can copy and paste the code or just type it.

  1. Our Author object inherits from Person since we want to extend its capabilities.

if you haven’t completed the People Management Project it is required to compile library model.

Step 2.d: Create Subscription Entity

Open up the project in your IDE and create the Subscription.java file in the src/main/java/com/flexicore/example/library/model folder. Now change the contents of the file by adding the extra method and annotations shown in the code below. You can copy and paste the code or just type it.

  1. Our Subscription object inherits from Baseclass since we want to extend its capabilities.
  2. our OffsetDateTime typed fields are annotated by@Column(columnDefinition = "timestamp with time zone") to tell our database to save the date information with  time zone.

Step 2.e: Create Persistence.xml

copy persistence.xml content from here.

this will allow automatic generation of JPA metamodels required to implement Criteria API based queries.

Step 2.f: install library model

./mvn install

./cp target/library-model-1.0.0-jar /home/flexicore/entities

Step 3: Library Service

Step 3.a: create Library service plugin

create a maven project with the following structure:

Library Service Project Structure

update your pom.xml from here

Step 3.b: Create library service request objects

lets define objects that will be consumed by our api:

  1. BookCreate – this object will contain all required details for creating a Book it will also inherit from BaseclassCreate object as we would like to extend Baseclass capabilities
  2. BookUpdate – this object will extend BookCreate object and id of the book to update
  3. BookFilter – this object will be sent by the client when fetching Books containing filtering options on them.
  4. AuthorCreate – this object will contain all required details for creating an Author it will also inherit from PersonCreate object as we would like to extend Person capabilities
  5. AuthorUpdate – this object will extend AuthorCreate object and id of the Author to update
  6. AuthorFilter – this object will be sent by the client when fetching Authors containing filtering options on them.
  7. SubscriptionCreate – this object will contain all required details for creating an Subscription it will also inherit from BaseclassCreate object as we would like to extend Baseclass capabilities
  8. SubscriptionUpdate – this object will extend SubscriptionCreate object and id of the Subscription to update
  9. SubscriptionFilter – this object will be sent by the client when fetching Subscriptions containing filtering options on them.

 

Step 3.c: Create Repositories

lets define the repositories that will be used to fetch and save books, authors and subscriptions from the database.

  1. the repository is annotated by
    •  @Extension annotation to allow FlexiCore to load it as a plugin 
    • @PluginInfo annotation to allow future versioning support
    • @Component annotation to let spring know it is a bean.
  2. the repository class extends AbstractRepositoryPlugin which provides easy method for access control and out of the box methods for persisting objects
  3. the repository exposes methods for listing and counting Book/Author/Subscription both are calling the relevant addBookPredicates/addAuthorPredicates/addSubscriptionPredicates which adds the required predicates , all access control predicates are automatically added when countAllFiltered and getAllFiltered are called
  4. addBookPredicates/addAuthorPredicates/addSubscriptionPredicates uses JPA Criteria Api to filter data based on BookFilter/AuthorFilter/SubscriptionFilter objects we have created in previous phase.

Step 3.d: Create Services

lets define the services that will be used by other plugins and REST api (or any other API implementations for that matter)

Step 3.e: Create REST Services

lets define the REST services that will expose REST API that our clients will use: