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.
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.
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.
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.
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.
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.
Arbitrarily set the namespace of the corresponding service below.
service.xml
<namespace>FOO</namespace>
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">
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" />
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" />
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>
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.
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.
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
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.
Next, create a portlet to actually place it on Liferay and display the data.
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.
In a development environment, if you launch a Liferay server locally, blade will deploy to the server that is running automatically.
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
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