Site icon Pro Liferay

An introduction to Liferay Service Builder

liferay-service-builder


Liferay Service Builder is a tool which is generally used to generate code to interact with database. Liferay Service Builder auto generate service layer code to interact with underlying database. The service builder tool takes xml file (its called service.xml) as an input and generate code based on the input provided in service.xml. This article explains the basics of liferay service builder. 


Effective service.xml:

For generating service layer we must provide valid service.xml file. Liferay Service Builder generates code based on service.xml. To construct a valid service.xml we must follow the below DTD

http://www.liferay.com/dtd/liferay-service-builder_6_2_0.dtd

Where to put service.xml file:

The service.xml file should be placed under WEB-INF directory of the portlet application. For example if book-portlet is my project directory then the service.xml should be placed in book-portlet/docroot/WEB-INF directory. 

How to run liferay service builder:

Its only one step process. We just need to run build-service target of the build.xml file. In the eclipse Ant view we can double click on build-service

What happens after successful build service

After build service there are some check points to ensure that our service codes are generated successfully. 

i) There should not be any error message while running the service builder. 

ii) service folder will be created under WEB-INF directory. 

iii) sql folder  will be created under WEB-INF directory.

iv) META-INF folder  will be created under src directory.

iv) Other files like localServiceImpl, serviceImpl  will be generated under src directory.

v) service.properties file will be created directly under src folder.

 [focus]

Note:

Liferay Service Builder will generate SQL statements which will be available under sql folder.

[/focus]

Some Theory:
In traditional approach if we need to interact with the database we need to take care of JDBC Connection, DataSource, DriverManager and so on. But with the use of Liferay Service Builder we will get all the infrastructure like Hibernate Configuration files, Spring Configuration files, model layer, SQL to generate table structure, Session management etc. on the fly. Is not it fantastic?

 We all know about DTO and DAO layers to persist data. These two layers can be written by developers manually, and often a lot of time is spent writing and debugging code in them. Service Builder generates both layers for us automatically.   

Most of the time the Liferay Developer works in the DTO layer (i.e., on –impl files). This layer talks to the service layer, which is automatically generated and allows us to work with objects that need to be persisted or have been retrieved from the database. These are the –Impl classes we’ve generated, and this is the reason why they were placed in the src folder with the rest of your portlet code. So all the generated -impl files falls under DTO layer. We call DAO layer methods that invoke methods in the persistence layer to do the actual persisting. These methods are generated from the <finder> tags you placed in the service.xml file.

Example of service.xml: 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE service-builder PUBLIC "-//Liferay//DTD Service Builder 6.2.0//EN" "http://www.liferay.com/dtd/liferay-service-builder_6_2_0.dtd">
<service-builder package-path="com.proliferay.sbuilder">
	<author>Hamidul Islam</author>
	<namespace>Proliferay</namespace>

	<entity name="Book" table="BOOK_PORTLET" local-service="true" remote-service="true">
		<column name="bookId" type="long" primary="true" id-type="increment"/>
		<column name="bookName" type="String" />
		<column name="description" type="String" />
		<column name="authorName" type="String" />
		<column name="isbn" type="int" />
		<column name="price" type="int" />
	</entity>
</service-builder>

After generating the service by the command ant build-service it will generate many class and interfaces which are ready to use to interact with database. Along with all generated class and interfaces it will also generate sql create statements as below 

create table BOOK_PORTLET (
	bookId LONG not null primary key,
	bookName VARCHAR(75) null,
	description VARCHAR(75) null,
	authorName VARCHAR(75) null,
	isbn INTEGER,
	price INTEGER
);

[focus]
Key Point:
In service.xml file we define our database table where our data will be persisted.For persisting and retrieving data we use service layer which is generated by service builder tool. Though Liferay Service Builder generates lots of codes, we have full freedom to write our own custom code whenever necessary.
[/focus]

Liferay Service-Builder provides flexibility to map Java data types into SQL data types as follows:

[focus]

Conclusion:

In this article only introduction of service builder is given. In fact service builder topic is very large. So to keep this article short only theoretical aspects of service builder is provided. In details of Liferay Service Builder will be published in other articles. Keep reading.  

[/focus]

Exit mobile version