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="http://java.sun.com/portlet_2_0" prefix="portlet" %>

<portlet:defineObjects />

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

<html>

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

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 "prac.java" or something like that

public class controller extends MVCPortlet{

public void exp(ActionRequest arq,ActionResponse ars) 
{
String foo=arq.getParameter("foo");
System.out.println("fffff"+foo);
String title=arq.getParameter("title");
System.out.println("mmmmmm"+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">
<author>MEHDI</author>
<namespace>cc</namespace>

<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" />
</entity>
</service-builder>
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 FoolocalServiceImpl.java and insert below code

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

fooid=CounterLocalServiceUtil.increment();
Foo f=fooLocalService.createFoo(fooid);
f.setUserName(foo);
f.setTitle(title);
f.setCreateDate(new Date());
f.setModifiedDate(new Date());
fooPersistence.update(f, false);
System.out.println("ooookkkkk");
} 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
chiragmsc007it@gmail.com
mehdisunasara@gmail.com











2 comments:

  1. I am getting fooLocalService as null. What may be the possible reason

    ReplyDelete