Pages

Monday 30 September 2013

Creating Service layer in Service Builder in Liferay


Creating Service layer in Service Builder in Liferay

Have you ever imagine any web application without database ? … No . right ? Then how come liferay is exception ?

Liferay provide nice way of creating service and persistence layer. It uses Spring for providing Service layer implementation and Hibernate for persistence layer implementation. Both of these frameworks (Spring & Hibernate) is industry standard and proven to construct scalable web application.

Let us see how to develop service-persistence layer in liferay by simple example.

Refer my previous blog on How to create custom Liferay Portlet and create 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
Name
Age
Father Name
Mother Name
Standard
etc.

The goal of this article is to create CRUD methods to save / update /add /remove Students details into database.
Please following below steps to create service for Student entity.


Right click on Project in Eclipse, choose New -->Liferay Service. It will show following window asking to enter Package Path, Name space, and Author name







Explanation:-


Package Path :- the location where the generated service-persistence classes will resides.
Namespace :- Backend side, liferay will create SQL script for each entity. We can group the tables generated for entities by giving Name Space value.

For Example,

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, Markes and Leave then the tables which are generated will be education_student, education_marks and education_leave respectively.

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.

For simplicity, we will take just one entity. Its time now to give the value for Package Name, Namespace and Author name in above screen.
give the name as below

Package path:- com.opensource.techblog.myservice
Namespace:- education
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.

view plainprint?
<?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">
<service-builder package-path="com.opensource.techblog.myservice">
<author>nilang</author>
<namespace>education</namespace>

<entity name="Student">
<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"/>
</entity>
</service-builder>


Explanation:-
We have created Student entity by defining local-service as true and remote-service as false. This is because the generated service will be resides within the portlet and this portlet reside in liferay server. So for liferay server this service is local. Most of the cases we are setting local-service true and remote-service as false.
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
long
int
String
boolean
Date
Blob etc.


We have done our homework. Now its lifera’y tern. We only have to declare the entities in service.xml file and rest all will be handled by liferay. Let’s see how the magic is happening behind the scene.


go to command prompt upto 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

/WEB/INF/service/com/opensource/techblog/myservice
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 words.

/WEB-INF/src/com/opensource/techblog/myservice
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.

Classes and interfaces which are generated at service , persistence and model layer.


Persistence Layer:-

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

Service Classes:-

StudentLocalService.java
local service interface for Student entity.
StudentLocalServiceImpl.java
local service implementation which implements StudentLocalService interface.
StudentLocalServiceBaseImpl.java
Base local service interface
StudentLocalServiceUtil.java
local service util class which having instance of StudentLocalServiceImpl. Out of above classes, only this class is accessible to other API (outside of service layer) for CRUD operation.

Model classes:-

Model class Represent a row in education_student table.

StudentModel.java
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)
StudentModelImpl.java
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).
Student.java
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.
StudentImpl.java
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 :-

Following diagram shows the relation between model interface and 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 allow to add additional (custom) methods at service level to developer.

Relations among Persistence interfaces / classes :-


Following diagram shows the relation between persistence interface and classes











None of the class is allow to add customized methods to developer.

So this is all about service layer generated by liferay for each entity.To perform CRUD operation, we need just one service class is StudentLocalServiceUtil.java. It contains following methods. We can call methods of this call in our portlet to Add / update / delete Students.






You will observe that this class have various CRUD methods like

createStudent
addStudent
deleteStudent
updateStudent
fetchStudent
getStudent.


Note that, this class have instance variable of type StudentLocalService, which will get the instance of StudentLocalServiceImpl at run time. We don't have direct access to StudentLocalServiceImpl method. All methods of this (StudentLocalServiceUtil) class will internally call corresponding method of StudentLocalServiceImpl to perform CRUD operation.


In general, we only have access to LocalServiceUtil class. all method of LocalServiceUtil class will internally call method of LocalServiceImpl class.


And that's all. You can create more than one entity to understand the underlying class structure generated by liferay.

Please read following blogs to get idea of more feature of Service builder in Liferay


Reference Link : http://www.opensource-techblog.com/2013/03/creating-service-layer-in-service.html

No comments:

Post a Comment