How to create a service builder portlet in Liferay 7 / DXP

Introduction

In Liferay, a mechanism called Service Builder is used to create a model definition and execute Service Builder to automatically generate a persistent layer access class such as a database and a service layer class for users to use without directly defining a table. There is a mechanism to help you.

In Liferay 6.2, Service Builder created a service and a portlet to display its data at the same time, but from Liferay 7 / DXP, due to the move to the OSGi platform and the accompanying thorough modularization, the service has become Services and portlets are now created separately in portlets.

Also, as for the development environment, it was necessary to prepare an SDK separately for Liferay 6.2, but with the introduction of the Blade tool, it is now possible to create a development environment with commands.

With that difference in mind, here's a summary of how to create service builder portlets in Liferay 7 / DXP and best practices.

Install build tool (Gradle / blade)

Gradle installation

Although it is a build tool, Ant / Maven was used at the time of Liferay 6.2, but Gradle / Maven can be used from Liferay 7 / DXP. Gradle is the standard for Liferay, so we'll follow that in this article as well.

Please refer to Gradle official document for the installation method. There is no problem because the latest version is always used.

blade installation

From Liferay 7 / DXP, a tool called Blade was introduced as a development support tool. This makes it possible to cover the construction and deployment of the development environment with a single command.

Please refer to the official document INSTALLING BLADE CLI to install.

Creating a Liferay workspace

In Liferay 6.2, it was necessary to create a development environment using SDK, but in Liferay 7 / DXP, you can create a development environment (Liferay workspace) with the following command.

blade init workspace(Any Liferay workspace name)

Or if you want to initialize in a directory that already exists

blade init --force

Will be initialized as a Liferay workspace.

Creating a Service

Create a service skeleton

When the Liferay workspace is created, a folder called modules is created directly under it. Go under modules, for example

blade create -t service-builder -p com.liferay.todo -c Todo Todo

If you run like

Todo-api/  Todo-service/  build.gradle

A folder for services such as, and a build file for gradle will be generated.

Creating a model

Next, create a model. If you move directly under the Todo-service folder, there is a file called service.xml, so edit it. Please refer to the Official Document for details of various settings, but the main settings Is as follows.

Namespace

Arbitrarily set the namespace of the corresponding service below.

service.xml


<namespace>FOO</namespace>

Entity settings

Set the entity with the following attributes. Multiple entities can be defined in one service.xml. One entity corresponds to one table.

When local-service is set to true, a service that can be referenced from bundles deployed on the same OSGi platform is automatically created.

When remote-service is set to true, an interface (JSON / Web API) that can call the service from outside Liferay is automatically generated.

uuid is specified when you want to generate a unique ID string in the record.

service.xml


<entity local-service="true" name="Foo" remote-service="true" uuid="true">

Primary key

As the name implies, it is the primary key for this entity. It is customary to specify the entity name + Id.

service.xml


<column name="fooId" primary="true" type="long" />

Group ID Liferay uses Group IDs everywhere to classify target data records, such as for controlling permissions. Be sure to include this item in the entity. You can also use this key in the Finder, which will be explained later.

service.xml


<column name="groupId" type="long" />

Audit field

Liferay comes standard with an auditing mechanism, but these fields are needed for that mechanism to record audit logs. Therefore, be sure to include these items in the entity.

service.xml


        <column name="companyId" type="long" />
        <column name="userId" type="long" />
        <column name="userName" type="String" />
        <column name="createDate" type="Date" />
        <column name="modifiedDate" type="Date" />

Sort order

When retrieving entity data, specify the sort order in which the data can be retrieved in the list.

service.xml


        <order by="asc">
            <order-column name="field1" />
        </order>

Finder method

Liferay has a mechanism to automatically generate an interface for simple data acquisition, which is called Finder. By setting these, the Finder method for retrieving the data will be generated. Replace it with any content and create a Finder. You can also define multiple Finder. Collection and entity name can be specified for return-type. Please refer to DTD (http://www.liferay.com/dtd/liferay-service-builder_7_0_0.dtd) for the details of the setting contents.

service.xml


        <finder name="Field2" return-type="Collection">
            <finder-column name="field2" />
        </finder>

References Specify the table related to the relevant entity. It is specified by default.

service.xml


        <reference entity="AssetEntry" package-path="com.liferay.portlet.asset" />
        <reference entity="AssetTag" package-path="com.liferay.portlet.asset" />

Is specified when using the common infrastructure provided by Liferay such as comments, stars, and tags.

Service generation

Go to the Todo-service directory and

gradle buildService

To generate a skeleton for the service. Then, according to the setting of service.xml, Finder method etc. are automatically generated, a table is created in the database, and a method for CRUD to operate it is also generated.

Add method to Service

Service Builder works by automatically generating the corresponding interface from the implementation class (file with * Impl.java).

In addition, the skeleton generated by Lliferay side is regenerated many times by gradle build Service every time the model or service is changed in the development process. Therefore, if you edit a file other than the one allowed by Liferay, it will be overwritten, so be careful.

Editable files are

  1. *LocalServiceImpl.java
  2. *ServiceImpl.java
  3. *** Namespace name Impl.java (not namespace name ModelImpl.java) **

Will be.

For example, add the following to FooLocalServiceImpl.java and

FooLocalServiceImpl.java


    public String getHelloWorld() {
        return "Hello World";
    }

When you run gradle buildService, the interface corresponding to getHelloWorld will be generated and this service will be available.

Creating a Portlet

Next, create a portlet to actually place it on Liferay and display the data.

Create portlet skeleton with blade

By convention in Liferay 7 / DXP

function suffix
Service (implementation) *-service
Services (API, interface) *-api
Portlet(View part) *-web

Name it. Move directly under the Todo folder and generate a portlet with the following command.

blade create -t mvc-portlet -p com.liferay.todo -c TodoPortlet Todo-web

Now the directory structure under Todo is as follows.

Todo-api/  Todo-service/  Todo-web/  build.gradle

Go directly under the generated Todo-web directory and add Todo-api to build.gradle as below

Todo-api/build.gradle


dependencies {
    compileOnly group: "com.liferay.portal", name: "com.liferay.portal.kernel", version: "2.0.0"
    compileOnly group: "com.liferay.portal", name: "com.liferay.util.taglib", version: "2.0.0"
    compileOnly group: "javax.portlet", name: "portlet-api", version: "2.0"
    compileOnly group: "javax.servlet", name: "javax.servlet-api", version: "3.0.1"
    compileOnly group: "jstl", name: "jstl", version: "1.2"
    compileOnly group: "org.osgi", name: "osgi.cmpn", version: "6.0.0"
    compileOnly project(":modules:Todo:Todo-api") //Add this.
}

Also, add the following along with the import statement so that FooLocalService can be called from TodoPortlet.java.

FooLocalServiceImpl.java


    @Reference
    protected void setFooLocalService(
            FooLocalService fooLocalService) {

        _fooLocalService = fooLocalService;
    }   
    
    private FooLocalService _fooLocalService;

The FooLocalService is now available within Todo-web.

Deploy

In a development environment, if you launch a Liferay server locally, blade will deploy to the server that is running automatically.

  1. Start Liferay (assuming you are running on localhost: 8080)
  2. Move directly under the Todo directory and execute blade deploy. Todo-service, Todo-api, Todo-web are deployed respectively.
  3. Log in to Liferay and select Todo-web deployed from the Application pane on the right side of the screen + you can place it on the screen.

Service Builder Portlet Automatic Generation Tool

There is also a tool called Damascus that automatically generates service builder portlets (* -service, * -api, * -web). This makes it possible to generate templates that already have the functions required for business applications such as CRUD, search, and workflow.

Liferay DXP / 7.0 Portlet Automatic Generation Tool, Damascus

Summary

The above is a quick start, but it is an example of the basic setup when using the service builder portlet in Liferay 7 / DXP.

Recommended Posts

How to create a service builder portlet in Liferay 7 / DXP
How to create a theme in Liferay 7 / DXP
How to easily create a pull-down in Rails
How to create a method
How to create a Java environment in just 3 seconds
How to create a Spring Boot project in IntelliJ
How to create a data URI (base64) in Java
How to create a placeholder part to use in the IN clause
[Java] How to create a folder
How to create a new Gradle + Java + Jar project in Intellij 2016.03
How to insert a video in Rails
[Swift5] How to create a splash screen
[rails] How to create a partial template
How to publish a library in jCenter
How to create a query using variables in GraphQL [Using Ruby on Rails]
How to call libraries such as JQuery and JQuery UI in Liferay 7 / DXP
How to create a database for H2 Database anywhere
[Rails] How to create a graph using lazy_high_charts
Try to create a bulletin board in Java
How to create pagination for a "kaminari" array
How to create a class that inherits class information
How to run a djUnit task in Ant
How to add a classpath in Spring Boot
[1st] How to create a Spring-MVC framework project
How to implement a like feature in Rails
[Rails] How to create a Twitter share button
How to make a follow function in Rails
How to automatically generate a constructor in Eclipse
How to create docker-compose
How to clear all data in a particular table
How to run the SpringBoot app as a service
How to connect MySQL / MariaDB + HikariCP with Liferay 7 / DXP
[Rails] How to create a signed URL for CloudFront
How to implement a like feature in Ajax in Rails
How to create a JDBC URL (Oracle Database, Thin)
[Spring Boot] How to create a project (for beginners)
I tried to create a Clova skill in Java
How to launch another command in a Ruby program
How to display a browser preview in VS Code
How to write a date comparison search in Rails
How to store Rakuten API data in a table
How to mock a super method call in PowerMock
How to convert A to a and a to A using AND and OR in Java
How to convert a file to a byte array in Java
[Rails] How to load JavaScript in a specific view
How to write a core mod in Minecraft Forge 1.15.2
[Apple Subscription Offer] How to create a promotional offer signature
[Ruby/Rails] How to generate a password in a regular expression
How to create a lightweight container image for Java apps
How to create a form to select a date from the calendar
How to change a string in an array to a number in Ruby
How to leave a comment
How to use @Builder (Lombok)
How to store a string from ArrayList to String in Java (Personal)
Create a method to return the tax rate in Java
How to display a graph in Ruby on Rails (LazyHighChart)
How to add the same Indexes in a nested array
I want to create a Parquet file even in Ruby
Mapping to a class with a value object in How to MyBatis
How to develop and register a Sota app in Java
How to simulate uploading a post-object form to OSS in Java