Thursday, 20 June 2013

Database Operation liferay using Service Builder(CRUD Portlet)

In this Blog we will learn how to insert data in database using  Service Builder.

Create a new Liferay Project:-
1)Click on New Liferay Project as shown below:

2)Give the project name as "prac"
3)Check "Liferay Plugin SDK" and Liferay Portal Runtime are Properly configured.
4)Check "Portlet" option
5)Put Portlet Name as "pracportlet"
6)put java package as "com.control"

7)Choose Superclass as "com.liferay.util.bridges.mvc.MVCPortlet"
8)Click next and make display name as "prac portlet"
9)Click next and specify Display category where your portlet should be displayed in liferay "sample"
10)Now deploy whole project

Add Prac Portlet to Liferay Page:
1)Go to page and click on add-->portlet-->sample-->prac
2)now Modify View.jsp as below

<%@ taglib uri="" prefix="portlet" %>

<portlet:defineObjects />

This is the <b>Prac</b> portlet.


<portlet:actionURL name="exp" var="actionFoo"></portlet:actionURL>
<form action="<%=actionFoo.toString()%>" method="post">
Name<input type="text" name="foo" id="foo" />
Title<input type="text" name="title" id="title" />
<br />
<input type="submit" value="ok"/>

2)Now Deploy and check portlet in page it will look like 

Enter values in textbox and click "ok"  and you will get some error in this page
"portlet is temporarily unavailable"
This Error occurs because portal server is unable to find a  method
Lets See where to add method Method to make portlet Works

3)Go to com.control where you will fing Java class named as "" or something like that

public class controller extends MVCPortlet{

public void exp(ActionRequest arq,ActionResponse ars) 
String foo=arq.getParameter("foo");
String title=arq.getParameter("title");


4)Now Redeploy Portlet and check the page in Liferay .Enter Details and click on Submit button
5)After Submitting open Console you  will get details there.

Service Layer:-
1)Select the project in Eclipse and click "New Liferay Service Builder"
2)In next Pop up ,give package path and  Namespace as prac
3)Once Finished you will see service.xml open

<service-builder package-path="com.sample">

<entity name="Foo" local-service="true" remote-service="true">

<!-- PK fields -->

<column name="fooId" type="long" primary="true" />

<!-- Audit fields -->

<column name="companyId" type="long" />
<column name="userId" type="long" />
<column name="userName" type="String" />
<column name="title" type="String" />
<column name="createDate" type="Date" />
<column name="modifiedDate" type="Date" />
4)Now Build Service,If everything goes fine you will get "BUILD SUCCESSFUL" message on console
5)Now you will see list of files generated by service layer.
6)Now Open your java class and insert below code
           FooLocalServiceUtil.addtodb(foo, title);
7)Now Open and insert below code

public void addtodb(String foo,String title)
long fooid=0L;
try {

Foo f=fooLocalService.createFoo(fooid);
f.setCreateDate(new Date());
f.setModifiedDate(new Date());
fooPersistence.update(f, false);
} catch (Exception e) {
// TODO: handle exception

8)If everything goes fine You will see  that your details are being inserted in database and also you database table name will be cc_foo.

If there is any problem plz do comment or email us at