Liferay Service Builder CRUD Example

Liferay provides a code generation tool called Service builder that allows developers to define object models called entities. Service builder generates the service layer, persistence layer, and required model classes. This helps the developer concentrate more on business logic implementation.

The service builder tool creates the hibernate and spring configuration to connect to the database.

In this article, we will learn how to create a Liferay service builder CRUD application.

Creating the Liferay service builder CRUD portlet

Here is a simple CRUD operation example using the service builder.

  • Create a Liferay plugin project with the name “service-builder-crud-portlet“.
  • Create a service.xml file under the WEB-INF folder of the plugin project. In Liferay IDE(LDS) from the toolbar, select New >> Liferay service builder. Choose the plugin project from the available project’s list.
  • Add the below code to the service.xml file.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE service-builder PUBLIC 
"-//Liferay//DTD Service Builder 6.2.0//EN" 
"http://www.liferay.com/dtd/liferay-service-builder_6_2_0.dtd">
<service-builder package-path="com.asb.test">
 <author>ASB</author>
 <namespace>ASB</namespace>

 <entity name="Student" local-service="true" remote-service="false">

 <!-- PK fields -->
 <column name="studentId" type="long" primary="true" />
 
 <!-- Group instance -->
 <column name="groupId" type="long" />
 
 <!-- Audit fields -->

 <column name="companyId" type="long" />
 <column name="userId" type="long" />
 <column name="userName" type="String" />
 <column name="createDate" type="Date" />
 <column name="modifiedDate" type="Date" />

 <!-- Other fields -->
 <column name="studentName" type="String" />
 <column name="studentGrade" type="String" />
 <column name="Gender" type="String" />
 <column name="age" type="int" />

 <!-- Order -->
 <order by="asc">
 <order-column name="studentName" />
 </order>

 <!-- Finder methods -->
 <finder name="studentName" return-type="Collection">
 <finder-column name="studentName" />
 </finder>
 <finder return-type="Colection" name="age">
 <finder-column name="age"></finder-column>
 </finder>
 
 </entity>
</service-builder>
  • In the above XML file, we have added an entity called Student. We have also added few columns for the entity.
  • Now right-click on project navigate to Liferay and select the Build services option to create the required classes for the database operations. Note that the service should build without any errors.
  • Create a simple portlet called StudentPortlet, by rightclik >> New >> Liferay Portlet and then select com.liferay.util.bridges.mvc.MVCPortlet as portlet’s super class and click on finish.

Creating the UI

Add view.jsp page with below code.

<%@page import="com.liferay.taglib.portlet.ActionURLTag"%>
<%@page import="com.asb.test.service.StudentLocalServiceUtil"%>
<%@page import="javax.portlet.PortletURL"%>
<%@ taglib uri="http://alloy.liferay.com/tld/aui" prefix="aui" %>
<%@ taglib uri="http://liferay.com/tld/portlet" prefix="liferay-portlet" %>
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<%@page import="com.asb.test.model.Student" %>
<%@page import="java.util.List" %>
<portlet:defineObjects />

<%
List<Student> studentList = StudentLocalServiceUtil.getStudents(-1, -1);
%>

<liferay-portlet:actionURL var="addStudentAction" name="addStudent">
</liferay-portlet:actionURL>
<style>
.myTable{
 border:solid; border-width: 1px; width: 15%; text-align:center;"
}
.myTable1{
 border:solid; border-width: 1px; width: 10%; text-align:center;"
}
.myTable3{
 border:solid; border-width: 1px; width: 100%; text-align:center;"
}
.control-group .input-text-wrapper{
 margin-bottom:-1%;
}
</style>
<table class="myTable3">
<tr class="myTable3">
 <th class="myTable">student Name</th>
 <th class="myTable">student Grade</th>
 <th class="myTable">Gender</th>
 <th class="myTable">Age</th>
 <th class="myTable1">Edit</th>
 <th class="myTable1">Delete</th>
</tr>
 <%for(Student st : studentList)
 {%> 
<tr class="myTable3"> 
 <td class="myTable"><%=st.getStudentName() %></td>
 <td class="myTable"><%=st.getStudentGrade() %></td>
 <td class="myTable"><%=st.getGender() %></td>
 <td class="myTable"><%=st.getAge() %></td>
 
<liferay-portlet:renderURL var="editStudent">
 <portlet:param name="jspPage" value="/jsp/editStudent.jsp" />
 <portlet:param name="name" value="<%=st.getStudentName() %>"/>
 <portlet:param name="grade" value="<%=String.valueOf(st.getStudentGrade()) %>"/>
 <portlet:param name="gender" value="<%=st.getGender() %>"/>
 <portlet:param name="age" value="<%=String.valueOf(st.getAge()) %>"/>
 <portlet:param name="stId" value="<%=String.valueOf(st.getStudentId()) %>"/>
</liferay-portlet:renderURL>
 
 <td class="myTable1">
 <aui:button name="" value="Update" onClick="<%=editStudent %>" ></aui:button>
 </td>
 
 <portlet:actionURL name="deleteStudent" var="deleteStudent"></portlet:actionURL>
 <aui:form action="<%=deleteStudent %>" method="post" class="myTable1">
 <aui:input name="delStId" type="hidden" value="<%=st.getStudentId() %>"/>
 <td class="myTable1"> 
 <aui:button name="" type="submit" value="Delete" />
 </td> 
 </aui:form>
</tr>
<%} %>
</table><span style="border:solid; border-width:1px;
padding:5px; text-align:center;">
<h5 style="text-align:center;">Add New Student</h5>
<aui:form action="<%=addStudentAction %>" method="post" name="name">
 <aui:input name="name" type="text" value=""/>
 <aui:input name="gender" type="text" value=""/>
 <aui:input name="age" type="number" value="" />
 <aui:input name="grade" type="number" value="" />
 <aui:input name="" type="submit" value="Add Student" />
</aui:form>
</span>

Here, we are getting the data of the student entity from the database using the StudentLocalServiceUtil.getStudents(-1, -1) method created by running the service builder.

We are displaying all the available records in a table structure.

We also have a form to add a new student entity to the database and an option to edit and delete any record.

Here, Each button is bound to a particular action in the back end.

Now Add a JSP page called editStudent.jsp to provide an edit form with the below code.

<%@page import="com.liferay.portal.kernel.util.ParamUtil"%>
<%@ taglib uri="http://alloy.liferay.com/tld/aui" prefix="aui" %>
<%@ taglib uri="http://liferay.com/tld/portlet" prefix="liferay-portlet" %>
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>

<portlet:defineObjects/>

<portlet:actionURL name="updateStudent" var="editStudentAction">
</portlet:actionURL>

<%
 String studentName = renderRequest.getParameter("name");
 String age = renderRequest.getParameter("age");
 String gender = renderRequest.getParameter("gender");
 String grade = renderRequest.getParameter("grade");
 String stId = renderRequest.getParameter("stId");
%>

<h3>Edit Student: <%= studentName%></h3>

<aui:form action="<%=editStudentAction %>" method="post" name="name">
 <aui:input name="name" type="text" value="<%=studentName%>"/>
 <aui:input name="gender" type="text" value="<%=gender %>"/>
 <aui:input name="age" type="number" value="<%=Integer.parseInt(age) %>"/>
 <aui:input name="grade" type="number" value="<%=Integer.parseInt(grade) %>" />
 <aui:input name="stId" type="hidden" value="<%=Long.parseLong(stId) %>"/> 
 <aui:input type="submit" value="Update" name="update"></aui:input>
</aui:form>
  • This page will contain the corresponding values of the record, which is to be edited. Submitting the form stores the new values in the database.
  • Add the action methods for add, update and delete actions as shown below.
package com.asb.test.portlet;
import com.asb.test.model.Student;
import com.asb.test.model.impl.StudentImpl;
import com.asb.test.service.StudentLocalServiceUtil;
import com.liferay.counter.service.CounterLocalServiceUtil;
import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.util.bridges.mvc.MVCPortlet;
import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
public class StudentPortlet extends MVCPortlet {
 public void addStudent(ActionRequest actionRequest, 
ActionResponse actionResponse) 
 throws SystemException, PortalException 
 {
   long studentId=CounterLocalServiceUtil.increment();
   Student student=null;
   student=StudentLocalServiceUtil.createStudent(studentId);
   student.setStudentName(ParamUtil.getString(actionRequest, "name"));
   student.setStudentGrade(String.valueOf((ParamUtil
.getInteger(actionRequest, "grade"))));
   student.setGender(ParamUtil.getString(actionRequest, "gender"));
   student.setAge(ParamUtil.getInteger(actionRequest, "age"));
   StudentLocalServiceUtil.addStudent(student);
 }
 public void deleteStudent(ActionRequest actionRequest, 
ActionResponse actionResponse)
 throws SystemException, PortalException
 {
   StudentLocalServiceUtil.deleteStudent(ParamUtil.getLong(actionRequest, 
"delStId"));
 }
 
 public void updateStudent(ActionRequest actionRequest, 
ActionResponse actionResponse)
 throws SystemException, PortalException
 {
   Student student=StudentLocalServiceUtil
.fetchStudent(ParamUtil.getInteger(actionRequest, "stId"));
 
   student.setStudentName(ParamUtil.getString(actionRequest, "name"));
   student.setStudentGrade(String.valueOf(
(ParamUtil.getInteger(actionRequest, "grade"))));
   student.setGender(ParamUtil.getString(actionRequest, "gender"));
   student.setAge(ParamUtil.getInteger(actionRequest, "age"));
   StudentLocalServiceUtil.updateStudent(student);
 }
}

Here, we have different methods for add, update and delete records.

The add action method creates a new studentId by calling the CounterLocalServiceUtil.increment() and then creates a student object with the user submitted form values.

The addStudent() method that is generated by the service builder saves the object into the database.

Similarly, we are using service builder generated deleteStudent() and updateStudent() methods for delete and update operations.

Testing the output

Add student form and results with the update and delete options.

viewPage

The edit Page:

EditPage

The below image shows the values stored in the database.

DB

Note: Version used: Liferay 6.2 + Apache Tomcat 7.

Conclusion

In this article, we learned about the liferay service builder.

We also learned how to perform Liferay service builder CRUD operations by creating a portlet application.