Service builder is an automated tool to construct service and persistence layer in Liferay. We need to define business models (entities) and rest is taken care by service builder.
Service Builder is one of the most powerful, time saver, proven and peace of mind feature of Liferay.
- Service layer by Service Builder
- Persistence Layer:-
- Service Classes:-
- Model classes:-
- Relations among Model interfaces / classes :-
- Out of these model classes / interfaces, only StudentImpl.java is allowed to add additional (custom) methods at model level to developer. Relations among Service interfaces / classes :-
- Relations among Persistence interfaces / classes :-
- Summing Up
what is Liferay Service Builder ?
Liferay provide nice way of creating service and persistence layer. Liferay Service Builder is a tool to create service-persistence layer in Liferay. It generates set of classes and interfaces which is used to interact with Database (CRUD operation). This way it saves development time of building service-persistence layer manually. You can refer my previous blog on Liferay Service Builder concept to get little more background bout it.
Liferay Service Builder uses Spring for providing Service layer implementation and Hibernate for persistence layer implementation. Both of these frameworks (Spring & Hibernate) are industry standard and proven to construct scale-able web applications.
Let us see how to develop service-persistence layer in liferay by simple example. First we need to create custom Liferay MVC Portlet.
Give project name as service-builder-test and eclipse will append -portlet at the end so that final project name will become service-builder-test-portlet.Create portlet class call called TestServiceBuilderPortlet under package com.opensource.techblog.portlet. Also update portlet class entry in portlet.xml file. After this the project structure will look like below screenshot.
We want to create service and persistence layer that will actually provide CRUD operations.
In Real life, the business objects are encapsulated by Entity classes. From an object-oriented perspective, an entity object represents an object in the real-world problem domain (Business Object). Each Business object will have attributes and behaviour (instance variable and methods in Object Oriented perspective).
In our case we will take Student as business object. In real world Student will have following attributes
- Father Name
- Mother Name
Service layer by Service Builder
Package Path :- the location where the generated service-persistence classes will resides.
Namespace :- Back end side, liferay will create SQL script for each entity. We can group the tables generated for entities by giving Name Space value.
- If we give Name space as education and if we defined 3 entities (soon we see how to define the entities) then there will be 3 tables created (each for individual entity).
- Name of these (3) tables will start with education_.
- Suppose the entities are Student, Marks and Leave then the corresponding table name would be education_student, education_marks and education_leave.
The generated Tables for each entities defined in service.xml file will have common namespace defined in that service.xml file
Author :- Name of the author who is going to create the services.
We can define multiple entity in service.xml. For simplicity, we took just one entity. Give the value for Package Name, Namespace and Author name as below.
Package path:- com.opensource.techblog.myservice
Author:- <<Your Name>> (I gave mine)
Click on Finish button. You will observe that a new file called service.xml will be created under WEB-INF folder as shown in below screenshot.
You can observe that the package-path, author and namespace will be defined as elements in service.xml. Now we will start defining our entity – Student. Add the following snippet (for Student entity) in service.xml file.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE service-builder PUBLIC "-//Liferay//DTD Service Builder 6.0.0//EN" "http://www.liferay.com/dtd/liferay-service-builder_6_0_0.dtd">
<entity name="Student" local-service="true" remote-service="false">
<column name="studentId" type="long" primary="true"/>
<column name="name" type="String"/>
<column name="age" type="int"/>
<column name="fatherName" type="String"/>
<column name="motherName" type="String"/>
<column name="standard" type="int"/>
- Liferay service builder can generate local or remote or both type of services. Local service can be used by client code running in same VM(Virtual Machine) where liferay instance is running. Liferay also supports remote service which can be accessed as a web-service (SOAP or JSON) over internet / intranet. In our case we have created Student entity by defining local-service as true and remote-service as false so it will generate only local service.
- Child elements of Student element’s are columns which exactly reflect the column in the DB table.
- Each column will have name and type. We can declare the primary key by giving primary=”true”. If more than one column defined as primary=”true” then compound key will be generated.
- The available column types are
- Blob etc.
go to command prompt up to service-builder-test-portlet and give command ant build-service. It should show the below text in console and at the end it will show the message “Build Successful”.
Now come back to portlet in eclipse and right click on project service-builder-test-portlet and click on Refresh. At this stage, the project structure looks like below screenshot.
You will observe that few classes / files are added in project as shown in below screenshot. They all are related to service-persistence layer. Let’s see all them in details.
As shown in the screenshot, the generated service classes are located at following two places
- Which contains util classes and interface.
- All the interface / classes resides under this location will be packed in JAR file. This jar file will be generated after each service build (through ant build-service command).
- So the interfaces and util classes can be available directly to outer world.
- which contains the implementation of interfaces defined under folder /WEB-INF/service/com/opensource/techblog/myservice.
- It will be part of class path (under src folder) and will not be directly available to outer word.
- StudentPersistence.java (under /WEB/INF/service/com/opensource/techblog/myservice)
- Student persistence interface which defines CRUD methods related to Student entity like create, remove, countAll, find, findAll etc
- StudentPersistenceImpl.java (under /WEB-INF/src/com/opensource/techblog/myservice)
- This is the implementation class, which implements StudentPersistence.
- StudentUtil.java (under /WEB/INF/service/com/opensource/techblog/myservice)
- This util class, which having the instance of StudentPersistenceImpl class
- local service interface for Student entity.
- local service implementation which implements StudentLocalService interface. Basic CRUD methods will be added by liferay service builder. If you want to add any additional service methods then must be added in this class.
- However you can’t directly access them from this class. You have to re-run the service builder (through ant build-service command) and service builder will create corresponding (same method signature) static method in StudentLocalSerivceUtil class. You can then access StudentLocalSeriviceUtil class which internally make call to corresponding (same signature) method of StudentLocalServiceImpl.
- This class implements StudentLocalSerivce interface and provides all basic CRUD methods. StudentLocalServiceImpl where we can define additional service methods extends this class.
- All service Impls and persistence Impls classes related to Student entity are injected into this class. Additionally if you give reference of any other entity, it’s service impls and persistence impls classes are also injected in this class.
- Object of type StudentLocalServiceImpl is injected in StudentLocalServiceUtil class by Liferay service builder through spring injection. Out of all above service classes, only this class is accessible to other API (outside of service layer) for CRUD operation.
- StuddentLocalServiceUtil has all static method. Each method of StudentLocalServiceUtil will make call to corresponding (same signature) method in StudentLocalSerivceImpl class.
- Base model (interface) for Student.
- This interface and its corresponding implementation (StudentModelImpl) exist only as a container for the default property accessors generated by ServiceBuilder. Helper methods and all application logic should be put in (StudentImpl)
- Base model impl which implements StudentModel interface
- This implementation and its corresponding interface (StudentModel) exist only as a container for the default property accessors generated by ServiceBuilder. Helper methods and all application logic should be put in (StudentImpl).
- extends studentModel.java
- By defaul there is not method defined.
- Whenever any new (custom) method added to StudentImpl, they will be added to this interface on next service build.
- extends StudentBaseImpl.java.
- implements Student.java interface.
- Helper methods and all application logic should be put in this class.
- Whenever custom methods are added in this class, the corresponding methods will be added to Student interface on next service build.
Relations among Model interfaces / classes :-
Out of these model classes / interfaces, only StudentImpl.java is allowed to add additional (custom) methods at model level to developer. Relations among Service interfaces / classes :-
Following diagram shows the relation between service interface and classes
Out of these service classes / interfaces, only StudentLocalServiceImpl is allowed to add additional (custom) methods at service level to developer.
Relations among Persistence interfaces / classes :-
You will observe that this class have various CRUD methods like
- Liferay Service Builder concept
- Create Finder method for Service builder in Liferay
- Call service from one portlet to another portlet in liferay
- Service builder is used to create service and persistence layer in Liferay
- We need to define business model and service builder will take care for rest.
- Service builder creates set of service and persistence classes and interface.
- Go to Liferay documentation and search for Service Builder to get more idea.