Service Builder is a code generation tool built by Liferay that allows developer to create custom entities. Service Builder generates necessary model, persistence and service layers based on service.xml. These layers provide a clean separation of concerns and allowing you to focus on the higher level aspects of the service design. All of Liferay’s services either local or remote services are generated by Service Builder.
Prerequisites
- Java
- Liferay portal 7/7.x
Environment Requirement
Assuming that you have already created a Liferay-workspace project in eclipse IDE.
Here we will create custom entity called “Student” and perform Create, Read, Update and Delete operation on this custom entity.
Follow below steps to complete CRUD operation for your custom entity.
1. Create service-builder module for your custom entity
- Go to liferay workspace project → modules → new
- Select other → Liferay → Liferay Module Project and Click on “Next”
- Enter project name
- Select “Project Template Name” as “service-builder” and Click on “Next”
- Enter Package name and click on “Finish”. The necessary file structure for service-builder automatically get created as below.
2. Add student entity in xxx-service/service.xml like as below
// service.xml //
stud
3. Build the service-builder module
- In terminal, navigate to the root directory of your service-builder project
- Execute the below command to build the service-builder module
blade gw buildService
- The necessary file structure for service-builder automatically gets created as below. It will automatically generate model, persistence, and service layers
- Execute below command to deploy the service-builder module
blade gw deploy
4. Create MVC Portlet
- Go to liferay workspace project → modules → new
- Select other → Liferay → Liferay Module Project and Click on “Next”
- Enter project name
- Select “Project Template Name” as “mvc-portlet” and Click on “Next”
- Enter Package name and Click on “Finish”. The necessary file structure for mvc module will gets created as below
5. In order to add dependency of student-api in mvc portlet, add below line in “build.gradle” of your mvc portlet.
// build.gradle //
dependencies {
...
/*
compileOnly project(":modules:{your service-builder module name}:{your module-api}")
*/
compileOnly project(":modules:student:student-api")
}
6. Now create add-student.jsp file in same directory as view.jsp. Add below code to add-student.jsp.
<%@ include file="init.jsp"%>
Add Student Form here !
Output of add-student.jsp
7. Now implement the process action method to add student in database
// StudentCrudPortlet.java //
package com.ignek.student.portlet;
@Component(
...
)
public class StudentCrudPortlet extends MVCPortlet {
private Log log = LogFactoryUtil.getLog(this.getClass().getName());
@Reference
CounterLocalService counterLocalService;
@Reference
StudentLocalService studentLocalService;
@ProcessAction(name = "addStudent")
public void addStudent(ActionRequest actionRequest,ActionResponse actionResponse) {
long studentId = counterLocalService.increment(Student.class.getName());
String enrollmentNo = ParamUtil.getString(actionRequest, "enrollmentNo");
String firstName = ParamUtil.getString(actionRequest, "firstName");
String lastName = ParamUtil.getString(actionRequest, "lastName");
String contactNo = ParamUtil.getString(actionRequest, "contactNo");
String city = ParamUtil.getString(actionRequest, "city");
Student student = studentLocalService.createStudent(studentId);
student.setStudentId(studentId);
student.setEnrollmentNo(enrollmentNo);
student.setFirstName(firstName);
student.setLastName(lastName);
student.setContactNo(contactNo);
student.setCity(city);
studentLocalService.addStudent(student);
}
}
8. Now override render method of portlet to fetch all students and send students list to view.jsp
// StudentCrudPortlet.java //
package com.ignek.student.portlet;
@Component(
...
)
public class StudentCrudPortlet extends MVCPortlet {
@Reference
StudentLocalService studentLocalService;
@Override
public void render(RenderRequest renderRequest, RenderResponse renderResponse) throws IOException, PortletException{
List studentList = studentLocalService.getStudents(QueryUtil.ALL_POS, QueryUtil.ALL_POS);
renderRequest.setAttribute("studentList", studentList);
super.render(renderRequest, renderResponse);
}
}
9. Now add below code in view.jsp
Output of view.jsp
10. Now create update-student.jsp file in same directory as view.jsp. Add below code to update-student.jsp
// update-student.jsp //
<%@ include file="init.jsp"%>
<%
String studentId = renderRequest.getParameter("studentId");
String enrollmentNo = renderRequest.getParameter("enrollmentNo");
String firstName = renderRequest.getParameter("firstName");
String lastName = renderRequest.getParameter("lastName");
String contactNo = renderRequest.getParameter("contactNo");
String city = renderRequest.getParameter("city");
%>
Output of update-student.jsp
11. Now implement the process action methods for update and delete actions
// StudentCrudPortlet.java //
package com.ignek.student.portlet;
@Component(
...
)
public class StudentCrudPortlet extends MVCPortlet {
private Log log = LogFactoryUtil.getLog(StudentCrudPortlet.class);
@Reference
StudentLocalService studentLocalService;
@ProcessAction(name = "addStudent")
public void addStudent(ActionRequest actionRequest,ActionResponse actionResponse) {
...
}
@ProcessAction(name = "updateStudent")
public void updateStudent(ActionRequest actionRequest, ActionResponse actionResponse) {
long studentId = ParamUtil.getLong(actionRequest,"studentId", GetterUtil.DEFAULT_LONG);
String enrollmentNo = ParamUtil.getString(actionRequest, "enrollmentNo", GetterUtil.DEFAULT_STRING);
String firstName = ParamUtil.getString(actionRequest, "firstName", GetterUtil.DEFAULT_STRING);
String lastName = ParamUtil.getString(actionRequest, "lastName", GetterUtil.DEFAULT_STRING);
String contactNo = ParamUtil.getString(actionRequest, "contactNo", GetterUtil.DEFAULT_STRING);
String city = ParamUtil.getString(actionRequest, "city", GetterUtil.DEFAULT_STRING);
Student student = null;
try {
student = studentLocalService.getStudent(studentId);
} catch (Exception e) {
log.error(e.getCause(), e);
}
if(Validator.isNotNull(student)) {
student.setEnrollmentNo(enrollmentNo);
student.setFirstName(firstName);
student.setLastName(lastName);
student.setContactNo(contactNo);
student.setCity(city);
studentLocalService.updateStudent(student);
}
}
@ProcessAction(name = "deleteStudent")
public void deleteStudent(ActionRequest actionRequest, ActionResponse actionResponse){
long studentId = ParamUtil.getLong(actionRequest, "studentId", GetterUtil.DEFAULT_LONG);
try {
studentLocalService.deleteStudent(studentId);
} catch (Exception e) {
log.error(e.getMessage(), e);
}
}
}