Laliwala IT Services

Laliwala IT Services
Website Development

Thursday, March 31, 2011

Liferay tutorial web service


 Liferay tutorial web service
Liferay update service 
Liferay book



===============================================================
7. Exposing the method as a web service
===============================================================
7.1 update service.xml
----------------------
specify remote-service="true" (default)
difference between local service and remote service.
Needless to say, once you do any modifications to service.xml file, please run "ant build-service"
7.2 updating service layer class - 2
------------------------------------
(file location - ext\ext-impl\src\com\ext\portlet\library\service\impl\BookServiceImpl.java)
public Book create(String title)
throws PortalException, SystemException, RemoteException {
return bookLocalService.create(title, "abcd");
}
(insert the additional import statements required to compile this class,)
import com.ext.portlet.library.model.Book;
import java.rmi.RemoteException;
import com.liferay.portal.PortalException;
import com.liferay.portal.SystemException;
Important: Dont forget to re-generate the service layer after inserting this method into BookServiceImpl.java
7.3 steps to expose as a web-service
------------------------------------
a. go to "/ext/ext-impl"
b. execute "ant build-wsdd -Dservice.file=src/com/ext/portlet/library/service.xml"
c. make sure the build is successful
d. from ext level run "ant deploy"
e. re-start the server
g. make sure our new web-service is listed out and hence exposed.
h. in subsequent sessions we'll see how to consume this web-service from any other application.
Excercise:
Expose the getAllBooks method as a web service.

struts portlet tutorial, struts portlet, Irshad Mansuri

Prepared by Aasif and Ahmed under the guidance of Irshad Mansuri

Attune Infocom
----------------------------------------------------------------------------------
===============================================================
6. Struts Portlet - Adding Redirect to avoid duplicate insertion
===============================================================

6.1 update struts-config.xml
----------------------------

add /ext/library/success so that we have a landing path

(location "ext/ext-web/docroot/WEB-INF")

<action path="/ext/library/success" forward="portlet.ext.library.success" />

run "ant deploy" from "ext-web"


6.2 update AddBookAction.java
-----------------------------

* We are going to modify our java to use a redirect instead of a forward
* A forward still submits to the page that it lands on. But a redirect will no longer retain the submit state on the landing page

Comment out the forward line
//setForward(req, "portlet.ext.library.success");

replace with these lines

PortletURL redirectURL = ((ActionResponseImpl) res).createRenderURL();
redirectURL.setParameter("struts_action", "/ext/library/success");
res.sendRedirect(redirectURL + "&title=" + bookTitle);

Make necessary imports


6.3 ant deploy
--------------

ant deploy from "ext-impl", re-start server and check the previous problem is not happening anymore.

6.4 Exercise
------------

What changes are required if we dont want to display the book title in the success.jsp file.

Adding database interaction, Retrieving records

Prepared by Aasif and Ahmed under the guidance of Irshad Mansuri

Attune Infocom
----------------------------------------------------------------------------------
===============================================================
5. Adding database interaction - Retrieving records

5.1 Update BookLocalServicImpl.java
------------------------------------

add the new method

public List<Book> getAllBooks()
throws PortalException, SystemException, RemoteException {
return bookPersistence.findAll();
}

append the new import,

import java.util.List;

5.2 run ant build-service again to re-generate the corresponding Util classes
-----------------------------------------------------------------------------

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

b. execute "ant deploy" from ext-impl to compile and deploy all the generated file and make sure the build is SUCCESSFUL.


5.3 update "init.jsp"
---------------------

Make additional imports. The intent is to get access to generated service layer classes inside any of the jsp's of this portlet.

(location "ext/ext-web/docroot/html/portlet/ext/library")

<%@ page import="java.util.List" %>
<%@ page import="com.ext.portlet.library.model.Book" %>
<%@ page import="com.ext.portlet.library.service.BookLocalServiceUtil" %>








5.4 update "success.jsp" (append the below code)
------------------------------------------------

(location "ext/ext-web/docroot/html/portlet/ext/library")

<%
String bookTitle = request.getParameter("title");

// new line inserted.
List<Book> books = BookLocalServiceUtil.getAllBooks();
%>

<table align="center" cellspacing="10" cellpadding="3">
<tr>
<td style="font-weight:bold">Book Title: </td>
<td><%= bookTitle %></td>
</tr>
</table>

<!-- new code added -->
<table border="1">
<%
for (Book book : books) {
%>
<tr>
<td>Book Id: <%= book.getBookId()%></td>
<td>Title: <%= book.getTitle() %></td>
</tr>
<%
}
%>
</table>

5.5 ant deploy to tomcat
------------------------

a. run "ant deploy-fast" from "ext-web"

b. re-start the tomcat server

c. see the updates to the library portlet (the records are retrieved properly)

Exercise:

display the dateAdded column in the table where all the books are listed.


5.6 the problem with page re-fresh
----------------------------------

everything is working fine. try to refresh the page and see what happens.

duplicate record is getting added to the database. In the next section, we'll see how to handle this
















See that after refreshing duplicate record is added.

Check the table contents which shows duplicate recors.



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.





Monday, March 28, 2011

portlet tutorial, struts portlet with action class, liferay portal tutorial, struts portlet action

Irshad Mansuri

Attune Infocom
----------------------------------------------------------------------------------
===============================================================3. Struts Portlet with action class.
===============================================================

(update the below 2 xml files)

3.1. struts-config.xml
----------------------

(add a new action)

<action path="/ext/library/add_book" type="com.ext.portlet.library.action.AddBookAction">
<forward name="portlet.ext.library.view" path="portlet.ext.library.view" />
<forward name="portlet.ext.library.success" path="portlet.ext.library.success" />
<forward name="portlet.ext.library.failure" path="portlet.ext.library.failure" />
</action>

3.2. tiles-defs.xml
-------------------

(add two more definitions)

<definition name="portlet.ext.library.failure" extends="portlet.ext.library">
<put name="portlet_content" value="/portlet/ext/library/failure.jsp" />
</definition>

<definition name="portlet.ext.library.success" extends="portlet.ext.library">
<put name="portlet_content" value="/portlet/ext/library/success.jsp" />
</definition>

3.3. init.jsp
-------------

(remove the text line added earlier, keeping just the below include directive)

<%@ include file="/html/portlet/init.jsp" %>









3.4. view.jsp
-------------

(modify this file)

<%@ include file="/html/portlet/ext/library/init.jsp" %>

<%
PortletURL actionURL = renderResponse.createActionURL();
actionURL.setWindowState(WindowState.MAXIMIZED);
actionURL.setParameter("struts_action", "/ext/library/add_book");
%>

Add a book entry to the library:
<br/>

<form action="<%= actionURL.toString() %>" method="POST" name="<portlet:namespace />fm">

Book Title:

<input type="text" name="<portlet:namespace />book_title" /><br/><br/>

<input type="submit" value="Add Book"/>
</form>

3.5. success.jsp
----------------

<%@ include file="/html/portlet/ext/library/init.jsp" %>
<%
String bookTitle = request.getParameter("book_title");
%>

<table align="center" cellspacing="10" cellpadding="3">
<tr>
<td style="font-weight:bold">Book Title: </td>
<td><%= bookTitle %></td>
</tr>
</table>

3.6. failure.jsp
--------------

<%@ include file="/html/portlet/ext/library/init.jsp" %>
<font color="red">Error in page...</font>
3.7. AddBookAction.java
-----------------------

(location: ext/ext-impl/src/com/ext/portlet/library/action/AddBookAction.java)

package com.ext.portlet.library.action;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletConfig;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.liferay.portal.struts.PortletAction;
import com.liferay.portal.kernel.util.Validator;

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 (Validator.isNull(bookTitle)) {
setForward(req, "portlet.ext.library.failure");
} else {
setForward(req, "portlet.ext.library.success");
}
}

public ActionForward render(ActionMapping mapping, ActionForm form,
PortletConfig config, RenderRequest req, RenderResponse res)
throws Exception {
if (getForward(req) != null && !getForward(req).equals("")) {
return mapping.findForward(getForward(req));
} else {
return mapping.findForward("portlet.ext.library.view");
}
}
}


liferay tutorial









Check the Library Portlet is updated.

Exercise:

. From the success and failure pages, give a link to come back to the view page.

HINT:
To have link in Liferay Portel use following tag in both success and failure jsp pages

<table>
<tr>
<a href="<portlet:renderURL ><liferay-portlet:param name="struts_action" value="/ext/library/view"/></portlet:renderURL>">
Back</a>
</tr>
</table>







---------------------------------------------------------


Attune Infocom Inc.
911 S. Wa Pella Ave
Mount Prospect Illinois 60056
Email: sales@attuneinfocom.com
Telephone: +1-732-703-9847


 -------------------------------------------------------


Attune Infocom Pvt. Ltd.
Ground Floor G-2, Aakruti Complex,
Above Stadium Under Bridge,
Navrangpura, Ahmedabad - 380009
Gujarat, India
Email: contact@attuneinfocom.com

Telephone: +91-79-40047256 /7 /8

__________________________________________




portlet tutorial, struts portlet with action class, liferay portal tutorial, struts portlet action



liferay multiple browser

Simple Struts Portlet, tutorial, portlet, struts tutorial, liferay strusts portlet

Code Snippets for various examples.

Prepared by Aasif and Ahmed under the guidance of Irshad Mansuri

Attune Infocom
----------------------------------------------------------------------------------
===============================================================2. Simple Struts Portlet

2.1. portlet-ext.xml
--------------------
<portlet>
<portlet-name>library</portlet-name>
<display-name>library Portlet</display-name>
<portlet-class>com.liferay.portlet.StrutsPortlet</portlet-class>
<init-param>
<name>view-action</name>
<value>/ext/library/view</value>
</init-param>
<expiration-cache>0</expiration-cache>
<supports>
<mime-type>text/html</mime-type>
</supports>
<resource-bundle>com.liferay.portlet.StrutsResourceBundle</resource-bundle>
<security-role-ref>
<role-name>power-user</role-name>
</security-role-ref>
<security-role-ref>
<role-name>user</role-name>
</security-role-ref>
</portlet>

2.2. liferay-portlet-ext.xml
----------------------------
<portlet>
<portlet-name>library</portlet-name>
<struts-path>ext/library</struts-path>
<use-default-template>false</use-default-template>
</portlet>

2.3. liferay-display.xml
------------------------

add

<portlet id="library" />

inside

<category name="category.example">

2.4. struts-config.xml
----------------------
<action path="/ext/library/view" forward="portlet.ext.library.view" />

2.5. tiles-defs.xml
-------------------
<definition name="portlet.ext.library" extends="portlet" />
<definition name="portlet.ext.library.view" extends="portlet.ext.library">
<put name="portlet_content" value="/portlet/ext/library/view.jsp" />
</definition>


(create the following two jsp files under "/ext/ext-web/docroot/html/portlet/ext/library")

2.6. init.jsp
-------------
<%@ include file="/html/portlet/init.jsp" %>
<p>Add commonly used variables and declarations here!</p>

2.7. view.jsp
-------------
<%@ include file="/html/portlet/ext/library/init.jsp" %>
Simple Struts Portlet!

2.8 Language-ext.properties
---------------------------

Add a new entry in the above file,
javax.portlet.title.library=library Portlet

2.9 Deploy and verify
---------------------

run "ant deploy" from ext to move all the changes we have done so far both under "ext-web" and "ext-impl"

Restart the server and check the new library portlet is working perfectly fine.

This portlet will be the basis for all other concepts we are going to learn in the subsequent chapters.

All the best !!







Check that Library Portlet is available under Training Categroty.





Check out that Library Portlet is added successfully.









Simple Struts Portlet, tutorial, portlet, struts tutorial, liferay strusts portlet