• An introduction to Liferay Service Builder

    Posted on April 27, 2015 by Hamidul Islam in Liferay.

    liferay-service-builder


    aim

    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. 

    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. 

    position-service-file

    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.

     

    Note:

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

    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?

     service-builder-toolWe 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.   

    service-builder-tool-generated-layers

    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.

    threee-layers-generated-by-service-builder

    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
    );
    

    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.

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

    java-data-type-to-sql-data-type

    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.  

7 Responses so far.

  1. Jeet chatterjee says:

    Fantastic

  2. Bobby says:

    Can the service builder create the 'Image' field? And what is the image field called in the service.xml? Image? Blob?

  3. James says:

    Hai, I followed your steps for creating service.xml file for liferay but I am getting some errors. Can you help me with that...?

    • James says:

      error,

      BUILD FAILED
      C:\Temp\LifeRayPortal\liferay-plugins-sdk-6.2\build-common-plugin.xml:464: The following error occurred while executing this line:
      C:\Temp\LifeRayPortal\liferay-plugins-sdk-6.2\build-common-plugin.xml:147: Service Builder generated exceptions.

    • Hamidul Islam says:

      Hi James,
      Can you please send me the complete stack trace of the exception?

      Regards
      Admin
      http://www.proliferay.com

  4. Alexander says:

    Nice explanation, thank you!

Leave a Reply

Your email address will not be published. Required fields are marked *

Are you human? * Time limit is exhausted. Please reload CAPTCHA.

Top
%d bloggers like this:

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close