Wednesday, January 21, 2015

Liferay Service Builder in Detail

Today we will see concept of Service Builder in liferay


Before reading this blog it is highly recommended to read my blog on Service Builder in Liferay .Liferay Service Builder based on hibernate and Spring DI .Service builder create tables automatically as in Hibernate.Service builder generates Services for the entities in two location. These location use the package path describe in service.xml :-

1)docroot/WEB-INF/services/package name
2)docroot/WEB-INF/src/package name

Service Builder divide the source in 2 layers:
1)An interface Layer
2)An implementation Layer

You ’ll never change anything in the interface layer manually.The implementation layer is generated in your src folder and is initially skeleton code that allows you to implement the functionality you need.




Service builder also create src folder inside META-INF folder .This folder contains all the Spring configuration files and Hibernate configuration.

Sample service.xml

<service-builder package-path="com.student.services" auto-namespace-tables="true">
    <author>monu</author>
    <namespace>Aditya</namespace>
    <entity name="Student" local-service="true" cache-enabled="false" table="student">
        <column primary="true" name="studentId" type="int" id-type="increment"></column>
        <column name="name" type="String"></column>
        <column name="lastname" type="String"></column>
        <column name="std" type="String"></column>
        <column name="address" type="String"></column>
        <column name="phone" type="String"></column>
        <column name="gender" type="int"></column>
    </entity>
</service-builder>





We will discuss each and every tag in detail






1)<service-builder package-path="com.student.services" auto-namespace-tables="true">

a)service-builder package-path

This is the path of package in which all the java file is generated

b)auto-namespace-tables

By default it is false if this is true then table is created in database with a prefix of namespace.
(But if there is no table tag we see it later) 

2) <author>monu</author>

This tag is not mandatory.


Service Builder also generates Javadoc, and the name you place in the Author tags winds up in the Javadoc as the author of the code.

You can skip this tag but if you mention all Java classes have author name as mention like in this case monu.

Ex -
/
 * @author monu
 * @see StudentModel
 * @see com.student.services.model.impl.StudentImpl
 * @see com.student.services.model.impl.StudentModelImpl
 * @generated
 */
public interface Student extends StudentModel, PersistedModel {

}

3) <namespace>Aditya</namespace>

This tag is mandatory .

By default, tables you define with Service Builder go in the Liferay database. To set them off from the rest of Liferay’s tables, you can prefix them with a namespace. This table, when created, will be called Aditya_Student in the database.

If auto-namespace-tables is true than this name is appended before table name.The entry of this namespace is also mention in database with a table name servicecomponent that contain various columns like build number ,build date.


Ex-



Note :-

a)When ever you want to change the structure of table like number of columns and changes not reflect then delete entry of namespace from this table and build service again.

b)Namespace must be some valid name .Ex- Aditya9 this number 9 will create a problem in build service.

4) <entity name="Student" local-service="true" cache-enabled="false" table="student">

a)entity name="Student"

This name is compulsory but need not to be same as table name.

All Service classes are made by this name like entityLocalServiceUtil etc.
Ex- StudentLocalServiceUtil

b)cache-enabled=""

By using cache-enabled  true data base data is cached.

c)table="student"

This tag is not Compulsory

If you skip this tag then table in database is created with same name as entity name. Otherwise table name in database is same as table tag

d)local-service="true"

By using local-service true we can use our services as web services like SOAP

5) <column primary="true" name="studentId" type="int" id-type="increment"></column>

a)primary="true"

Means this field is primary key for database.

b)name="studentId"

Column is created with studentId in database

c)type="int"

Field is of int type. We have many options like int,long,String etc.

d)id-type="increment"

This field is of autoincrement type. Other options are sequence,identity etc.


Some Cases:-

1)<service-builder package-path="com.student.services" auto-namespace-tables="true">
<namespace>Aditya</namespace>
    <entity name="Student" local-service="true" cache-enabled="false" table="StudentData">
        <column primary="true" name="id" type="int"></column>
        <column name="name" type="String"></column>
    </entity>
</service-builder>

Table name = StudentData

Reason:-Because we provide table tag so namespace has no significance here.

2)<service-builder package-path="com.student.services" auto-namespace-tables="true">
<namespace>Aditya</namespace>
    <entity name="Student" local-service="true" cache-enabled="false">
        <column primary="true" name="id" type="int"></column>
        <column name="name" type="String"></column>
    </entity>
</service-builder>

Table name = aditya_student (namespace_entityname)

Reason:-Because auto-namespace-table="true"

3)<service-builder package-path="com.student.services" auto-namespace-tables="false">
<namespace>Aditya</namespace>
    <entity name="Student" local-service="true" cache-enabled="false">
        <column primary="true" name="id" type="int"></column>
        <column name="name" type="String"></column>
    </entity>
</service-builder>

Table name = Student

Reason:-Because  auto-namespace-table="false" and no table tag so table is created with name same as entity name.

Complsory Fields

1)<namespace>Aditya</namespace> because entry in service component
2)entity name all java files created with this

Optional Fields

1)<author></author> 
2)table name="optional"

Classes Created by Service Builder

1)com.student.services.model.impl

a)StudentBaseImpl
b)StudentCacheModel
c)StudentImpl
d)StudentModelImpl

2)com.student.services.service.base

a)StudentLocalServiceBaseImpl
b)StudentLocalServiceClpInvoker
c)StudentServiceBaseImpl
d)StudentServiceClpInvoker

3)com.student.services.service.http

a)StudentServiceSoap

4)com.student.services.service.impl

a)StudentLocalServiceImpl
b)StudentServiceImpl

5)com.student.services.service.persistence

a)StudentPersistenceImpl


Thats it Hope this Help Someone

 Related Post:-

Service Builder in Liferay

Finder method for Service builder in liferay

Custom Sql/Query in Liferay

Custom Sql/Query with two table in Liferay

Many To Many Relationship mapping in Liferay Services

2 comments:

  1. Can you please guide me to create JSON service in liferay...step by step

    ReplyDelete
  2. Hi harmeet just create remote service true and liferay automatically provide SOAP based web services
    ex- entity name="Student" local-service="true" remote-service="true"

    ReplyDelete

Total Pageviews

Like Us on Facebook

Number Of Unique Visitor

Free counters!