Laliwala IT Services

Laliwala IT Services
Website Development

Thursday, March 31, 2011

database interaction, portlet, portal, tutorial

Prepared by Aasif and Ahmed under the guidance of Irshad Mansuri

Attune Infocom
----------------------------------------------------------------------------------
===============================================================
4. Adding database interaction
===============================================================

4.1 service.xml
---------------

(save this file under "/ext/ext-impl/src/com/ext/portlet/library")

<?xml version="1.0"?>
<!DOCTYPE service-builder PUBLIC "-//Liferay//DTD Service Builder 5.2.0//EN"
"http://www.liferay.com/dtd/liferay-service-builder_5_2_0.dtd">

<service-builder package-path="com.ext.portlet.library">
<namespace>library</namespace>
<entity name="Book" local-service="true" remote-service="false">
<!-- PK fields -->
<column name="bookId" type="long" primary="true" />

<!-- Other fields -->
<column name="title" type="String" />

<!-- Audit fields -->
<column name="dateAdded" type="Date" />

<!--If you wanna add another column say author you can do that by adding following line-->
<!--<column name="author" type="String" /> -->


<!-- order by -->

<!-- finder methods -->
</entity>
</service-builder>


4.2 run build-service with "service.xml" as argument
----------------------------------------------------

a. go to "/ext/ext-impl"

b. execute "ant build-service -Dservice.file=src/com/ext/portlet/library/service.xml"

c. make sure the build is successful

d. confirm all the service layer files are generated as per the Spring/Hibernate framework. check under

ext/ext-impl/src/com/ext/portlet/library
ext/ext-service/src/com/ext/portlet/library


4.3 create the table "Book"
---------------------------

use the below script to create the table inside database "lportal". To run the SQL directly, you've to go to mysql prompt by giving "mysql -u root lportal"

create table Book (
bookId bigint(10) not null primary key,
title VARCHAR(75) null,
dateAdded datetime
);

confirm that the table is created by giving "show tables" or "desc Book" as below :-



mysql> desc book;
+-----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+-------+
| bookId | bigint(10) | NO | PRI | NULL | |
| title | varchar(75) | YES | | NULL | |
| dateAdded | datetime | YES | | NULL | |
+-----------+-------------+------+-----+---------+-------+
3 rows in set (0.09 sec)

mysql>

4.4 updating service layer class - 1
------------------------------------

(file location - ext\ext-impl\src\com\ext\portlet\library\service\impl\BookLocalServiceImpl.java)

(write the functionality for database insert. insert this method. )

public Book create(String title)
throws PortalException, SystemException, RemoteException {

// create a primary key
long bookId = CounterServiceUtil.increment(Book.class.getName());

// instantiate an empty object
Book book = new BookImpl();
book.setBookId(bookId);
book.setTitle(title);

// persist the book object
book = bookPersistence.update(book, false);

// return the object to caller
return book;
}

(insert the additional import statements required to compile this class,)

import com.ext.portlet.library.model.Book;
import com.ext.portlet.library.model.impl.BookImpl;

import java.rmi.RemoteException;
import com.liferay.portal.PortalException;
import com.liferay.portal.SystemException;
import com.liferay.counter.service.CounterServiceUtil;

4.6 run ant build-service again to re-generate the corresponding Util interfaces
--------------------------------------------------------------------------------

a. execute "ant build-service -Dservice.file=src/com/ext/portlet/library/service.xml"

b. execute "ant compile" to compile all the generated file and make sure the build is SUCCESSFUL.


4.7 update AddBookAction.java
------------------------------

(location "ext/ext-impl/src/com/ext/portlet/library/action")

else {
// new line to be inserted
BookLocalServiceUtil.create(bookTitle);
setForward(req, "portlet.ext.library.success");
}

append the new import,

import com.ext.portlet.library.service.BookLocalServiceUtil;

4.8 run "ant deploy" from "ext"
--------------------------------

a. go back to /ext folder and execute "ant deploy"

b. make sure the build is SUCCESSFUL.

c. check the newly generated jar files "ext-impl.jar" and "ext-service.jar"


4.9 start tomcat and check the portlet
---------------------------------------

start tomcat and check the portlet. try to add some books and make sure the values are getting saved in the database.

Congratulations!!

Exercise:

1. set proper value for dateAdded while the record is inserted.
2. make the portlet to accept one more parameter "author" and store it in the table.
3. How to make use of the api's already generated by the service layer.





No comments:

Post a Comment