• Liferay Service Builder

    Liferay 7 Service Builder

    Lets see how to create a service builder module in liferay 7. Before creating the service builder module let me give the some introduction about service builder and its features,Service builder is used in liferay to create database tables and generate the api to read and write into the database.

    Environment
    Liferay IDE Milestone 3.0.1 M2
    Liferay CE Portal Tomcat 7 GA3
    JDK 8
    MySql 5.7

    To create a service builder module click on "New Liferay Module Project"



    Lets give the Service Builder Module's name as "products-data" and select the Project template name as "service-builer".


    Click on next button.


    Click on Finish, This will create a new Liferay Service Builder module for us.
    You can find this newely created service builder module inside the Liferay Wokspace's module folder.
    The folder structure of a service builder module will be as shown below.


    service.xml is the file where we will be creating and configuring new tables, in liferay relational database table is referred as entity or model, when you create a new service builder module it will come with some sample entity, lets remove that and use the entity defined below in service.xml
    <?xml version="1.0"?> <!DOCTYPE service-builder PUBLIC "-//Liferay//DTD Service Builder 7.0.0//EN" "http://www.liferay.com/dtd/liferay-service-builder_7_0_0.dtd"> <service-builder package-path="com.liferaystack"> <author>Syed Ali</author> <namespace>LS</namespace> <entity local-service="true" name="Product" remote-service="true" uuid="true"> <!-- PK fields --> <column name="productId" primary="true" type="long" /> <!-- Group instance --> <column name="groupId" type="long" /> <column name="companyId" type="long" /> <!-- Audit fields --> <column name="name" type="String" /> <column name="description" type="String" /> <column name="manufacturer" type="String" /> <column name="userId" type="long" /> <column name="createDate" type="Date" /> <column name="modifiedDate" type="Date" /> <!-- Order --> <order by="asc"> <order-column name="name" /> </order> <!-- Finder methods --> <finder name="manufacturer" return-type="Collection"> <finder-column name="manufacturer" /> </finder> <!-- References --> <reference entity="AssetEntry" package-path="com.liferay.portlet.asset" /> <reference entity="AssetTag" package-path="com.liferay.portlet.asset" /> </entity> </service-builder>

    Liferay Service Builder Tags and Their Uses

    <namespace>LS</namespace>
    This tag is used to specify the namesspace for the taable, if we create a table named
    product then in database it will be created with namespace as "LS_Product"

    <entity local-service="true" name="Product" remote-service="true" uuid="true">
    
    Using entity tag we can create a new table/model/entity in liferay.
    • name: Used to specify the name of the entity or table
    • local-service: If it is set to true, local service will generated
    • remote-service: If it is set to true, remote services(Web Services) will generated
    • uuid: universal unique identifier will be generated for each entity

    <column name="productId" primary="true" type="long" />
    column tag is used to define the different columns for the database tables with different types
    • name: Name of the column
    • primary: Primary key of the table
    • type: types such as long,int,double,String,boolean,blob,Date,Collection

    <order by="asc">
     <order-column name="name" />
    </order>
    
    When we fetch or read from the tables then the records will be order as specified in this tag.
    • by: asc(Ascending order) or desc(Descending order)
    • <order-column name="name" /> Used to order based on the column name specified in this tag

    <finder name="manufacturer" return-type="Collection">
     <finder-column name="manufacturer" />
    </finder>
    
    When you have many rows of products in the table and you want to fetch all the product belongs to a single manufacturer at that time you can go with finder which allows you fetch the rows based on the finder-column name specified.

    Once you are done with defining the fields, the next step is to build the service, to build the service
    just right click on the module go to Liferay>> then click on build service.



    after building the service, deploy the module, you will be able to see the new table in your database.



  • 3 comments: