Liferay 7 – Service builder notes

Liferay MVC

MVC is a great design pattern for web applications because it splits your application into three parts (the model, the view and the controller). This lets you swap out those parts if necessary.

Liferay portlet is based on MVC framework.

In Liferay, the persistence layer saves and retrieves your model data. In other words, it talks to database directly. The service layer is a buffer between your application and persistence layers. Having it lets you swap out your persistence layer for a different implementation without modifying anything but the calls in the service layer.

Service.xml fields introduction

This defines the author, namespace, and the entity name. The namespace keeps the database field names from conflicting. The groupId defines the entity instance belongs to.

Audit section defines Liferay Portal metadata. The companyId is the primary key of a portal instance. The userId is the primary key of a user. The createDate and modifiedDate store the respective dates on which the entity instance is created and modified.

The Status section is used later to implement workflow.

<finder name="GroupId" return-type="Collection"><finder-column name="groupId" /></finder>

This defines a finder that generates a get method you’ll use to retrieve Guestbook entities.

The fields used by the finder define the scope of the data retrieved.

Status fields

<!-- Status fields -->
<column name="status" type="int" />
<column name="statusByUserId" type="long" />
<column name="statusByUserName" type="String" />
<column name="statusDate" type="Date" />

Audit fields

<!-- Audit fields -->
<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" />

Exception types

Before the closing </service-builder> tag,  these generate exception classes you’ll use later in try/catch statements.


Integrating the back-end

After running the service builder, the back-end code has been generated. You get to do some dependency management. For the web module to access the generated services, you must make it aware of the API and service modules. Then you can update the addEntry method in GuestbookPortlet to use the new services.

Update build.gradle file and add these dependencies.

compileOnly project(":modules:guestbook:guestbook-api")
compileOnly project(":modules:guestbook:guestbook-service")

Add references to the service builder services you need. To do this, add them as class variables with @Reference annotations on their setter methods.

@Reference(unbind = "-")
protected void setEntryService(EntryLocalService entryLocalService){
    _entryLocalService = entryLocalService;

@Reference(unbind = "-")
protected void setGuestbookService(GuestbookLocalService guestbookLocalService) {
    _guestbookLocalService = guestbookLocalService;

private EntryLocalService _entryLocalService;
private GuestbookLocalService _guestbookLocalService;

If you want to extend Liferay Portal’s panel categories and apps. To do this, open guestbook-web’s build.gradle file and add this dependency.

compileOnly group: "com.liferay", name: "com.liferay.application.list.api", version: "2.0.0"