1.Create a database structure
Move service.xml in .../ext/ext-ejb/ to .../ext/ext-ejb/src/com/ext/portlet/reports/
Create a new service.xml in .../ext/ext-ejb/ , contents:
<! DOCTYPE service-builder PUBLIC "-//Liferay//DTD Service Builder 4.0.0//EN" "http://www.liferay.com/dtd/liferay-service-builder_4_0_0.dtd" >
< service-builder root-dir =".." package-path ="com.ext.portlet" >
< portlet name ="Library" short-name ="Library" />
< entity name ="Book" local-service ="true" >
<!-- PK fields -->
< column name ="bookId" type ="String" primary ="true" />
<!-- Other fields -->
< column name ="title" type ="String" />
</ entity >
</ service-builder >
2.Auto generating the Service Layer code and SQL
.../ext/ext-ejb>ant build-service
3.Modifying MySQL to include new table
portal-tables.sql Updated, the following is added
bookId VARCHAR ( 75 ) not null primary key ,
title VARCHAR ( 75 ) null
);
Login MySQL,
mysql>use training;
mysql>show tables;
confirm that the "Book" table does not exist yet,
Copy the generate SQL code, paste it into the CMD prompt
mysql>show tables;
comfirm that the "Book" table exist now.
4.Create method to add record to the database
BookLocalServiceImpl.java in .../ext/ext-ejb/src/com/ext/portlet/library/service/impl/ , content:
import java.rmi.RemoteException;
import java.util.List;
import com.ext.portlet.library.model.Book;
import com.ext.portlet.library.service.persistence.BookUtil;
import com.ext.portlet.library.service.BookLocalService;
import com.liferay.counter.service.CounterServiceUtil;
import com.liferay.portal.PortalException;
import com.liferay.portal.SystemException;
public class BookLocalServiceImpl implements BookLocalService ... {
public Book addBook(String userId, String title) throws PortalException, SystemException, RemoteException ...{
Book book = BookUtil.create(Long.toString(CounterServiceUtil.increment(Book.class.getName())));
book.setTitle(title);
return BookUtil.update(book);
}
public List getAll() throws PortalException, SystemException ...{
return BookUtil.findAll();
}
}
...ext/ext-ejb>ant build-service (regenerate service layer code)
5.Update existing files
AddBookAction.java
import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletConfig;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import com.ext.portlet.library.service.BookLocalServiceUtil;
import com.liferay.portal.struts.PortletAction;
public class AddBookAction extends PortletAction ... {
public void processAction(
ActionMapping mapping, ActionForm form, PortletConfig config,
ActionRequest req, ActionResponse res)
throws Exception ...{
String bookTitle = req.getParameter("book_title");
if ( "".equals(bookTitle) || bookTitle == null ) ...{
setForward(req, "portlet.ext.library.error");
}
else ...{
BookLocalServiceUtil.addBook(req.getRemoteUser(),bookTitle);
setForward(req, "portlet.ext.library.success");
}
}
}
init.jsp
<% ... @ page import="com.ext.portlet.library.model.Book" %>
<% ... @ page import="com.ext.portlet.library.service.BookLocalServiceUtil" %>
<% ... @ page import="java.util.ArrayList" %>
<% ... @ page import="java.util.Iterator" %>
6.Retrieve Records from the Database for display
success.jsp
< c:if test ="<%= books != null %>" >
< h1 > Book Listings </ h1 >
< table align ="center" cellspacing ="10" cellpadding ="3" >
< tr >
< td style ="font-weight:bold" > Book Id </ td >
< td style ="font-weight:bold" > Book Title </ td >
</ tr >
< c:if test ="<%= books != null %>" >
<% ...
Iterator itr = books.iterator();
while (itr.hasNext()) {
book = (Book)itr.next();
%>
< tr >
< td >
<% = book.getBookId() %>
</ td >
< td >
<% = book.getTitle() %>
</ td >
</ tr >
<% ...
}
%>
</ c:if >
</ table >
</ c:if >
...ext/ext-ejb>ant deploy ...